use of org.drools.workbench.models.datamodel.rule.ActionFieldValue 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.ActionFieldValue in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testRHSFactBindingLastBound.
@Test
public void testRHSFactBindingLastBound() {
RuleModel m = new RuleModel();
m.name = "test";
ActionInsertFact ai0 = new ActionInsertFact("Person");
ai0.addFieldValue(new ActionFieldValue("field1", "55", DataType.TYPE_NUMERIC_LONG));
ActionInsertFact ai1 = new ActionInsertFact("Person");
ai1.setBoundName("fact0");
ai1.addFieldValue(new ActionFieldValue("field1", "55", DataType.TYPE_NUMERIC_LONG));
m.addRhsItem(ai0);
m.addRhsItem(ai1);
String result = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
String expected = "rule \"test\" \n" + "dialect \"mvel\"\n" + "when" + "then \n" + "Person fact1 = new Person(); \n" + "fact1.setField1( 55 ); \n" + "insert( fact1 ); \n" + "Person fact0 = new Person(); \n" + "fact0.setField1( 55 ); \n" + "insert( fact0 ); \n" + "end";
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.ActionFieldValue in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testRHSFactBindingFirstBound.
@Test
public void testRHSFactBindingFirstBound() {
RuleModel m = new RuleModel();
m.name = "test";
ActionInsertFact ai0 = new ActionInsertFact("Person");
ai0.setBoundName("fact0");
ai0.addFieldValue(new ActionFieldValue("field1", "55", DataType.TYPE_NUMERIC_LONG));
ActionInsertFact ai1 = new ActionInsertFact("Person");
ai1.addFieldValue(new ActionFieldValue("field1", "55", DataType.TYPE_NUMERIC_LONG));
m.addRhsItem(ai0);
m.addRhsItem(ai1);
String result = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
String expected = "rule \"test\" \n" + "dialect \"mvel\"\n" + "when" + "then \n" + "Person fact0 = new Person(); \n" + "fact0.setField1( 55 ); \n" + "insert( fact0 ); \n" + "Person fact1 = new Person(); \n" + "fact1.setField1( 55 ); \n" + "insert( fact1 ); \n" + "end";
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.ActionFieldValue in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testRHSFactBindingZeroBound.
@Test
public void testRHSFactBindingZeroBound() {
RuleModel m = new RuleModel();
m.addAttribute(new RuleAttribute("dialect", "mvel"));
m.name = "test";
ActionInsertFact ai0 = new ActionInsertFact("Person");
ai0.addFieldValue(new ActionFieldValue("field1", "55", DataType.TYPE_NUMERIC_LONG));
ActionInsertFact ai1 = new ActionInsertFact("Person");
ai1.addFieldValue(new ActionFieldValue("field1", "55", DataType.TYPE_NUMERIC_LONG));
m.addRhsItem(ai0);
m.addRhsItem(ai1);
String expected = "rule \"test\" \n" + "dialect \"mvel\"\n" + "when" + "then \n" + "Person fact0 = new Person(); \n" + "fact0.setField1( 55 ); \n" + "insert( fact0 ); \n" + "Person fact1 = new Person(); \n" + "fact1.setField1( 55 ); \n" + "insert( fact1 ); \n" + "end";
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.ActionFieldValue in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testRHSDateInsertAction.
@Test
public void testRHSDateInsertAction() {
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");
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);
ActionInsertFact ai = new ActionInsertFact("Birthday");
ai.addFieldValue(new ActionFieldValue("dob", "31-Jan-2000", DataType.TYPE_DATE));
m.addRhsItem(ai);
String result = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
assertTrue(result.indexOf("java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"dd-MMM-yyyy\");") != -1);
assertTrue(result.indexOf("fact0.setDob( sdf.parse(\"31-Jan-2000\"") != -1);
checkMarshalling(null, m);
} finally {
if (oldValue == null) {
System.clearProperty("drools.dateformat");
} else {
System.setProperty("drools.dateformat", oldValue);
}
}
}
Aggregations