use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testMethodCallCheckParameterDataTypes3.
@Test
public void testMethodCallCheckParameterDataTypes3() {
// BZ-1045423
String drl = "" + "package org.mortgages;\n" + "import org.mortgages.MyType;\n" + "rule \"my rule\"\n" + " dialect \"mvel\"\n" + " when\n" + " i : Integer( )\n" + " t : MyType( )\n" + " then\n" + " t.doSomething( i );\n" + "end\n";
Map<String, List<MethodInfo>> methodInformation = new HashMap<>();
List<MethodInfo> mapMethodInformation = new ArrayList<>();
mapMethodInformation.add(new MethodInfo("doSomething", Collections.singletonList(DataType.TYPE_NUMERIC_INTEGER), "void", "void", "org.mortgages.MyType"));
methodInformation.put("org.mortgages.MyType", mapMethodInformation);
when(dmo.getModuleMethodInformation()).thenReturn(methodInformation);
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertTrue(m.rhs[0] instanceof ActionCallMethod);
ActionCallMethod mc = (ActionCallMethod) m.rhs[0];
assertEquals("doSomething", mc.getMethodName());
assertEquals("t", mc.getVariable());
assertEquals(1, mc.getState());
assertEquals(1, mc.getFieldValues().length);
ActionFieldValue f1 = mc.getFieldValue(0);
assertEquals("i", f1.getValue());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, f1.getType());
assertEquals(FieldNatureType.TYPE_VARIABLE, f1.getNature());
String marshalled = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
logger.debug(marshalled);
assertEqualsIgnoreWhitespace(drl, marshalled);
}
use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testRHSModifyBlockSingleFieldSingleLine.
@Test
public void testRHSModifyBlockSingleFieldSingleLine() throws Exception {
// The value used in the "set" is intentionally yucky to catch extraction of the field's value errors!
String drl = "rule \"modify1\"\n" + " dialect \"mvel\"\n" + " when\n" + " $p : Person( )\n" + " then\n" + " modify( $p ) { setFirstName( \",)\" ) }\n" + "end";
addModelField("Person", "firstName", "java.lang.String", DataType.TYPE_STRING);
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertEquals(1, m.rhs.length);
assertTrue(m.rhs[0] instanceof ActionUpdateField);
ActionUpdateField field = (ActionUpdateField) m.rhs[0];
assertEquals("$p", field.getVariable());
assertNotNull(field.getFieldValues()[0]);
assertEquals(1, field.getFieldValues().length);
ActionFieldValue value = field.getFieldValues()[0];
assertEquals("firstName", value.getField());
assertEquals(",)", value.getValue());
assertEquals(FieldNatureType.TYPE_LITERAL, value.getNature());
assertEquals(DataType.TYPE_STRING, value.getType());
}
use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testDSLExpansionRHS.
@Test
public void testDSLExpansionRHS() {
String drl = "rule \"rule1\"\n" + "when\n" + "> $a : Applicant()\n" + "then\n" + "Set applicant name to Bob\n" + "end\n";
final String dslDefinition = "Set applicant name to {name:\\w+ \\w+}";
final String dslFile = "[then]" + dslDefinition + "=$a.setName( \"{name}\" )";
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshalUsingDSL(drl, Collections.emptyList(), dmo, dslFile);
assertNotNull(m);
assertTrue(m.lhs[0] instanceof FactPattern);
FactPattern pattern = (FactPattern) m.lhs[0];
assertEquals("Applicant", pattern.getFactType());
assertEquals("$a", pattern.getBoundName());
assertTrue(m.rhs[0] instanceof DSLSentence);
DSLSentence dslSentence = (DSLSentence) m.rhs[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("Bob", dslComplexVariableValue.getValue());
assertEquals("\\w+ \\w+", dslComplexVariableValue.getId());
}
use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testLHSFromCollectWithoutListDeclared.
@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1158813
void testLHSFromCollectWithoutListDeclared() throws Exception {
String drl = "package org.test;\n" + "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + " c : Customer( )\n" + " items : java.util.List( eval( size == c.items.size )) \n" + " from collect ( var : Item( price > 10 )) \n" + "then \n" + "end";
addModelField("org.test.Customer", "this", "org.test.Customer", DataType.TYPE_THIS);
addModelField("org.test.Item", "this", "org.test.Item", DataType.TYPE_THIS);
addModelField("org.test.Item", "price", "java.lang.Double", DataType.TYPE_NUMERIC_DOUBLE);
addMethodInformation("org.test.Customer", "items", new ArrayList<String>() {
{
}
}, DataType.TYPE_NUMERIC_INTEGER, null, DataType.TYPE_NUMERIC_INTEGER);
when(dmo.getPackageName()).thenReturn("org.test");
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(2, m.lhs.length);
assertEquals(0, m.rhs.length);
assertTrue(m.lhs[0] instanceof FactPattern);
final FactPattern fp = (FactPattern) m.lhs[0];
assertEquals("Customer", fp.getFactType());
assertEquals("c", fp.getBoundName());
assertEquals(0, fp.getNumberOfConstraints());
assertTrue(m.lhs[1] instanceof FromCollectCompositeFactPattern);
final FromCollectCompositeFactPattern fcfp = (FromCollectCompositeFactPattern) m.lhs[1];
assertNotNull(fcfp.getFactPattern());
assertEquals("java.util.List", fcfp.getFactPattern().getFactType());
assertEquals("items", fcfp.getFactPattern().getBoundName());
assertEquals(1, fcfp.getFactPattern().getNumberOfConstraints());
assertTrue(fcfp.getFactPattern().getConstraint(0) instanceof SingleFieldConstraint);
final SingleFieldConstraint sfc0 = (SingleFieldConstraint) fcfp.getFactPattern().getConstraint(0);
assertNull(sfc0.getFactType());
assertNull(sfc0.getFieldName());
assertEquals("size == c.items.size", sfc0.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_PREDICATE, sfc0.getConstraintValueType());
assertNotNull(fcfp.getRightPattern());
assertTrue(fcfp.getRightPattern() instanceof FactPattern);
final FactPattern rfp = (FactPattern) fcfp.getRightPattern();
assertEquals("Item", rfp.getFactType());
assertEquals("var", rfp.getBoundName());
assertEquals(1, rfp.getNumberOfConstraints());
assertTrue(rfp.getConstraint(0) instanceof SingleFieldConstraint);
final SingleFieldConstraint sfc1 = (SingleFieldConstraint) rfp.getConstraint(0);
assertEquals("Item", sfc1.getFactType());
assertEquals("price", sfc1.getFieldName());
assertEquals("10", sfc1.getValue());
assertEquals(">", sfc1.getOperator());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfc1.getConstraintValueType());
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testMethodCallCheckParameterDataTypes1.
@Test
public void testMethodCallCheckParameterDataTypes1() {
// BZ-1045423
String drl = "" + "package org.mortgages;\n" + "import org.mortgages.LoanApplication;\n" + "import java.util.Map;\n" + "rule \"my rule\"\n" + " dialect \"mvel\"\n" + " when\n" + " a : LoanApplication( )\n" + " m : Map()\n" + " then\n" + " m.put(\"key\", a );\n" + "end\n";
Map<String, List<MethodInfo>> methodInformation = new HashMap<>();
List<MethodInfo> mapMethodInformation = new ArrayList<>();
mapMethodInformation.add(new MethodInfo("put", Arrays.asList("java.lang.Object", "java.lang.Object"), "void", "void", "java.util.Map"));
methodInformation.put("java.util.Map", mapMethodInformation);
when(dmo.getModuleMethodInformation()).thenReturn(methodInformation);
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertTrue(m.rhs[0] instanceof ActionCallMethod);
ActionCallMethod mc = (ActionCallMethod) m.rhs[0];
assertEquals("put", mc.getMethodName());
assertEquals("m", mc.getVariable());
assertEquals(1, mc.getState());
assertEquals(2, mc.getFieldValues().length);
ActionFieldValue f1 = mc.getFieldValue(0);
assertEquals("\"key\"", f1.getValue());
assertEquals("java.lang.Object", f1.getType());
assertEquals(FieldNatureType.TYPE_LITERAL, f1.getNature());
ActionFieldValue f2 = mc.getFieldValue(1);
assertEquals("a", f2.getValue());
assertEquals("java.lang.Object", f2.getType());
assertEquals(FieldNatureType.TYPE_VARIABLE, f2.getNature());
String marshalled = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
logger.debug(marshalled);
assertEqualsIgnoreWhitespace(drl, marshalled);
}
Aggregations