Search in sources :

Example 16 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testDSLDollar.

@Test
public void testDSLDollar() {
    String drl = "package org.mortgages;\n" + "rule \"testdsl\"\n" + "  dialect \"mvel\"\n" + "  when\n" + "    Price is $111\n" + "  then\n" + "end";
    String dslDefinition = "Price is ${p}";
    String dslFile = "[when]" + dslDefinition + "= Item( price == \"{p}\" )";
    when(dmo.getPackageName()).thenReturn("org.mortgages");
    final RuleModel model = RuleModelDRLPersistenceImpl.getInstance().unmarshalUsingDSL(drl, Collections.emptyList(), dmo, dslFile);
    assertEquals(1, model.lhs.length);
    DSLSentence dslSentence = (DSLSentence) model.lhs[0];
    assertEquals("Price is ${p}", dslSentence.getDefinition());
    assertEquals("111", dslSentence.getValues().get(0).getValue());
}
Also used : RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence) Test(org.junit.Test)

Example 17 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testGlobalCollectionAdd.

@Test
public void testGlobalCollectionAdd() {
    // BZ-1013682
    String drl = "package org.mortgages;\n" + "\n" + "import org.mortgages.LoanApplication;\n" + "\n" + "rule \"Bankruptcy history\"\n" + "  dialect \"mvel\"\n" + "  when\n" + "    a : LoanApplication( )\n" + "  then\n" + "    keke.add( a );\n" + "end";
    HashMap<String, String> globals = new HashMap<>();
    globals.put("keke", "java.util.ArrayList");
    when(dmo.getPackageGlobals()).thenReturn(globals);
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertTrue(m.rhs[0] instanceof ActionGlobalCollectionAdd);
    ActionGlobalCollectionAdd actionGlobalCollectionAdd = (ActionGlobalCollectionAdd) m.rhs[0];
    assertEquals("keke", actionGlobalCollectionAdd.getGlobalName());
    assertEquals("a", actionGlobalCollectionAdd.getFactName());
}
Also used : HashMap(java.util.HashMap) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) ActionGlobalCollectionAdd(org.drools.workbench.models.datamodel.rule.ActionGlobalCollectionAdd) Test(org.junit.Test)

Example 18 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testMethodCallWithTwoParametersIntegerAndString.

@Test
public void testMethodCallWithTwoParametersIntegerAndString() throws Exception {
    String drl = "package org.mortgages;\n" + "rule \"test\"\n" + " dialect \"mvel\"\n" + " when\n" + "  $t : TestClass()\n" + " then\n" + "  $t.testFunction( 123, \"hello\" );\n" + "end";
    final HashMap<String, List<MethodInfo>> map = new HashMap<>();
    final ArrayList<MethodInfo> methodInfos = new ArrayList<>();
    final ArrayList<String> params = new ArrayList<>();
    params.add("Integer");
    params.add("String");
    methodInfos.add(new MethodInfo("testFunction", params, "java.lang.Void", null, "TestClass"));
    map.put("TestClass", methodInfos);
    when(dmo.getModuleMethodInformation()).thenReturn(map);
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals(1, m.lhs.length);
    assertEquals(1, m.rhs.length);
    ActionCallMethod actionCallMethod = (ActionCallMethod) m.rhs[0];
    assertEquals("testFunction", actionCallMethod.getMethodName());
    assertEquals("$t", actionCallMethod.getVariable());
    assertEquals(2, actionCallMethod.getFieldValues().length);
    assertEquals("testFunction", actionCallMethod.getFieldValue(0).getField());
    assertEquals("123", actionCallMethod.getFieldValue(0).getValue());
    assertEquals(FieldNatureType.TYPE_LITERAL, actionCallMethod.getFieldValue(0).getNature());
    assertEquals("Integer", actionCallMethod.getFieldValue(0).getType());
    assertEquals("testFunction", actionCallMethod.getFieldValue(1).getField());
    assertEquals("hello", actionCallMethod.getFieldValue(1).getValue());
    assertEquals(FieldNatureType.TYPE_LITERAL, actionCallMethod.getFieldValue(1).getNature());
    assertEquals("String", actionCallMethod.getFieldValue(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 19 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testFullyQualifiedClassNameEnumeration.

@Test
public void testFullyQualifiedClassNameEnumeration() throws Exception {
    // https://bugzilla.redhat.com/show_bug.cgi?id=1047879
    String drl = "import org.drools.workbench.models.commons.backend.rule.TestEnum;\n" + "import org.drools.workbench.models.commons.backend.rule.OuterClassWithEnums;\n" + "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "OuterClassWithEnums( outerField == TestEnum.VALUE1 )\n" + "then\n" + "end";
    addModelField("org.drools.workbench.models.commons.backend.rule.OuterClassWithEnums", "outerField", TestEnum.class.getSimpleName(), DataType.TYPE_COMPARABLE);
    addJavaEnumDefinition("org.drools.workbench.models.commons.backend.rule.OuterClassWithEnums", "outerField", new String[] { "TestEnum.VALUE1=TestEnum.VALUE1", "TestEnum.VALUE2=TestEnum.VALUE2" });
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    FactPattern pattern = (FactPattern) m.lhs[0];
    assertEquals(1, pattern.getNumberOfConstraints());
    assertTrue(pattern.getConstraint(0) instanceof SingleFieldConstraint);
    SingleFieldConstraint constraint = (SingleFieldConstraint) pattern.getConstraint(0);
    assertEquals("OuterClassWithEnums", constraint.getFactType());
    assertEquals("outerField", constraint.getFieldName());
    assertEquals(DataType.TYPE_COMPARABLE, constraint.getFieldType());
    assertEquals("==", constraint.getOperator());
    assertEquals("TestEnum.VALUE1", constraint.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_ENUM, constraint.getConstraintValueType());
    final String drl2 = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
    assertEqualsIgnoreWhitespace(drl, drl2);
}
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 20 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testReciprocal_SingleFieldConstraintCEPOperator1Parameter.

@Test
public void testReciprocal_SingleFieldConstraintCEPOperator1Parameter() {
    // This is the inverse of "SingleFieldConstraintCEPOperator1Parameter"
    String drl = "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + "$e : Event()\n" + "Event( this after[1d] $e )\n" + "then\n" + "end";
    RuleModel m = new RuleModel();
    m.name = "rule1";
    FactPattern fp1 = new FactPattern();
    fp1.setFactType("Event");
    fp1.setBoundName("$e");
    FactPattern fp2 = new FactPattern();
    fp2.setFactType("Event");
    SingleFieldConstraint sfp = new SingleFieldConstraint();
    sfp.setFactType("Event");
    sfp.setFieldName("this");
    sfp.setOperator("after");
    sfp.setValue("$e");
    sfp.setConstraintValueType(BaseSingleFieldConstraint.TYPE_VARIABLE);
    sfp.getParameters().put("0", "1d");
    sfp.getParameters().put("org.drools.workbench.models.commons.backend.rule.visibleParameterSet", "1");
    sfp.getParameters().put("org.drools.workbench.models.commons.backend.rule.operatorParameterGenerator", "org.drools.workbench.models.commons.backend.rule.CEPOperatorParameterDRLBuilder");
    fp2.addConstraint(sfp);
    m.addLhsItem(fp1);
    m.addLhsItem(fp2);
    String actualDrl = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
    assertEqualsIgnoreWhitespace(drl, actualDrl);
}
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)

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