use of org.drools.workbench.models.datamodel.rule.ActionCallMethod in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testMethodCallCheckParameterDataTypes3.
@Test
public void testMethodCallCheckParameterDataTypes3() {
// BZ-1045423
String drl = "" + "package org.mortgages;\n" + "import org.mortgages.MyType;\n" + "rule \"my rule\"\n" + " dialect \"mvel\"\n" + " when\n" + " i : Integer( )\n" + " t : MyType( )\n" + " then\n" + " t.doSomething( i );\n" + "end\n";
Map<String, List<MethodInfo>> methodInformation = new HashMap<>();
List<MethodInfo> mapMethodInformation = new ArrayList<>();
mapMethodInformation.add(new MethodInfo("doSomething", Collections.singletonList(DataType.TYPE_NUMERIC_INTEGER), "void", "void", "org.mortgages.MyType"));
methodInformation.put("org.mortgages.MyType", mapMethodInformation);
when(dmo.getModuleMethodInformation()).thenReturn(methodInformation);
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertTrue(m.rhs[0] instanceof ActionCallMethod);
ActionCallMethod mc = (ActionCallMethod) m.rhs[0];
assertEquals("doSomething", mc.getMethodName());
assertEquals("t", mc.getVariable());
assertEquals(1, mc.getState());
assertEquals(1, mc.getFieldValues().length);
ActionFieldValue f1 = mc.getFieldValue(0);
assertEquals("i", f1.getValue());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, f1.getType());
assertEquals(FieldNatureType.TYPE_VARIABLE, f1.getNature());
String marshalled = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
logger.debug(marshalled);
assertEqualsIgnoreWhitespace(drl, marshalled);
}
use of org.drools.workbench.models.datamodel.rule.ActionCallMethod in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testMethodCallCheckParameterDataTypes1.
@Test
public void testMethodCallCheckParameterDataTypes1() {
// BZ-1045423
String drl = "" + "package org.mortgages;\n" + "import org.mortgages.LoanApplication;\n" + "import java.util.Map;\n" + "rule \"my rule\"\n" + " dialect \"mvel\"\n" + " when\n" + " a : LoanApplication( )\n" + " m : Map()\n" + " then\n" + " m.put(\"key\", a );\n" + "end\n";
Map<String, List<MethodInfo>> methodInformation = new HashMap<>();
List<MethodInfo> mapMethodInformation = new ArrayList<>();
mapMethodInformation.add(new MethodInfo("put", Arrays.asList("java.lang.Object", "java.lang.Object"), "void", "void", "java.util.Map"));
methodInformation.put("java.util.Map", mapMethodInformation);
when(dmo.getModuleMethodInformation()).thenReturn(methodInformation);
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertTrue(m.rhs[0] instanceof ActionCallMethod);
ActionCallMethod mc = (ActionCallMethod) m.rhs[0];
assertEquals("put", mc.getMethodName());
assertEquals("m", mc.getVariable());
assertEquals(1, mc.getState());
assertEquals(2, mc.getFieldValues().length);
ActionFieldValue f1 = mc.getFieldValue(0);
assertEquals("\"key\"", f1.getValue());
assertEquals("java.lang.Object", f1.getType());
assertEquals(FieldNatureType.TYPE_LITERAL, f1.getNature());
ActionFieldValue f2 = mc.getFieldValue(1);
assertEquals("a", f2.getValue());
assertEquals("java.lang.Object", f2.getType());
assertEquals(FieldNatureType.TYPE_VARIABLE, f2.getNature());
String marshalled = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
logger.debug(marshalled);
assertEqualsIgnoreWhitespace(drl, marshalled);
}
use of org.drools.workbench.models.datamodel.rule.ActionCallMethod in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testMethodCallCheckParameterDataTypes2.
@Test
public void testMethodCallCheckParameterDataTypes2() {
// BZ-1045423
String drl = "" + "package org.mortgages;\n" + "import org.mortgages.MyType;\n" + "rule \"my rule\"\n" + " dialect \"mvel\"\n" + " when\n" + " t : MyType( )\n" + " then\n" + " t.doSomething( 1 * 2 );\n" + "end\n";
Map<String, List<MethodInfo>> methodInformation = new HashMap<>();
List<MethodInfo> mapMethodInformation = new ArrayList<>();
mapMethodInformation.add(new MethodInfo("doSomething", Collections.singletonList(DataType.TYPE_NUMERIC_INTEGER), "void", "void", "org.mortgages.MyType"));
methodInformation.put("org.mortgages.MyType", mapMethodInformation);
when(dmo.getModuleMethodInformation()).thenReturn(methodInformation);
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertTrue(m.rhs[0] instanceof ActionCallMethod);
ActionCallMethod mc = (ActionCallMethod) m.rhs[0];
assertEquals("doSomething", mc.getMethodName());
assertEquals("t", mc.getVariable());
assertEquals(1, mc.getState());
assertEquals(1, mc.getFieldValues().length);
ActionFieldValue f1 = mc.getFieldValue(0);
assertEquals("1 * 2", f1.getValue());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, f1.getType());
assertEquals(FieldNatureType.TYPE_FORMULA, f1.getNature());
String marshalled = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
logger.debug(marshalled);
assertEqualsIgnoreWhitespace(drl, marshalled);
}
use of org.drools.workbench.models.datamodel.rule.ActionCallMethod in project drools by kiegroup.
the class ActionCallMethodBuilder method get.
public ActionCallMethod get(String variable, String methodName, String[] parameters) {
this.variable = variable;
this.methodName = methodName;
this.parameters = parameters;
ActionCallMethod actionCallMethod = new ActionCallMethod();
actionCallMethod.setMethodName(methodName);
actionCallMethod.setVariable(variable);
actionCallMethod.setState(ActionCallMethod.TYPE_DEFINED);
for (ActionFieldFunction parameter : getActionFieldFunctions()) {
actionCallMethod.addFieldValue(parameter);
}
return actionCallMethod;
}
use of org.drools.workbench.models.datamodel.rule.ActionCallMethod 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);
}
}
}
}
Aggregations