Search in sources :

Example 11 with ActionUpdateNode

use of org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionUpdateNode in project drools by kiegroup.

the class GuidedDecisionTreeDRLPersistenceUnmarshallingTest method testSingleRule_ActionModifyMultipleFields.

@Test
public void testSingleRule_ActionModifyMultipleFields() throws Exception {
    final String drl = "rule \"test_0\"\n" + "when \n" + "  $p : Person( )\n" + "then \n" + "  modify( $p ) {\n" + "    setAge( 25 ), \n" + "    setName( \"Michael\" )\n" + "  }\n" + "end";
    final GuidedDecisionTree expected = new GuidedDecisionTree();
    expected.setTreeName("test");
    final TypeNode type = new TypeNodeImpl("Person");
    type.setBinding("$p");
    expected.setRoot(type);
    final ActionUpdateNode action = new ActionUpdateNodeImpl(type);
    action.setModify(true);
    action.getFieldValues().add(new ActionFieldValueImpl("age", new IntegerValue(25)));
    action.getFieldValues().add(new ActionFieldValueImpl("name", new StringValue("Michael")));
    type.addChild(action);
    addModelField("Person", "this", "Person", DataType.TYPE_THIS);
    addModelField("Person", "name", String.class.getName(), DataType.TYPE_STRING);
    addModelField("Person", "age", Integer.class.getName(), DataType.TYPE_NUMERIC_INTEGER);
    final GuidedDecisionTree model = getAndTestUnmarshalledModel(drl, "test", 0);
    assertEquals(expected.getTreeName(), model.getTreeName());
    assertNotNull(model.getRoot());
    assertEquals(type.getClassName(), model.getRoot().getClassName());
    assertTrue(model.getRoot().isBound());
    assertEquals(type.getBinding(), model.getRoot().getBinding());
    assertEquals(1, model.getRoot().getChildren().size());
    assertNotNull(model.getRoot().getChildren().get(0));
    assertTrue(model.getRoot().getChildren().get(0) instanceof ActionUpdateNode);
    final ActionUpdateNode _action = (ActionUpdateNode) model.getRoot().getChildren().get(0);
    assertEquals(action.getBoundNode().getBinding(), _action.getBoundNode().getBinding());
    assertEquals(action.isModify(), _action.isModify());
    assertEquals(action.getFieldValues().size(), _action.getFieldValues().size());
    assertEquals(2, _action.getFieldValues().size());
    assertEquals(action.getFieldValues().get(0).getFieldName(), _action.getFieldValues().get(0).getFieldName());
    assertEquals(action.getFieldValues().get(0).getValue().getValue().toString(), _action.getFieldValues().get(0).getValue().getValue().toString());
    assertEquals(action.getFieldValues().get(1).getFieldName(), _action.getFieldValues().get(1).getFieldName());
    assertEquals(action.getFieldValues().get(1).getValue().getValue().toString(), _action.getFieldValues().get(1).getValue().getValue().toString());
    assertEquals(0, _action.getChildren().size());
}
Also used : BigInteger(java.math.BigInteger) TypeNodeImpl(org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.TypeNodeImpl) ActionUpdateNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionUpdateNode) IntegerValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.IntegerValue) BigIntegerValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.BigIntegerValue) ActionUpdateNodeImpl(org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.ActionUpdateNodeImpl) GuidedDecisionTree(org.drools.workbench.models.guided.dtree.shared.model.GuidedDecisionTree) TypeNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.TypeNode) ActionFieldValueImpl(org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.ActionFieldValueImpl) StringValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.StringValue) Test(org.junit.Test)

Example 12 with ActionUpdateNode

use of org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionUpdateNode in project drools-wb by kiegroup.

the class GuidedDecisionTreeWidget method processChildren.

private void processChildren(final Node node, final WiresBaseTreeNode uiNode, final boolean isReadOnly) {
    uiNode.setSelectionManager(this);
    uiNode.setShapesManager(this);
    uiNode.setLayoutManager(layoutManager);
    if (uiNode instanceof BaseGuidedDecisionTreeShape) {
        ((BaseGuidedDecisionTreeShape) uiNode).setPresenter(presenter);
    }
    canvasLayer.add(uiNode);
    shapesInCanvas.add(uiNode);
    final Iterator<Node> itr = node.iterator();
    while (itr.hasNext()) {
        final Node child = itr.next();
        WiresBaseTreeNode uiChildNode = null;
        if (child instanceof TypeNode) {
            uiChildNode = typeNodeFactory.getShape((TypeNode) child, isReadOnly);
        } else if (child instanceof ConstraintNode) {
            uiChildNode = constraintNodeFactory.getShape((ConstraintNode) child, isReadOnly);
        } else if (child instanceof ActionInsertNode) {
            uiChildNode = actionInsertNodeFactory.getShape((ActionInsertNode) child, isReadOnly);
        } else if (child instanceof ActionUpdateNode) {
            uiChildNode = actionUpdateNodeFactory.getShape((ActionUpdateNode) child, isReadOnly);
        } else if (child instanceof ActionRetractNode) {
            uiChildNode = actionRetractNodeFactory.getShape((ActionRetractNode) child, isReadOnly);
        }
        if (uiChildNode != null) {
            uiNode.addChildNode(uiChildNode);
            processChildren(child, uiChildNode, isReadOnly);
        }
    }
}
Also used : ActionInsertNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionInsertNode) ConstraintNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ConstraintNode) WiresBaseTreeNode(org.uberfire.ext.wires.core.trees.client.shapes.WiresBaseTreeNode) ActionUpdateNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionUpdateNode) ActionRetractNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionRetractNode) WiresBaseTreeNode(org.uberfire.ext.wires.core.trees.client.shapes.WiresBaseTreeNode) TypeNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.TypeNode) Node(org.drools.workbench.models.guided.dtree.shared.model.nodes.Node) ActionUpdateNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionUpdateNode) ActionInsertNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionInsertNode) ConstraintNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ConstraintNode) ActionRetractNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionRetractNode) TypeNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.TypeNode) BaseGuidedDecisionTreeShape(org.drools.workbench.screens.guided.dtree.client.widget.shapes.BaseGuidedDecisionTreeShape)

Example 13 with ActionUpdateNode

use of org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionUpdateNode in project drools by kiegroup.

the class GuidedDecisionTreeModelMarshallingVisitor method generateRuleDRL.

protected void generateRuleDRL(final List<Node> path) {
    Node context = null;
    final StringBuilder drl = new StringBuilder();
    final boolean hasDateFieldValue = hasDateFieldValue(path);
    this.varCounter = 0;
    drl.append(generateRuleHeaderDRL());
    drl.append(INDENTATION).append("when \n");
    for (Node node : path) {
        if (node instanceof TypeNode) {
            generateTypeNodeDRL((TypeNode) node, context, drl);
        } else if (node instanceof ConstraintNode) {
            generateConstraintNodeDRL((ConstraintNode) node, context, drl);
        } else if (node instanceof ActionRetractNode) {
            generateActionRetractNodeDRL((ActionRetractNode) node, context, hasDateFieldValue, drl);
        } else if (node instanceof ActionUpdateNode) {
            generateActionUpdateNodeDRL((ActionUpdateNode) node, context, hasDateFieldValue, drl);
        } else if (node instanceof ActionInsertNode) {
            generateActionInsertNodeDRL((ActionInsertNode) node, context, hasDateFieldValue, drl);
        }
        context = node;
    }
    if (context == null) {
    // No previous context to close
    } else if (context instanceof ConstraintNode) {
        drl.append(")\n").append("then \n").append("end\n");
    } else if (context instanceof TypeNode) {
        drl.append(")\n").append("then \n").append("end\n");
    } else if (context instanceof ActionRetractNode) {
        drl.append("end\n");
    } else if (context instanceof ActionUpdateNode) {
        drl.append("end\n");
    } else if (context instanceof ActionInsertNode) {
        drl.append("end\n");
    }
    ruleCount++;
    rules.append(drl);
}
Also used : ActionInsertNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionInsertNode) ConstraintNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ConstraintNode) ActionUpdateNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionUpdateNode) ActionRetractNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionRetractNode) ActionUpdateNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionUpdateNode) ActionInsertNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionInsertNode) TypeNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.TypeNode) Node(org.drools.workbench.models.guided.dtree.shared.model.nodes.Node) ConstraintNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ConstraintNode) ActionRetractNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionRetractNode) TypeNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.TypeNode)

Example 14 with ActionUpdateNode

use of org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionUpdateNode in project drools by kiegroup.

the class GuidedDecisionTreeDRLPersistenceMarshallingTest method testSingleRule_ActionSetDateFieldValue.

@Test
public void testSingleRule_ActionSetDateFieldValue() throws Exception {
    final String expected = "rule \"test_0\"" + "when\n" + "  $p : Person()\n" + "then\n" + "  java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"dd-MMM-yyyy\");\n" + "  $p.setDateOfBirth( sdf.parse(\"15-Jul-1985\") );\n" + "end";
    final GuidedDecisionTree model = new GuidedDecisionTree();
    model.setTreeName("test");
    final TypeNode type = new TypeNodeImpl("Person");
    type.setBinding("$p");
    model.setRoot(type);
    final ActionUpdateNode action = new ActionUpdateNodeImpl(type);
    action.setModify(false);
    final Calendar dob = Calendar.getInstance();
    dob.set(Calendar.YEAR, 1985);
    dob.set(Calendar.MONTH, 6);
    dob.set(Calendar.DATE, 15);
    action.getFieldValues().add(new ActionFieldValueImpl("dateOfBirth", new DateValue(dob.getTime())));
    type.addChild(action);
    final String drl = GuidedDecisionTreeDRLPersistence.getInstance().marshal(model);
    assertEqualsIgnoreWhitespace(expected, drl);
}
Also used : TypeNodeImpl(org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.TypeNodeImpl) ActionUpdateNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionUpdateNode) DateValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.DateValue) Calendar(java.util.Calendar) ActionUpdateNodeImpl(org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.ActionUpdateNodeImpl) GuidedDecisionTree(org.drools.workbench.models.guided.dtree.shared.model.GuidedDecisionTree) TypeNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.TypeNode) ActionFieldValueImpl(org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.ActionFieldValueImpl) Test(org.junit.Test)

Example 15 with ActionUpdateNode

use of org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionUpdateNode in project drools by kiegroup.

the class GuidedDecisionTreeDRLPersistenceMarshallingTest method testSingleRule_ActionSetWithConstraint.

@Test
public void testSingleRule_ActionSetWithConstraint() throws Exception {
    final String expected = "rule \"test_0\"" + "when\n" + "  $p : Person( $n : name )\n" + "then\n" + "  $p.setAge( 25 );\n" + "end";
    final GuidedDecisionTree model = new GuidedDecisionTree();
    model.setTreeName("test");
    final TypeNode type = new TypeNodeImpl("Person");
    type.setBinding("$p");
    final ConstraintNode c1 = new ConstraintNodeImpl("Person", "name");
    c1.setBinding("$n");
    model.setRoot(type);
    type.addChild(c1);
    final ActionUpdateNode action = new ActionUpdateNodeImpl(type);
    action.setModify(false);
    action.getFieldValues().add(new ActionFieldValueImpl("age", new IntegerValue(25)));
    c1.addChild(action);
    final String drl = GuidedDecisionTreeDRLPersistence.getInstance().marshal(model);
    assertEqualsIgnoreWhitespace(expected, drl);
}
Also used : ConstraintNodeImpl(org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.ConstraintNodeImpl) ConstraintNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ConstraintNode) TypeNodeImpl(org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.TypeNodeImpl) ActionUpdateNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionUpdateNode) IntegerValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.IntegerValue) BigIntegerValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.BigIntegerValue) ActionUpdateNodeImpl(org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.ActionUpdateNodeImpl) GuidedDecisionTree(org.drools.workbench.models.guided.dtree.shared.model.GuidedDecisionTree) TypeNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.TypeNode) ActionFieldValueImpl(org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.ActionFieldValueImpl) Test(org.junit.Test)

Aggregations

ActionUpdateNode (org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionUpdateNode)31 TypeNode (org.drools.workbench.models.guided.dtree.shared.model.nodes.TypeNode)29 ActionUpdateNodeImpl (org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.ActionUpdateNodeImpl)28 GuidedDecisionTree (org.drools.workbench.models.guided.dtree.shared.model.GuidedDecisionTree)25 TypeNodeImpl (org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.TypeNodeImpl)25 Test (org.junit.Test)25 ActionFieldValueImpl (org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.ActionFieldValueImpl)22 BigIntegerValue (org.drools.workbench.models.guided.dtree.shared.model.values.impl.BigIntegerValue)17 IntegerValue (org.drools.workbench.models.guided.dtree.shared.model.values.impl.IntegerValue)17 ActionRetractNode (org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionRetractNode)9 BigInteger (java.math.BigInteger)8 ConstraintNode (org.drools.workbench.models.guided.dtree.shared.model.nodes.ConstraintNode)8 ActionRetractNodeImpl (org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.ActionRetractNodeImpl)6 ActionInsertNode (org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionInsertNode)5 DateValue (org.drools.workbench.models.guided.dtree.shared.model.values.impl.DateValue)5 StringValue (org.drools.workbench.models.guided.dtree.shared.model.values.impl.StringValue)5 Calendar (java.util.Calendar)4 ConstraintNodeImpl (org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.ConstraintNodeImpl)4 Node (org.drools.workbench.models.guided.dtree.shared.model.nodes.Node)3 Date (java.util.Date)2