use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method compositeFieldConstraintWithOnePredicateAndOneLiteral.
@Test
public void compositeFieldConstraintWithOnePredicateAndOneLiteral() {
String drl = "rule \"rule1\"\n" + "when\n" + "Person( eval( age > 18 ) && 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("Person", fp_cfp_sfp2.getFactType());
assertEquals("age", fp_cfp_sfp2.getFieldName());
assertEquals("<", fp_cfp_sfp2.getOperator());
assertEquals("45", fp_cfp_sfp2.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, fp_cfp_sfp2.getConstraintValueType());
}
use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testFromBoundVariable.
@Test
public void testFromBoundVariable() {
String drl = "import java.lang.Number;\n" + "import org.drools.workbench.models.commons.backend.rule.Counter;\n" + "rule \"rule1\"\n" + "when\n" + "cc : Counter()\n" + "Number() from cc.number\n" + "then\n" + "end";
addModelField("org.drools.workbench.models.commons.backend.rule.Counter", "number", "java.lang.Number", DataType.TYPE_NUMERIC);
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals("rule1", m.name);
assertEquals(2, m.lhs.length);
IPattern p0 = m.lhs[0];
assertTrue(p0 instanceof FactPattern);
FactPattern fp0 = (FactPattern) p0;
assertEquals("Counter", fp0.getFactType());
assertEquals("cc", fp0.getBoundName());
assertEquals(0, fp0.getNumberOfConstraints());
IPattern p1 = m.lhs[1];
assertTrue(p1 instanceof FromCompositeFactPattern);
FromCompositeFactPattern fcfp1 = (FromCompositeFactPattern) p1;
FactPattern fp1 = fcfp1.getFactPattern();
ExpressionFormLine efl1 = fcfp1.getExpression();
assertNotNull(fp1);
assertNotNull(efl1);
assertEquals("Number", fp1.getFactType());
assertEquals(0, fp1.getNumberOfConstraints());
assertEquals(2, efl1.getParts().size());
assertTrue(efl1.getParts().get(0) instanceof ExpressionVariable);
assertTrue(efl1.getParts().get(1) instanceof ExpressionField);
ExpressionVariable eflv1 = (ExpressionVariable) efl1.getParts().get(0);
assertEquals("cc", eflv1.getName());
assertEquals("Counter", eflv1.getClassType());
assertEquals(DataType.TYPE_NUMERIC, eflv1.getGenericType());
ExpressionField eflf1 = (ExpressionField) efl1.getParts().get(1);
assertEquals("number", eflf1.getName());
assertEquals("java.lang.Number", eflf1.getClassType());
assertEquals(DataType.TYPE_NUMERIC, eflf1.getGenericType());
}
use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testBigDecimal2.
@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1189930
void testBigDecimal2() 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));
}
use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testSingleFieldConstraintWithBinding.
@Test
public void testSingleFieldConstraintWithBinding() {
String drl = "rule \"rule1\"\n" + "when\n" + "Applicant( $a : 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());
assertEquals("$a", sfp.getFieldBinding());
}
use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testSingleFieldConstraintConnectives2.
@Test
public // https://issues.jboss.org/browse/GUVNOR-2141
void testSingleFieldConstraintConnectives2() {
String drl = "package org.test;\n" + "rule \"rule1\"\n" + "dialect \"mvel\"\n" + " when\n" + " Applicant( age == 55 || == 75 )\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("55", sfp.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp.getConstraintValueType());
assertEquals(1, sfp.getConnectives().length);
ConnectiveConstraint cc = sfp.getConnectives()[0];
assertEquals("Applicant", cc.getFactType());
assertEquals("age", cc.getFieldName());
assertEquals("|| ==", cc.getOperator());
assertEquals("75", cc.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cc.getConstraintValueType());
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
Aggregations