Skip to content

Commit

Permalink
Prepare tests
Browse files Browse the repository at this point in the history
- add BPMN conpare (1=1) based on camunda tool
- prepare all BPMN need to be tested
  • Loading branch information
NourEldin-Ali committed Jul 30, 2024
1 parent 9a29d61 commit 50fcfc2
Show file tree
Hide file tree
Showing 33 changed files with 5,155 additions and 341 deletions.
8 changes: 8 additions & 0 deletions open-bpmn.metamodel/open-bpmn.metamodel.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="AdditionalModuleElements">
<content url="file://$MODULE_DIR$" dumb="true">
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
</content>
</component>
</module>
23 changes: 22 additions & 1 deletion open-bpmn.metamodel/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,26 @@
<version>5.5.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependency>
<groupId>org.camunda.bpm.model</groupId>
<artifactId>camunda-bpmn-model</artifactId>
<version>7.22.0-alpha3</version>
</dependency>
</dependencies>

<repositories>
<repository>
<id>camunda-bpm-nexus</id>
<name>camunda-bpm-nexus</name>
<url>https://app.camunda.com/nexus/content/groups/public</url>
</repository>
</repositories>

<!-- <dependencies>-->
<!-- <dependency>-->
<!-- <groupId>org.camunda.bpm.model</groupId>-->
<!-- <artifactId>camunda-bpmn-model</artifactId>-->
<!-- <version>7.22.0-alpha3</version>-->
<!-- </dependency>-->
<!-- </dependencies>-->
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -451,16 +451,13 @@ public void DependencyGraphToBPMN() throws BPMNModelException, CloneNotSupported
// add loops
for (Pair<Set<String>, Set<String>> loop : loops) {
ArrayList<BPMNElementNode> sourceElements = new ArrayList<>();
for (String value : loop.getSource()) {
String sourceId = value;

for (String sourceId : loop.getSource()) {
BPMNElementNode sourceElement = (BPMNElementNode) process.findElementById(sourceId);
sourceElements.add(sourceElement);
}

ArrayList<BPMNElementNode> targetElements = new ArrayList<>();
for (String value : loop.getTarget()) {
String targetId = value;
for (String targetId : loop.getTarget()) {
BPMNElementNode targetElement = (BPMNElementNode) process.findElementById(targetId);
targetElements.add(targetElement);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.openbpmn.bpmn.discovery.compare;
import org.camunda.bpm.model.bpmn.instance.*;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

public class BPMNComparator {

public static boolean compareElements(List<FlowElement> elements1, List<FlowElement> elements2) {
Map<String, Integer> countMap1 = createCountMap(elements1);
Map<String, Integer> countMap2 = createCountMap(elements2);

return countMap1.equals(countMap2);
}

private static Map<String, Integer> createCountMap(List<FlowElement> elements) {
Map<String, Integer> countMap = new HashMap<>();

for (FlowElement element : elements) {
String key = "";
if (element instanceof Task) {
key = "Task:" + element.getName();
} else if (element instanceof Gateway) {
key = "Gateway:" + element.getElementType().getTypeName();
} else if (element instanceof StartEvent) {
key = "StartEvent:" + element.getElementType().getTypeName();
} else if (element instanceof EndEvent) {
key = "EndEvent:" + element.getElementType().getTypeName();
} else if (element instanceof IntermediateThrowEvent) {
key = "IntermediateEvent:" + element.getElementType().getTypeName();
} else if (element instanceof SubProcess) {
key = "SubProcess:" + element.getName();
}

countMap.put(key, countMap.getOrDefault(key, 0) + 1);
}

return countMap;
}

public static boolean compareFlows(List<SequenceFlow> flows1, List<SequenceFlow> flows2) {
if (flows1.size() != flows2.size()) {
return false;
}

for (SequenceFlow flow1 : flows1) {
boolean matchFound = false;
for (SequenceFlow flow2 : flows2) {
if (flow1.getSource().getElementType().getTypeName().equals(flow2.getSource().getElementType().getTypeName()) &&
flow1.getTarget().getElementType().getTypeName().equals(flow2.getTarget().getElementType().getTypeName())) {
matchFound = true;
break;
}
}
if (!matchFound) {
return false;
}
}
return true;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.openbpmn.bpmn.discovery.compare;

import org.camunda.bpm.model.bpmn.BpmnModelInstance;
import org.camunda.bpm.model.bpmn.instance.FlowElement;
import org.camunda.bpm.model.bpmn.instance.SequenceFlow;

import java.util.List;

public class BPMNComparatorExecutor {
public static void main(String[] args) {
BPMNComparatorExecutor.execute(
"C:\\Users\\AliNourEldin\\Desktop\\da-bpmn\\open-bpmn\\open-bpmn.metamodel\\src\\test\\resources\\discovery\\loop\\test.bpmn",
"C:\\Users\\AliNourEldin\\Desktop\\da-bpmn\\open-bpmn\\open-bpmn.metamodel\\src\\test\\resources\\discovery\\loop\\test1.bpmn"
);
}
public static boolean execute(String path1, String path2) {
BpmnModelInstance model1 = BPMNLoader.loadBPMN(path1);
BpmnModelInstance model2 = BPMNLoader.loadBPMN(path2);

List<FlowElement> elements1 = BPMNExtractor.extractElements(model1);
List<FlowElement> elements2 = BPMNExtractor.extractElements(model2);

List<SequenceFlow> flows1 = BPMNExtractor.extractFlows(model1);
List<SequenceFlow> flows2 = BPMNExtractor.extractFlows(model2);

boolean areElementsEqual = BPMNComparator.compareElements(elements1, elements2);
boolean areFlowsEqual = BPMNComparator.compareFlows(flows1, flows2);

if (areElementsEqual && areFlowsEqual) {
System.out.println("The BPMN models are equivalent.");
return true;
} else {
System.out.println("The BPMN models are not equivalent.");
return false;
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.openbpmn.bpmn.discovery.compare;

import org.camunda.bpm.model.bpmn.instance.*;
import java.util.*;
import org.camunda.bpm.model.bpmn.BpmnModelInstance;


public class BPMNExtractor {
public static List<FlowElement> extractElements(BpmnModelInstance modelInstance) {
Collection<FlowElement> flowElements = modelInstance.getModelElementsByType(FlowElement.class);
return new ArrayList<>(flowElements);
}

public static List<SequenceFlow> extractFlows(BpmnModelInstance modelInstance) {
return new ArrayList<>(modelInstance.getModelElementsByType(SequenceFlow.class));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.openbpmn.bpmn.discovery.compare;
import org.camunda.bpm.model.bpmn.Bpmn;
import org.camunda.bpm.model.bpmn.BpmnModelInstance;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.IOException;

public class BPMNLoader {
public static BpmnModelInstance loadBPMN(String filePath) {
cleanBPMN(filePath);
return Bpmn.readModelFromFile(new File(filePath));
}

static void cleanBPMN(String path) {
try {
// Load the XML file
File inputFile = new File(path);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;

dBuilder = dbFactory.newDocumentBuilder();

Document doc = dBuilder.parse(inputFile);
doc.getDocumentElement().normalize();

// Get all elements with the expand attribute
NodeList nodeList = doc.getElementsByTagName("*");
for (int i = 0; i < nodeList.getLength(); i++) {
Element element = (Element) nodeList.item(i);
if (element.hasAttribute("expand")) {
element.removeAttribute("expand");
}
if (element.hasAttribute("num")) {
element.removeAttribute("num");
}
}

// Save the modified XML to a new file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(path));
transformer.transform(source, result);

} catch (ParserConfigurationException | SAXException | IOException | TransformerException e) {
System.out.println(path);
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Loading

0 comments on commit 50fcfc2

Please sign in to comment.