Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
Issue #237
  • Loading branch information
rsoika committed Aug 11, 2024
1 parent 7c62092 commit 355dbd3
Show file tree
Hide file tree
Showing 5 changed files with 422 additions and 9 deletions.
31 changes: 22 additions & 9 deletions open-bpmn.metamodel/src/main/java/org/openbpmn/bpmn/BPMNModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -911,9 +911,7 @@ public Message addMessage(String id, String name) throws BPMNModelException {
* @throws BPMNMissingElementException
* @throws BPMNInvalidTypeException
*/
public Signal findSignalByName(String name)
throws BPMNInvalidReferenceException, BPMNMissingElementException, BPMNInvalidTypeException {

public Signal findSignalByName(String name) {
if (name == null || name.isEmpty()) {
return null;
}
Expand All @@ -926,16 +924,19 @@ public Signal findSignalByName(String name)
}

/**
* Deletes a Signal element from this diagram.
* Finds a Signal element by its ID withing the diagram
* <p>
* <bpmn2:signal id="Signal_1" name="My Signal"/>
*
* @param id
* @param name - name of the signal
* @throws BPMNInvalidReferenceException
* @throws BPMNMissingElementException
* @throws BPMNInvalidTypeException
*/
public void deleteSignal(String id) {
public Signal findSignal(String id) {
if (id == null || id.isEmpty()) {
return;
return null;
}

Signal signal = null;
for (Signal _signal : getSignals()) {
if (id.equals(_signal.getId())) {
Expand All @@ -944,7 +945,17 @@ public void deleteSignal(String id) {

}
}
return signal;
}

/**
* Deletes a Signal element from this diagram.
* <p>
*
* @param id
*/
public void deleteSignal(String id) {
Signal signal = findSignal(id);
if (signal == null) {
// does not exist
return;
Expand Down Expand Up @@ -1346,14 +1357,16 @@ public Participant findParticipantByPoint(BPMNPoint point) throws BPMNInvalidTyp
*
* @param name
* @return
* @throws BPMNModelException
*/
public BPMNProcess findProcessByName(String processName) {
public BPMNProcess findProcessByName(String processName) throws BPMNModelException {
if (processName == null || processName.isEmpty()) {
return null; // no name provided!
}
Set<BPMNProcess> processList = getProcesses();
for (BPMNProcess _process : processList) {
if (processName.equals(_process.getName())) {
_process.init();
return _process;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.openbpmn.bpmn.elements.core;

import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -572,4 +573,29 @@ public Set<Association> getAssociations() {
return result;

}

/**
* Returns a List of all DataObjects associated with this element
*
* @param event
* @return
*/
public Set<DataObject> getDataObjects() {
Set<DataObject> result = new HashSet<DataObject>();
Set<Association> associations = this.getAssociations();
// find Data objects...
for (Association association : associations) {
BPMNElementNode source = association.getSourceElement();
BPMNElementNode target = association.getTargetElement();
if (source instanceof DataObject) {
result.add((DataObject) source);
}
if (target instanceof DataObject) {
result.add((DataObject) target);
}

}
return result;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package org.openbpmn.bpmn.navigation;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import java.util.logging.Logger;

import org.openbpmn.bpmn.BPMNTypes;
import org.openbpmn.bpmn.elements.BPMNProcess;
import org.openbpmn.bpmn.elements.Event;
import org.openbpmn.bpmn.elements.core.BPMNElement;
import org.openbpmn.bpmn.elements.core.BPMNElementNode;
import org.openbpmn.bpmn.exceptions.BPMNValidationException;

/**
* The BPMNStartElementIterator returns Elements that immediately follow a start
* event. This can be either an Intermediate Catch/Throw Events or an Activity
* (Task).
*
* <p>
* With the filter argument (Functional Interface Predicate) an argument can be
* provided to return only specific elements.
*
*
*/
public class BPMNStartElementIterator<T> implements Iterator<BPMNElementNode> {

protected static Logger logger = Logger.getLogger(BPMNElementNode.class.getName());
Set<Event> _startEventNodes;

private List<BPMNElementNode> resultElementList;
private Iterator<BPMNElementNode> allStartElementsIterator;

/**
* Creates an Iterator with a given filter criteria.
* The method collects all BPMNElements following the given start element and
* matching the given filter
*
* @param bpmnElementNode
* @param filter
* @throws BPMNValidationException
*/
public BPMNStartElementIterator(BPMNProcess process, Predicate<BPMNElementNode> filter) {
resultElementList = new ArrayList<>();

// First find all Start Events in the model
_startEventNodes = new HashSet<>();
Set<Event> allEventNodes = process.getEvents();
for (Event _event : allEventNodes) {
if (BPMNTypes.START_EVENT.equals(_event.getType())) {
_startEventNodes.add(_event);
}
}

// next resolve all follow up tasks and events based on the given Predicate
for (BPMNElement element : _startEventNodes) {
BPMNFlowIterator<BPMNElement> followUpElements = new BPMNFlowIterator<BPMNElement>(
(BPMNElementNode) element,
filter);
// add the followup elements to the result list
while (followUpElements.hasNext()) {
resultElementList.add(followUpElements.next());
}
}
// create a local iterator instance
allStartElementsIterator = resultElementList.iterator();
}

@Override
public boolean hasNext() {
return allStartElementsIterator.hasNext();
}

@Override
public BPMNElementNode next() {
return allStartElementsIterator.next();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package org.openbpmn.metamodel.navigation;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import org.junit.jupiter.api.Test;
import org.openbpmn.bpmn.BPMNModel;
import org.openbpmn.bpmn.elements.Activity;
import org.openbpmn.bpmn.elements.BPMNProcess;
import org.openbpmn.bpmn.elements.Event;
import org.openbpmn.bpmn.elements.core.BPMNElementNode;
import org.openbpmn.bpmn.exceptions.BPMNModelException;
import org.openbpmn.bpmn.navigation.BPMNStartElementIterator;
import org.openbpmn.bpmn.util.BPMNModelFactory;
import org.openbpmn.metamodel.examples.TestCreateEdges;

/**
* Test class to test the StartNavigator.
*
*/
public class TestStartNavigation {
private static Logger logger = Logger.getLogger(TestCreateEdges.class.getName());

/**
* This test loads model 'refmodel-navigation-01.bpmn' and finds the start
* task-1.
*
* @throws BPMNModelException
*
*/
@SuppressWarnings("rawtypes")
@Test
public void testFindStartTask1() throws BPMNModelException {

logger.info("...read model");
BPMNModel model = BPMNModelFactory.read("/refmodel-navigation-01.bpmn");
BPMNProcess process = model.openDefaultProces();

// get start Task1
BPMNStartElementIterator startElements = new BPMNStartElementIterator<>(process, n -> n instanceof Activity);
assertNotNull(startElements);
assertTrue(startElements.hasNext());

BPMNElementNode task1 = startElements.next();
assertNotNull(task1);

assertEquals("Task-1", task1.getName());
// no more elements exprected
assertFalse(startElements.hasNext());
}

/**
* This test loads model 'refmodel-navigation-01.bpmn' and finds the start
* task-1.
*
* @throws BPMNModelException
*
*/
@SuppressWarnings("rawtypes")
@Test
public void testFindStartTask1WithEvents() throws BPMNModelException {

logger.info("...read model");
BPMNModel model = BPMNModelFactory.read("/refmodel-navigation-01.bpmn");
BPMNProcess process = model.openDefaultProces();

// get start Task1
BPMNStartElementIterator startElements = new BPMNStartElementIterator<>(process,
node -> (node instanceof Activity || node instanceof Event));
assertNotNull(startElements);
assertTrue(startElements.hasNext());

BPMNElementNode task1 = startElements.next();
assertNotNull(task1);

assertEquals("Task-1", task1.getName());
// no more elements expected
assertFalse(startElements.hasNext());
}

/**
* This test loads model 'refmodel-navigation-04.bpmn' and finds all conditional
* start events.
*
* In this test we ignore all conditions and expect 3 events
*
* @throws BPMNModelException
*
*/
@SuppressWarnings("rawtypes")
@Test
public void testFindConditionalStartEvents() throws BPMNModelException {

logger.info("...read model");
BPMNModel model = BPMNModelFactory.read("/refmodel-navigation-04.bpmn");
BPMNProcess process = model.openDefaultProces();

// get start Task1
BPMNStartElementIterator startElements = new BPMNStartElementIterator<>(process,
node -> (node instanceof Activity || node instanceof Event));
assertNotNull(startElements);
assertTrue(startElements.hasNext());

List<BPMNElementNode> resultList = new ArrayList<>();
while (startElements.hasNext()) {
resultList.add(startElements.next());
}

assertEquals(3, resultList.size());
}

}
Loading

0 comments on commit 355dbd3

Please sign in to comment.