use of org.drools.workbench.models.datamodel.rule.IPattern 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.IPattern 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.IPattern 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());
}
use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method compositeFieldConstraintWithTwoPredicates.
@Test
public void compositeFieldConstraintWithTwoPredicates() {
String drl = "rule \"rule1\"\n" + "when\n" + "Person( eval( age > 18 ) && eval(age < 45) )\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 FactPattern);
FactPattern fp = (FactPattern) p;
// LHS sub-patterns
assertEquals(1, fp.getNumberOfConstraints());
assertTrue(fp.getConstraint(0) instanceof CompositeFieldConstraint);
CompositeFieldConstraint fp_cfp = (CompositeFieldConstraint) fp.getConstraint(0);
assertEquals("&&", fp_cfp.getCompositeJunctionType());
assertEquals(2, fp_cfp.getNumberOfConstraints());
assertTrue(fp_cfp.getConstraint(0) instanceof SingleFieldConstraint);
assertTrue(fp_cfp.getConstraint(1) instanceof SingleFieldConstraint);
SingleFieldConstraint fp_cfp_sfp1 = (SingleFieldConstraint) fp_cfp.getConstraint(0);
assertEquals("age > 18", fp_cfp_sfp1.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_PREDICATE, fp_cfp_sfp1.getConstraintValueType());
SingleFieldConstraint fp_cfp_sfp2 = (SingleFieldConstraint) fp_cfp.getConstraint(1);
assertEquals("age < 45", fp_cfp_sfp2.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_PREDICATE, fp_cfp_sfp2.getConstraintValueType());
}
use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testBigDecimal1.
@Test
public // Formatted as generated from a Guided Rule in the Workbench
void testBigDecimal1() 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" + " 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));
}
Aggregations