use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testFunctionCalls.
@Test
public void testFunctionCalls() {
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( s );\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(2, m.rhs.length);
assertTrue(m.rhs[0] instanceof ActionCallMethod);
assertTrue(m.rhs[1] 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("s", actionCallMethod1.getFieldValues()[0].getValue());
assertEquals(FieldNatureType.TYPE_VARIABLE, actionCallMethod1.getFieldValues()[0].getNature());
assertEquals("String", actionCallMethod1.getFieldValues()[0].getType());
ActionCallMethod actionCallMethod2 = (ActionCallMethod) m.rhs[1];
assertEquals(1, actionCallMethod2.getState());
assertEquals("indexOf", actionCallMethod2.getMethodName());
assertEquals("s", actionCallMethod2.getVariable());
assertEquals(1, actionCallMethod2.getFieldValues().length);
assertEquals("indexOf", actionCallMethod2.getFieldValues()[0].getField());
assertEquals("0", actionCallMethod2.getFieldValues()[0].getValue());
assertEquals(FieldNatureType.TYPE_LITERAL, actionCallMethod2.getFieldValues()[0].getNature());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, actionCallMethod2.getFieldValues()[0].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 testBigDecimal3.
@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1189930
void testBigDecimal3() throws Exception {
String drl = "package org.test;\n" + "import java.lang.Number;\n" + "import java.math.BigDecimal;\n" + "import java.util.Calendar;\n" + "rule \"BigDecimalRule\"\n" + " dialect \"java\"\n" + "when\n" + " $bd : BigDecimal( )\n" + "then\n" + " LastRunInformation lastRun = new LastRunInformation();\n" + " lastRun.setLastNumber($bd);\n" + "\n" + " insert(lastRun);\n" + "end";
addModelField("org.test.LastRunInformation", "this", "org.test.LastRunInformation", DataType.TYPE_THIS);
addModelField("org.test.LastRunInformation", "lastNumber", BigDecimal.class.getName(), DataType.TYPE_NUMERIC_BIGDECIMAL);
when(dmo.getPackageName()).thenReturn("org.test");
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(1, m.lhs.length);
final IPattern p = m.lhs[0];
assertTrue(p instanceof FactPattern);
final FactPattern fp = (FactPattern) p;
assertEquals("$bd", fp.getBoundName());
assertEquals("BigDecimal", fp.getFactType());
assertEquals(1, m.rhs.length);
final IAction a = m.rhs[0];
assertTrue(a instanceof ActionInsertFact);
final ActionInsertFact aif = (ActionInsertFact) a;
assertEquals("lastRun", aif.getBoundName());
assertEquals("LastRunInformation", aif.getFactType());
assertEquals(1, aif.getFieldValues().length);
final ActionFieldValue afv0 = aif.getFieldValues()[0];
assertEquals("lastNumber", afv0.getField());
assertEquals(FieldNatureType.TYPE_VARIABLE, afv0.getNature());
assertEquals("=$bd", afv0.getValue());
// Check round-trip
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testLiteralStrFieldNames.
@Test
public void testLiteralStrFieldNames() throws Exception {
// The issue is fields that contain the "str" operator literal value
String drl = "rule \"rule1\"\n" + " dialect \"mvel\"\n" + " when\n" + " Room( decoration == \"tapestry\" , strangeField == 11 )\n" + " then\n" + "end";
addModelField("Room", "decoration", "java.lang.String", DataType.TYPE_STRING);
addModelField("Room", "strangeField", "java.lang.Integer", DataType.TYPE_NUMERIC_INTEGER);
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("Room", fp.getFactType());
assertEquals(2, fp.getNumberOfConstraints());
assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraint);
SingleFieldConstraint sfp0 = (SingleFieldConstraint) fp.getConstraint(0);
assertEquals("Room", sfp0.getFactType());
assertEquals("decoration", sfp0.getFieldName());
assertEquals("==", sfp0.getOperator());
assertEquals("tapestry", sfp0.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp0.getConstraintValueType());
assertEquals(DataType.TYPE_STRING, sfp0.getFieldType());
SingleFieldConstraint sfp1 = (SingleFieldConstraint) fp.getConstraint(1);
assertEquals("Room", sfp1.getFactType());
assertEquals("strangeField", sfp1.getFieldName());
assertEquals("==", sfp1.getOperator());
assertEquals("11", sfp1.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp1.getConstraintValueType());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, sfp1.getFieldType());
}
use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testInvalidSyntax1.
@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1218308
void testInvalidSyntax1() throws Exception {
String drl = "rule \"test\"\n" + "dialect \"mvel\"\n" + "when\n" + " Smurf( flange \n" + "then\n" + "end";
when(dmo.getPackageName()).thenReturn("org.test");
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(1, m.lhs.length);
final IPattern p0 = m.lhs[0];
assertTrue(p0 instanceof FreeFormLine);
final FreeFormLine ffl = (FreeFormLine) p0;
assertEquals("Smurf( flange", ffl.getText());
assertEquals(0, m.rhs.length);
// Check round-trip
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testCompositeFactPatternWithOr.
@Test
public void testCompositeFactPatternWithOr() {
String drl = "rule \"rule1\"\n" + "when\n" + "( Person( age == 42 ) or Person( age == 43 ) )\n" + "then\n" + "end";
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals("rule1", m.name);
// LHS Pattern
assertEquals(1, m.lhs.length);
IPattern p = m.lhs[0];
assertTrue(p instanceof CompositeFactPattern);
CompositeFactPattern cfp = (CompositeFactPattern) p;
assertEquals(CompositeFactPattern.COMPOSITE_TYPE_OR, cfp.getType());
// LHS sub-patterns
assertEquals(2, cfp.getPatterns().length);
IPattern cfp_p1 = cfp.getPatterns()[0];
assertTrue(cfp_p1 instanceof FactPattern);
FactPattern fp1 = (FactPattern) cfp_p1;
assertEquals("Person", fp1.getFactType());
assertEquals(1, fp1.getConstraintList().getConstraints().length);
assertTrue(fp1.getConstraint(0) instanceof SingleFieldConstraint);
SingleFieldConstraint cfp_sfp1 = (SingleFieldConstraint) fp1.getConstraint(0);
assertEquals("Person", cfp_sfp1.getFactType());
assertEquals("age", cfp_sfp1.getFieldName());
assertEquals("==", cfp_sfp1.getOperator());
assertEquals("42", cfp_sfp1.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cfp_sfp1.getConstraintValueType());
IPattern cfp_p2 = cfp.getPatterns()[1];
assertTrue(cfp_p2 instanceof FactPattern);
FactPattern fp2 = (FactPattern) cfp_p2;
assertEquals("Person", fp2.getFactType());
assertTrue(fp2.getConstraint(0) instanceof SingleFieldConstraint);
SingleFieldConstraint cfp_sfp2 = (SingleFieldConstraint) fp2.getConstraint(0);
assertEquals("Person", cfp_sfp2.getFactType());
assertEquals("age", cfp_sfp2.getFieldName());
assertEquals("==", cfp_sfp2.getOperator());
assertEquals("43", cfp_sfp2.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cfp_sfp2.getConstraintValueType());
}
Aggregations