Search in sources :

Example 31 with SingleFieldConstraintEBLeftSide

use of org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide in project drools by kiegroup.

the class RuleModelDRLPersistenceUnmarshallingTest method testLHSNestedMethodCalls.

@Test
public void testLHSNestedMethodCalls() throws Exception {
    String drl = "package org.mortgages;\n" + "rule \"test\"\n" + " dialect \"mvel\"\n" + " when\n" + "  Parent( methodToGetChild1().methodToGetChild2().field1 > 5 )\n" + " then\n" + "end";
    addMethodInformation("Parent", "methodToGetChild1", Collections.emptyList(), "Child1", null, "Child1");
    addMethodInformation("Child1", "methodToGetChild2", Collections.emptyList(), "Child2", null, "Child2");
    addModelField("Child2", "field1", "int", DataType.TYPE_NUMERIC_INTEGER);
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals(1, m.lhs.length);
    assertTrue(m.lhs[0] instanceof FactPattern);
    final FactPattern fp = (FactPattern) m.lhs[0];
    assertEquals("Parent", fp.getFactType());
    assertEquals(1, fp.getNumberOfConstraints());
    assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraintEBLeftSide);
    final SingleFieldConstraintEBLeftSide exp = (SingleFieldConstraintEBLeftSide) fp.getConstraint(0);
    assertEquals("int", exp.getFieldType());
    assertEquals(">", exp.getOperator());
    assertEquals("5", exp.getValue());
    assertEquals(4, exp.getExpressionLeftSide().getParts().size());
    assertTrue(exp.getExpressionLeftSide().getParts().get(0) instanceof ExpressionUnboundFact);
    final ExpressionUnboundFact expPart0 = (ExpressionUnboundFact) exp.getExpressionLeftSide().getParts().get(0);
    assertEquals("Parent", expPart0.getFactType());
    assertTrue(exp.getExpressionLeftSide().getParts().get(1) instanceof ExpressionMethod);
    final ExpressionMethod expPart1 = (ExpressionMethod) exp.getExpressionLeftSide().getParts().get(1);
    assertEquals("methodToGetChild1", expPart1.getName());
    assertTrue(exp.getExpressionLeftSide().getParts().get(2) instanceof ExpressionMethod);
    final ExpressionMethod expPart2 = (ExpressionMethod) exp.getExpressionLeftSide().getParts().get(2);
    assertEquals("methodToGetChild2", expPart2.getName());
    assertTrue(exp.getExpressionLeftSide().getParts().get(3) instanceof ExpressionField);
    final ExpressionField expPart3 = (ExpressionField) exp.getExpressionLeftSide().getParts().get(3);
    assertEquals("field1", expPart3.getName());
}
Also used : SingleFieldConstraintEBLeftSide(org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide) ExpressionMethod(org.drools.workbench.models.datamodel.rule.ExpressionMethod) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) ExpressionField(org.drools.workbench.models.datamodel.rule.ExpressionField) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) ExpressionUnboundFact(org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact) Test(org.junit.Test)

Example 32 with SingleFieldConstraintEBLeftSide

use of org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide in project drools by kiegroup.

the class RuleModelDRLPersistenceUnmarshallingTest method testNestedFieldConstraintsAsExpression.

@Test
public void testNestedFieldConstraintsAsExpression() {
    String drl = "rule \"rule1\"\n" + "when\n" + "Person( contact.telephone > 12345 )\n" + "then\n" + "end";
    addModelField("org.test.Person", "contact", "org.test.Contact", "Contact");
    addModelField("org.test.Contact", "telephone", "java.lang.Integer", DataType.TYPE_NUMERIC_INTEGER);
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals("rule1", m.name);
    assertEquals(1, m.lhs.length);
    IPattern p = m.lhs[0];
    assertTrue(p instanceof FactPattern);
    FactPattern fp = (FactPattern) p;
    assertEquals("Person", fp.getFactType());
    assertEquals(1, fp.getConstraintList().getConstraints().length);
    assertTrue(((FactPattern) m.lhs[0]).getFieldConstraints()[0] instanceof SingleFieldConstraintEBLeftSide);
    SingleFieldConstraintEBLeftSide ebLeftSide = (SingleFieldConstraintEBLeftSide) ((FactPattern) m.lhs[0]).getFieldConstraints()[0];
    assertEquals("telephone", ebLeftSide.getFieldName());
    assertEquals("java.lang.Integer", ebLeftSide.getFieldType());
    assertEquals(">", ebLeftSide.getOperator());
    assertEquals("12345", ebLeftSide.getValue());
    assertEquals(3, ebLeftSide.getExpressionLeftSide().getParts().size());
    assertTrue(ebLeftSide.getExpressionLeftSide().getParts().get(0) instanceof ExpressionUnboundFact);
    ExpressionUnboundFact expressionUnboundFact = ((ExpressionUnboundFact) ebLeftSide.getExpressionLeftSide().getParts().get(0));
    assertEquals("Person", expressionUnboundFact.getName());
    assertEquals("Person", expressionUnboundFact.getClassType());
    assertEquals("Person", expressionUnboundFact.getGenericType());
    assertEquals(((FactPattern) m.lhs[0]).getFactType(), expressionUnboundFact.getFactType());
    assertEquals(null, expressionUnboundFact.getPrevious());
    assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(1), expressionUnboundFact.getNext());
    assertTrue(ebLeftSide.getExpressionLeftSide().getParts().get(1) instanceof ExpressionField);
    ExpressionField expressionField1 = (ExpressionField) ebLeftSide.getExpressionLeftSide().getParts().get(1);
    assertEquals("contact", expressionField1.getName());
    assertEquals("org.test.Contact", expressionField1.getClassType());
    assertEquals("Contact", expressionField1.getGenericType());
    assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(0), expressionField1.getPrevious());
    assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(2), expressionField1.getNext());
    assertTrue(ebLeftSide.getExpressionLeftSide().getParts().get(2) instanceof ExpressionField);
    ExpressionField expressionField2 = (ExpressionField) ebLeftSide.getExpressionLeftSide().getParts().get(2);
    assertEquals("telephone", expressionField2.getName());
    assertEquals("java.lang.Integer", expressionField2.getClassType());
    assertEquals(DataType.TYPE_NUMERIC_INTEGER, expressionField2.getGenericType());
    assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(1), expressionField2.getPrevious());
    assertNull(expressionField2.getNext());
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) SingleFieldConstraintEBLeftSide(org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) ExpressionField(org.drools.workbench.models.datamodel.rule.ExpressionField) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) ExpressionUnboundFact(org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact) Test(org.junit.Test)

Example 33 with SingleFieldConstraintEBLeftSide

use of org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide in project drools by kiegroup.

the class RuleModelDRLPersistenceUnmarshallingTest method testNestedFieldExpressionsWithAFunction.

@Test
public void testNestedFieldExpressionsWithAFunction() {
    String drl = "rule rule1\n" + "when\n" + "Person( address.postalCode == myFunction() )\n" + "then\n" + "end";
    addModelField("org.test.Person", "address", "org.test.Address", "Address");
    addModelField("org.test.Address", "postalCode", "java.lang.Integer", "Integer");
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals(1, m.lhs.length);
    assertTrue(m.lhs[0] instanceof FactPattern);
    assertTrue(((FactPattern) m.lhs[0]).getFieldConstraints()[0] instanceof SingleFieldConstraintEBLeftSide);
    SingleFieldConstraintEBLeftSide ebLeftSide = (SingleFieldConstraintEBLeftSide) ((FactPattern) m.lhs[0]).getFieldConstraints()[0];
    assertEquals("postalCode", ebLeftSide.getFieldName());
    assertEquals("java.lang.Integer", ebLeftSide.getFieldType());
    assertEquals("==", ebLeftSide.getOperator());
    assertEquals("myFunction()", ebLeftSide.getValue());
    assertEquals(3, ebLeftSide.getExpressionLeftSide().getParts().size());
    assertTrue(ebLeftSide.getExpressionLeftSide().getParts().get(0) instanceof ExpressionUnboundFact);
    ExpressionUnboundFact expressionUnboundFact = ((ExpressionUnboundFact) ebLeftSide.getExpressionLeftSide().getParts().get(0));
    assertEquals("Person", expressionUnboundFact.getName());
    assertEquals("Person", expressionUnboundFact.getClassType());
    assertEquals("Person", expressionUnboundFact.getGenericType());
    assertEquals(((FactPattern) m.lhs[0]).getFactType(), expressionUnboundFact.getFactType());
    assertNull(expressionUnboundFact.getPrevious());
    assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(1), expressionUnboundFact.getNext());
    assertTrue(ebLeftSide.getExpressionLeftSide().getParts().get(1) instanceof ExpressionField);
    ExpressionField expressionField1 = (ExpressionField) ebLeftSide.getExpressionLeftSide().getParts().get(1);
    assertEquals("address", expressionField1.getName());
    assertEquals("org.test.Address", expressionField1.getClassType());
    assertEquals("Address", expressionField1.getGenericType());
    assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(0), expressionField1.getPrevious());
    assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(2), expressionField1.getNext());
    assertTrue(ebLeftSide.getExpressionLeftSide().getParts().get(2) instanceof ExpressionField);
    ExpressionField expressionField2 = (ExpressionField) ebLeftSide.getExpressionLeftSide().getParts().get(2);
    assertEquals("postalCode", expressionField2.getName());
    assertEquals("java.lang.Integer", expressionField2.getClassType());
    assertEquals("Integer", expressionField2.getGenericType());
    assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(1), expressionField2.getPrevious());
    assertNull(expressionField2.getNext());
}
Also used : SingleFieldConstraintEBLeftSide(org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) ExpressionField(org.drools.workbench.models.datamodel.rule.ExpressionField) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) ExpressionUnboundFact(org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact) Test(org.junit.Test)

Example 34 with SingleFieldConstraintEBLeftSide

use of org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide in project drools-wb by kiegroup.

the class RuleModelCloneVisitor method visitSingleFieldConstraint.

private SingleFieldConstraintEBLeftSide visitSingleFieldConstraint(SingleFieldConstraintEBLeftSide sfexp) {
    SingleFieldConstraintEBLeftSide clone = new SingleFieldConstraintEBLeftSide();
    clone.setConstraintValueType(sfexp.getConstraintValueType());
    clone.setExpressionLeftSide((ExpressionFormLine) visit(sfexp.getExpressionLeftSide()));
    clone.setExpressionValue((ExpressionFormLine) visit(sfexp.getExpressionValue()));
    clone.setFactType(sfexp.getFactType());
    // skip setting fieldBinding, fieldName and fieldType, these are computed from expressionLeftSide
    clone.setId(sfexp.getId());
    clone.setOperator(sfexp.getOperator());
    for (Map.Entry<String, String> entry : sfexp.getParameters().entrySet()) {
        clone.setParameter(entry.getKey(), entry.getValue());
    }
    clone.setParent(sfexp.getParent());
    clone.setValue(sfexp.getValue());
    if (sfexp.getConnectives() != null) {
        clone.setConnectives(new ConnectiveConstraint[sfexp.getConnectives().length]);
        for (int i = 0; i < sfexp.getConnectives().length; i++) {
            clone.getConnectives()[i] = (ConnectiveConstraint) visit(sfexp.getConnectives()[i]);
        }
    }
    return clone;
}
Also used : SingleFieldConstraintEBLeftSide(org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide) HashMap(java.util.HashMap) Map(java.util.Map) ConnectiveConstraint(org.drools.workbench.models.datamodel.rule.ConnectiveConstraint) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) FieldConstraint(org.drools.workbench.models.datamodel.rule.FieldConstraint)

Example 35 with SingleFieldConstraintEBLeftSide

use of org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide in project drools-wb by kiegroup.

the class PopupCreator method makeSingleFieldConstraintEBLeftSide.

SingleFieldConstraintEBLeftSide makeSingleFieldConstraintEBLeftSide(final String factType) {
    final SingleFieldConstraintEBLeftSide constraint = new SingleFieldConstraintEBLeftSide();
    final ExpressionFormLine expression = makeExpressionFormLine(factType);
    constraint.setConstraintValueType(SingleFieldConstraint.TYPE_UNDEFINED);
    constraint.setExpressionLeftSide(expression);
    constraint.setFactType(factType);
    return constraint;
}
Also used : SingleFieldConstraintEBLeftSide(org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide) ExpressionFormLine(org.drools.workbench.models.datamodel.rule.ExpressionFormLine)

Aggregations

SingleFieldConstraintEBLeftSide (org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide)37 Test (org.junit.Test)31 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)30 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)29 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)29 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)29 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)28 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)28 ExpressionUnboundFact (org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact)24 ExpressionField (org.drools.workbench.models.datamodel.rule.ExpressionField)21 FromEntryPointFactPattern (org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern)13 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)10 ExpressionMethod (org.drools.workbench.models.datamodel.rule.ExpressionMethod)7 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)6 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)3 CompositeFieldConstraint (org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint)3 ExpressionFormLine (org.drools.workbench.models.datamodel.rule.ExpressionFormLine)3 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)3 List (java.util.List)2