Search in sources :

Example 21 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testLHSInOperatorFieldNameContainingNotInLiteral.

@Test
public void testLHSInOperatorFieldNameContainingNotInLiteral() {
    String drl = "package org.test\n" + "rule \"in\"\n" + "dialect \"mvel\"\n" + "when\n" + "Person( rating not in (1, 2) )\n" + "then\n" + "end";
    addModelField("org.test.Person", "rating", "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("rating", 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 22 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testLHSNestedMethodTwoParameters.

@Test
public void testLHSNestedMethodTwoParameters() {
    String drl = "package org.mortgages;\n" + "import java.lang.Number;\n" + "import java.util.List;\n" + "import org.drools.workbench.models.commons.backend.rule.classes.MyListContainerClass;\n" + "import org.drools.workbench.models.commons.backend.rule.classes.MyStringContainerClass;\n" + "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "  $foo : MyListContainerClass()\n" + "  $bar : MyStringContainerClass( $foo.myList.set(1, \"hello\" ) == true )\n" + "then\n" + "end\n";
    addModelField("org.drools.workbench.models.commons.backend.rule.classes.MyListContainerClass", "this", "org.drools.workbench.models.commons.backend.rule.classes.MyListContainerClass", DataType.TYPE_THIS);
    addModelField("org.drools.workbench.models.commons.backend.rule.classes.MyListContainerClass", "myList", "java.util.List", "java.util.List");
    addModelField("org.drools.workbench.models.commons.backend.rule.classes.MyStringContainerClass", "this", "org.drools.workbench.models.commons.backend.rule.classes.MyStringContainerClass", DataType.TYPE_THIS);
    addModelField("org.drools.workbench.models.commons.backend.rule.classes.MyStringContainerClass", "myString", "java.lang.String", DataType.TYPE_STRING);
    addModelField("java.util.List", "this", "java.util.List", DataType.TYPE_THIS);
    addMethodInformation("java.util.List", "set", new ArrayList<String>() {

        {
            add("Integer");
            add("String");
        }
    }, DataType.TYPE_BOOLEAN, null, DataType.TYPE_BOOLEAN);
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
Also used : RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 23 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testFieldVarsFactTypeInTheSamePackage.

@Test
public void testFieldVarsFactTypeInTheSamePackage() throws Exception {
    String drl = "" + "package org.test\n" + "rule \"Borked\"\n" + "  dialect \"mvel\"\n" + "  when\n" + "    Customer( var:contact )\n" + "  then\n" + "end";
    addModelField("org.test.Customer", "contact", "org.test.Contact", "org.test.Contact");
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    FactPattern pattern = (FactPattern) m.lhs[0];
    SingleFieldConstraint constraint = (SingleFieldConstraint) pattern.getFieldConstraints()[0];
    assertEquals("var", constraint.getFieldBinding());
    assertEquals("Customer", constraint.getFactType());
    assertEquals("contact", constraint.getFieldName());
    assertEquals("org.test.Contact", constraint.getFieldType());
}
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) Test(org.junit.Test)

Example 24 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testLHSValidLiteralFieldName.

@Test
public // https://issues.jboss.org/browse/DROOLS-715
void testLHSValidLiteralFieldName() throws Exception {
    String drl = "package org.test;\n" + "rule \"MyRule\"\n" + "dialect \"mvel\"\n" + "when\n" + "  MyClass( valid == true )\n" + "then\n" + "end";
    addModelField("org.test.MyClass", "this", "org.test.MyClass", DataType.TYPE_THIS);
    addModelField("org.test.MyClass", "valid", Boolean.class.getName(), DataType.TYPE_BOOLEAN);
    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("MyClass", fp0.getFactType());
    assertEquals(1, fp0.getNumberOfConstraints());
    assertTrue(fp0.getConstraint(0) instanceof SingleFieldConstraint);
    final SingleFieldConstraint sfc1 = (SingleFieldConstraint) fp0.getConstraint(0);
    assertEquals("MyClass", sfc1.getFactType());
    assertEquals("valid", sfc1.getFieldName());
    assertEquals(DataType.TYPE_BOOLEAN, sfc1.getFieldType());
    assertEquals("==", sfc1.getOperator());
    assertEquals("true", sfc1.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_ENUM, sfc1.getConstraintValueType());
    // 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) 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 25 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testSimpleDSLExpansionLHS.

@Test
public void testSimpleDSLExpansionLHS() {
    String drl = "rule \"rule1\"\n" + "when\n" + "Es gibt einen Vertrag\n" + "- Rabatt nicht mehr als 123\n" + "then\n" + "end\n";
    final String dslDefinition = "Es gibt einen Vertrag";
    final String dslFile = "[condition][vertrag]" + dslDefinition + "=vertrag : Vertrag()";
    final String dslDefinition2 = "- Rabatt nicht mehr als {rabatt}";
    final String dslFile2 = "[condition][vertrag]" + dslDefinition2 + "=rabatt < {rabatt}";
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshalUsingDSL(drl, Collections.emptyList(), dmo, dslFile, dslFile2);
    assertNotNull(m);
    assertTrue(m.lhs[0] instanceof DSLSentence);
    DSLSentence dslSentence = (DSLSentence) m.lhs[0];
    assertEquals("vertrag : Vertrag()", dslSentence.getDrl());
    assertEquals(dslDefinition, dslSentence.getDefinition());
    assertEquals(0, dslSentence.getValues().size());
    DSLSentence dslSentence2 = (DSLSentence) m.lhs[1];
    assertEquals("rabatt < {rabatt}", dslSentence2.getDrl());
    assertEquals(dslDefinition2, dslSentence2.getDefinition());
    assertEquals(1, dslSentence2.getValues().size());
    assertNotNull(dslSentence2.getValues().get(0));
    DSLVariableValue dslComplexVariableValue = dslSentence2.getValues().get(0);
    assertEquals("123", dslComplexVariableValue.getValue());
}
Also used : DSLVariableValue(org.drools.workbench.models.datamodel.rule.DSLVariableValue) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence) 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