Search in sources :

Example 36 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testListSize.

@Test
public void testListSize() throws Exception {
    String drl = "" + "package org.mortgages;\n" + "import java.lang.Number;\n" + "rule \"Test\"\n" + "  dialect \"mvel\"\n" + "  when\n" + "    Person( addresses.size() == 0 )\n" + "  then\n" + "end\n";
    addModelField("Person", "addresses", "java.util.List", DataType.TYPE_COLLECTION);
    addMethodInformation("java.util.List", "size", Collections.emptyList(), "int", null, DataType.TYPE_NUMERIC_INTEGER);
    HashMap<String, String> map = new HashMap<>();
    map.put("Person#addresses", "Address");
    when(dmo.getModuleFieldParametersType()).thenReturn(map);
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertEquals(1, m.lhs.length);
    FactPattern pattern = (FactPattern) m.lhs[0];
    assertEquals(1, pattern.getConstraintList().getConstraints().length);
    SingleFieldConstraintEBLeftSide constraint = (SingleFieldConstraintEBLeftSide) pattern.getConstraint(0);
    assertEquals("size", constraint.getFieldName());
    assertEquals("int", constraint.getFieldType());
    assertEquals("0", constraint.getValue());
    assertEquals("==", constraint.getOperator());
    assertEquals(1, constraint.getConstraintValueType());
    assertTrue(constraint.getExpressionLeftSide().getParts().get(0) instanceof ExpressionUnboundFact);
    assertTrue(constraint.getExpressionLeftSide().getParts().get(1) instanceof ExpressionCollection);
    assertTrue(constraint.getExpressionLeftSide().getParts().get(2) instanceof ExpressionMethod);
    assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
Also used : SingleFieldConstraintEBLeftSide(org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide) ExpressionMethod(org.drools.workbench.models.datamodel.rule.ExpressionMethod) HashMap(java.util.HashMap) ExpressionCollection(org.drools.workbench.models.datamodel.rule.ExpressionCollection) 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) ExpressionUnboundFact(org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact) Test(org.junit.Test)

Example 37 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testRHSTemplateKeys.

@Test
public // https://issues.jboss.org/browse/GUVNOR-2030
void testRHSTemplateKeys() throws Exception {
    String drl = "package org.test;\n" + "rule \"MyRule\"\n" + "dialect \"mvel\"\n" + "when\n" + "  $p : Person( name == \"Fred\" )\n" + "then\n" + "  modify( $p ) { setName( \"@{k1}\" ) }\n" + "end";
    addModelField("org.test.Person", "this", "org.test.Person", DataType.TYPE_THIS);
    addModelField("org.test.Person", "name", String.class.getName(), DataType.TYPE_STRING);
    when(dmo.getPackageName()).thenReturn("org.test");
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals(1, m.lhs.length);
    final IPattern p0 = m.lhs[0];
    assertTrue(p0 instanceof FactPattern);
    final FactPattern fp0 = (FactPattern) p0;
    assertEquals("Person", fp0.getFactType());
    assertEquals(1, fp0.getNumberOfConstraints());
    assertTrue(fp0.getConstraint(0) instanceof SingleFieldConstraint);
    final SingleFieldConstraint sfc1 = (SingleFieldConstraint) fp0.getConstraint(0);
    assertEquals("Person", sfc1.getFactType());
    assertEquals("name", sfc1.getFieldName());
    assertEquals(DataType.TYPE_STRING, sfc1.getFieldType());
    assertEquals(SingleFieldConstraint.TYPE_LITERAL, sfc1.getConstraintValueType());
    assertEquals("Fred", sfc1.getValue());
    assertEquals(1, m.rhs.length);
    assertTrue(m.rhs[0] instanceof ActionUpdateField);
    ActionUpdateField auf = (ActionUpdateField) m.rhs[0];
    assertEquals("$p", auf.getVariable());
    assertEquals(1, auf.getFieldValues().length);
    ActionFieldValue afv = auf.getFieldValues()[0];
    assertEquals("name", afv.getField());
    assertEquals("k1", afv.getValue());
    assertEquals(FieldNatureType.TYPE_TEMPLATE, afv.getNature());
}
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) ActionUpdateField(org.drools.workbench.models.datamodel.rule.ActionUpdateField) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) 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 38 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testNewKeywordVariableNamePrefix3.

@Test
public // https://issues.jboss.org/browse/GUVNOR-2143
void testNewKeywordVariableNamePrefix3() {
    String oldValue = System.getProperty("drools.dateformat");
    try {
        System.setProperty("drools.dateformat", "dd-MMM-yyyy");
        String drl = "package org.test;\n" + "rule \"rule1\"\n" + "  dialect \"java\"\n" + "  when\n" + "    $a : Applicant()\n" + "  then\n" + "    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"dd-MMM-yyyy\");\n" + "    java.util.Date newStartDate = new java.util.Date();\n" + "    modify( $a ) \n" + "    { setApplicantDate( newStartDate ) }\n" + "end\n";
        addModelField("org.test.Applicant", "this", "org.test.Applicant", DataType.TYPE_THIS);
        addModelField("org.test.Applicant", "applicantDate", Date.class.getName(), DataType.TYPE_DATE);
        when(dmo.getPackageName()).thenReturn("org.test");
        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("Applicant", fp.getFactType());
        assertEquals("$a", fp.getBoundName());
        assertNull(fp.getConstraintList());
        assertEquals(2, m.rhs.length);
        assertTrue(m.rhs[0] instanceof FreeFormLine);
        FreeFormLine ffl = (FreeFormLine) m.rhs[0];
        assertEquals("java.util.Date newStartDate = new java.util.Date();", ffl.getText());
        assertTrue(m.rhs[1] instanceof ActionUpdateField);
        ActionUpdateField auf = (ActionUpdateField) m.rhs[1];
        assertEquals("$a", auf.getVariable());
        assertEquals(1, auf.getFieldValues().length);
        ActionFieldValue afv = auf.getFieldValues()[0];
        assertEquals("applicantDate", afv.getField());
        assertEquals("newStartDate", afv.getValue());
        assertEquals(FieldNatureType.TYPE_FORMULA, afv.getNature());
        assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
    } finally {
        if (oldValue == null) {
            System.clearProperty("drools.dateformat");
        } else {
            System.setProperty("drools.dateformat", oldValue);
        }
    }
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) FreeFormLine(org.drools.workbench.models.datamodel.rule.FreeFormLine) ActionUpdateField(org.drools.workbench.models.datamodel.rule.ActionUpdateField) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) 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) Date(java.util.Date) Test(org.junit.Test)

Example 39 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testLHSMultipleAllOfTheFollowing.

@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1174360
void testLHSMultipleAllOfTheFollowing() throws Exception {
    String drl = "package org.test;\n" + "rule \"test\"\n" + "dialect \"mvel\"\n" + "when\n" + "PhoneNumber(\n" + "( homePhone != null && homePhone matches \"\\\"+9199\\\"\" ) || \n" + "( personalPhone != null && personalPhone matches \"\\\"+9188\\\"\" ) || \n" + "( workPhone != null && workPhone matches \"\\\"+9177\\\"\") \n" + ")\n" + "then\n" + "end\n";
    addModelField("org.test.PhoneNumber", "this", "org.test.PhoneNumber", DataType.TYPE_THIS);
    addModelField("org.test.PhoneNumber", "homePhone", String.class.getName(), DataType.TYPE_STRING);
    addModelField("org.test.PhoneNumber", "personalPhone", String.class.getName(), DataType.TYPE_STRING);
    addModelField("org.test.PhoneNumber", "workPhone", String.class.getName(), DataType.TYPE_STRING);
    when(dmo.getPackageName()).thenReturn("org.test");
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals(1, m.lhs.length);
    assertEquals(0, m.rhs.length);
    // Check Pattern
    assertTrue(m.lhs[0] instanceof FactPattern);
    final FactPattern fp = (FactPattern) m.lhs[0];
    assertEquals("PhoneNumber", fp.getFactType());
    assertEquals(1, fp.getNumberOfConstraints());
    assertTrue(fp.getConstraint(0) instanceof CompositeFieldConstraint);
    final CompositeFieldConstraint cfc = (CompositeFieldConstraint) fp.getConstraint(0);
    assertEquals("||", cfc.getCompositeJunctionType());
    assertEquals(3, cfc.getNumberOfConstraints());
    assertTrue(cfc.getConstraint(0) instanceof CompositeFieldConstraint);
    assertTrue(cfc.getConstraint(1) instanceof CompositeFieldConstraint);
    assertTrue(cfc.getConstraint(2) instanceof CompositeFieldConstraint);
    // Check first composite field constraint
    final CompositeFieldConstraint cfc_0 = (CompositeFieldConstraint) cfc.getConstraint(0);
    assertEquals("&&", cfc_0.getCompositeJunctionType());
    assertEquals(2, cfc_0.getNumberOfConstraints());
    assertTrue(cfc_0.getConstraint(0) instanceof SingleFieldConstraint);
    assertTrue(cfc_0.getConstraint(1) instanceof SingleFieldConstraint);
    final SingleFieldConstraint sfc_0_0 = (SingleFieldConstraint) cfc_0.getConstraint(0);
    assertEquals("PhoneNumber", sfc_0_0.getFactType());
    assertEquals("homePhone", sfc_0_0.getFieldName());
    assertEquals(DataType.TYPE_STRING, sfc_0_0.getFieldType());
    assertEquals("!= null", sfc_0_0.getOperator());
    assertNull(sfc_0_0.getValue());
    final SingleFieldConstraint sfc_0_1 = (SingleFieldConstraint) cfc_0.getConstraint(1);
    assertEquals("PhoneNumber", sfc_0_1.getFactType());
    assertEquals("homePhone", sfc_0_1.getFieldName());
    assertEquals(DataType.TYPE_STRING, sfc_0_1.getFieldType());
    assertEquals("matches", sfc_0_1.getOperator());
    assertEquals("\\\"+9199\\\"", sfc_0_1.getValue());
    // Check second composite field constraint
    final CompositeFieldConstraint cfc_1 = (CompositeFieldConstraint) cfc.getConstraint(1);
    assertEquals("&&", cfc_1.getCompositeJunctionType());
    assertEquals(2, cfc_1.getNumberOfConstraints());
    assertTrue(cfc_1.getConstraint(0) instanceof SingleFieldConstraint);
    assertTrue(cfc_1.getConstraint(1) instanceof SingleFieldConstraint);
    final SingleFieldConstraint sfc_1_0 = (SingleFieldConstraint) cfc_1.getConstraint(0);
    assertEquals("PhoneNumber", sfc_1_0.getFactType());
    assertEquals("personalPhone", sfc_1_0.getFieldName());
    assertEquals(DataType.TYPE_STRING, sfc_1_0.getFieldType());
    assertEquals("!= null", sfc_1_0.getOperator());
    assertNull(sfc_1_0.getValue());
    final SingleFieldConstraint sfc_1_1 = (SingleFieldConstraint) cfc_1.getConstraint(1);
    assertEquals("PhoneNumber", sfc_1_1.getFactType());
    assertEquals("personalPhone", sfc_1_1.getFieldName());
    assertEquals(DataType.TYPE_STRING, sfc_1_1.getFieldType());
    assertEquals("matches", sfc_1_1.getOperator());
    assertEquals("\\\"+9188\\\"", sfc_1_1.getValue());
    // Check third composite field constraint
    final CompositeFieldConstraint cfc_2 = (CompositeFieldConstraint) cfc.getConstraint(2);
    assertEquals("&&", cfc_2.getCompositeJunctionType());
    assertEquals(2, cfc_2.getNumberOfConstraints());
    assertTrue(cfc_2.getConstraint(0) instanceof SingleFieldConstraint);
    assertTrue(cfc_2.getConstraint(1) instanceof SingleFieldConstraint);
    final SingleFieldConstraint sfc_2_0 = (SingleFieldConstraint) cfc_2.getConstraint(0);
    assertEquals("PhoneNumber", sfc_2_0.getFactType());
    assertEquals("workPhone", sfc_2_0.getFieldName());
    assertEquals(DataType.TYPE_STRING, sfc_2_0.getFieldType());
    assertEquals("!= null", sfc_2_0.getOperator());
    assertNull(sfc_2_0.getValue());
    final SingleFieldConstraint sfc_2_1 = (SingleFieldConstraint) cfc_2.getConstraint(1);
    assertEquals("PhoneNumber", sfc_2_1.getFactType());
    assertEquals("workPhone", sfc_2_1.getFieldName());
    assertEquals(DataType.TYPE_STRING, sfc_2_1.getFieldType());
    assertEquals("matches", sfc_2_1.getOperator());
    assertEquals("\\\"+9177\\\"", sfc_2_1.getValue());
    // Check round-trip
    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) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) Test(org.junit.Test)

Example 40 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testDSLWithMultilineBracketsTwoFields.

@Test
public void testDSLWithMultilineBracketsTwoFields() {
    String drl = "rule \"rule1\"\n" + "when\n" + "> Applicant( name == \n" + "> \"John\"\n" + "> , \n" + "> age > 18 )\n" + "then\n" + "end\n";
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshalUsingDSL(drl, Collections.emptyList(), dmo, "");
    assertNotNull(m);
    assertTrue(m.lhs[0] instanceof FactPattern);
    FactPattern pattern = (FactPattern) m.lhs[0];
    assertEquals("Applicant", pattern.getFactType());
    assertEquals(2, pattern.getNumberOfConstraints());
    assertEquals("name", ((SingleFieldConstraint) pattern.getConstraint(0)).getFieldName());
    assertEquals("==", ((SingleFieldConstraint) pattern.getConstraint(0)).getOperator());
    assertEquals("John", ((SingleFieldConstraint) pattern.getConstraint(0)).getValue());
    assertEquals("age", ((SingleFieldConstraint) pattern.getConstraint(1)).getFieldName());
    assertEquals(">", ((SingleFieldConstraint) pattern.getConstraint(1)).getOperator());
    assertEquals("18", ((SingleFieldConstraint) pattern.getConstraint(1)).getValue());
}
Also used : 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)

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