Skip to content

Commit

Permalink
added new filter method
Browse files Browse the repository at this point in the history
Issue #238
  • Loading branch information
rsoika committed Apr 26, 2023
1 parent f2c655a commit b799091
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.Predicate;
import java.util.logging.Logger;

import org.openbpmn.bpmn.BPMNModel;
Expand Down Expand Up @@ -1057,6 +1058,26 @@ public BPMNElementNode findElementNodeById(String id) {
return null;
}

/**
* Returns a filtered list of elements
*
* @param <T>
*
* @param <T>
* @param id
* @return
*/
public <T> Set<? extends BPMNElementNode> findElementNodes(Predicate<BPMNElementNode> filter) {
Set<BPMNElementNode> result = new LinkedHashSet<BPMNElementNode>();
Set<BPMNElementNode> allElements = this.getAllElementNodes();
for (BPMNElementNode _node : allElements) {
if (filter.test(_node)) {
result.add(_node);
}
}
return result;

}
/**
* Returns all BPMNFlowElements contained in this process
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.openbpmn.metamodel.navigation;

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

import java.util.Set;
import java.util.logging.Logger;

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

/**
* This JUnit test verifies the filter methods
*
*/
public class TestFilter {
private static Logger logger = Logger.getLogger(TestCreateEdges.class.getName());

/**
* Loads a model and test the filter method.
*
* The method applies a filter to match ThrowEvents only within a process.
*
* @throws BPMNModelException
*
*/
@Test
public void testFilterMethod() throws BPMNModelException {

logger.info("...read model");
BPMNModel model = BPMNModelFactory.read("/refmodel-15.bpmn");

assertEquals(1, model.getProcesses().size());
BPMNProcess process = model.openDefaultProces();

Set<? extends BPMNElementNode> throwEvents = process
.findElementNodes(n -> BPMNTypes.THROW_EVENT.equals(n.getType()));

assertNotNull(throwEvents);
// We expect exactly one result.

assertEquals(1, throwEvents.size());

}

}

0 comments on commit b799091

Please sign in to comment.