use of org.drools.workbench.models.datamodel.rule.ActionSetField in project drools by kiegroup.
the class RuleTemplateModelDRLPersistenceTest method testEmptyFreeForm.
@Test
public void testEmptyFreeForm() {
// https://bugzilla.redhat.com/show_bug.cgi?id=1058247
TemplateModel m = new TemplateModel();
m.name = "Empty FreeFormLine";
FreeFormLine fl = new FreeFormLine();
m.addLhsItem(fl);
FactPattern fp = new FactPattern("Person");
fp.setBoundName("$p");
SingleFieldConstraint sfc1 = new SingleFieldConstraint();
sfc1.setFieldName("field1");
sfc1.setFieldType(DataType.TYPE_STRING);
sfc1.setConstraintValueType(SingleFieldConstraint.TYPE_TEMPLATE);
sfc1.setValue("$f1");
sfc1.setOperator("==");
fp.addConstraint(sfc1);
m.addLhsItem(fp);
FreeFormLine fr = new FreeFormLine();
m.addRhsItem(fr);
ActionSetField asf = new ActionSetField("$p");
ActionFieldValue afv0 = new ActionFieldValue();
afv0.setNature(FieldNatureType.TYPE_TEMPLATE);
afv0.setType(DataType.TYPE_STRING);
afv0.setField("field1");
afv0.setValue("$asf1");
asf.addFieldValue(afv0);
m.addRhsItem(asf);
String expected = "rule \"Empty FreeFormLine_0\"\n" + "dialect \"mvel\"\n" + "when\n" + "$p : Person( field1 == \"foo\" )\n" + "then\n" + "$p.setField1(\"bar\");\n" + "end";
m.addRow(new String[] { "foo", "bar" });
checkMarshall(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.ActionSetField in project drools by kiegroup.
the class RuleTemplateModelDRLPersistenceTest method testActionUpdateFactBothValues.
@Test
public void testActionUpdateFactBothValues() {
TemplateModel m = new TemplateModel();
m.name = "r1";
FactPattern fp = new FactPattern("Person");
fp.setBoundName("$p");
m.addLhsItem(fp);
ActionSetField asf = new ActionSetField("$p");
ActionFieldValue afv0 = new ActionFieldValue();
afv0.setNature(FieldNatureType.TYPE_TEMPLATE);
afv0.setField("field1");
afv0.setValue("$f1");
asf.addFieldValue(afv0);
ActionFieldValue afv1 = new ActionFieldValue();
afv1.setNature(FieldNatureType.TYPE_TEMPLATE);
afv1.setField("field2");
afv1.setValue("$f2");
asf.addFieldValue(afv1);
m.addRhsItem(asf);
String expected = "rule \"r1_0\"\n" + "dialect \"mvel\"\n" + "when\n" + "$p : Person()\n" + "then\n" + "$p.setField1(\"foo\");\n" + "$p.setField2(\"bar\");\n" + "end";
m.addRow(new String[] { "foo", "bar" });
checkMarshall(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.ActionSetField in project drools by kiegroup.
the class RuleTemplateModelDRLPersistenceTest method testActionUpdateFactZeroValues.
@Test
public void testActionUpdateFactZeroValues() {
TemplateModel m = new TemplateModel();
m.name = "r1";
FactPattern fp = new FactPattern("Person");
fp.setBoundName("$p");
m.addLhsItem(fp);
ActionSetField asf = new ActionSetField("$p");
ActionFieldValue afv0 = new ActionFieldValue();
afv0.setNature(FieldNatureType.TYPE_TEMPLATE);
afv0.setField("field1");
afv0.setValue("$f1");
asf.addFieldValue(afv0);
ActionFieldValue afv1 = new ActionFieldValue();
afv1.setNature(FieldNatureType.TYPE_TEMPLATE);
afv1.setField("field2");
afv1.setValue("$f2");
asf.addFieldValue(afv1);
m.addRhsItem(asf);
String expected = "rule \"r1_0\"\n" + "dialect \"mvel\"\n" + "when\n" + "$p : Person()\n" + "then\n" + "end";
m.addRow(new String[] { null, null });
checkMarshall(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.ActionSetField in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testActionSetFieldValue.
@Test
public void testActionSetFieldValue() {
final String drl = "rule \"r0\"\n" + "dialect \"mvel\"\n" + "when\n" + "$a : Applicant( )\n" + "then\n" + "$a.setName( \"Michael\" );\n" + "end\n";
PackageDataModelOracle dmo = mock(PackageDataModelOracle.class);
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.EMPTY_LIST, dmo);
assertNotNull(m);
// LHS
assertEquals(1, m.lhs.length);
assertTrue(m.lhs[0] instanceof FactPattern);
final FactPattern p = (FactPattern) m.lhs[0];
assertEquals("$a", p.getBoundName());
assertEquals("Applicant", p.getFactType());
// RHS
assertEquals(1, m.rhs.length);
assertTrue(m.rhs[0] instanceof ActionSetField);
final ActionSetField a = (ActionSetField) m.rhs[0];
assertEquals("$a", a.getVariable());
assertEquals(1, a.getFieldValues().length);
final ActionFieldValue fv = a.getFieldValues()[0];
assertEquals("name", fv.getField());
assertEquals("Michael", fv.getValue());
}
use of org.drools.workbench.models.datamodel.rule.ActionSetField in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testRHSDateUpdateAction.
@Test
public void testRHSDateUpdateAction() {
String oldValue = System.getProperty("drools.dateformat");
try {
System.setProperty("drools.dateformat", "dd-MMM-yyyy");
RuleModel m = new RuleModel();
m.name = "RHS Date";
FactPattern p = new FactPattern("Person");
p.setBoundName("$p");
SingleFieldConstraint con = new SingleFieldConstraint();
con.setFieldType(DataType.TYPE_DATE);
con.setFieldName("dateOfBirth");
con.setOperator("==");
con.setValue("31-Jan-2000");
con.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
p.addConstraint(con);
m.addLhsItem(p);
ActionSetField au = new ActionSetField("$p");
au.addFieldValue(new ActionFieldValue("dob", "31-Jan-2000", DataType.TYPE_DATE));
m.addRhsItem(au);
String result = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
assertTrue(result.indexOf("java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"dd-MMM-yyyy\");") != -1);
assertTrue(result.indexOf("$p.setDob( sdf.parse(\"31-Jan-2000\"") != -1);
assertTrue(result.indexOf("update( $p );") == -1);
checkMarshalling(null, m);
} finally {
if (oldValue == null) {
System.clearProperty("drools.dateformat");
} else {
System.setProperty("drools.dateformat", oldValue);
}
}
}
Aggregations