use of org.drools.workbench.models.datamodel.rule.ActionUpdateField in project drools by kiegroup.
the class RuleTemplateModelDRLPersistenceTest method testActionModifyTwoFieldsFirstTemplateSecondTemplate2.
@Test
public void testActionModifyTwoFieldsFirstTemplateSecondTemplate2() {
TemplateModel m = new TemplateModel();
m.name = "r1";
FactPattern fp = new FactPattern("Person");
fp.setBoundName("$p");
m.addLhsItem(fp);
ActionUpdateField auf1 = new ActionUpdateField("$p");
ActionFieldValue afv0 = new ActionFieldValue();
afv0.setNature(FieldNatureType.TYPE_TEMPLATE);
afv0.setField("field1");
afv0.setValue("$f1");
auf1.addFieldValue(afv0);
ActionFieldValue afv1 = new ActionFieldValue();
afv1.setNature(FieldNatureType.TYPE_TEMPLATE);
afv1.setField("field2");
afv1.setValue("$f2");
auf1.addFieldValue(afv1);
m.addRhsItem(auf1);
String expected = "rule \"r1_0\"\n" + "dialect \"mvel\"\n" + "when\n" + " $p : Person()\n" + "then\n" + " modify( $p ) {\n" + " setField1(\"foo\")\n" + " }\n" + "end";
m.addRow(new String[] { "foo", null });
checkMarshall(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.ActionUpdateField in project drools by kiegroup.
the class RuleTemplateModelDRLPersistenceTest method testActionModifyTwoFieldsFirstLiteralSecondTemplate2.
@Test
public void testActionModifyTwoFieldsFirstLiteralSecondTemplate2() {
TemplateModel m = new TemplateModel();
m.name = "r1";
FactPattern fp = new FactPattern("Person");
fp.setBoundName("$p");
m.addLhsItem(fp);
ActionUpdateField auf1 = new ActionUpdateField("$p");
ActionFieldValue afv0 = new ActionFieldValue();
afv0.setNature(FieldNatureType.TYPE_LITERAL);
afv0.setField("field1");
afv0.setValue("foo");
auf1.addFieldValue(afv0);
ActionFieldValue afv1 = new ActionFieldValue();
afv1.setNature(FieldNatureType.TYPE_TEMPLATE);
afv1.setField("field2");
afv1.setValue("$f2");
auf1.addFieldValue(afv1);
m.addRhsItem(auf1);
String expected = "rule \"r1_0\"\n" + "dialect \"mvel\"\n" + "when\n" + " $p : Person()\n" + "then\n" + " modify( $p ) {\n" + " setField1(\"foo\")\n" + " }\n" + "end";
m.addRow(new String[] { null });
checkMarshall(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.ActionUpdateField in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method newRHSFactsCanBeUsedInRHSBinding.
@Test
public void newRHSFactsCanBeUsedInRHSBinding() throws Exception {
String drl = "package org.mortgages;\n" + "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "$l : LoanApplication()\n" + "then\n" + "Applicant $a = new Applicant();\n" + "insert( $a );\n" + "modify( $l ) {\n" + " setApplicant( $a )" + "}\n" + "end";
addModelField("org.mortgages.Applicant", "this", "org.mortgages.Applicant", DataType.TYPE_THIS);
addModelField("org.mortgages.LoanApplication", "this", "org.mortgages.LoanApplication", DataType.TYPE_THIS);
addModelField("org.mortgages.LoanApplication", "applicant", "org.mortgages.Applicant", "org.mortgages.Applicant");
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("LoanApplication", fp.getFactType());
assertEquals(0, fp.getNumberOfConstraints());
assertEquals(2, m.rhs.length);
assertTrue(m.rhs[0] instanceof ActionInsertFact);
ActionInsertFact aif = (ActionInsertFact) m.rhs[0];
assertEquals("$a", aif.getBoundName());
assertEquals("Applicant", aif.getFactType());
assertEquals(0, aif.getFieldValues().length);
assertTrue(m.rhs[1] instanceof ActionUpdateField);
ActionUpdateField auf = (ActionUpdateField) m.rhs[1];
assertEquals("$l", auf.getVariable());
assertEquals(1, auf.getFieldValues().length);
ActionFieldValue afv0 = auf.getFieldValues()[0];
assertEquals("applicant", afv0.getField());
assertEquals("=$a", afv0.getValue());
assertEquals(FieldNatureType.TYPE_VARIABLE, afv0.getNature());
assertEquals("Applicant", afv0.getType());
}
use of org.drools.workbench.models.datamodel.rule.ActionUpdateField in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testRHSUpdateFactWithFieldAsVariableSamePackage.
@Test
public void testRHSUpdateFactWithFieldAsVariableSamePackage() {
// https://bugzilla.redhat.com/show_bug.cgi?id=1077212
String drl = "package org.test\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);
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("$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.ActionUpdateField in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testRHSModifyBlockMultipleFieldsSingleLine.
@Test
public void testRHSModifyBlockMultipleFieldsSingleLine() throws Exception {
// The value used in the "set" is intentionally yucky to catch extraction of the field's value errors!
String drl = "rule \"modify1\"\n" + " dialect \"mvel\"\n" + " when\n" + " $p : Person( )\n" + " then\n" + " modify( $p ) { setFirstName( \",)\" ), setLastName( \",)\" ) }\n" + "end";
addModelField("Person", "firstName", "java.lang.String", DataType.TYPE_STRING);
addModelField("Person", "lastName", "java.lang.String", DataType.TYPE_STRING);
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertEquals(1, m.rhs.length);
assertTrue(m.rhs[0] instanceof ActionUpdateField);
ActionUpdateField field = (ActionUpdateField) m.rhs[0];
assertEquals("$p", field.getVariable());
assertNotNull(field.getFieldValues()[0]);
assertEquals(2, field.getFieldValues().length);
ActionFieldValue value1 = field.getFieldValues()[0];
assertEquals("firstName", value1.getField());
assertEquals(",)", value1.getValue());
assertEquals(FieldNatureType.TYPE_LITERAL, value1.getNature());
assertEquals(DataType.TYPE_STRING, value1.getType());
ActionFieldValue value2 = field.getFieldValues()[1];
assertEquals("lastName", value2.getField());
assertEquals(",)", value2.getValue());
assertEquals(FieldNatureType.TYPE_LITERAL, value2.getNature());
assertEquals(DataType.TYPE_STRING, value2.getType());
}
Aggregations