Search in sources :

Example 6 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testFunctionCalls2.

@Test
public void testFunctionCalls2() {
    String drl = "package org.mortgages;\n" + "import java.lang.Number;\n" + "import java.lang.String;\n" + "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + "  s : String()\n" + "then\n" + "  s.indexOf( 0 );\n" + "end\n";
    Map<String, List<MethodInfo>> methodInformation = new HashMap<>();
    List<MethodInfo> mapMethodInformation = new ArrayList<>();
    mapMethodInformation.add(new MethodInfo("indexOf", Collections.singletonList(DataType.TYPE_STRING), "int", null, DataType.TYPE_STRING));
    mapMethodInformation.add(new MethodInfo("indexOf", Collections.singletonList(DataType.TYPE_NUMERIC_INTEGER), "int", null, DataType.TYPE_STRING));
    methodInformation.put("java.lang.String", mapMethodInformation);
    when(dmo.getModuleMethodInformation()).thenReturn(methodInformation);
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals(1, m.rhs.length);
    assertTrue(m.rhs[0] instanceof ActionCallMethod);
    ActionCallMethod actionCallMethod1 = (ActionCallMethod) m.rhs[0];
    assertEquals(1, actionCallMethod1.getState());
    assertEquals("indexOf", actionCallMethod1.getMethodName());
    assertEquals("s", actionCallMethod1.getVariable());
    assertEquals(1, actionCallMethod1.getFieldValues().length);
    assertEquals("indexOf", actionCallMethod1.getFieldValues()[0].getField());
    assertEquals("0", actionCallMethod1.getFieldValues()[0].getValue());
    assertEquals(FieldNatureType.TYPE_LITERAL, actionCallMethod1.getFieldValues()[0].getNature());
    assertEquals(DataType.TYPE_NUMERIC_INTEGER, actionCallMethod1.getFieldValues()[0].getType());
    assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ActionCallMethod(org.drools.workbench.models.datamodel.rule.ActionCallMethod) List(java.util.List) ArrayList(java.util.ArrayList) MethodInfo(org.kie.soup.project.datamodel.oracle.MethodInfo) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 7 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testLHSNestedMethodOneParameter.

@Test
public void testLHSNestedMethodOneParameter() {
    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( myString == $foo.myList.get(1))\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);
    addMethodInformation("java.util.List", "get", new ArrayList<String>() {

        {
            add("Integer");
        }
    }, "java.lang.Object", null, "java.lang.Object");
    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 8 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testFactPatternWithBinding.

@Test
public void testFactPatternWithBinding() {
    String drl = "rule \"rule1\"\n" + "when\n" + "$a : Applicant()\n" + "then\n" + "end";
    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());
    assertEquals(0, fp.getNumberOfConstraints());
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) 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 9 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testNestedFieldConstraintsOnlyLeafOperator.

@Test
public void testNestedFieldConstraintsOnlyLeafOperator() {
    String drl = "rule \"rule1\"\n" + "when\n" + "ParentType( parentChildField.childField == \"hello\" )\n" + "then\n" + "end";
    addModelField("org.test.ParentType", "parentChildField", "org.test.ChildType", "ChildType");
    addModelField("org.test.ChildType", "childField", "java.lang.String", "String");
    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("childField", ebLeftSide.getFieldName());
    assertEquals("java.lang.String", ebLeftSide.getFieldType());
    assertEquals("==", ebLeftSide.getOperator());
    assertEquals("hello", 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("ParentType", expressionUnboundFact.getName());
    assertEquals("ParentType", expressionUnboundFact.getClassType());
    assertEquals("ParentType", 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("parentChildField", expressionField1.getName());
    assertEquals("org.test.ChildType", expressionField1.getClassType());
    assertEquals("ChildType", 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("childField", expressionField2.getName());
    assertEquals("java.lang.String", expressionField2.getClassType());
    assertEquals("String", 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 10 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testDSLExpansionLHS_WithKeyword_then.

@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1173842
void testDSLExpansionLHS_WithKeyword_then() {
    String expected_dslr = "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + "There is an Applicant\n" + "- age more then 55\n" + "then\n" + "end\n";
    String expected_drl = "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + "Applicant( age > 55 )\n" + "then\n" + "end\n";
    final String dslDefinition1 = "There is an Applicant";
    final String dslFile1 = "[when]" + dslDefinition1 + "=Applicant( )";
    final String dslDefinition2 = "- age more then {age}";
    final String dslFile2 = "[when]" + dslDefinition2 + "=age > {age}";
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshalUsingDSL(expected_dslr, Collections.emptyList(), dmo, dslFile1, dslFile2);
    assertNotNull(m);
    assertEquals(2, m.lhs.length);
    assertTrue(m.lhs[0] instanceof DSLSentence);
    assertTrue(m.lhs[1] instanceof DSLSentence);
    DSLSentence dslSentence1 = (DSLSentence) m.lhs[0];
    assertEquals(dslDefinition1, dslSentence1.getDefinition());
    assertEquals(0, dslSentence1.getValues().size());
    DSLSentence dslSentence2 = (DSLSentence) m.lhs[1];
    assertEquals(dslDefinition2, dslSentence2.getDefinition());
    assertEquals(1, dslSentence2.getValues().size());
    DSLVariableValue dslVariableValue = dslSentence2.getValues().get(0);
    assertEquals("55", dslVariableValue.getValue());
    // Check round-trip
    assertEqualsIgnoreWhitespace(expected_dslr, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
    // Check DSL expansion (as BZ stated runtime was flawed as well)
    final Expander expander = new DefaultExpander();
    final List<DSLMappingFile> dsls = new ArrayList<>();
    try {
        final DSLTokenizedMappingFile dslTokenizer1 = new DSLTokenizedMappingFile();
        if (dslTokenizer1.parseAndLoad(new StringReader(dslFile1))) {
            dsls.add(dslTokenizer1);
        } else {
            fail();
        }
        final DSLTokenizedMappingFile dslTokenizer2 = new DSLTokenizedMappingFile();
        if (dslTokenizer2.parseAndLoad(new StringReader(dslFile2))) {
            dsls.add(dslTokenizer2);
        } else {
            fail();
        }
    } catch (IOException e) {
        fail();
    }
    for (DSLMappingFile dsl : dsls) {
        expander.addDSLMapping(dsl.getMapping());
    }
    final String actual_drl = expander.expand(expected_dslr);
    assertEqualsIgnoreWhitespace(expected_drl, actual_drl);
}
Also used : Expander(org.drools.compiler.lang.Expander) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) DSLVariableValue(org.drools.workbench.models.datamodel.rule.DSLVariableValue) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) IOException(java.io.IOException) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) DSLMappingFile(org.drools.compiler.lang.dsl.DSLMappingFile) DSLTokenizedMappingFile(org.drools.compiler.lang.dsl.DSLTokenizedMappingFile) 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