use of org.drools.workbench.models.datamodel.rule.ActionCallMethod in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testFunctionsWithVariableParameters.
@Test
public void testFunctionsWithVariableParameters() throws Exception {
String drl = "package org.mortgages;\n" + "rule \"test\"\n" + " dialect \"mvel\"\n" + " when\n" + " Calculator( s : summer)\n" + " Applicant( $age : age)\n" + " then\n" + " s.sum( $age, $age );\n" + "end";
addModelField("Calculator", "summer", "Summer", "Summer");
addModelField("Applicant", "age", "java.lang.Integer", DataType.TYPE_NUMERIC_INTEGER);
HashMap<String, List<MethodInfo>> map = new HashMap<>();
ArrayList<MethodInfo> methodInfos = new ArrayList<>();
ArrayList<String> params = new ArrayList<>();
params.add("Integer");
params.add("Integer");
methodInfos.add(new MethodInfo("sum", params, "java.lang.Integer", null, "Summer"));
map.put("Calculator", methodInfos);
when(dmo.getModuleMethodInformation()).thenReturn(map);
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(2, m.lhs.length);
assertEquals(1, m.rhs.length);
ActionCallMethod actionCallMethod = (ActionCallMethod) m.rhs[0];
assertEquals("sum", actionCallMethod.getMethodName());
assertEquals("s", actionCallMethod.getVariable());
assertEquals(2, actionCallMethod.getFieldValues().length);
assertEquals("sum", actionCallMethod.getFieldValue(0).getField());
assertEquals("$age", actionCallMethod.getFieldValue(0).getValue());
assertEquals(2, actionCallMethod.getFieldValue(0).getNature());
assertEquals("java.lang.Integer", actionCallMethod.getFieldValue(0).getType());
assertEquals("sum", actionCallMethod.getFieldValue(1).getField());
assertEquals("$age", actionCallMethod.getFieldValue(1).getValue());
assertEquals(2, actionCallMethod.getFieldValue(1).getNature());
assertEquals("java.lang.Integer", actionCallMethod.getFieldValue(1).getType());
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.ActionCallMethod in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testFunctionCalls4_MultiParameterSetter.
@Test
public void testFunctionCalls4_MultiParameterSetter() {
String drl = "package org.mortgages;\n" + "import org.mortgages.classes.MyClass;\n" + "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + " $c : MyClass()\n" + "then\n" + " $c.setSomething(0, 1);\n" + "end\n";
Map<String, List<MethodInfo>> methodInformation = new HashMap<>();
List<MethodInfo> mapMethodInformation = new ArrayList<>();
mapMethodInformation.add(new MethodInfo("setSomething", Arrays.asList(DataType.TYPE_NUMERIC_INTEGER, DataType.TYPE_NUMERIC_INTEGER), "void", null, DataType.TYPE_STRING));
methodInformation.put("org.mortgages.classes.MyClass", mapMethodInformation);
when(dmo.getModuleMethodInformation()).thenReturn(methodInformation);
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(1, m.rhs.length);
assertTrue(m.rhs[0] instanceof ActionCallMethod);
ActionCallMethod actionCallMethod1 = (ActionCallMethod) m.rhs[0];
assertEquals(1, actionCallMethod1.getState());
assertEquals("setSomething", actionCallMethod1.getMethodName());
assertEquals("$c", actionCallMethod1.getVariable());
assertEquals(2, actionCallMethod1.getFieldValues().length);
assertEquals("setSomething", actionCallMethod1.getFieldValues()[0].getField());
assertEquals("0", actionCallMethod1.getFieldValues()[0].getValue());
assertEquals(FieldNatureType.TYPE_LITERAL, actionCallMethod1.getFieldValues()[0].getNature());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, actionCallMethod1.getFieldValues()[0].getType());
assertEquals("setSomething", actionCallMethod1.getFieldValues()[1].getField());
assertEquals("1", actionCallMethod1.getFieldValues()[1].getValue());
assertEquals(FieldNatureType.TYPE_LITERAL, actionCallMethod1.getFieldValues()[1].getNature());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, actionCallMethod1.getFieldValues()[1].getType());
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.ActionCallMethod in project drools-wb by kiegroup.
the class RuleModelCloneVisitor method visitActionCallMethod.
private ActionCallMethod visitActionCallMethod(ActionCallMethod acm) {
ActionCallMethod clone = new ActionCallMethod();
clone.setVariable(acm.getVariable());
clone.setState(acm.getState());
clone.setMethodName(acm.getMethodName());
for (ActionFieldValue aff : acm.getFieldValues()) {
clone.addFieldValue(cloneActionFieldFunction((ActionFieldFunction) aff));
}
return clone;
}
use of org.drools.workbench.models.datamodel.rule.ActionCallMethod in project drools-wb by kiegroup.
the class BRLConditionColumnSynchronizerTest method checkBRLFragmentConditionCannotBeDeletedWhenFieldBindingIsUsedInAction.
@Test
public void checkBRLFragmentConditionCannotBeDeletedWhenFieldBindingIsUsedInAction() throws VetoException {
final BRLConditionColumn column = new BRLConditionColumn();
column.setDefinition(Collections.singletonList(new FactPattern("Applicant") {
{
setBoundName("$a");
addConstraint(new SingleFieldConstraint("age") {
{
setBoundName("$age");
}
});
}
}));
final BRLConditionVariableColumn columnV0 = new BRLConditionVariableColumn("$age", DataType.TYPE_NUMERIC_INTEGER, "Applicant", "age");
column.getChildColumns().add(columnV0);
column.setHeader("col1");
columnV0.setHeader("col1v0");
modelSynchronizer.appendColumn(column);
final BRLActionColumn action = new BRLActionColumn();
action.setDefinition(Collections.singletonList(new ActionCallMethod() {
{
setVariable("$age");
setMethodName("toString()");
}
}));
final BRLActionVariableColumn columnV1 = new BRLActionVariableColumn("$age", DataType.TYPE_NUMERIC_INTEGER, "Applicant", "age");
action.getChildColumns().add(columnV1);
action.setHeader("col2");
columnV1.setHeader("col2v0");
modelSynchronizer.appendColumn(action);
try {
modelSynchronizer.deleteColumn(column);
fail("Deletion of the column should have been vetoed.");
} catch (VetoDeletePatternInUseException veto) {
// This is expected
} catch (VetoException veto) {
fail("VetoDeletePatternInUseException was expected.");
}
assertEquals(4, model.getExpandedColumns().size());
assertTrue(model.getExpandedColumns().get(0) instanceof RowNumberCol52);
assertTrue(model.getExpandedColumns().get(1) instanceof DescriptionCol52);
assertEquals(columnV0, model.getExpandedColumns().get(2));
assertEquals(action.getChildColumns().get(0), model.getExpandedColumns().get(3));
}
use of org.drools.workbench.models.datamodel.rule.ActionCallMethod in project drools-wb by kiegroup.
the class RuleModellerWidgetFactory method getWidget.
public RuleModellerWidget getWidget(RuleModeller ruleModeller, EventBus eventBus, IAction action, Boolean readOnly) {
if (action instanceof ActionCallMethod) {
return new ActionCallMethodWidget(ruleModeller, eventBus, (ActionCallMethod) action, readOnly);
}
if (action instanceof ActionSetField) {
return new ActionSetFieldWidget(ruleModeller, eventBus, (ActionSetField) action, readOnly);
}
if (action instanceof ActionInsertFact) {
return new ActionInsertFactWidget(ruleModeller, eventBus, (ActionInsertFact) action, readOnly);
}
if (action instanceof ActionRetractFact) {
return new ActionRetractFactWidget(ruleModeller, eventBus, (ActionRetractFact) action, readOnly);
}
if (action instanceof DSLSentence) {
RuleModellerWidget w = new DSLSentenceWidget(ruleModeller, eventBus, (DSLSentence) action, readOnly);
return w;
}
if (action instanceof FreeFormLine) {
return new FreeFormLineWidget(ruleModeller, eventBus, (FreeFormLine) action, readOnly);
}
if (action instanceof ActionGlobalCollectionAdd) {
return new GlobalCollectionAddWidget(ruleModeller, eventBus, (ActionGlobalCollectionAdd) action, readOnly);
}
// All hardcoded action widgets have been checked, perform a plugin lookup
List<RuleModellerActionPlugin> matchingActionPlugins = actionPlugins.stream().filter(p -> p.accept(action)).collect(Collectors.toList());
if (matchingActionPlugins.size() > 1) {
throw new IllegalStateException("Ambigious " + RuleModellerActionPlugin.class.getName() + " implementations for action " + action);
}
if (matchingActionPlugins.size() == 1) {
RuleModellerActionPlugin actionPlugin = matchingActionPlugins.get(0);
RuleModellerWidget ruleModellerWidget = actionPlugin.createWidget(ruleModeller, eventBus, action, readOnly);
return ruleModellerWidget;
}
// NON-NLS
throw new RuntimeException("I don't know what type of action is: " + action);
}
Aggregations