ConfigurationAlgorithm { static void replaceConfiguredGateway(final Definitions definitions, final Gateway gateway) { Gateway reconfiguredGateway = null; for (final BaseElement rootElement : definitions.getRootElement()) { if (rootElement instanceof Process) { final Process process = (Process) rootElement; for (final FlowElement flowElement : new ArrayList<FlowElement>(process.getFlowElement())) { if (flowElement.equals(gateway)) { TGatewayType gatewayType = gatewayConfigurationType(gateway); switch (gatewayType) { case DATA_BASED_EXCLUSIVE: reconfiguredGateway = new ExclusiveGateway(); break; case EVENT_BASED_EXCLUSIVE: reconfiguredGateway = new EventBasedGateway(); break; case INCLUSIVE: reconfiguredGateway = new InclusiveGateway(); break; case PARALLEL: reconfiguredGateway = new ParallelGateway(); break; default: assert false : "Unknown gateway type: " + gatewayType; } assert reconfiguredGateway != null; reconfiguredGateway.getAny().addAll(flowElement.getAny()); reconfiguredGateway.getDocumentation().addAll(flowElement.getDocumentation()); reconfiguredGateway.setId(flowElement.getId()); ExtensionElements extensionElements = new ExtensionElements(); extensionElements.getAny().addAll(flowElement.getExtensionElements().getAny()); extensionElements.getAnyExternal().addAll(flowElement.getExtensionElements().getAnyExternal()); Configurable configurable = extensionElements.getFirstExtensionElementOfType(Configurable.class); assert configurable != null : "No <configurable> element was found"; boolean removed = extensionElements.getAny().remove(configurable); assert removed : "No <configurable> element was removed"; if (!(extensionElements.getAny().isEmpty() && extensionElements.getAnyExternal().isEmpty())) { reconfiguredGateway.setExtensionElements(extensionElements); } reconfiguredGateway.setAuditing(flowElement.getAuditing()); reconfiguredGateway.getIncoming().addAll(flowElement.getIncoming()); reconfiguredGateway.setMonitoring(flowElement.getMonitoring()); reconfiguredGateway.setName(flowElement.getName()); reconfiguredGateway.getOutgoing().addAll(flowElement.getOutgoing()); reconfiguredGateway.setGatewayDirection(((Gateway) flowElement).getGatewayDirection()); if (flowElement instanceof GatewayWithDefaultFlow && reconfiguredGateway instanceof GatewayWithDefaultFlow) { ((GatewayWithDefaultFlow) reconfiguredGateway).setDefault(((GatewayWithDefaultFlow) flowElement).getDefault()); } process.getFlowElement().set(process.getFlowElement().indexOf(gateway), reconfiguredGateway); } } } } if (reconfiguredGateway != null) { for (final BaseElement rootElement : definitions.getRootElement()) { if (rootElement instanceof Process) { final Process process = (Process) rootElement; for (final FlowElement flowElement : process.getFlowElement()) { Set<BaseElement> substitutionSet = new HashSet(); substitutionSet.add(gateway); substituteReferences(flowElement, substitutionSet, reconfiguredGateway); } } } ((BPMNShape) findBpmndiMap(definitions).get(gateway)).setBpmnElement(reconfiguredGateway); } }  static void configure(final Definitions definitions); static Map<BaseElement, DiagramElement> findBpmndiMap(final Definitions definitions); static Set<Gateway> findConfiguredGateways(final Definitions definitions); static Set<SequenceFlow> findAbsentSequenceFlows(final Set<Gateway> reconfiguredGatewaySet); static void main(final String[] arg);  }
@Test public final void testReplaceConfiguredGateway1() throws IOException, JAXBException, SAXException { Unmarshaller unmarshaller = context.createUnmarshaller(); unmarshaller.setProperty(IDResolver.class.getName(), new DefinitionsIDResolver()); Definitions definitions = (Definitions) unmarshaller.unmarshal(test1File); Gateway gateway = (Gateway) ((Process) definitions.getRootElement().get(0)).getFlowElement().get(1); assertEquals("sid-B044A443-736E-495B-9DD8-90EB4860F9AC", gateway.getId()); List<Edge> oldIncoming = gateway.getIncoming(), oldOutgoing = gateway.getOutgoing(); BPMNShape gatewayGui = (BPMNShape) definitions.getDiagram().get(0).getBPMNPlane().getDiagramElement().get(1); assertEquals("sid-B044A443-736E-495B-9DD8-90EB4860F9AC_gui", gatewayGui.getId()); assertSame(gateway, gatewayGui.getBpmnElement()); ConfigurationAlgorithm.replaceConfiguredGateway(definitions, gateway); Gateway newGateway = (Gateway) ((Process) definitions.getRootElement().get(0)).getFlowElement().get(1); assertTrue(newGateway instanceof InclusiveGateway); assertNotSame(gateway, newGateway); assertEquals(gateway.getId(), newGateway.getId()); assertNull("Reconfigured gateway still has a <pc:configurable> extension element", newGateway.getExtensionElements().getFirstExtensionElementOfType(Configurable.class)); for (Edge edge : oldIncoming) { assertEquals(edge.getId() + " does not target configured gateway", newGateway, edge.getTargetRef()); } for (Edge edge : oldOutgoing) { assertEquals(edge.getId() + " is not sourced from configured gateway", newGateway, edge.getSourceRef()); } assertSame(gatewayGui, definitions.getDiagram().get(0).getBPMNPlane().getDiagramElement().get(1)); assertSame(newGateway, gatewayGui.getBpmnElement()); assertValidBPMN(definitions, "test-replaceConfiguredGateway1.bpmn.xml"); }