Search in sources :

Example 1 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testBoundListConstraint.

@Test
public void testBoundListConstraint() throws Exception {
    // https://bugzilla.redhat.com/show_bug.cgi?id=1264339
    String drl = "package org.test;\n" + "rule \"List_Of_Values_Issue\"\n" + "dialect \"mvel\"\n" + "when\n" + "  MyTransactionVO( $myData : myData )\n" + "  MyDataList( myDataList contains $myData )\n" + "     then\n" + "end";
    addModelField("org.test.MyTransactionVO", "this", "org.test.MyTransactionVO", DataType.TYPE_THIS);
    addModelField("org.test.MyTransactionVO", "myData", "org.test.MyDataList", "org.test.MyDataList");
    addModelField("org.test.MyDataList", "this", "org.test.MyDataList", DataType.TYPE_THIS);
    addModelField("org.test.MyDataList", "myDataList", List.class.getName(), DataType.TYPE_COLLECTION);
    when(dmo.getPackageName()).thenReturn("org.test");
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals(2, m.lhs.length);
    final IPattern p0 = m.lhs[0];
    assertTrue(p0 instanceof FactPattern);
    final FactPattern fp0 = (FactPattern) p0;
    assertEquals("MyTransactionVO", fp0.getFactType());
    assertEquals(1, fp0.getNumberOfConstraints());
    final IPattern p1 = m.lhs[1];
    assertTrue(p1 instanceof FactPattern);
    final FactPattern fp1 = (FactPattern) p1;
    assertEquals("MyDataList", fp1.getFactType());
    assertEquals(1, fp1.getNumberOfConstraints());
    assertTrue(fp0.getConstraint(0) instanceof SingleFieldConstraint);
    final SingleFieldConstraint fp0sfc0 = (SingleFieldConstraint) fp0.getConstraint(0);
    assertEquals("MyTransactionVO", fp0sfc0.getFactType());
    assertEquals("myData", fp0sfc0.getFieldName());
    assertEquals("$myData", fp0sfc0.getFieldBinding());
    assertEquals("MyDataList", fp0sfc0.getFieldType());
    assertNull(fp0sfc0.getOperator());
    assertNull(fp0sfc0.getValue());
    assertTrue(fp1.getConstraint(0) instanceof SingleFieldConstraint);
    final SingleFieldConstraint fp1sfc0 = (SingleFieldConstraint) fp1.getConstraint(0);
    assertEquals("MyDataList", fp1sfc0.getFactType());
    assertEquals("myDataList", fp1sfc0.getFieldName());
    assertEquals("contains", fp1sfc0.getOperator());
    assertEquals("$myData", fp1sfc0.getValue());
    assertEquals(SingleFieldConstraint.TYPE_VARIABLE, fp1sfc0.getConstraintValueType());
    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) 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) List(java.util.List) ArrayList(java.util.ArrayList) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 2 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testDSLExpansionLHS.

@Test
public void testDSLExpansionLHS() {
    String drl = "rule \"rule1\"\n" + "when\n" + "The credit rating is AA\n" + "then\n" + "end\n";
    final String dslDefinition = "The credit rating is {rating:ENUM:Applicant.creditRating}";
    final String dslFile = "[when]" + dslDefinition + "=Applicant( creditRating == {rating} )";
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshalUsingDSL(drl, Collections.emptyList(), dmo, dslFile);
    assertNotNull(m);
    assertTrue(m.lhs[0] instanceof DSLSentence);
    DSLSentence dslSentence = (DSLSentence) m.lhs[0];
    assertEquals(dslDefinition, dslSentence.getDefinition());
    assertEquals(1, dslSentence.getValues().size());
    assertTrue(dslSentence.getValues().get(0) instanceof DSLComplexVariableValue);
    DSLComplexVariableValue dslComplexVariableValue = (DSLComplexVariableValue) dslSentence.getValues().get(0);
    assertEquals("AA", dslComplexVariableValue.getValue());
    assertEquals("ENUM:Applicant.creditRating", dslComplexVariableValue.getId());
}
Also used : RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence) DSLComplexVariableValue(org.drools.workbench.models.datamodel.rule.DSLComplexVariableValue) Test(org.junit.Test)

Example 3 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testAttributes.

@Test
public void testAttributes() {
    String drl = "rule rule1\n" + "salience 42\n" + "when\n" + "then\n" + "end";
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals(1, m.attributes.length);
    assertEquals("salience", m.attributes[0].getAttributeName());
    assertEquals("42", m.attributes[0].getValue());
}
Also used : RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 4 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testFunctionCalls3.

@Test
public void testFunctionCalls3() {
    String drl = "package org.mortgages;\n" + "import java.lang.Number;\n" + "import java.lang.String;\n" + "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + "  $var : String()\n" + "then\n" + "  $var.indexOf( $var );\n" + "  $var.endsWith( \".\" );\n" + "  $var.substring( 0, 1 );\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));
    mapMethodInformation.add(new MethodInfo("endsWith", Collections.singletonList(DataType.TYPE_STRING), "boolean", null, DataType.TYPE_BOOLEAN));
    mapMethodInformation.add(new MethodInfo("substring", Collections.singletonList(DataType.TYPE_NUMERIC_INTEGER), "String", null, DataType.TYPE_STRING));
    mapMethodInformation.add(new MethodInfo("substring", Arrays.asList(DataType.TYPE_NUMERIC_INTEGER, DataType.TYPE_NUMERIC_INTEGER), "String", 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(3, m.rhs.length);
    assertTrue(m.rhs[0] instanceof ActionCallMethod);
    assertTrue(m.rhs[1] instanceof ActionCallMethod);
    assertTrue(m.rhs[2] instanceof ActionCallMethod);
    ActionCallMethod actionCallMethod1 = (ActionCallMethod) m.rhs[0];
    assertEquals(1, actionCallMethod1.getState());
    assertEquals("indexOf", actionCallMethod1.getMethodName());
    assertEquals("$var", actionCallMethod1.getVariable());
    assertEquals(1, actionCallMethod1.getFieldValues().length);
    assertEquals("indexOf", actionCallMethod1.getFieldValues()[0].getField());
    assertEquals("$var", actionCallMethod1.getFieldValues()[0].getValue());
    assertEquals(FieldNatureType.TYPE_VARIABLE, actionCallMethod1.getFieldValues()[0].getNature());
    assertEquals(DataType.TYPE_STRING, actionCallMethod1.getFieldValues()[0].getType());
    ActionCallMethod actionCallMethod2 = (ActionCallMethod) m.rhs[1];
    assertEquals(1, actionCallMethod2.getState());
    assertEquals("endsWith", actionCallMethod2.getMethodName());
    assertEquals("$var", actionCallMethod2.getVariable());
    assertEquals(1, actionCallMethod2.getFieldValues().length);
    assertEquals("endsWith", actionCallMethod2.getFieldValues()[0].getField());
    assertEquals(".", actionCallMethod2.getFieldValues()[0].getValue());
    assertEquals(FieldNatureType.TYPE_LITERAL, actionCallMethod2.getFieldValues()[0].getNature());
    assertEquals(DataType.TYPE_STRING, actionCallMethod2.getFieldValues()[0].getType());
    ActionCallMethod actionCallMethod3 = (ActionCallMethod) m.rhs[2];
    assertEquals(1, actionCallMethod3.getState());
    assertEquals("substring", actionCallMethod3.getMethodName());
    assertEquals("$var", actionCallMethod3.getVariable());
    assertEquals(2, actionCallMethod3.getFieldValues().length);
    assertEquals("substring", actionCallMethod3.getFieldValues()[0].getField());
    assertEquals("0", actionCallMethod3.getFieldValues()[0].getValue());
    assertEquals(FieldNatureType.TYPE_LITERAL, actionCallMethod3.getFieldValues()[0].getNature());
    assertEquals(DataType.TYPE_NUMERIC_INTEGER, actionCallMethod3.getFieldValues()[0].getType());
    assertEquals("substring", actionCallMethod3.getFieldValues()[1].getField());
    assertEquals("1", actionCallMethod3.getFieldValues()[1].getValue());
    assertEquals(FieldNatureType.TYPE_LITERAL, actionCallMethod3.getFieldValues()[1].getNature());
    assertEquals(DataType.TYPE_NUMERIC_INTEGER, actionCallMethod3.getFieldValues()[1].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 5 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testSingleFieldConstraintCEPOperatorTimeWindow.

@Test
public void testSingleFieldConstraintCEPOperatorTimeWindow() {
    String drl = "rule \"rule1\"\n" + "when\n" + "Event() over window:time (1d)\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 p1 = m.lhs[0];
    assertTrue(p1 instanceof FactPattern);
    FactPattern fp1 = (FactPattern) p1;
    assertEquals("Event", fp1.getFactType());
    assertNull(fp1.getBoundName());
    assertEquals(0, fp1.getNumberOfConstraints());
    assertNotNull(fp1.getWindow());
    CEPWindow window = fp1.getWindow();
    assertEquals("over window:time", window.getOperator());
    assertEquals(2, window.getParameters().size());
    assertEquals("1d", window.getParameter("1"));
    assertEquals("org.drools.workbench.models.commons.backend.rule.CEPWindowOperatorParameterDRLBuilder", window.getParameter("org.drools.workbench.models.commons.backend.rule.operatorParameterGenerator"));
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) CEPWindow(org.drools.workbench.models.datamodel.rule.CEPWindow) 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