Skip to content

Commit

Permalink
refactoring, fixes
Browse files Browse the repository at this point in the history
issue #823
  • Loading branch information
rsoika committed Sep 1, 2024
1 parent e9447a9 commit 053560a
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 43 deletions.
179 changes: 141 additions & 38 deletions imixs-workflow-core/src/main/java/org/imixs/workflow/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import org.imixs.workflow.exceptions.PluginException;
import org.openbpmn.bpmn.BPMNModel;
import org.openbpmn.bpmn.BPMNNS;
import org.openbpmn.bpmn.BPMNTypes;
import org.openbpmn.bpmn.elements.Activity;
import org.openbpmn.bpmn.elements.BPMNProcess;
import org.openbpmn.bpmn.elements.Event;
import org.openbpmn.bpmn.elements.Participant;
import org.openbpmn.bpmn.elements.core.BPMNElement;
import org.openbpmn.bpmn.elements.core.BPMNElementNode;
import org.openbpmn.bpmn.exceptions.BPMNModelException;
Expand Down Expand Up @@ -348,7 +350,7 @@ public BPMNModel findModelByWorkitem(ItemCollection workitem) throws ModelExcept

// Still no match, try to find model version by group
if (!workitem.getWorkflowGroup().isEmpty()) {
List<String> versions = findVersionsByGroup(workitem.getWorkflowGroup());
List<String> versions = findAllVersionsByGroup(workitem.getWorkflowGroup());
if (!versions.isEmpty()) {
String newVersion = versions.get(0);
if (!newVersion.isEmpty()) {
Expand All @@ -371,14 +373,54 @@ public BPMNModel findModelByWorkitem(ItemCollection workitem) throws ModelExcept
}

/**
* This method returns a sorted list of model versions containing the requested
* workflow group. The result is sorted in reverse order, so the highest version
* number is the first in the result list.
* This method returns a sorted list of all workflow groups contained in a BPMN
* model.
* <p>
* In case the model is a collaboration diagram, the method returns only group
* names from private process instances (Pools)!
*
* @param group
* @return
*/
public List<String> findVersionsByGroup(String group) {
public Set<String> findAllGroups(BPMNModel _model) {
Set<String> result = new LinkedHashSet<>();

Set<BPMNProcess> processList = _model.getProcesses();
for (BPMNProcess _process : processList) {
String groupName = _process.getName();
if (_model.isCollaborationDiagram()) {
// collaboration diagram - only add private processes (Pools)
if (BPMNTypes.PROCESS_TYPE_PRIVATE.equals(_process.getProcessType())) {
// add only private process types
result.add(groupName);
}
} else {
// if it is not a collaboration diagram we return the name of the first Public
// Process
if (BPMNTypes.PROCESS_TYPE_PUBLIC.equals(_process.getProcessType())) {
result.add(groupName);
break;
}
}
}

if (result.size() == 0) {
logger.warning("Model " + BPMNUtil.getVersion(_model)
+ " does not contain valid process elements! Please check your model file!");
}

return result;
}

/**
* Returns a sorted list of all model versions containing the requested
* workflow group. The result is sorted in reverse order, so the highest version
* number is the first in the result list.
*
* @param group - name of the workflow group
* @return list of matching model versions
*/
public List<String> findAllVersionsByGroup(String group) {
boolean debug = logger.isLoggable(Level.FINE);
List<String> result = new ArrayList<String>();
if (debug) {
Expand All @@ -387,19 +429,37 @@ public List<String> findVersionsByGroup(String group) {
// try to find matching model version by group
Collection<BPMNModel> models = modelStore.values();
for (BPMNModel _model : models) {

Set<BPMNProcess> processList = _model.getProcesses();
for (BPMNProcess _process : processList) {
if (group.equals(_process.getName())) {
result.add(BPMNUtil.getVersion(_model));
}
Set<String> allGroups = findAllGroups(_model);
if (allGroups.contains(group)) {
result.add(BPMNUtil.getVersion(_model));
}
}
// sort result
Collections.sort(result, Collections.reverseOrder());
return result;
}

/**
* Returns the first matching model version by a given workflow group.
* <p>
* If multiple models are containing the same group the latest version will be
* returned.
* <p>
* In case the model is a collaboration diagram, the method compares the given
* group name only with private process instances (Pools)!
*
* @param group
* @return
*/
public String findVersionByGroup(String group) {
String result = null;
List<String> versions = findAllVersionsByGroup(group);
if (versions.size() > 0) {
result = versions.get(0);
}
return result;
}

/**
* This method returns a sorted list of model versions matching a given regex
* for a model version. The result is sorted in reverse order, so the highest
Expand Down Expand Up @@ -427,22 +487,6 @@ public List<String> findVersionsByRegEx(String modelRegex) {
return result;
}

/**
* This method returns a sorted list of all workflow groups contained in a BPMN
* model
*
* @param group
* @return
*/
public Set<String> findAllGroups(BPMNModel _model) {
Set<String> result = new LinkedHashSet<>();
Set<BPMNProcess> processList = _model.getProcesses();
for (BPMNProcess _process : processList) {
result.add(_process.getName());
}
return result;
}

/**
* This method finds a Imixs task element by its ID (imixs:activityid).
* The method returns a cloned Instance of the model entity to avoid
Expand Down Expand Up @@ -494,14 +538,41 @@ public ItemCollection findEventByID(final BPMNModel model, int taskID, int event

/**
* Returns a list of start Tasks of a given Process Group
* <p>
* In case of a collaboration diagram only Pool names are compared. The default
* process (Public Process) will be ignored.
*
* @param processGroup
* @return
* @param model - the BPMN model
* @param processGroup - the name of the process group to match
* @return list of all Start Task entities or null if not process with the given
* name was found.
* @throws BPMNModelException
*/
public List<ItemCollection> findStartTasks(final BPMNModel model, String processGroup) throws BPMNModelException {
List<ItemCollection> result = new ArrayList<>();
BPMNProcess process = model.findProcessByName(processGroup);
BPMNProcess process = null;

if (processGroup == null || processGroup.isEmpty()) {
logger.warning("findEndTasks processGroup is empty!");
return result;
}
// find Process containing matching the process group
if (model.isCollaborationDiagram()) {
Set<Participant> poolList = model.getParticipants();
for (Participant pool : poolList) {
process = pool.openProcess();
if (processGroup.equals(process.getName())) {
break;
}
}
} else {
process = model.openDefaultProces();
if (!processGroup.equals(process.getName())) {
// no match!
process = null;
}
}

// test start task....
BPMNStartElementIterator<Activity> startElements = new BPMNStartElementIterator<>(process,
node -> (node instanceof Activity));
Expand All @@ -513,20 +584,52 @@ public List<ItemCollection> findStartTasks(final BPMNModel model, String process

/**
* Returns a list of End Tasks of a given Process Group
* <p>
* In case of a collaboration diagram only Pool names are compared. The default
* process (Public Process) will be ignored.
*
* @param processGroup
* @return
* @param model - the BPMN model
* @param processGroup - the name of the process group to match
* @return list of all End Task entities or null if not process with the given
* name was found.
* @throws BPMNModelException
*/
public List<ItemCollection> findEndTasks(final BPMNModel model, String processGroup) throws BPMNModelException {
List<ItemCollection> result = new ArrayList<>();
BPMNProcess process = model.findProcessByName(processGroup);
// test End task....
BPMNEndElementIterator<Activity> endElements = new BPMNEndElementIterator<>(process,
node -> (node instanceof Activity));
while (endElements.hasNext()) {
result.add(BPMNEntityBuilder.build(endElements.next()));
BPMNProcess process = null;

if (processGroup == null || processGroup.isEmpty()) {
logger.warning("findEndTasks processGroup is empty!");
return result;
}

// find Process containing matching the process group
if (model.isCollaborationDiagram()) {
Set<Participant> poolList = model.getParticipants();
for (Participant pool : poolList) {
process = pool.openProcess();
if (processGroup.equals(process.getName())) {
break;
}
}
} else {
process = model.openDefaultProces();
if (!processGroup.equals(process.getName())) {
// no match!
process = null;
}
}

// now get the End task ...
if (process != null) {
// test End task....
BPMNEndElementIterator<Activity> endElements = new BPMNEndElementIterator<>(process,
node -> (node instanceof Activity));
while (endElements.hasNext()) {
result.add(BPMNEntityBuilder.build(endElements.next()));
}
}

return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public void testSingleGroup() {
/**
* This test tests the resolution of multiple groups in a collaboration diagram
*
* The method findAllGroups should return only the group names from the Pools
* but not the default process name.
*
* @throws ParseException
* @throws ParserConfigurationException
* @throws SAXException
Expand All @@ -85,10 +88,10 @@ public void testMultiGroups()

// Test Groups
Set<String> groups = openBPMNModelManager.findAllGroups(model);
Assert.assertEquals(3, groups.size());
Assert.assertEquals(2, groups.size());
Assert.assertTrue(groups.contains("Protokoll"));
Assert.assertTrue(groups.contains("Protokollpunkt"));
Assert.assertTrue(groups.contains("Default Process"));
Assert.assertFalse(groups.contains("Default Process"));

// Test tasks per group
BPMNProcess process = null;
Expand All @@ -107,6 +110,11 @@ public void testMultiGroups()
}
activities = process.getActivities();
Assert.assertEquals(4, activities.size());

// test the Default Group (Public Process)
BPMNProcess defaultProcess = model.openDefaultProces();
Assert.assertEquals("Default Process", defaultProcess.getName());

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public void startup() {
logger.info("...initializing models...");

// first we scan for default models
List<String> models = modelService.getVersions();
List<String> models = modelService.getModelManager().getVersions();
if (models.isEmpty() || modelDefaultDataOverwrite == true) {
scanDefaultModels();
} else {
Expand All @@ -179,7 +179,7 @@ public void startup() {
migrateWorkflowScheduler();

// Finally start optional schedulers
logger.info("...initalizing schedulers...");
logger.info("...initializing schedulers...");
schedulerService.startAllSchedulers();

}
Expand All @@ -190,7 +190,7 @@ public void startup() {
* @return
*/
public int getModelVersionCount() {
return modelService.getVersions().size();
return modelService.getModelManager().getVersions().size();
}

/**
Expand Down

0 comments on commit 053560a

Please sign in to comment.