Search in sources :

Example 71 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testSingleFieldConstraintEBLeftSide.

@Test
public void testSingleFieldConstraintEBLeftSide() throws Exception {
    String drl = "" + "rule \" broken \"\n" + "dialect \"mvel\"\n" + "  when\n" + "    Customer( contact != null , contact.tel1 > \"15\" )\n" + "  then\n" + "end";
    addModelField("Customer", "contact", "Contact", "Contact");
    addModelField("Contact", "tel1", "String", "String");
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    FactPattern pattern = (FactPattern) m.lhs[0];
    SingleFieldConstraint constraint = (SingleFieldConstraint) pattern.getFieldConstraints()[0];
    assertEquals("Customer", constraint.getFactType());
    assertEquals("contact", constraint.getFieldName());
    assertEquals("Contact", constraint.getFieldType());
    SingleFieldConstraintEBLeftSide constraint2 = (SingleFieldConstraintEBLeftSide) pattern.getFieldConstraints()[1];
    assertEquals("tel1", constraint2.getFieldName());
    assertEquals("String", constraint2.getFieldType());
    assertEquals("15", constraint2.getValue());
    assertEquals(">", constraint2.getOperator());
    assertEquals(3, constraint2.getExpressionLeftSide().getParts().size());
    ExpressionPart part1 = constraint2.getExpressionLeftSide().getParts().get(0);
    assertEquals("Customer", part1.getName());
    assertEquals("Customer", part1.getClassType());
    assertEquals("Customer", part1.getGenericType());
    ExpressionPart part2 = constraint2.getExpressionLeftSide().getParts().get(1);
    assertEquals("contact", part2.getName());
    assertEquals("Contact", part2.getClassType());
    assertEquals("Contact", part2.getGenericType());
    ExpressionPart part3 = constraint2.getExpressionLeftSide().getParts().get(2);
    assertEquals("tel1", part3.getName());
    assertEquals("String", part3.getClassType());
    assertEquals("String", part3.getGenericType());
}
Also used : SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) SingleFieldConstraintEBLeftSide(org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide) ExpressionPart(org.drools.workbench.models.datamodel.rule.ExpressionPart) 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) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 72 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testNestedFieldExpressionsWithAnotherExpression.

@Test
public void testNestedFieldExpressionsWithAnotherExpression() {
    String drl = "rule rule1\n" + "when\n" + "p : Person( address.postalCode == p.address.postalCode )\n" + "then\n" + "end";
    addModelField("org.test.Person", "address", "org.test.Address", "Address");
    addModelField("org.test.Person", "this", "org.test.Person", DataType.TYPE_THIS);
    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("", ebLeftSide.getValue());
    assertEquals(3, ebLeftSide.getExpressionValue().getParts().size());
    assertTrue(ebLeftSide.getExpressionValue().getParts().get(0) instanceof ExpressionVariable);
    ExpressionVariable expressionVariable = (ExpressionVariable) ebLeftSide.getExpressionValue().getParts().get(0);
    assertEquals("p", expressionVariable.getName());
    assertEquals("org.test.Person", expressionVariable.getClassType());
    assertEquals(DataType.TYPE_THIS, expressionVariable.getGenericType());
    assertTrue(ebLeftSide.getExpressionValue().getParts().get(1) instanceof ExpressionField);
    ExpressionField ef1 = (ExpressionField) ebLeftSide.getExpressionValue().getParts().get(1);
    assertEquals("address", ef1.getName());
    assertEquals("org.test.Address", ef1.getClassType());
    assertEquals("Address", ef1.getGenericType());
    assertTrue(ebLeftSide.getExpressionValue().getParts().get(2) instanceof ExpressionField);
    ExpressionField ef2 = (ExpressionField) ebLeftSide.getExpressionValue().getParts().get(2);
    assertEquals("postalCode", ef2.getName());
    assertEquals("java.lang.Integer", ef2.getClassType());
    assertEquals(DataType.TYPE_NUMERIC_INTEGER, ef2.getGenericType());
    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(DataType.TYPE_NUMERIC_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) ExpressionVariable(org.drools.workbench.models.datamodel.rule.ExpressionVariable) 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 73 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testLHSInOperatorFieldNameNotContainingNotInLiteral.

@Test
public void testLHSInOperatorFieldNameNotContainingNotInLiteral() {
    String drl = "package org.test\n" + "rule \"in\"\n" + "dialect \"mvel\"\n" + "when\n" + "Person( field1 not in (1, 2) )\n" + "then\n" + "end";
    addModelField("org.test.Person", "field1", "java.lang.Integer", DataType.TYPE_NUMERIC_INTEGER);
    when(dmo.getPackageName()).thenReturn("org.test");
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    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(fp.getConstraint(0) instanceof SingleFieldConstraint);
    SingleFieldConstraint sfp = (SingleFieldConstraint) fp.getConstraint(0);
    assertEquals("Person", sfp.getFactType());
    assertEquals("field1", sfp.getFieldName());
    assertEquals("not in", sfp.getOperator());
    assertEquals("1, 2", sfp.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp.getConstraintValueType());
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) 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) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 74 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testLHSFromCollectWithListDeclared.

@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1158813
void testLHSFromCollectWithListDeclared() throws Exception {
    String drl = "package org.test;\n" + "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + "  c : Customer( )\n" + "  items : List( eval( size == c.items.size )) \n" + "  from collect ( var : Item( price > 10 )) \n" + "then \n" + "end";
    addModelField("org.test.Customer", "this", "org.test.Customer", DataType.TYPE_THIS);
    addModelField("org.test.Item", "this", "org.test.Item", DataType.TYPE_THIS);
    addModelField("org.test.Item", "price", "java.lang.Double", DataType.TYPE_NUMERIC_DOUBLE);
    addModelField("java.util.List", "this", "java.util.List", DataType.TYPE_THIS);
    addMethodInformation("org.test.Customer", "items", new ArrayList<String>() {

        {
        }
    }, DataType.TYPE_NUMERIC_INTEGER, null, DataType.TYPE_NUMERIC_INTEGER);
    when(dmo.getPackageName()).thenReturn("org.test");
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals(2, m.lhs.length);
    assertEquals(0, m.rhs.length);
    assertTrue(m.lhs[0] instanceof FactPattern);
    final FactPattern fp = (FactPattern) m.lhs[0];
    assertEquals("Customer", fp.getFactType());
    assertEquals("c", fp.getBoundName());
    assertEquals(0, fp.getNumberOfConstraints());
    assertTrue(m.lhs[1] instanceof FromCollectCompositeFactPattern);
    final FromCollectCompositeFactPattern fcfp = (FromCollectCompositeFactPattern) m.lhs[1];
    assertNotNull(fcfp.getFactPattern());
    assertEquals("List", fcfp.getFactPattern().getFactType());
    assertEquals("items", fcfp.getFactPattern().getBoundName());
    assertEquals(1, fcfp.getFactPattern().getNumberOfConstraints());
    assertTrue(fcfp.getFactPattern().getConstraint(0) instanceof SingleFieldConstraint);
    final SingleFieldConstraint sfc0 = (SingleFieldConstraint) fcfp.getFactPattern().getConstraint(0);
    assertNull(sfc0.getFactType());
    assertNull(sfc0.getFieldName());
    assertEquals("size == c.items.size", sfc0.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_PREDICATE, sfc0.getConstraintValueType());
    assertNotNull(fcfp.getRightPattern());
    assertTrue(fcfp.getRightPattern() instanceof FactPattern);
    final FactPattern rfp = (FactPattern) fcfp.getRightPattern();
    assertEquals("Item", rfp.getFactType());
    assertEquals("var", rfp.getBoundName());
    assertEquals(1, rfp.getNumberOfConstraints());
    assertTrue(rfp.getConstraint(0) instanceof SingleFieldConstraint);
    final SingleFieldConstraint sfc1 = (SingleFieldConstraint) rfp.getConstraint(0);
    assertEquals("Item", sfc1.getFactType());
    assertEquals("price", sfc1.getFieldName());
    assertEquals("10", sfc1.getValue());
    assertEquals(">", sfc1.getOperator());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfc1.getConstraintValueType());
    assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
Also used : SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) 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) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 75 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testValidSyntaxNonImportedType.

@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1218308
void testValidSyntaxNonImportedType() throws Exception {
    String drl = "rule \"x\"\n" + "dialect \"mvel\"\n" + "when\n" + "  var : NotImported( )\n" + "  OtherType( field != var.field )\n" + "  (MyType( ) from var.collectionField)\n" + "then\n" + "end";
    addModelField("org.test.OtherType", "this", "org.test.OtherType", DataType.TYPE_THIS);
    addModelField("org.test.OtherType", "field", String.class.getName(), DataType.TYPE_STRING);
    addModelField("org.test.MyType", "this", "org.test.MyType", DataType.TYPE_THIS);
    when(dmo.getPackageName()).thenReturn("org.test");
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals(3, m.lhs.length);
    final IPattern p0 = m.lhs[0];
    assertTrue(p0 instanceof FactPattern);
    final FactPattern fp0 = (FactPattern) p0;
    assertEquals("NotImported", fp0.getFactType());
    assertEquals("var", fp0.getBoundName());
    assertEquals(0, fp0.getNumberOfConstraints());
    final IPattern p1 = m.lhs[1];
    assertTrue(p1 instanceof FactPattern);
    final FactPattern fp1 = (FactPattern) p1;
    assertEquals("OtherType", fp1.getFactType());
    assertEquals(1, fp1.getNumberOfConstraints());
    assertTrue(fp1.getConstraint(0) instanceof SingleFieldConstraint);
    final SingleFieldConstraint fp1sfc0 = (SingleFieldConstraint) fp1.getConstraint(0);
    assertEquals("OtherType", fp1sfc0.getFactType());
    assertEquals("field", fp1sfc0.getFieldName());
    assertEquals("!=", fp1sfc0.getOperator());
    assertEquals(2, fp1sfc0.getExpressionValue().getParts().size());
    assertTrue(fp1sfc0.getExpressionValue().getParts().get(0) instanceof ExpressionText);
    final ExpressionText fp1sfc0e0 = (ExpressionText) fp1sfc0.getExpressionValue().getParts().get(0);
    assertEquals("var", fp1sfc0e0.getName());
    assertTrue(fp1sfc0.getExpressionValue().getParts().get(1) instanceof ExpressionText);
    final ExpressionText fp1sfc0e1 = (ExpressionText) fp1sfc0.getExpressionValue().getParts().get(1);
    assertEquals("field", fp1sfc0e1.getName());
    final IPattern p2 = m.lhs[2];
    assertTrue(p2 instanceof FromCompositeFactPattern);
    final FromCompositeFactPattern fp2 = (FromCompositeFactPattern) p2;
    assertEquals("MyType", fp2.getFactType());
    assertEquals(2, fp2.getExpression().getParts().size());
    assertTrue(fp2.getExpression().getParts().get(0) instanceof ExpressionVariable);
    final ExpressionVariable fp2e0 = (ExpressionVariable) fp2.getExpression().getParts().get(0);
    assertEquals("var", fp2e0.getName());
    assertTrue(fp2.getExpression().getParts().get(1) instanceof ExpressionText);
    final ExpressionText fp2e1 = (ExpressionText) fp2.getExpression().getParts().get(1);
    assertEquals("collectionField", fp2e1.getName());
    assertEquals(0, m.rhs.length);
    // Check round-trip
    assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) 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) ExpressionText(org.drools.workbench.models.datamodel.rule.ExpressionText) ExpressionVariable(org.drools.workbench.models.datamodel.rule.ExpressionVariable) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Aggregations

RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)384 Test (org.junit.Test)352 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)223 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)216 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)215 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)201 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)201 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)145 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)134 FromEntryPointFactPattern (org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern)91 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)85 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)63 ActionInsertFact (org.drools.workbench.models.datamodel.rule.ActionInsertFact)47 ArrayList (java.util.ArrayList)44 IAction (org.drools.workbench.models.datamodel.rule.IAction)31 FreeFormLine (org.drools.workbench.models.datamodel.rule.FreeFormLine)29 SingleFieldConstraintEBLeftSide (org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide)29 ActionUpdateField (org.drools.workbench.models.datamodel.rule.ActionUpdateField)27 RuleAttribute (org.drools.workbench.models.datamodel.rule.RuleAttribute)25 List (java.util.List)24