use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testRHSUpdateFactWithFormulaSamePackage.
@Test
public void testRHSUpdateFactWithFormulaSamePackage() {
// https://bugzilla.redhat.com/show_bug.cgi?id=1079253
String drl = "package org.test;\n" + "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "$sc : ShoppingCart( )\n" + "then\n" + "$sc.setCartItemPromoSavings( ($sc.cartItemPromoSavings == 0.0) ? 0.0 : $sc.cartItemPromoSavings * -1 );\n" + "update( $sc );\n" + "end\n";
addModelField("org.test.ShoppingCart", "cartItemPromoSavings", "java.lang.Double", DataType.TYPE_NUMERIC_DOUBLE);
when(dmo.getPackageName()).thenReturn("org.test");
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("ShoppingCart", fp.getFactType());
assertEquals("$sc", fp.getBoundName());
assertEquals(0, fp.getNumberOfConstraints());
assertEquals(1, m.rhs.length);
IAction a = m.rhs[0];
assertTrue(a instanceof ActionUpdateField);
ActionUpdateField ap = (ActionUpdateField) a;
assertEquals("$sc", ap.getVariable());
assertEquals(1, ap.getFieldValues().length);
ActionFieldValue afv = ap.getFieldValues()[0];
assertEquals("cartItemPromoSavings", afv.getField());
assertEquals("($sc.cartItemPromoSavings == 0.0) ? 0.0 : $sc.cartItemPromoSavings * -1", afv.getValue());
assertEquals(FieldNatureType.TYPE_FORMULA, afv.getNature());
}
use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testRHSUpdateFactWithFieldAsLiteralSamePackage.
@Test
public void testRHSUpdateFactWithFieldAsLiteralSamePackage() {
String drl = "package org.test\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);
when(dmo.getPackageName()).thenReturn("org.test");
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 testRHSUpdateFactWithFieldAsVariable.
@Test
public void testRHSUpdateFactWithFieldAsVariable() {
// https://bugzilla.redhat.com/show_bug.cgi?id=1077212
String drl = "package org.mortgages\n" + "import org.test.Person\n" + "rule \"variable\"\n" + "dialect \"mvel\"\n" + "when\n" + "$p : Person( $f : field1 == 44 )\n" + "then\n" + "$p.setField1( $f );\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("$p", fp.getBoundName());
assertEquals(1, fp.getConstraintList().getConstraints().length);
assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraint);
SingleFieldConstraint sfp = (SingleFieldConstraint) fp.getConstraint(0);
assertEquals("Person", sfp.getFactType());
assertEquals("field1", sfp.getFieldName());
assertEquals("==", sfp.getOperator());
assertEquals("44", sfp.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp.getConstraintValueType());
assertEquals("$f", sfp.getFieldBinding());
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_VARIABLE, afv.getNature());
assertEquals("=$f", 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 testSingleFieldConstraintIsNotNullOperator.
@Test
public void testSingleFieldConstraintIsNotNullOperator() {
String drl = "rule \"rule1\"\n" + "when\n" + "Applicant( age != null )\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("!= null", sfp.getOperator());
assertNull(sfp.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_UNDEFINED, sfp.getConstraintValueType());
}
use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testFactPattern.
@Test
public void testFactPattern() {
String drl = "rule \"rule1\"\n" + "when\n" + "Applicant()\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(0, fp.getNumberOfConstraints());
}
Aggregations