use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testRHSFreeFormDRL.
@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1175333
void testRHSFreeFormDRL() throws Exception {
String drl = "package org.test;\n" + "rule \"Validate Down Payment\"\n" + "dialect \"mvel\"\n" + "ruleflow-group \"validation\"\n" + "when\n" + " property : Property( )\n" + " Application( downPayment < 0 || downPayment > property.price )\n" + "then\n" + " ValidationError fact0 = new ValidationError();\n" + " fact0.setCause( \"Down payment can't be negative or larger than property value\" );\n" + " insert( fact0 );\n" + " System.out.println(\"Executed Rule: \" + drools.getRule().getName() );\n" + "end\n";
addModelField("org.test.Property", "this", "org.test.Property", DataType.TYPE_THIS);
addModelField("org.test.Property", "price", Integer.class.getName(), DataType.TYPE_NUMERIC_INTEGER);
addModelField("org.test.Application", "this", "org.test.Application", DataType.TYPE_THIS);
addModelField("org.test.Application", "downPayment", Integer.class.getName(), DataType.TYPE_NUMERIC_INTEGER);
addModelField("org.test.ValidationError", "this", "org.test.ValidationError", DataType.TYPE_THIS);
when(dmo.getPackageName()).thenReturn("org.test");
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
// 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 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.RuleModel 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.RuleModel 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.RuleModel 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