use of org.drools.workbench.models.datamodel.rule.ActionExecuteWorkItem in project drools by kiegroup.
the class RuleModelDRLPersistenceImpl method parseRhs.
private void parseRhs(final RuleModel m, final String rhs, final boolean isJavaDialect, final Map<String, String> boundParams, final ExpandedDRLInfo expandedDRLInfo, final PackageDataModelOracle dmo, final Collection<RuleModelIActionPersistenceExtension> extensions) throws RuleModelDRLPersistenceException {
PortableWorkDefinition pwd = null;
Map<String, List<String>> setStatements = new HashMap<String, List<String>>();
Map<String, Integer> setStatementsPosition = new HashMap<String, Integer>();
Map<String, String> factsType = new HashMap<String, String>();
String modifiedVariable = null;
String modifiers = null;
int lineCounter = -1;
String[] lines = rhs.split("\n");
for (String line : lines) {
lineCounter++;
line = line.trim();
List<RuleModelIActionPersistenceExtension> matchingExtensions = getMatchingExtensionsForLine(line, extensions);
if (matchingExtensions.isEmpty()) {
// Continue with hardcoded parsers
} else if (matchingExtensions.size() > 1) {
throw new RuleModelDRLPersistenceException("Ambiguous RuleModelIActionPersistenceExtension implementations (" + matchingExtensions + ") found for line " + line);
} else {
unmarshalUsingExtension(m, matchingExtensions.get(0), line);
continue;
}
if (expandedDRLInfo.hasDsl) {
String dslLine = expandedDRLInfo.dslStatementsInRhs.get(lineCounter);
while (dslLine != null) {
List<RuleModelIActionPersistenceExtension> matchingExtensionsDslLine = getMatchingExtensionsForLine(dslLine, extensions);
if (matchingExtensionsDslLine.isEmpty()) {
m.addRhsItem(toDSLSentence(expandedDRLInfo.rhsDslPatterns, dslLine));
} else if (matchingExtensionsDslLine.size() > 1) {
throw new RuleModelDRLPersistenceException("Ambiguous RuleModelIActionPersistenceExtension implementations (" + matchingExtensionsDslLine + ") found for line " + line);
} else {
unmarshalUsingExtension(m, matchingExtensionsDslLine.get(0), dslLine);
}
dslLine = expandedDRLInfo.dslStatementsInRhs.get(++lineCounter);
}
}
if (modifiedVariable != null) {
int modifyBlockEnd = line.lastIndexOf('}');
if (modifiers == null) {
modifiers = modifyBlockEnd > 0 ? line.substring(line.indexOf('{') + 1, modifyBlockEnd).trim() : line.substring(line.indexOf('{') + 1).trim();
} else if (modifyBlockEnd != 0) {
modifiers += modifyBlockEnd > 0 ? line.substring(0, modifyBlockEnd).trim() : line;
}
if (modifyBlockEnd >= 0) {
ActionUpdateField action = new ActionUpdateField();
action.setVariable(modifiedVariable);
m.addRhsItem(action);
addModifiersToAction(modifiers, action, modifiedVariable, boundParams, dmo, m, isJavaDialect);
modifiedVariable = null;
modifiers = null;
}
} else if (line.startsWith("insertLogical")) {
String fact = unwrapParenthesis(line);
String type = getStatementType(fact, factsType);
if (type != null) {
boundParams.put(fact, type);
ActionInsertLogicalFact action = new ActionInsertLogicalFact(type);
m.addRhsItem(action);
if (factsType.containsKey(fact)) {
action.setBoundName(fact);
addSettersToAction(setStatements, fact, action, boundParams, dmo, m, isJavaDialect);
}
}
} else if (line.startsWith("insert")) {
String fact = unwrapParenthesis(line);
String type = getStatementType(fact, factsType);
if (type != null) {
boundParams.put(fact, type);
ActionInsertFact action = new ActionInsertFact(type);
m.addRhsItem(action);
if (factsType.containsKey(fact)) {
action.setBoundName(fact);
addSettersToAction(setStatements, fact, action, boundParams, dmo, m, isJavaDialect);
}
}
} else if (line.startsWith("update")) {
String variable = unwrapParenthesis(line);
ActionUpdateField action = new ActionUpdateField();
action.setVariable(variable);
m.addRhsItem(action);
addSettersToAction(setStatements, variable, action, boundParams, dmo, m, isJavaDialect);
} else if (line.startsWith("modify")) {
int modifyBlockEnd = line.lastIndexOf('}');
if (modifyBlockEnd > 0) {
String variable = line.substring(line.indexOf('(') + 1, line.indexOf(')')).trim();
ActionUpdateField action = new ActionUpdateField();
action.setVariable(variable);
m.addRhsItem(action);
addModifiersToAction(line.substring(line.indexOf('{') + 1, modifyBlockEnd).trim(), action, variable, boundParams, dmo, m, isJavaDialect);
} else {
modifiedVariable = line.substring(line.indexOf('(') + 1, line.indexOf(')')).trim();
int modifyBlockStart = line.indexOf('{');
if (modifyBlockStart > 0) {
modifiers = line.substring(modifyBlockStart + 1).trim();
}
}
} else if (line.startsWith("retract") || line.startsWith("delete")) {
String variable = unwrapParenthesis(line);
m.addRhsItem(new ActionRetractFact(variable));
} else if (line.startsWith("org.drools.core.process.instance.impl.WorkItemImpl wiWorkItem")) {
ActionExecuteWorkItem awi = new ActionExecuteWorkItem();
pwd = new PortableWorkDefinition();
pwd.setName("WorkItem");
awi.setWorkDefinition(pwd);
m.addRhsItem(awi);
} else if (line.startsWith("wiWorkItem.getParameters().put")) {
String statement = line.substring("wiWorkItem.getParameters().put".length());
statement = unwrapParenthesis(statement);
int commaPos = statement.indexOf(',');
String name = statement.substring(0, commaPos).trim();
String value = statement.substring(commaPos + 1).trim();
pwd.addParameter(buildPortableParameterDefinition(name, value, boundParams));
} else if (line.startsWith("wim.internalExecuteWorkItem") || line.startsWith("wiWorkItem.setName")) {
// ignore
} else {
int dotPos = line.indexOf('.');
int argStart = line.indexOf('(');
if (dotPos > 0 && argStart > dotPos) {
String variable = line.substring(0, dotPos).trim();
if (boundParams.containsKey(variable) || factsType.containsKey(variable) || expandedDRLInfo.hasGlobal(variable)) {
if (isJavaIdentifier(variable)) {
String methodName = line.substring(dotPos + 1, argStart).trim();
if (isJavaIdentifier(methodName)) {
if (getSettedField(m, methodName, boundParams.get(variable), dmo) != null) {
List<String> setters = setStatements.get(variable);
if (setters == null) {
setters = new ArrayList<String>();
setStatements.put(variable, setters);
}
if (!setStatementsPosition.containsKey(variable)) {
setStatementsPosition.put(variable, lineCounter);
}
setters.add(line);
} else if (methodName.equals("add") && expandedDRLInfo.hasGlobal(variable)) {
String factName = line.substring(argStart + 1, line.lastIndexOf(')')).trim();
ActionGlobalCollectionAdd actionGlobalCollectionAdd = new ActionGlobalCollectionAdd();
actionGlobalCollectionAdd.setGlobalName(variable);
actionGlobalCollectionAdd.setFactName(factName);
m.addRhsItem(actionGlobalCollectionAdd);
} else {
m.addRhsItem(getActionCallMethod(m, isJavaDialect, boundParams, dmo, line, variable, methodName));
}
continue;
}
}
}
}
int eqPos = line.indexOf('=');
boolean addFreeFormLine = line.trim().length() > 0;
if (eqPos > 0) {
String field = line.substring(0, eqPos).trim();
if ("java.text.SimpleDateFormat sdf".equals(field) || "org.drools.core.process.instance.WorkItemManager wim".equals(field)) {
addFreeFormLine = false;
}
String[] split = field.split(" ");
if (split.length == 2) {
factsType.put(split[1], split[0]);
addFreeFormLine &= !isInsertedFact(lines, lineCounter, split[1]);
}
}
if (addFreeFormLine) {
FreeFormLine ffl = new FreeFormLine();
ffl.setText(line);
m.addRhsItem(ffl);
}
}
}
// variable they are modifying was recorded as Free Format DRL and hence the "sets" need to be Free Format DRL too.
for (Map.Entry<String, List<String>> entry : setStatements.entrySet()) {
if (boundParams.containsKey(entry.getKey())) {
ActionSetField action = new ActionSetField(entry.getKey());
addSettersToAction(entry.getValue(), action, entry.getKey(), boundParams, dmo, m, isJavaDialect);
m.addRhsItem(action, setStatementsPosition.get(entry.getKey()));
} else {
FreeFormLine action = new FreeFormLine();
StringBuilder sb = new StringBuilder();
for (String setter : entry.getValue()) {
sb.append(setter).append("\n");
}
action.setText(sb.toString());
m.addRhsItem(action, setStatementsPosition.get(entry.getKey()));
}
}
if (expandedDRLInfo.hasDsl) {
String dslLine = expandedDRLInfo.dslStatementsInRhs.get(++lineCounter);
while (dslLine != null) {
m.addRhsItem(toDSLSentence(expandedDRLInfo.rhsDslPatterns, dslLine));
dslLine = expandedDRLInfo.dslStatementsInRhs.get(++lineCounter);
}
}
}
use of org.drools.workbench.models.datamodel.rule.ActionExecuteWorkItem in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testRHSExecuteWorkItem2.
@Test
public void testRHSExecuteWorkItem2() {
RuleModel m = new RuleModel();
m.name = "WorkItem";
FactPattern p = new FactPattern("Person");
p.setBoundName("$p");
SingleFieldConstraint con = new SingleFieldConstraint();
con.setFieldType(DataType.TYPE_STRING);
con.setFieldName("name");
con.setOperator("==");
con.setValue("Michael");
con.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
p.addConstraint(con);
m.addLhsItem(p);
ActionExecuteWorkItem awi = new ActionExecuteWorkItem();
PortableWorkDefinition pwd = new PortableWorkDefinition();
pwd.setName("WorkItem");
awi.setWorkDefinition(pwd);
PortableBooleanParameterDefinition p1 = new PortableBooleanParameterDefinition();
p1.setName("BooleanParameter");
p1.setValue(Boolean.TRUE);
p1.setBinding("");
pwd.addParameter(p1);
PortableFloatParameterDefinition p2 = new PortableFloatParameterDefinition();
p2.setName("FloatParameter");
p2.setValue(123.456f);
p2.setBinding("");
pwd.addParameter(p2);
PortableIntegerParameterDefinition p3 = new PortableIntegerParameterDefinition();
p3.setName("IntegerParameter");
p3.setValue(123);
p3.setBinding("");
pwd.addParameter(p3);
PortableStringParameterDefinition p4 = new PortableStringParameterDefinition();
p4.setName("StringParameter");
p4.setValue("hello");
p4.setBinding("");
pwd.addParameter(p4);
m.addRhsItem(awi);
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("wiWorkItem.getParameters().put( \"BooleanParameter\", Boolean.TRUE );") != -1);
assertTrue(result.indexOf("wiWorkItem.getParameters().put( \"FloatParameter\", 123.456f );") != -1);
assertTrue(result.indexOf("wiWorkItem.getParameters().put( \"IntegerParameter\", 123 );") != -1);
assertTrue(result.indexOf("wiWorkItem.getParameters().put( \"StringParameter\", \"hello\" );") != -1);
assertTrue(result.indexOf("wim.internalExecuteWorkItem( wiWorkItem );") != -1);
checkMarshalling(null, m);
}
use of org.drools.workbench.models.datamodel.rule.ActionExecuteWorkItem 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.ActionExecuteWorkItem in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testRHSExecuteWorkItem1.
@Test
public void testRHSExecuteWorkItem1() {
RuleModel m = new RuleModel();
m.name = "WorkItem";
FactPattern p = new FactPattern("Person");
p.setBoundName("$p");
SingleFieldConstraint con = new SingleFieldConstraint();
con.setFieldType(DataType.TYPE_STRING);
con.setFieldName("name");
con.setOperator("==");
con.setValue("Michael");
con.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
p.addConstraint(con);
m.addLhsItem(p);
ActionExecuteWorkItem awi = new ActionExecuteWorkItem();
PortableWorkDefinition pwd = new PortableWorkDefinition();
pwd.setName("WorkItem");
awi.setWorkDefinition(pwd);
PortableBooleanParameterDefinition p1 = new PortableBooleanParameterDefinition();
p1.setName("BooleanParameter");
p1.setValue(Boolean.TRUE);
pwd.addParameter(p1);
PortableFloatParameterDefinition p2 = new PortableFloatParameterDefinition();
p2.setName("FloatParameter");
p2.setValue(123.456f);
pwd.addParameter(p2);
PortableIntegerParameterDefinition p3 = new PortableIntegerParameterDefinition();
p3.setName("IntegerParameter");
p3.setValue(123);
pwd.addParameter(p3);
PortableStringParameterDefinition p4 = new PortableStringParameterDefinition();
p4.setName("StringParameter");
p4.setValue("hello");
pwd.addParameter(p4);
m.addRhsItem(awi);
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("wiWorkItem.getParameters().put( \"BooleanParameter\", Boolean.TRUE );") != -1);
assertTrue(result.indexOf("wiWorkItem.getParameters().put( \"FloatParameter\", 123.456f );") != -1);
assertTrue(result.indexOf("wiWorkItem.getParameters().put( \"IntegerParameter\", 123 );") != -1);
assertTrue(result.indexOf("wiWorkItem.getParameters().put( \"StringParameter\", \"hello\" );") != -1);
assertTrue(result.indexOf("wim.internalExecuteWorkItem( wiWorkItem );") != -1);
checkMarshalling(null, m);
}
use of org.drools.workbench.models.datamodel.rule.ActionExecuteWorkItem in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testRHSExecuteWorkItemWithMissingBindings2.
@Test
public // populated in the RHS if the Pattern is *NOT* bound to the same variable
void testRHSExecuteWorkItemWithMissingBindings2() {
RuleModel m = new RuleModel();
m.name = "WorkItem";
FactPattern fp1 = new FactPattern("Person");
fp1.setBoundName("$p");
SingleFieldConstraint con1 = new SingleFieldConstraint();
con1.setFieldType(DataType.TYPE_STRING);
con1.setFieldName("name");
con1.setOperator("==");
con1.setValue("Michael");
con1.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
fp1.addConstraint(con1);
m.addLhsItem(fp1);
ActionExecuteWorkItem awi = new ActionExecuteWorkItem();
PortableWorkDefinition pwd = new PortableWorkDefinition();
pwd.setName("WorkItem");
awi.setWorkDefinition(pwd);
PortableBooleanParameterDefinition p1 = new PortableBooleanParameterDefinition();
p1.setName("BooleanParameter");
p1.setBinding("$b2");
p1.setValue(Boolean.TRUE);
pwd.addParameter(p1);
PortableFloatParameterDefinition p2 = new PortableFloatParameterDefinition();
p2.setName("FloatParameter");
p2.setBinding("$f2");
p2.setValue(123.456f);
pwd.addParameter(p2);
PortableIntegerParameterDefinition p3 = new PortableIntegerParameterDefinition();
p3.setName("IntegerParameter");
p3.setBinding("$i2");
p3.setValue(123);
pwd.addParameter(p3);
PortableStringParameterDefinition p4 = new PortableStringParameterDefinition();
p4.setName("StringParameter");
p4.setBinding("$s2");
p4.setValue("hello");
pwd.addParameter(p4);
m.addRhsItem(awi);
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);
assertFalse(result.indexOf("wiWorkItem.getParameters().put( \"BooleanParameter\", $b );") != -1);
assertFalse(result.indexOf("wiWorkItem.getParameters().put( \"FloatParameter\", $f );") != -1);
assertFalse(result.indexOf("wiWorkItem.getParameters().put( \"IntegerParameter\", $i );") != -1);
assertFalse(result.indexOf("wiWorkItem.getParameters().put( \"StringParameter\", $s );") != -1);
assertTrue(result.indexOf("wim.internalExecuteWorkItem( wiWorkItem );") != -1);
checkMarshalling(null, m);
}
Aggregations