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());
}
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());
}
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));
}
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);
}
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);
}
Aggregations