-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #237
- Loading branch information
Showing
5 changed files
with
422 additions
and
9 deletions.
There are no files selected for viewing
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
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
82 changes: 82 additions & 0 deletions
82
open-bpmn.metamodel/src/main/java/org/openbpmn/bpmn/navigation/BPMNStartElementIterator.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,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(); | ||
} | ||
|
||
} |
118 changes: 118 additions & 0 deletions
118
open-bpmn.metamodel/src/test/java/org/openbpmn/metamodel/navigation/TestStartNavigation.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,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()); | ||
} | ||
|
||
} |
Oops, something went wrong.