Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
Issue #823
  • Loading branch information
rsoika committed Sep 2, 2024
1 parent 542b54f commit d7fc909
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 250 deletions.
105 changes: 66 additions & 39 deletions imixs-workflow-core/src/main/java/org/imixs/workflow/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -58,6 +58,7 @@ public class ModelManager {
// cache
private final Map<String, ItemCollection> bpmnEntityCache = new ConcurrentHashMap<>();
private final Map<String, BPMNElement> bpmnElementCache = new ConcurrentHashMap<>();
private final Map<String, Set<String>> groupCache = new ConcurrentHashMap<>();

private RuleEngine ruleEngine = null;

Expand Down Expand Up @@ -310,7 +311,7 @@ public List<String> getVersions() {
// convert to List
List<String> result = new ArrayList<>();
result.addAll(versions);
Collections.sort(result);
Collections.sort(result, Collections.reverseOrder());
return result;
}

Expand All @@ -335,7 +336,7 @@ public BPMNModel findModelByWorkitem(ItemCollection workitem) throws ModelExcept
} else {
// try to find model by regex if version is not empty...
if (version != null && !version.isEmpty()) {
List<String> matchingVersions = findVersionsByRegEx(version);
Set<String> matchingVersions = findVersionsByRegEx(version);
for (String matchingVersion : matchingVersions) {
result = modelStore.get(matchingVersion);
if (result != null) {
Expand All @@ -350,9 +351,9 @@ public BPMNModel findModelByWorkitem(ItemCollection workitem) throws ModelExcept

// Still no match, try to find model version by group
if (!workitem.getWorkflowGroup().isEmpty()) {
List<String> versions = findAllVersionsByGroup(workitem.getWorkflowGroup());
Set<String> versions = findAllVersionsByGroup(workitem.getWorkflowGroup());
if (!versions.isEmpty()) {
String newVersion = versions.get(0);
String newVersion = versions.iterator().next();
if (!newVersion.isEmpty()) {
logger.log(Level.WARNING, "Deprecated model version: ''{0}'' -> migrating to ''{1}'',"
+ " $workflowgroup: ''{2}'', $uniqueid: {3}",
Expand Down Expand Up @@ -382,33 +383,59 @@ public BPMNModel findModelByWorkitem(ItemCollection workitem) throws ModelExcept
* @param group
* @return
*/
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;
public Set<String> findAllGroupsByModel(BPMNModel _model) {
Set<String> result = null;
// test cache
String version = BPMNUtil.getVersion(_model);
result = groupCache.get(version);
if (result == null) {
result = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);

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!");
}
// finally cache the new group set
groupCache.put(version, result);
}

if (result.size() == 0) {
logger.warning("Model " + BPMNUtil.getVersion(_model)
+ " does not contain valid process elements! Please check your model file!");
}
// Create an immutable set from the sorted set
return Set.copyOf(result);
}

/**
* This method returns a sorted list of all unique workflow groups contained in
* the model store.
* <p>
* In case the model is a collaboration diagram, the method returns only group
* names from private process instances (Pools)!
*
* @param group
* @return
*/
public Set<String> findAllGroups() {
Set<String> result = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
for (BPMNModel model : modelStore.values()) {
result.addAll(findAllGroupsByModel(model));
}
return result;
}

Expand All @@ -420,22 +447,22 @@ public Set<String> findAllGroups(BPMNModel _model) {
* @param group - name of the workflow group
* @return list of matching model versions
*/
public List<String> findAllVersionsByGroup(String group) {
public Set<String> findAllVersionsByGroup(String group) {
boolean debug = logger.isLoggable(Level.FINE);
List<String> result = new ArrayList<String>();
// Sorted in reverse order
Set<String> result = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
if (debug) {
logger.log(Level.FINEST, "......searching model versions for workflowgroup ''{0}''...", group);
}
// try to find matching model version by group
Collection<BPMNModel> models = modelStore.values();
for (BPMNModel _model : models) {
Set<String> allGroups = findAllGroups(_model);
Set<String> allGroups = findAllGroupsByModel(_model);
if (allGroups.contains(group)) {
result.add(BPMNUtil.getVersion(_model));
}
}
// sort result
Collections.sort(result, Collections.reverseOrder());

return result;
}

Expand All @@ -453,9 +480,9 @@ public List<String> findAllVersionsByGroup(String group) {
*/
public String findVersionByGroup(String group) {
String result = null;
List<String> versions = findAllVersionsByGroup(group);
Set<String> versions = findAllVersionsByGroup(group);
if (versions.size() > 0) {
result = versions.get(0);
result = versions.iterator().next();
}
return result;
}
Expand All @@ -468,9 +495,11 @@ public String findVersionByGroup(String group) {
* @param group
* @return
*/
public List<String> findVersionsByRegEx(String modelRegex) {
public Set<String> findVersionsByRegEx(String modelRegex) {
boolean debug = logger.isLoggable(Level.FINE);
List<String> result = new ArrayList<String>();
// List<String> result = new ArrayList<String>();
// Sorted in reverse order
Set<String> result = new TreeSet<>(Collections.reverseOrder());
if (debug) {
logger.log(Level.FINEST, "......searching model versions for regex ''{0}''...", modelRegex);
}
Expand All @@ -482,8 +511,6 @@ public List<String> findVersionsByRegEx(String modelRegex) {
result.add(_version);
}
}
// sort result
Collections.sort(result, Collections.reverseOrder());
return result;
}

Expand Down Expand Up @@ -680,6 +707,7 @@ public boolean evaluateCondition(String expression, ItemCollection workitem) {
private void clearCache() {
bpmnEntityCache.clear();
bpmnElementCache.clear();
groupCache.clear();
}

/**
Expand Down Expand Up @@ -741,7 +769,6 @@ private Activity lookupTaskElementByID(final BPMNModel model, int taskID) {
String id = activity.getExtensionAttribute(BPMNUtil.getNamespace(), "processid");
try {
if (taskID == Long.parseLong(id)) {
logger.info("lookupTaskElementByID " + taskID + " took " + (System.currentTimeMillis() - l) + "ms");
return activity;
}
} catch (NumberFormatException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,13 +427,7 @@ private ItemCollection processEvent(ItemCollection workitem, ItemCollection even
BPMNModel model = this.ctx.getModelManager().getModel(workitem.getModelVersion());
// set $lastEventDate
workitem.replaceItemValue(LASTEVENTDATE, new Date());

// invalidate deprecated models!
if (event.hasItem("keyFollowUp")) {
throw new ModelException(ModelException.INVALID_MODEL_ENTRY,
"Invalid Event Item: keyFollowUp no longer supported!");
}

// Execute Plugins and Adapters....
workitem = executeMicroKernels(workitem, event);

// test if a new model version was assigned by the last event
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,12 @@ public void testModifyTask() throws ModelException {

// test task 1000
ItemCollection task = openBPMNModelManager.findTaskByID(model, 1000);

Assert.assertNotNull(task);
Assert.assertEquals("Task 1", task.getItemValueString("name"));
Assert.assertEquals("Some documentation...", task.getItemValueString("documentation"));

// change some attributes of task....
task.replaceItemValue("txtworkflowgroup", "test");
Assert.assertEquals("test", task.getItemValueString("txtworkflowgroup"));

// test task 1000 once again
task = openBPMNModelManager.findTaskByID(model, 1000);
Assert.assertNotNull(task);
Expand All @@ -194,25 +191,20 @@ public void testModifyTask() throws ModelException {
*/
@Test
public void testModifyDefinition() throws ModelException {

ItemCollection workitem = new ItemCollection();
workitem.model("1.0.0");

// test definition
ItemCollection definition = openBPMNModelManager.loadDefinition(model);

Assert.assertNotNull(definition);
Assert.assertEquals("1.0.0", definition.getItemValueString("$ModelVersion"));

// change name of definition....
definition.replaceItemValue("$ModelVersion", "test");
Assert.assertEquals("test", definition.getItemValueString("$ModelVersion"));

// test definition once again
definition = openBPMNModelManager.loadDefinition(model);
Assert.assertNotNull(definition);
Assert.assertEquals("1.0.0", definition.getItemValueString("$ModelVersion"));

}

/**
Expand All @@ -227,18 +219,20 @@ public void testModifyDefinition() throws ModelException {
*/
@Test
public void testModifyGroups() throws ModelException {

Set<String> groups = openBPMNModelManager.findAllGroups(model);
Set<String> groups = openBPMNModelManager.findAllGroupsByModel(model);
Assert.assertNotNull(groups);
Assert.assertEquals(1, groups.size());
// add a new group....
groups.add("test-group1");
Assert.assertEquals(2, groups.size());
// test groups once again
groups = openBPMNModelManager.findAllGroups(model);
Assert.assertNotNull(groups);
Assert.assertEquals(1, groups.size());

try {
groups.add("test-group1");
Assert.fail();
} catch (UnsupportedOperationException e) {
// we expect a UnsupportedOperationException
// test groups once again
groups = openBPMNModelManager.findAllGroupsByModel(model);
Assert.assertNotNull(groups);
Assert.assertEquals(1, groups.size());
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void testSimple()
Assert.assertEquals("WorkflowEnvironmentEntity", profile.getItemValueString("type"));
Assert.assertEquals("1.0.0", profile.getItemValueString("$ModelVersion"));

Set<String> groups = openBPMNModelManager.findAllGroups(model);
Set<String> groups = openBPMNModelManager.findAllGroupsByModel(model);
// List<String> groups = model.getGroups();
// Test Groups
Assert.assertFalse(groups.contains("Collaboration"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void testSimple() throws ModelException {
BPMNModel model = openBPMNModelManager.getModel("1.0.0");
Assert.assertNotNull(model);

Set<String> groups = openBPMNModelManager.findAllGroups(model);
Set<String> groups = openBPMNModelManager.findAllGroupsByModel(model);
// Test Groups
Assert.assertFalse(groups.contains("Collaboration"));
Assert.assertTrue(groups.contains("Protokoll"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void testSingleGroup() {
Assert.fail(e.getMessage());
}
// Test Groups
Set<String> groups = openBPMNModelManager.findAllGroups(model);
Set<String> groups = openBPMNModelManager.findAllGroupsByModel(model);

Assert.assertTrue(groups.contains("Simple"));

Expand Down Expand Up @@ -87,7 +87,7 @@ public void testMultiGroups()
}

// Test Groups
Set<String> groups = openBPMNModelManager.findAllGroups(model);
Set<String> groups = openBPMNModelManager.findAllGroupsByModel(model);
Assert.assertEquals(2, groups.size());
Assert.assertTrue(groups.contains("Protokoll"));
Assert.assertTrue(groups.contains("Protokollpunkt"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void testSimple() throws ParseException,
Assert.fail();
}

Set<String> groups = openBPMNModelManager.findAllGroups(model);
Set<String> groups = openBPMNModelManager.findAllGroupsByModel(model);
Assert.assertTrue(groups.contains("Message Example"));

// test count of elements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void testLinkEventSimple() {
}

// Test Environment
Assert.assertTrue(openBPMNModelManager.findAllGroups(model).contains("Simple"));
Assert.assertTrue(openBPMNModelManager.findAllGroupsByModel(model).contains("Simple"));

// test count of elements
Assert.assertEquals(3, model.findAllActivities().size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void testSimple() throws ModelException {
Assert.assertEquals("org.imixs.workflow.plugins.HistoryPlugin", plugins.get(2));
Assert.assertEquals("org.imixs.workflow.plugins.ResultPlugin", plugins.get(3));

Set<String> groups = openBPMNModelManager.findAllGroups(model);
Set<String> groups = openBPMNModelManager.findAllGroupsByModel(model);
Assert.assertTrue(groups.contains("Ticket"));

// test task 1000
Expand Down Expand Up @@ -149,7 +149,7 @@ public void testSimpleDeprecatedItemNames() throws ModelException {
Assert.assertEquals("org.imixs.workflow.plugins.HistoryPlugin", plugins.get(2));
Assert.assertEquals("org.imixs.workflow.plugins.ResultPlugin", plugins.get(3));

Set<String> groups = openBPMNModelManager.findAllGroups(model);
Set<String> groups = openBPMNModelManager.findAllGroupsByModel(model);
Assert.assertTrue(groups.contains("Ticket"));

// test task 1000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ public class ModelService {

private static final Logger logger = Logger.getLogger(ModelService.class.getName());

private ModelManager modelManager = null;
protected ModelManager modelManager = null;

@Inject
private DocumentService documentService;
protected DocumentService documentService;

@Resource
private SessionContext ctx;
protected SessionContext ctx;

public ModelService() {
super();
Expand Down
Loading

0 comments on commit d7fc909

Please sign in to comment.