use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testRHSActionWorkItemInsertFacts.
@Test
public // Test that WorkItem Parameters can be used to set fields on new Fact
void testRHSActionWorkItemInsertFacts() {
RuleModel m = new RuleModel();
m.name = "WorkItem";
ActionExecuteWorkItem awi = new ActionExecuteWorkItem();
PortableWorkDefinition pwd = new PortableWorkDefinition();
pwd.setName("WorkItem");
awi.setWorkDefinition(pwd);
PortableBooleanParameterDefinition p1 = new PortableBooleanParameterDefinition();
p1.setName("BooleanResult");
pwd.addResult(p1);
PortableFloatParameterDefinition p2 = new PortableFloatParameterDefinition();
p2.setName("FloatResult");
pwd.addResult(p2);
PortableIntegerParameterDefinition p3 = new PortableIntegerParameterDefinition();
p3.setName("IntegerResult");
pwd.addResult(p3);
PortableStringParameterDefinition p4 = new PortableStringParameterDefinition();
p4.setName("StringResult");
pwd.addResult(p4);
m.addRhsItem(awi);
ActionInsertFact aif = new ActionInsertFact();
aif.setBoundName("$r");
aif.setFactType("Results");
ActionWorkItemFieldValue fv1 = new ActionWorkItemFieldValue("ResultsBooleanResult", DataType.TYPE_BOOLEAN, "WorkItem", "BooleanResult", Boolean.class.getName());
aif.addFieldValue(fv1);
ActionWorkItemFieldValue fv2 = new ActionWorkItemFieldValue("ResultsFloatResult", DataType.TYPE_NUMERIC_FLOAT, "WorkItem", "FloatResult", Float.class.getName());
aif.addFieldValue(fv2);
ActionWorkItemFieldValue fv3 = new ActionWorkItemFieldValue("ResultsIntegerResult", DataType.TYPE_NUMERIC_INTEGER, "WorkItem", "IntegerResult", Integer.class.getName());
aif.addFieldValue(fv3);
ActionWorkItemFieldValue fv4 = new ActionWorkItemFieldValue("ResultsStringResult", DataType.TYPE_STRING, "WorkItem", "StringResult", String.class.getName());
aif.addFieldValue(fv4);
m.addRhsItem(aif);
String result = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
assertTrue(result.indexOf("org.drools.core.process.instance.WorkItemManager wim = (org.drools.core.process.instance.WorkItemManager) drools.getWorkingMemory().getWorkItemManager();") != -1);
assertTrue(result.indexOf("org.drools.core.process.instance.impl.WorkItemImpl wiWorkItem = new org.drools.core.process.instance.impl.WorkItemImpl();") != -1);
assertTrue(result.indexOf("Results $r = new Results();") != -1);
assertTrue(result.indexOf("$r.setResultsBooleanResult( (java.lang.Boolean) wiWorkItem.getResult( \"BooleanResult\" ) );") != -1);
assertTrue(result.indexOf("$r.setResultsFloatResult( (java.lang.Float) wiWorkItem.getResult( \"FloatResult\" ) );") != -1);
assertTrue(result.indexOf("$r.setResultsIntegerResult( (java.lang.Integer) wiWorkItem.getResult( \"IntegerResult\" ) );") != -1);
assertTrue(result.indexOf("$r.setResultsStringResult( (java.lang.String) wiWorkItem.getResult( \"StringResult\" ) );") != -1);
assertTrue(result.indexOf("insert( $r );") != -1);
checkMarshalling(null, m);
}
use of org.drools.workbench.models.datamodel.rule.ActionInsertFact 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.ActionInsertFact in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testEnumTypeDate.
@Test
public void testEnumTypeDate() {
// A legacy "Guvnor" enums (i.e pick-list of underlying field data-type)
String expected = "rule \"my rule\"\n\tdialect \"mvel\"\n\twhen\n\t\tCheese( dateMade == \"31-Jan-2010\" )\n" + "\tthen\n\t\tinsert( new Report() );\nend\n";
final RuleModel m = new RuleModel();
final FactPattern pat = new FactPattern("Cheese");
m.addLhsItem(pat);
final SingleFieldConstraint con = new SingleFieldConstraint();
con.setFieldName("dateMade");
con.setOperator("==");
con.setValue("31-Jan-2010");
con.setFieldType(DataType.TYPE_DATE);
con.setConstraintValueType(BaseSingleFieldConstraint.TYPE_ENUM);
pat.addConstraint(con);
m.addRhsItem(new ActionInsertFact("Report"));
m.name = "my rule";
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.ActionInsertFact 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.ActionInsertFact 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