use of org.drools.workbench.models.datamodel.rule.ActionFieldValue in project drools by kiegroup.
the class RuleModelDRLPersistenceImpl method marshalRHS.
protected void marshalRHS(final StringBuilder buf, final RuleModel model, final boolean isDSLEnhanced, final RHSGeneratorContextFactory generatorContextFactory) {
String indentation = "\t\t";
if (model.rhs != null) {
// Add boiler-plate for actions operating on Dates
Map<String, List<ActionFieldValue>> classes = getRHSClassDependencies(model);
if (classes.containsKey(DataType.TYPE_DATE)) {
buf.append(indentation);
if (isDSLEnhanced) {
buf.append(">");
}
buf.append("java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"" + DateUtils.getDateFormatMask() + "\");\n");
}
// Add boiler-plate for actions operating on WorkItems
if (!getRHSWorkItemDependencies(model).isEmpty()) {
buf.append(indentation);
buf.append("org.drools.core.process.instance.WorkItemManager wim = (org.drools.core.process.instance.WorkItemManager) drools.getWorkingMemory().getWorkItemManager();\n");
}
// Marshall the model itself
RHSActionVisitor actionVisitor = getRHSActionVisitor(isDSLEnhanced, buf, indentation, generatorContextFactory);
// Reconcile ActionSetField and ActionUpdateField calls
final List<IAction> actions = new ArrayList<IAction>();
for (IAction action : model.rhs) {
if (action instanceof ActionCallMethod) {
actions.add(action);
} else if (action instanceof ActionSetField) {
final ActionSetField asf = (ActionSetField) action;
final ActionSetFieldWrapper afw = findExistingAction(asf, actions);
if (afw == null) {
actions.add(new ActionSetFieldWrapper(asf, (asf instanceof ActionUpdateField)));
} else {
final List<ActionFieldValue> existingActionFieldValue = new ArrayList<ActionFieldValue>(Arrays.asList(afw.getAction().getFieldValues()));
for (ActionFieldValue afv : asf.getFieldValues()) {
existingActionFieldValue.add(afv);
}
final ActionFieldValue[] temp = new ActionFieldValue[existingActionFieldValue.size()];
afw.getAction().setFieldValues(existingActionFieldValue.toArray(temp));
}
} else {
actions.add(action);
}
}
model.rhs = new IAction[actions.size()];
for (int i = 0; i < actions.size(); i++) {
final IAction action = actions.get(i);
if (action instanceof ActionSetFieldWrapper) {
model.rhs[i] = ((ActionSetFieldWrapper) action).getAction();
} else {
model.rhs[i] = action;
}
}
for (IAction action : model.rhs) {
if (action instanceof PluggableIAction) {
PluggableIAction processedIAction = (PluggableIAction) actionVisitor.preProcessIActionForExtensions(action);
buf.append(indentation).append(processedIAction.getStringRepresentation()).append(";\n");
} else {
actionVisitor.visit(action);
}
}
}
}
use of org.drools.workbench.models.datamodel.rule.ActionFieldValue in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testRHSDateInsertActionWithoutSystemProperty.
@Test
public void testRHSDateInsertActionWithoutSystemProperty() {
// RHBRMS-3034
String oldValue = System.getProperty("drools.dateformat");
try {
System.clearProperty("drools.dateformat");
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 DRL : " + result, 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);
}
}
}
use of org.drools.workbench.models.datamodel.rule.ActionFieldValue in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testActionCallMethod.
@Test
public void testActionCallMethod() {
final String drl = "rule \"r0\"\n" + "dialect \"mvel\"\n" + "when\n" + "$a : Applicant( )\n" + "then\n" + "$a.addName( \"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 ActionCallMethod);
final ActionCallMethod a = (ActionCallMethod) m.rhs[0];
assertEquals("$a", a.getVariable());
assertEquals("addName", a.getMethodName());
assertEquals(1, a.getFieldValues().length);
final ActionFieldValue fv = a.getFieldValue(0);
assertEquals("Michael", fv.getValue());
}
use of org.drools.workbench.models.datamodel.rule.ActionFieldValue in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testSumAsGivenValue.
@Test
public void testSumAsGivenValue() {
// BZ-1013682
String expected = "" + "rule \"my rule\" \n" + " dialect \"mvel\"\n" + " when\n" + " m:Message()\n" + " then\n" + " modify( m ) {\n" + " setText( \"Hello \" + \"world\" )\n" + " }\n" + "end\n";
final RuleModel m = new RuleModel();
FactPattern factPattern = new FactPattern();
factPattern.setFactType("Message");
factPattern.setBoundName("m");
m.lhs = new IPattern[] { factPattern };
ActionUpdateField actionUpdateField = new ActionUpdateField();
actionUpdateField.setVariable("m");
ActionFieldValue actionFieldValue = new ActionFieldValue();
actionFieldValue.setField("text");
actionFieldValue.setType("String");
actionFieldValue.setNature(FieldNatureType.TYPE_FORMULA);
actionFieldValue.setValue("\"Hello \" + \"world\"");
actionUpdateField.setFieldValues(new ActionFieldValue[] { actionFieldValue });
m.rhs = new IAction[] { actionUpdateField };
m.name = "my rule";
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.ActionFieldValue in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testRHSDateModifyAction.
@Test
public void testRHSDateModifyAction() {
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);
ActionUpdateField am = new ActionUpdateField("$p");
am.addFieldValue(new ActionFieldValue("dob", "31-Jan-2000", DataType.TYPE_DATE));
m.addRhsItem(am);
String result = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
assertTrue(result.indexOf("java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"dd-MMM-yyyy\");") != -1);
assertTrue(result.indexOf("setDob( sdf.parse(\"31-Jan-2000\"") != -1);
assertTrue(result.indexOf("modify( $p ) {") != -1);
checkMarshalling(null, m);
} finally {
if (oldValue == null) {
System.clearProperty("drools.dateformat");
} else {
System.setProperty("drools.dateformat", oldValue);
}
}
}
Aggregations