Search in sources :

Example 16 with ConstraintNode

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

the class GuidedDecisionTreeModelUnmarshallingVisitor method visit.

private List<Node> visit(final FieldConstraint fc, final GuidedDecisionTree model, final PackageDataModelOracle dmo, final List<ParserMessage> messages) {
    final List<Node> nodes = new ArrayList<Node>();
    if (fc instanceof CompositeFieldConstraint) {
        messages.add(new UnsupportedFieldConstraintParserMessage());
        return nodes;
    } else if (fc instanceof SingleFieldConstraintEBLeftSide) {
        messages.add(new UnsupportedFieldConstraintParserMessage());
        return nodes;
    }
    if (!(fc instanceof SingleFieldConstraint)) {
        messages.add(new UnsupportedFieldConstraintParserMessage());
        return nodes;
    }
    final SingleFieldConstraint sfc = (SingleFieldConstraint) fc;
    if (sfc.getConnectives() != null) {
        messages.add(new UnsupportedFieldConstraintParserMessage());
        return nodes;
    }
    ConstraintNode node = null;
    final String className = sfc.getFactType();
    final String fieldName = sfc.getFieldName();
    if (sfc.getConstraintValueType() == BaseSingleFieldConstraint.TYPE_LITERAL) {
        final String operator = sfc.getOperator();
        final boolean isValueRequired = OperatorsOracle.isValueRequired(operator);
        if (isValueRequired) {
            final Value value = getValue(className, fieldName, model, dmo, messages, sfc.getValue());
            if (value != null) {
                node = new ConstraintNodeImpl(className, fieldName, operator, value);
            }
        } else {
            node = new ConstraintNodeImpl(className, fieldName);
        }
    } else if (sfc.getConstraintValueType() == BaseSingleFieldConstraint.TYPE_ENUM) {
        final String operator = sfc.getOperator();
        final boolean isValueRequired = OperatorsOracle.isValueRequired(operator);
        if (isValueRequired) {
            final Value value = getValue(className, fieldName, model, dmo, messages, sfc.getValue());
            if (value != null) {
                node = new ConstraintNodeImpl(className, fieldName, operator, value);
            }
        } else {
            node = new ConstraintNodeImpl(className, fieldName);
        }
    } else if (sfc.getConstraintValueType() == BaseSingleFieldConstraint.TYPE_UNDEFINED) {
        final String operator = sfc.getOperator();
        final boolean isValueRequired = OperatorsOracle.isValueRequired(operator);
        if (isValueRequired) {
            node = new ConstraintNodeImpl(className, fieldName);
        } else {
            node = new ConstraintNodeImpl(className, fieldName, operator, null);
        }
    } else {
        messages.add(new UnsupportedFieldConstraintTypeParserMessage());
        return nodes;
    }
    if (node != null) {
        if (sfc.isBound()) {
            node.setBinding(sfc.getFieldBinding());
        }
        nodes.add(node);
    }
    return nodes;
}
Also used : ConstraintNodeImpl(org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.ConstraintNodeImpl) ActionRetractNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionRetractNode) 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) ArrayList(java.util.ArrayList) UnsupportedFieldConstraintTypeParserMessage(org.drools.workbench.models.guided.dtree.shared.model.parser.messages.UnsupportedFieldConstraintTypeParserMessage) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) SingleFieldConstraintEBLeftSide(org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) UnsupportedFieldConstraintParserMessage(org.drools.workbench.models.guided.dtree.shared.model.parser.messages.UnsupportedFieldConstraintParserMessage) ConstraintNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ConstraintNode) ShortValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.ShortValue) StringValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.StringValue) FloatValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.FloatValue) BigDecimalValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.BigDecimalValue) ByteValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.ByteValue) Value(org.drools.workbench.models.guided.dtree.shared.model.values.Value) DoubleValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.DoubleValue) IntegerValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.IntegerValue) DateValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.DateValue) BooleanValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.BooleanValue) BigIntegerValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.BigIntegerValue) LongValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.LongValue) ActionFieldValue(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionFieldValue) EnumValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.EnumValue)

Example 17 with ConstraintNode

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

the class GuidedDecisionTreeDRLPersistenceMarshallingTest method testMultipleRules_2Rules_1Simple_1Complex.

@Test
public void testMultipleRules_2Rules_1Simple_1Complex() throws Exception {
    final String expected = "rule \"test_0\"" + "when\n" + "  Person( name == \"Michael\" )\n" + "then\n" + "end" + "rule \"test_1\"" + "when\n" + "  Person( name == \"Fred\", age == 41 )\n" + "then\n" + "end";
    final GuidedDecisionTree model = new GuidedDecisionTree();
    model.setTreeName("test");
    final TypeNode type = new TypeNodeImpl("Person");
    final ConstraintNode c1 = new ConstraintNodeImpl("Person", "name", "==", new StringValue("Michael"));
    final ConstraintNode c2 = new ConstraintNodeImpl("Person", "name", "==", new StringValue("Fred"));
    final ConstraintNode c3 = new ConstraintNodeImpl("Person", "age", "==", new IntegerValue(41));
    model.setRoot(type);
    type.addChild(c1);
    type.addChild(c2);
    c2.addChild(c3);
    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) IntegerValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.IntegerValue) BigIntegerValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.BigIntegerValue) GuidedDecisionTree(org.drools.workbench.models.guided.dtree.shared.model.GuidedDecisionTree) TypeNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.TypeNode) StringValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.StringValue) Test(org.junit.Test)

Example 18 with ConstraintNode

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

the class GuidedDecisionTreeDRLPersistenceMarshallingTest method testValue_Integer.

@Test
public void testValue_Integer() throws Exception {
    final String expected = "rule \"test_0\"" + "when\n" + "  Person( integerField == 1000000 )\n" + "then\n" + "end";
    final GuidedDecisionTree model = new GuidedDecisionTree();
    model.setTreeName("test");
    final TypeNode type = new TypeNodeImpl("Person");
    final ConstraintNode c1 = new ConstraintNodeImpl("Person", "integerField", "==", new IntegerValue(1000000));
    model.setRoot(type);
    type.addChild(c1);
    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) IntegerValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.IntegerValue) BigIntegerValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.BigIntegerValue) GuidedDecisionTree(org.drools.workbench.models.guided.dtree.shared.model.GuidedDecisionTree) TypeNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.TypeNode) Test(org.junit.Test)

Example 19 with ConstraintNode

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

the class GuidedDecisionTreeDRLPersistenceMarshallingTest method testSingleRule_ActionRetractWithConstraint.

@Test
public void testSingleRule_ActionRetractWithConstraint() throws Exception {
    final String expected = "rule \"test_0\"" + "when\n" + "  $p : Person( $n : name )\n" + "then\n" + "  retract( $p );\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 ActionRetractNode action = new ActionRetractNodeImpl(type);
    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) ActionRetractNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionRetractNode) ActionRetractNodeImpl(org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.ActionRetractNodeImpl) GuidedDecisionTree(org.drools.workbench.models.guided.dtree.shared.model.GuidedDecisionTree) TypeNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.TypeNode) Test(org.junit.Test)

Example 20 with ConstraintNode

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

the class GuidedDecisionTreeDRLPersistenceMarshallingTest method testSingleRule_ActionModifyWithConstraint.

@Test
public void testSingleRule_ActionModifyWithConstraint() throws Exception {
    final String expected = "rule \"test_0\"" + "when\n" + "  $p : Person( $n : name )\n" + "then\n" + "  modify( $p ) {\n" + "    setAge( 25 )\n" + "  }\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(true);
    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

ConstraintNode (org.drools.workbench.models.guided.dtree.shared.model.nodes.ConstraintNode)60 TypeNode (org.drools.workbench.models.guided.dtree.shared.model.nodes.TypeNode)58 ConstraintNodeImpl (org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.ConstraintNodeImpl)56 TypeNodeImpl (org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.TypeNodeImpl)53 GuidedDecisionTree (org.drools.workbench.models.guided.dtree.shared.model.GuidedDecisionTree)52 Test (org.junit.Test)52 StringValue (org.drools.workbench.models.guided.dtree.shared.model.values.impl.StringValue)20 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)15 BigInteger (java.math.BigInteger)9 ActionUpdateNode (org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionUpdateNode)8 ActionRetractNode (org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionRetractNode)6 ActionInsertNode (org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionInsertNode)4 ActionFieldValueImpl (org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.ActionFieldValueImpl)4 ActionUpdateNodeImpl (org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.ActionUpdateNodeImpl)4 Node (org.drools.workbench.models.guided.dtree.shared.model.nodes.Node)3 BigDecimalValue (org.drools.workbench.models.guided.dtree.shared.model.values.impl.BigDecimalValue)3 BooleanValue (org.drools.workbench.models.guided.dtree.shared.model.values.impl.BooleanValue)3 ByteValue (org.drools.workbench.models.guided.dtree.shared.model.values.impl.ByteValue)3 DateValue (org.drools.workbench.models.guided.dtree.shared.model.values.impl.DateValue)3