package eymp.services.ruleengines.PTARuleManager; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Collection; import java.util.List; import org.camunda.bpm.model.dmn.Dmn; import org.camunda.bpm.model.dmn.DmnModelInstance; import org.camunda.bpm.model.dmn.HitPolicy; import org.camunda.bpm.model.dmn.impl.DmnModelConstants; import org.camunda.bpm.model.dmn.instance.Decision; import org.camunda.bpm.model.dmn.instance.DecisionTable; import org.camunda.bpm.model.dmn.instance.Definitions; import org.camunda.bpm.model.dmn.instance.Description; import org.camunda.bpm.model.dmn.instance.DmnElement; import org.camunda.bpm.model.dmn.instance.Input; import org.camunda.bpm.model.dmn.instance.InputEntry; import org.camunda.bpm.model.dmn.instance.InputExpression; import org.camunda.bpm.model.dmn.instance.NamedElement; import org.camunda.bpm.model.dmn.instance.Output; import org.camunda.bpm.model.dmn.instance.OutputEntry; import org.camunda.bpm.model.dmn.instance.Rule; import org.camunda.bpm.model.dmn.instance.Text; public class DMNGenerator { DmnModelInstance model = Dmn.createEmptyModel(); public static void main( String[] args ) { DMNGenerator generator = new DMNGenerator(); //generator.createDMN(); generator.updateDMN(); } public void updateDMN() { FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { try { fileInputStream = new FileInputStream("c:/camunda/test/GenerateDMN.dmn"); DmnModelInstance dmnModel = Dmn.readModelFromStream(fileInputStream); DecisionTable decisionTable = dmnModel.getModelElementById("decisionTable"); Rule newRule = generateElement(dmnModel, Rule.class, "DecisionRule_4"); decisionTable.addChildElement(newRule); InputEntry inputEntry1 = generateElement(dmnModel, InputEntry.class, "UnaryTests_41"); String inputTextValue1 = "2"; Text text1 = generateText(dmnModel, inputTextValue1); inputEntry1.setText(text1); newRule.addChildElement(inputEntry1); OutputEntry outputEntry1 = generateElement(dmnModel, OutputEntry.class, "LiteralExpression_41"); String outputTextValue1 = "\"2\""; Text text2 = generateText(dmnModel, outputTextValue1); outputEntry1.setText(text2); newRule.addChildElement(outputEntry1); Description description1 = generateDescription(dmnModel, "Rule 4 Description"); newRule.setDescription(description1); fileOutputStream = new FileOutputStream("c:/camunda/test/GenerateDMN_v1.dmn"); Dmn.writeModelToStream(fileOutputStream, dmnModel); } finally { if (fileInputStream != null) { fileInputStream.close(); } if (fileOutputStream != null) { fileOutputStream.close(); } } } catch(Exception e) { System.out.println("Could not convert file: " + e.getMessage()); e.printStackTrace(System.out); } return; } public void createDMN() { DmnModelInstance dmnModel = initializeEmptyDmnModel(); Decision decision = generateElement(dmnModel, Decision.class, "GenerateDMN"); decision.setName("Generate DMN"); dmnModel.getDefinitions().addChildElement(decision); DecisionTable decisionTable = generateElement(dmnModel, DecisionTable.class, "decisionTable"); decision.addChildElement(decisionTable); setHitPolicy(decisionTable); createInputsOutputs(dmnModel, decisionTable); createRules(dmnModel, decisionTable); FileOutputStream fileOutputStream = null; try { try { fileOutputStream = new FileOutputStream("c:/camunda/test/GenerateDMN.dmn"); Dmn.writeModelToStream(fileOutputStream, dmnModel); } finally { if (fileOutputStream != null) { fileOutputStream.close(); } } } catch (Exception e) { System.out.println("Could not convert file: " + e.getMessage()); e.printStackTrace(System.out); } } protected void createInputsOutputs(DmnModelInstance dmnModel, DecisionTable decisionTable) { // inputs Input input = generateElement(dmnModel, Input.class, "Input_1"); decisionTable.addChildElement(input); // mandatory InputExpression inputExpression = generateElement(dmnModel, InputExpression.class); Text text = generateText(dmnModel, "Input1"); inputExpression.setText(text); input.setInputExpression(inputExpression); // Optional input.setLabel("Input1"); inputExpression.setTypeRef("integer"); Output output = generateElement(dmnModel, Output.class, "Output_1"); decisionTable.addChildElement(output); // mandatory output.setName("Outcome"); // Optional output.setLabel("Outcome"); output.setTypeRef("string"); return; } protected void createRules(DmnModelInstance dmnModel, DecisionTable decisionTable) { // **************** Rule 1 Rule rule1 = generateElement(dmnModel, Rule.class, "DecisionRule_1"); decisionTable.addChildElement(rule1); InputEntry inputEntry1 = generateElement(dmnModel, InputEntry.class, "UnaryTests_1"); String inputTextValue1 = "0"; Text text1 = generateText(dmnModel, inputTextValue1); inputEntry1.setText(text1); rule1.addChildElement(inputEntry1); OutputEntry outputEntry1 = generateElement(dmnModel, OutputEntry.class, "LiteralExpression_1"); String outputTextValue1 = "\"0\""; Text text2 = generateText(dmnModel, outputTextValue1); outputEntry1.setText(text2); rule1.addChildElement(outputEntry1); Description description1 = generateDescription(dmnModel, "Rule 1 Description"); rule1.setDescription(description1); // **************** Rule 2 Rule rule2 = generateElement(dmnModel, Rule.class, "DecisionRule_2"); decisionTable.addChildElement(rule2); InputEntry inputEntry2 = generateElement(dmnModel, InputEntry.class, "UnaryTests_2"); String inputTextValue2 = "1"; Text text3 = generateText(dmnModel, inputTextValue2); inputEntry2.setText(text3); rule2.addChildElement(inputEntry2); OutputEntry outputEntry2 = generateElement(dmnModel, OutputEntry.class, "LiteralExpression_2"); String outputTextValue2 = "\"1\""; Text text4 = generateText(dmnModel, outputTextValue2); outputEntry2.setText(text4); rule2.addChildElement(outputEntry2); Description description2 = generateDescription(dmnModel, "Rule 2 Description"); rule2.setDescription(description2); // **************** Rule 3 Rule rule3 = generateElement(dmnModel, Rule.class, "DecisionRule_3"); decisionTable.addChildElement(rule3); InputEntry inputEntry3 = generateElement(dmnModel, InputEntry.class, "UnaryTests_3"); String inputTextValue3 = "not(0,1)"; Text text5 = generateText(dmnModel, inputTextValue3); inputEntry3.setText(text5); rule3.addChildElement(inputEntry3); OutputEntry outputEntry3 = generateElement(dmnModel, OutputEntry.class, "LiteralExpression_3"); String outputTextValue3 = "\"-1\""; Text text6 = generateText(dmnModel, outputTextValue3); outputEntry3.setText(text6); rule3.addChildElement(outputEntry3); Description description3 = generateDescription(dmnModel, "Rule 3 Description"); rule3.setDescription(description3); } protected DmnModelInstance initializeEmptyDmnModel() { DmnModelInstance dmnModel = Dmn.createEmptyModel(); Definitions definitions = generateNamedElement(dmnModel, Definitions.class, "definitions"); definitions.setNamespace(DmnModelConstants.CAMUNDA_NS); dmnModel.setDefinitions(definitions); return dmnModel; } protected void setHitPolicy(DecisionTable decisionTable) { HitPolicy hitPolicy = HitPolicy.UNIQUE; decisionTable.setHitPolicy(hitPolicy); } public E generateNamedElement(DmnModelInstance modelInstance, Class elementClass, String name) { E element = generateElement(modelInstance, elementClass, name); element.setName(name); return element; } public E generateElement(DmnModelInstance modelInstance, Class elementClass, String id) { E element = modelInstance.newInstance(elementClass); element.setId(id); return element; } public E generateElement(DmnModelInstance modelInstance, Class elementClass) { // TODO: use a proper generator for random IDs String generatedId = elementClass.getSimpleName() + Integer.toString((int) (Integer.MAX_VALUE * Math.random())); return generateElement(modelInstance, elementClass, generatedId); } protected Text generateText(DmnModelInstance dmnModel, String content) { Text text = dmnModel.newInstance(Text.class); text.setTextContent(content); return text; } protected Description generateDescription(DmnModelInstance dmnModel, String content) { Description description = dmnModel.newInstance(Description.class); description.setTextContent(content); return description; } }