use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testSingleFieldConstraintConnectives3.
@Test
public // https://issues.jboss.org/browse/RHBRMS-2854
void testSingleFieldConstraintConnectives3() {
String drl = "package org.test;\n" + "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + " Applicant(age != 1 && < 10 && > 5)\n" + "then\n" + "end";
addModelField("org.test.Applicant", "this", "org.test.Applicant", DataType.TYPE_THIS);
addModelField("org.test.Applicant", "age", Integer.class.getName(), DataType.TYPE_NUMERIC_INTEGER);
when(dmo.getPackageName()).thenReturn("org.test");
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("Applicant", fp.getFactType());
assertEquals(1, fp.getConstraintList().getConstraints().length);
assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraint);
SingleFieldConstraint sfp = (SingleFieldConstraint) fp.getConstraint(0);
assertEquals("Applicant", sfp.getFactType());
assertEquals("age", sfp.getFieldName());
assertEquals("!=", sfp.getOperator());
assertEquals("1", sfp.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp.getConstraintValueType());
assertEquals(2, sfp.getConnectives().length);
ConnectiveConstraint cc1 = sfp.getConnectives()[0];
assertEquals("Applicant", cc1.getFactType());
assertEquals("age", cc1.getFieldName());
assertEquals("&& <", cc1.getOperator());
assertEquals("10", cc1.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cc1.getConstraintValueType());
ConnectiveConstraint cc2 = sfp.getConnectives()[1];
assertEquals("Applicant", cc2.getFactType());
assertEquals("age", cc2.getFieldName());
assertEquals("&& >", cc2.getOperator());
assertEquals("5", cc2.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cc2.getConstraintValueType());
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method actionInsertFactWithFormula.
@Test
public void actionInsertFactWithFormula() throws Exception {
String drl = "package org.mortgages;\n" + "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "$a : Application()\n" + "then\n" + "Applicant $a = new Applicant();\n" + "$a.setName( \"Pupa\" + 20 + \"Smurf\" );\n" + "insert( $a );\n" + "end";
addModelField("org.mortgages.Applicant", "this", "org.mortgages.Applicant", DataType.TYPE_THIS);
addModelField("org.mortgages.Applicant", "name", String.class.getName(), DataType.TYPE_STRING);
when(dmo.getPackageName()).thenReturn("org.mortgages");
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals("r1", m.name);
// LHS Pattern
assertEquals(1, m.lhs.length);
IPattern p = m.lhs[0];
assertTrue(p instanceof FactPattern);
FactPattern fp = (FactPattern) p;
assertEquals("Application", fp.getFactType());
assertEquals(0, fp.getNumberOfConstraints());
assertEquals(1, m.rhs.length);
assertTrue(m.rhs[0] instanceof ActionInsertFact);
ActionInsertFact aif = (ActionInsertFact) m.rhs[0];
assertEquals("$a", aif.getBoundName());
assertEquals(1, aif.getFieldValues().length);
ActionFieldValue afv0 = aif.getFieldValues()[0];
assertEquals("name", afv0.getField());
assertEquals("\"Pupa\" + 20 + \"Smurf\"", afv0.getValue());
assertEquals(FieldNatureType.TYPE_FORMULA, afv0.getNature());
}
use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testCompositeFieldConstraint.
@Test
public void testCompositeFieldConstraint() {
String drl = "rule \"rule1\"\n" + "when\n" + "Applicant( age < 55 || age > 70 )\n" + "then\n" + "end";
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("Applicant", fp.getFactType());
assertEquals(1, fp.getConstraintList().getConstraints().length);
assertTrue(fp.getConstraint(0) instanceof CompositeFieldConstraint);
CompositeFieldConstraint cfp = (CompositeFieldConstraint) fp.getConstraint(0);
assertEquals("||", cfp.getCompositeJunctionType());
assertEquals(2, cfp.getNumberOfConstraints());
assertTrue(cfp.getConstraint(0) instanceof SingleFieldConstraint);
assertTrue(cfp.getConstraint(1) instanceof SingleFieldConstraint);
SingleFieldConstraint sfp1 = (SingleFieldConstraint) cfp.getConstraint(0);
assertEquals("Applicant", sfp1.getFactType());
assertEquals("age", sfp1.getFieldName());
assertEquals("<", sfp1.getOperator());
assertEquals("55", sfp1.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp1.getConstraintValueType());
SingleFieldConstraint sfp2 = (SingleFieldConstraint) cfp.getConstraint(1);
assertEquals("Applicant", sfp2.getFactType());
assertEquals("age", sfp2.getFieldName());
assertEquals(">", sfp2.getOperator());
assertEquals("70", sfp2.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp2.getConstraintValueType());
}
use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testRHSUpdateFactWithFieldAsLiteral.
@Test
public void testRHSUpdateFactWithFieldAsLiteral() {
String drl = "package org.mortgages\n" + "import org.test.Person\n" + "rule \"variable\"\n" + "dialect \"mvel\"\n" + "when\n" + "$p : Person()\n" + "then\n" + "$p.setField1( 55 );\n" + "update( $p );\n" + "end";
addModelField("org.test.Person", "field1", "java.lang.Integer", DataType.TYPE_NUMERIC_INTEGER);
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(1, m.lhs.length);
IPattern p = m.lhs[0];
assertTrue(p instanceof FactPattern);
FactPattern fp = (FactPattern) p;
assertEquals("Person", fp.getFactType());
assertEquals(0, fp.getNumberOfConstraints());
assertEquals(1, m.rhs.length);
IAction a = m.rhs[0];
assertTrue(a instanceof ActionUpdateField);
ActionUpdateField ap = (ActionUpdateField) a;
assertEquals("$p", ap.getVariable());
assertEquals(1, ap.getFieldValues().length);
ActionFieldValue afv = ap.getFieldValues()[0];
assertEquals("field1", afv.getField());
assertEquals(FieldNatureType.TYPE_LITERAL, afv.getNature());
assertEquals("55", afv.getValue());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, afv.getType());
}
use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testSingleFieldConstraint.
@Test
public void testSingleFieldConstraint() {
String drl = "rule \"rule1\"\n" + "when\n" + "Applicant( age < 55 )\n" + "then\n" + "end";
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("Applicant", fp.getFactType());
assertEquals(1, fp.getConstraintList().getConstraints().length);
assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraint);
SingleFieldConstraint sfp = (SingleFieldConstraint) fp.getConstraint(0);
assertEquals("Applicant", sfp.getFactType());
assertEquals("age", sfp.getFieldName());
assertEquals("<", sfp.getOperator());
assertEquals("55", sfp.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp.getConstraintValueType());
}
Aggregations