-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added EndElementIterator and BPMNLinkNavigator
Issue #237
- Loading branch information
Showing
4 changed files
with
233 additions
and
8 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
open-bpmn.metamodel/src/main/java/org/openbpmn/bpmn/navigation/BPMNEndElementIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
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.SequenceFlow; | ||
import org.openbpmn.bpmn.elements.core.BPMNElementNode; | ||
import org.openbpmn.bpmn.exceptions.BPMNValidationException; | ||
|
||
/** | ||
* The BPMNEndElementIterator returns Elements that are immediately followed by | ||
* a end 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 BPMNEndElementIterator<T> implements Iterator<BPMNElementNode> { | ||
|
||
protected static Logger logger = Logger.getLogger(BPMNElementNode.class.getName()); | ||
Set<Event> _endEventNodes; | ||
|
||
private List<BPMNElementNode> resultElementList; | ||
private Iterator<BPMNElementNode> allEndElementsIterator; | ||
|
||
/** | ||
* 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 BPMNEndElementIterator(BPMNProcess process, Predicate<BPMNElementNode> filter) { | ||
resultElementList = new ArrayList<>(); | ||
|
||
// First find all Start Events in the model | ||
_endEventNodes = new HashSet<>(); | ||
Set<Event> allEventNodes = process.getEvents(); | ||
for (Event _event : allEventNodes) { | ||
if (BPMNTypes.END_EVENT.equals(_event.getType())) { | ||
_endEventNodes.add(_event); | ||
} | ||
} | ||
|
||
// next resolve all immediate predecessors. | ||
for (BPMNElementNode element : _endEventNodes) { | ||
Set<SequenceFlow> incomingFlows = element.getIngoingSequenceFlows(); | ||
for (SequenceFlow _flow : incomingFlows) { | ||
BPMNElementNode sourceNode = _flow.getSourceElement(); | ||
if (filter.test(sourceNode)) { | ||
resultElementList.add(sourceNode); | ||
} | ||
} | ||
} | ||
// create a local iterator instance | ||
allEndElementsIterator = resultElementList.iterator(); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return allEndElementsIterator.hasNext(); | ||
} | ||
|
||
@Override | ||
public BPMNElementNode next() { | ||
return allEndElementsIterator.next(); | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
open-bpmn.metamodel/src/main/java/org/openbpmn/bpmn/navigation/BPMNLinkNavigator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.openbpmn.bpmn.navigation; | ||
|
||
import java.util.Set; | ||
|
||
import org.openbpmn.bpmn.BPMNTypes; | ||
import org.openbpmn.bpmn.elements.Event; | ||
import org.openbpmn.bpmn.elements.core.BPMNElementNode; | ||
|
||
/** | ||
* The BPMNLinkNavigator navigates from a Link Throw Event to a Link Catch event | ||
* | ||
* | ||
*/ | ||
public class BPMNLinkNavigator { | ||
|
||
public BPMNElementNode findNext(BPMNElementNode node) { | ||
|
||
// is it a throw event? | ||
if (BPMNTypes.THROW_EVENT.equals(node.getType())) { | ||
String linkName = node.getName(); | ||
// now find the corresponding first catch event with the same name | ||
Set<? extends BPMNElementNode> filteredElementList = node.getBpmnProcess() | ||
.findElementNodes( | ||
n -> (BPMNTypes.CATCH_EVENT.equals(n.getType()) // | ||
&& ((Event) n).getEventDefinitionsByType(BPMNTypes.EVENT_DEFINITION_LINK) | ||
.size() > 0 // | ||
&& linkName.equals(n.getName()))); | ||
|
||
// return the first one... | ||
if (filteredElementList != null && filteredElementList.size() > 0) { | ||
return filteredElementList.iterator().next(); | ||
} | ||
|
||
} | ||
|
||
// no match | ||
return null; | ||
} | ||
|
||
} |
88 changes: 88 additions & 0 deletions
88
open-bpmn.metamodel/src/test/java/org/openbpmn/metamodel/navigation/TestEndNavigation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
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.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.core.BPMNElementNode; | ||
import org.openbpmn.bpmn.exceptions.BPMNModelException; | ||
import org.openbpmn.bpmn.navigation.BPMNEndElementIterator; | ||
import org.openbpmn.bpmn.util.BPMNModelFactory; | ||
import org.openbpmn.metamodel.examples.TestCreateEdges; | ||
|
||
/** | ||
* Test class to test the EndNavigator. | ||
* | ||
*/ | ||
public class TestEndNavigation { | ||
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 testFindEndTask1() throws BPMNModelException { | ||
|
||
logger.info("...read model"); | ||
BPMNModel model = BPMNModelFactory.read("/refmodel-navigation-01.bpmn"); | ||
BPMNProcess process = model.openDefaultProces(); | ||
|
||
// get start Task1 | ||
BPMNEndElementIterator endElements = new BPMNEndElementIterator<>(process, n -> n instanceof Activity); | ||
assertNotNull(endElements); | ||
assertTrue(endElements.hasNext()); | ||
|
||
BPMNElementNode task2 = endElements.next(); | ||
assertNotNull(task2); | ||
|
||
assertEquals("Task-2", task2.getName()); | ||
// no more elements expected | ||
assertFalse(endElements.hasNext()); | ||
} | ||
|
||
/** | ||
* This test loads model 'refmodel-navigation-02.bpmn' and finds 2 End Elements | ||
* (Task-2 and Task-3) | ||
* | ||
* @throws BPMNModelException | ||
* | ||
*/ | ||
@SuppressWarnings("rawtypes") | ||
@Test | ||
public void testFindStartTask1WithEvents() throws BPMNModelException { | ||
|
||
logger.info("...read model"); | ||
BPMNModel model = BPMNModelFactory.read("/refmodel-navigation-02.bpmn"); | ||
BPMNProcess process = model.openDefaultProces(); | ||
|
||
// get start Task1 | ||
BPMNEndElementIterator endElements = new BPMNEndElementIterator<>(process, n -> n instanceof Activity); | ||
assertNotNull(endElements); | ||
assertTrue(endElements.hasNext()); | ||
|
||
BPMNElementNode task = endElements.next(); | ||
assertNotNull(task); | ||
assertEquals("Task-3", task.getName()); | ||
|
||
// next should be task-2 | ||
task = endElements.next(); | ||
assertNotNull(task); | ||
assertEquals("Task-2", task.getName()); | ||
|
||
// no more elements expected | ||
assertFalse(endElements.hasNext()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters