use of org.drools.workbench.models.datamodel.rule.ActionSetField in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testRHSActionWorkItemSetFields.
@Test
public // Test that WorkItem Parameters can be used to set fields on existing Facts
void testRHSActionWorkItemSetFields() {
RuleModel m = new RuleModel();
m.name = "WorkItem";
FactPattern fp1 = new FactPattern("Results");
fp1.setBoundName("$r");
m.addLhsItem(fp1);
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);
ActionSetField asf = new ActionSetField();
asf.setVariable("$r");
ActionWorkItemFieldValue fv1 = new ActionWorkItemFieldValue("ResultsBooleanResult", DataType.TYPE_BOOLEAN, "WorkItem", "BooleanResult", Boolean.class.getName());
asf.addFieldValue(fv1);
ActionWorkItemFieldValue fv2 = new ActionWorkItemFieldValue("ResultsFloatResult", DataType.TYPE_NUMERIC_FLOAT, "WorkItem", "FloatResult", Float.class.getName());
asf.addFieldValue(fv2);
ActionWorkItemFieldValue fv3 = new ActionWorkItemFieldValue("ResultsIntegerResult", DataType.TYPE_NUMERIC_INTEGER, "WorkItem", "IntegerResult", Integer.class.getName());
asf.addFieldValue(fv3);
ActionWorkItemFieldValue fv4 = new ActionWorkItemFieldValue("ResultsStringResult", DataType.TYPE_STRING, "WorkItem", "StringResult", String.class.getName());
asf.addFieldValue(fv4);
m.addRhsItem(asf);
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("$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("wim.internalExecuteWorkItem( wiWorkItem );") != -1);
checkMarshalling(null, m);
}
use of org.drools.workbench.models.datamodel.rule.ActionSetField in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testRHSChangeMultipleFieldsBlockModify.
@Test
public void testRHSChangeMultipleFieldsBlockModify() {
String expected = "" + "rule \"my rule\" \n" + " dialect \"mvel\"\n" + " when\n" + " $p : Person()\n" + " then\n" + " modify( $p ) {\n" + " setName( \"Fred\" ),\n" + " setAge( 55 )\n" + " }\n" + " $p.setGender( \"X\" );" + "end\n";
final RuleModel m = new RuleModel();
FactPattern factPattern = new FactPattern();
factPattern.setFactType("Person");
factPattern.setBoundName("$p");
m.lhs = new IPattern[] { factPattern };
ActionUpdateField auf1 = new ActionUpdateField();
auf1.setVariable("$p");
ActionFieldValue afv1 = new ActionFieldValue();
afv1.setField("name");
afv1.setType(DataType.TYPE_STRING);
afv1.setNature(FieldNatureType.TYPE_LITERAL);
afv1.setValue("Fred");
auf1.setFieldValues(new ActionFieldValue[] { afv1 });
ActionSetField asf = new ActionSetField();
asf.setVariable("$p");
ActionFieldValue afv2 = new ActionFieldValue();
afv2.setField("gender");
afv2.setType(DataType.TYPE_STRING);
afv2.setNature(FieldNatureType.TYPE_LITERAL);
afv2.setValue("X");
asf.setFieldValues(new ActionFieldValue[] { afv2 });
ActionUpdateField auf2 = new ActionUpdateField();
auf2.setVariable("$p");
ActionFieldValue afv3 = new ActionFieldValue();
afv3.setField("age");
afv3.setType(DataType.TYPE_NUMERIC_INTEGER);
afv3.setNature(FieldNatureType.TYPE_LITERAL);
afv3.setValue("55");
auf2.setFieldValues(new ActionFieldValue[] { afv3 });
m.rhs = new IAction[] { auf1, asf, auf2 };
m.name = "my rule";
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.ActionSetField in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testRHSChangeMultipleFieldsModifyOneUpdateOther.
@Test
public void testRHSChangeMultipleFieldsModifyOneUpdateOther() {
String expected = "" + "rule \"my rule\" \n" + " dialect \"mvel\"\n" + " when\n" + " $p : Person()\n" + " then\n" + " modify( $p ) {\n" + " setName( \"Fred\" )\n" + " }\n" + " $p.setAge( 55 );\n" + "end\n";
final RuleModel m = new RuleModel();
FactPattern factPattern = new FactPattern();
factPattern.setFactType("Person");
factPattern.setBoundName("$p");
m.lhs = new IPattern[] { factPattern };
ActionUpdateField auf = new ActionUpdateField();
auf.setVariable("$p");
ActionFieldValue afv1 = new ActionFieldValue();
afv1.setField("name");
afv1.setType(DataType.TYPE_STRING);
afv1.setNature(FieldNatureType.TYPE_LITERAL);
afv1.setValue("Fred");
auf.setFieldValues(new ActionFieldValue[] { afv1 });
ActionSetField asf = new ActionSetField();
asf.setVariable("$p");
ActionFieldValue afv2 = new ActionFieldValue();
afv2.setField("age");
afv2.setType(DataType.TYPE_NUMERIC_INTEGER);
afv2.setNature(FieldNatureType.TYPE_LITERAL);
afv2.setValue("55");
asf.setFieldValues(new ActionFieldValue[] { afv2 });
m.rhs = new IAction[] { auf, asf };
m.name = "my rule";
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.ActionSetField in project drools by kiegroup.
the class GuidedDTDRLPersistence method addAction.
private void addAction(IAction action, List<LabelledAction> actions) {
String binding = null;
if (action instanceof ActionInsertFact) {
final ActionInsertFact af = (ActionInsertFact) action;
binding = af.getBoundName();
} else if (action instanceof ActionSetField) {
final ActionSetField af = (ActionSetField) action;
binding = af.getVariable();
}
// the binding use a unique identifier, in this case the Object itself.
if (binding == null) {
binding = action.toString();
}
final LabelledAction a = new LabelledAction();
a.boundName = binding;
a.action = action;
actions.add(a);
}
use of org.drools.workbench.models.datamodel.rule.ActionSetField in project drools by kiegroup.
the class GuidedDTDRLPersistence method doAction.
private void doAction(List<LabelledAction> actions, ActionSetFieldCol52 sf, String cell) {
LabelledAction a = findByLabelledAction(actions, sf.getBoundName(), sf.isUpdate());
if (a == null) {
a = new LabelledAction();
a.boundName = sf.getBoundName();
a.isUpdate = sf.isUpdate();
if (!sf.isUpdate()) {
a.action = new ActionSetField(sf.getBoundName());
} else {
a.action = new ActionUpdateField(sf.getBoundName());
}
actions.add(a);
} else if (sf.isUpdate() && !(a.action instanceof ActionUpdateField)) {
// lets swap it out for an update as the user has asked for it.
ActionSetField old = (ActionSetField) a.action;
ActionUpdateField update = new ActionUpdateField(sf.getBoundName());
update.setFieldValues(old.getFieldValues());
a.action = update;
}
ActionSetField asf = (ActionSetField) a.action;
ActionFieldValue val = new ActionFieldValue(sf.getFactField(), cell, sf.getType());
asf.addFieldValue(val);
}
Aggregations