-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(import): import BPMN Semantic from CSV file (#48)
Create BPMN Semantic using 2 CSV * one contains the 'nodes' * one contains the 'edges' Once the BPMN semantic is created, the full diagram can be generated
- Loading branch information
Showing
16 changed files
with
371 additions
and
20 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
38 changes: 38 additions & 0 deletions
38
java/src/main/java/io/process/analytics/tools/bpmn/generator/App2.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,38 @@ | ||
package io.process.analytics.tools.bpmn.generator; | ||
|
||
import static io.process.analytics.tools.bpmn.generator.internal.BpmnInOut.defaultBpmnInOut; | ||
import static io.process.analytics.tools.bpmn.generator.internal.FileUtils.fileContent; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import io.process.analytics.tools.bpmn.generator.input.CSVtoBPMN; | ||
import io.process.analytics.tools.bpmn.generator.internal.BpmnInOut; | ||
import io.process.analytics.tools.bpmn.generator.internal.generated.model.TDefinitions; | ||
import lombok.extern.log4j.Log4j2; | ||
|
||
@Log4j2 | ||
public class App2 extends App { | ||
|
||
public App2(BpmnInOut bpmnInOut) { | ||
super(bpmnInOut); | ||
} | ||
|
||
public static void main(String[] args) throws IOException { | ||
File nodeDiscovery = new File(args[0]); | ||
File edgeDiscovery = new File(args[1]); | ||
File outputFile = new File(args[2]); | ||
|
||
new App2(defaultBpmnInOut()).process(nodeDiscovery, edgeDiscovery, outputFile); | ||
} | ||
|
||
private void process(File nodeDiscovery, File edgeDiscovery, File outputFile) throws IOException { | ||
log.info("Converting CSV into internal model"); | ||
TDefinitions definitions = new CSVtoBPMN().readFromCSV(fileContent(nodeDiscovery), fileContent(edgeDiscovery)); | ||
log.info("Conversion done"); | ||
|
||
LayoutSortedDiagram layoutSortedDiagram = computeLayout(definitions); | ||
exportToBpmn(layoutSortedDiagram, outputFile); | ||
} | ||
|
||
} |
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
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
113 changes: 113 additions & 0 deletions
113
java/src/main/java/io/process/analytics/tools/bpmn/generator/input/CSVtoBPMN.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,113 @@ | ||
package io.process.analytics.tools.bpmn.generator.input; | ||
|
||
import static io.process.analytics.tools.bpmn.generator.internal.Semantic.addFlowNodes; | ||
import static io.process.analytics.tools.bpmn.generator.internal.Semantic.addSequenceFlows; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import javax.xml.XMLConstants; | ||
|
||
import io.process.analytics.tools.bpmn.generator.internal.Semantic; | ||
import io.process.analytics.tools.bpmn.generator.internal.generated.model.*; | ||
|
||
public class CSVtoBPMN { | ||
|
||
// map original flow element id with the values we are using | ||
// id cannot be numeric, in that case we map the id with a generated one, letting edge reference element ids with the values we are using for BPMN | ||
private final Map<String, String> mappingShapeId = new HashMap<>(); | ||
|
||
public TDefinitions readFromCSV(String nodes, String edges) { | ||
TProcess process = new TProcess(); | ||
process.setId("process_1"); | ||
TDefinitions definitions = new TDefinitions(); | ||
definitions.setId("definitions_1"); | ||
definitions.setTargetNamespace(XMLConstants.NULL_NS_URI); | ||
Semantic semantic = new Semantic(definitions); | ||
semantic.add(process); | ||
|
||
addFlowNodes(process, getFlowNodeElements(nodes)); | ||
addSequenceFlows(process, getEdgeElements(edges)); | ||
|
||
return definitions; | ||
} | ||
|
||
private List<TFlowNode> getFlowNodeElements(String nodes) { | ||
String[] lines = toLinesWithoutHeader(nodes); | ||
List<TFlowNode> flowElements = new ArrayList<>(); | ||
for (String line : lines) { | ||
if(line == null){ | ||
continue; | ||
} | ||
String[] node = line.split(","); | ||
TFlowNode userTask = new TUserTask(); | ||
userTask.setName(removeEnclosingDoubleQuote(node[2])); | ||
|
||
String originalId = node[1]; | ||
String bpmnId = originalId; | ||
if (isNumeric(originalId)) { | ||
bpmnId = "bpmnElement_" + originalId; | ||
} | ||
this.mappingShapeId.put(originalId, bpmnId); | ||
|
||
userTask.setId(bpmnId); | ||
flowElements.add(userTask); | ||
} | ||
return flowElements; | ||
} | ||
|
||
private static String removeEnclosingDoubleQuote(String s) { | ||
return s.replaceAll("^\"|\"$", ""); | ||
} | ||
|
||
private String[] toLinesWithoutHeader(String fileContent) { | ||
String[] lines = fileContent.split("\n"); | ||
lines[0] = null; | ||
return lines; | ||
} | ||
|
||
private List<TSequenceFlow> getEdgeElements(String edges) { | ||
String[] lines = toLinesWithoutHeader(edges); | ||
List<TSequenceFlow> flowElements = new ArrayList<>(); | ||
for (String line : lines) { | ||
if(line == null){ | ||
continue; | ||
} | ||
String[] edge = line.split(","); | ||
TSequenceFlow tSequenceFlow = new TSequenceFlow(); | ||
|
||
TUserTask sourceRef = new TUserTask(); | ||
tSequenceFlow.setSourceRef(sourceRef); | ||
sourceRef.setId(this.mappingShapeId.getOrDefault(edge[2], edge[2])); | ||
|
||
TUserTask targetRef = new TUserTask(); | ||
tSequenceFlow.setTargetRef(targetRef); | ||
targetRef.setId(this.mappingShapeId.getOrDefault(edge[3], edge[3])); | ||
|
||
String id = edge[1]; | ||
if (isNumeric(id)) { | ||
id = "sequenceFlow_" + id; | ||
} | ||
tSequenceFlow.setId(id); | ||
flowElements.add(tSequenceFlow); | ||
} | ||
return flowElements; | ||
} | ||
|
||
// TODO not optimal for performance, see https://www.baeldung.com/java-check-string-number | ||
// bpmn element id cannot be numeric | ||
private static boolean isNumeric(String s) { | ||
if (s == null) { | ||
return false; | ||
} | ||
try { | ||
Double.parseDouble(s); | ||
} catch (NumberFormatException nfe) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.