BPMN2DiagramConverter { public List<BasicDiagram> getDiagramFromBpmn20(Definitions definitions) { final Map<BaseElement,DiagramElement> bpmndiMap = new HashMap<>(); final Set<String> messageRefSet = new HashSet<>(); logger.fine("Populating id map"); for (BPMNDiagram bpmnDiagram : definitions.getDiagram()) { for (DiagramElement element : bpmnDiagram.getBPMNPlane().getDiagramElement()) { element.acceptVisitor(new AbstractVisitor() { DiagramElement bpmndiElement = null; @Override public void visitBaseElement(BaseElement that) { that._diagramElement = bpmndiElement; logger.finer(that.getId() + " -> " + bpmndiElement.getId()); bpmndiMap.put(that, bpmndiElement); } @Override public void visitMessageFlow(MessageFlow that) { if (that.getMessageRef() != null) { messageRefSet.add(that.getMessageRef().getId()); } } @Override public void visitBpmnEdge(BPMNEdge that) { super.visitBpmnEdge(that); bpmndiElement = that; assert that.getBpmnElement() != null : that.getId() + " has no bpmnElement attribute"; if (that.getBpmnElement() == null) { logger.warning(that.getId() + " has no bpmnElement attribute"); } else { that.getBpmnElement().acceptVisitor(this); } } @Override public void visitBpmnShape(BPMNShape that) { super.visitBpmnShape(that); bpmndiElement = that; assert that.getBpmnElement() != null : that.getId() + " has no bpmnElement attribute"; if (that.getBpmnElement() == null) { logger.warning(that.getId() + " has no bpmnElement attribute"); } else { that.getBpmnElement().acceptVisitor(this); } } }); } } logger.fine("Populating transient JAXB fields"); for (BPMNDiagram bpmnDiagram : definitions.getDiagram()) { for (DiagramElement element : bpmnDiagram.getBPMNPlane().getDiagramElement()) { logger.finer("Scanning " + element.getId()); element.acceptVisitor(new AbstractVisitor() { BPMNEdge precedingEdge = null; @Override public void visitBpmnEdge(BPMNEdge that) { if (that.getBpmnElement() == null) { logger.warning(that.getId() + " has no bpmnElement attribute"); } else { precedingEdge = that; that.getBpmnElement().acceptVisitor(this); } } @Override public void visitBpmnShape(BPMNShape that) { if (that.getBpmnElement() == null) { logger.warning(that.getId() + " has no bpmnElement attribute"); } else { that.getBpmnElement().acceptVisitor(this); } } @Override public void visitEdge(Edge that) { logger.finer(that.getId() + ": " + that.getSourceRef() + " -> " + that.getTargetRef()); if (that.getSourceRef() == null) { logger.warning(that.getId() + " has no sourceRef attribute"); } else { that.getSourceRef().getOutgoing().add(that); if(precedingEdge!=null) precedingEdge.setSourceElement(bpmndiMap.get(that.getSourceRef())); } if (that.getTargetRef() == null) { logger.warning(that.getId() + " has no targetRef attribute"); } else { that.getTargetRef().getIncoming().add(that); if(precedingEdge!=null) precedingEdge.setTargetElement(bpmndiMap.get(that.getTargetRef())); } } @Override public void visitBoundaryEvent(BoundaryEvent that) { if (that.getAttachedToRef() == null) { throw new IllegalArgumentException(that.getId() + " has no attachedToRef attribute"); } that.getAttachedToRef().getAttachedBoundaryEvents().addAll(that.getAttachedToRef().getBoundaryEventRefs()); } }); } } final Set<SequenceFlow> absentInConfiguration = new HashSet<>(); for (BPMNDiagram bpmnDiagram : definitions.getDiagram()) { for (DiagramElement element : bpmnDiagram.getBPMNPlane().getDiagramElement()) { logger.finer("Re-scanning " + element.getId()); element.acceptVisitor(new AbstractVisitor() { @Override public void visitBpmnEdge(BPMNEdge that) { if (that.getBpmnElement() == null) { logger.warning(that.getId() + " has no bpmnElement attribute"); } else { that.getBpmnElement().acceptVisitor(this); } } @Override public void visitBpmnShape(BPMNShape that) { if (that.getBpmnElement() == null) { logger.warning(that.getId() + " has no bpmnElement attribute"); } else { that.getBpmnElement().acceptVisitor(this); } } @Override public void visitGateway(Gateway that) { ExtensionElements extensionElements = that.getExtensionElements(); if (extensionElements == null) { return; } Configurable configurable = extensionElements.getFirstExtensionElementOfType(Configurable.class); if (configurable == null || configurable.getConfiguration() == null) { return; } switch (that.getGatewayDirection()) { case CONVERGING: case MIXED: List<SequenceFlow> absentInflows = new ArrayList<>(that.getIncomingSequenceFlows()); absentInflows.removeAll(configurable.getConfiguration().getSourceRefs()); absentInConfiguration.addAll(absentInflows); break; } switch (that.getGatewayDirection()) { case DIVERGING: case MIXED: List<SequenceFlow> absentOutflows = new ArrayList<>(that.getOutgoingSequenceFlows()); absentOutflows.removeAll(configurable.getConfiguration().getTargetRefs()); absentInConfiguration.addAll(absentOutflows); break; } } }); } } List<BasicDiagram> diagrams = new ArrayList<>(); logger.fine("Generating JSON diagram from BPMN JAXB"); BasicDiagram diagram = new BasicDiagram( "canvas", "BPMNDiagram", new StencilSetReference( "http: rootDir + "stencilsets ) ); diagram.addSsextension("http: diagram.setBounds(new Bounds(new Point(0, 0), new Point(2400, 2000))); diagram.setProperty("expressionlanguage", "http: diagram.setProperty("targetnamespace", "http: diagram.setProperty("typelanguage", "http: Map<Process, BasicShape> poolMap = new HashMap<>(); BasicShape parentShape = diagram; for (BaseElement root: definitions.getRootElement()) { ExtensionElements extensionElements = root.getExtensionElements(); if (extensionElements != null) { ConfigurationMapping configurationMapping = extensionElements.getFirstExtensionElementOfType(ConfigurationMapping.class); if (configurationMapping != null) { diagram.setProperty("cmap", configurationMapping.getHref()); } } if (root instanceof Collaboration) { for (Participant participant: ((Collaboration) root).getParticipant()) { DiagramElement pool = bpmndiMap.get(participant); BPMN2DiagramConverterVisitor visitor = new BPMN2DiagramConverterVisitor(diagram, bpmndiMap, absentInConfiguration, 0, 0); pool.acceptVisitor(visitor); BasicShape poolShape = visitor.getShape(); diagram.addChildShape(poolShape); if (participant.getProcessRef() != null) { poolMap.put(participant.getProcessRef(), poolShape); parentShape = poolShape; } } } if (root instanceof Process) { Process process = (Process) root; BasicShape poolShape = poolMap.get(process); Map<FlowNode, BasicShape> laneMap = new HashMap<>(); for (LaneSet pool: process.getLaneSet()) { for (Lane lane: pool.getAllLanes()) { DiagramElement laneDiagramElement = bpmndiMap.get(lane); BPMN2DiagramConverterVisitor visitor = new BPMN2DiagramConverterVisitor(diagram, bpmndiMap, absentInConfiguration, 0, 0); laneDiagramElement.acceptVisitor(visitor); BasicShape laneShape = visitor.getShape(); poolShape.addChildShape(laneShape); for (FlowNode flowNode: lane.getFlowNodeRef()) { laneMap.put(flowNode, laneShape); } } } flowElementsToShapes(process.getFlowElement(), parentShape, bpmndiMap, messageRefSet, absentInConfiguration, 0, 0, laneMap); } } diagrams.add(diagram); return diagrams; } BPMN2DiagramConverter(String rootDir); List<BasicDiagram> getDiagramFromBpmn20(Definitions definitions); void getBPMN(String bpmnString, String encoding, OutputStream jsonStream, ClassLoader classLoader); static Definitions parseBPMN(String bpmnString); static Definitions parseBPMN(String bpmnString, ClassLoader classLoader); static void main(String[] args);  }
@Ignore("Need to write a more robust comparison for JSON objects that ignores property ordering") @Test public void testGetDiagramFromBpmn20_1() throws IOException, JAXBException, JSONException { Unmarshaller unmarshaller = JAXBContext.newInstance(Definitions.class, ConfigurationAnnotationAssociation.class, ConfigurationAnnotationShape.class) .createUnmarshaller(); unmarshaller.setProperty(IDResolver.class.getName(), new DefinitionsIDResolver()); Definitions definitions = (Definitions) unmarshaller.unmarshal(ConfigurationAlgorithmTest.test1File); BPMN2DiagramConverter converter = new BPMN2DiagramConverter("/signaviocore/editor/"); List<BasicDiagram> diagrams = converter.getDiagramFromBpmn20(definitions); String expectedJSON = toString(expectedJSONFile); assertEquals(1, diagrams.size()); String actualJSON = diagrams.get(0).getString(); FileWriter writer = new FileWriter(new File(new File("target"), "GetDiagramFromBpmn20_1.json")); writer.write(actualJSON); writer.close(); assertEquals(expectedJSON, actualJSON); }