Search in sources :

Example 21 with ActionCallMethod

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));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ActionCallMethod(org.drools.workbench.models.datamodel.rule.ActionCallMethod) List(java.util.List) ArrayList(java.util.ArrayList) MethodInfo(org.kie.soup.project.datamodel.oracle.MethodInfo) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 22 with ActionCallMethod

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));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ActionCallMethod(org.drools.workbench.models.datamodel.rule.ActionCallMethod) List(java.util.List) ArrayList(java.util.ArrayList) MethodInfo(org.kie.soup.project.datamodel.oracle.MethodInfo) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 23 with ActionCallMethod

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;
}
Also used : ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ActionCallMethod(org.drools.workbench.models.datamodel.rule.ActionCallMethod) ActionFieldFunction(org.drools.workbench.models.datamodel.rule.ActionFieldFunction)

Example 24 with ActionCallMethod

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));
}
Also used : BRLConditionColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLConditionColumn) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) VetoException(org.drools.workbench.screens.guided.dtable.client.widget.table.model.synchronizers.ModelSynchronizer.VetoException) BRLActionColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn) DescriptionCol52(org.drools.workbench.models.guided.dtable.shared.model.DescriptionCol52) ActionCallMethod(org.drools.workbench.models.datamodel.rule.ActionCallMethod) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) BRLActionVariableColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn) BRLConditionVariableColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLConditionVariableColumn) VetoDeletePatternInUseException(org.drools.workbench.screens.guided.dtable.client.widget.table.model.synchronizers.ModelSynchronizer.VetoDeletePatternInUseException) RowNumberCol52(org.drools.workbench.models.guided.dtable.shared.model.RowNumberCol52) Test(org.junit.Test)

Example 25 with ActionCallMethod

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);
}
Also used : ExpressionBuilder(org.drools.workbench.screens.guided.rule.client.widget.ExpressionBuilder) FromEntryPointFactPattern(org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) CompositeFactPatternWidget(org.drools.workbench.screens.guided.rule.client.widget.CompositeFactPatternWidget) RuleModellerActionPlugin(org.drools.workbench.screens.guided.rule.client.editor.plugin.RuleModellerActionPlugin) FromCompositeFactPatternWidget(org.drools.workbench.screens.guided.rule.client.widget.FromCompositeFactPatternWidget) ActionCallMethodWidget(org.drools.workbench.screens.guided.rule.client.widget.ActionCallMethodWidget) ActionInsertFactWidget(org.drools.workbench.screens.guided.rule.client.widget.ActionInsertFactWidget) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromEntryPointFactPatternWidget(org.drools.workbench.screens.guided.rule.client.widget.FromEntryPointFactPatternWidget) PortablePreconditions(org.kie.soup.commons.validation.PortablePreconditions) ExpressionFormLine(org.drools.workbench.models.datamodel.rule.ExpressionFormLine) IPattern(org.drools.workbench.models.datamodel.rule.IPattern) ActionSetFieldWidget(org.drools.workbench.screens.guided.rule.client.widget.ActionSetFieldWidget) GlobalCollectionAddWidget(org.drools.workbench.screens.guided.rule.client.widget.GlobalCollectionAddWidget) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FreeFormLine(org.drools.workbench.models.datamodel.rule.FreeFormLine) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence) IAction(org.drools.workbench.models.datamodel.rule.IAction) EventBus(com.google.gwt.event.shared.EventBus) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) FactPatternWidget(org.drools.workbench.screens.guided.rule.client.widget.FactPatternWidget) Collection(java.util.Collection) RuleModellerWidget(org.drools.workbench.screens.guided.rule.client.widget.RuleModellerWidget) FreeFormLineWidget(org.drools.workbench.screens.guided.rule.client.widget.FreeFormLineWidget) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) Collectors(java.util.stream.Collectors) FromCollectCompositeFactPatternWidget(org.drools.workbench.screens.guided.rule.client.widget.FromCollectCompositeFactPatternWidget) ActionRetractFactWidget(org.drools.workbench.screens.guided.rule.client.widget.ActionRetractFactWidget) List(java.util.List) ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) ActionRetractFact(org.drools.workbench.models.datamodel.rule.ActionRetractFact) FromAccumulateCompositeFactPatternWidget(org.drools.workbench.screens.guided.rule.client.widget.FromAccumulateCompositeFactPatternWidget) ActionGlobalCollectionAdd(org.drools.workbench.models.datamodel.rule.ActionGlobalCollectionAdd) ActionCallMethod(org.drools.workbench.models.datamodel.rule.ActionCallMethod) DSLSentenceWidget(org.drools.workbench.screens.guided.rule.client.widget.DSLSentenceWidget) Collections(java.util.Collections) GlobalCollectionAddWidget(org.drools.workbench.screens.guided.rule.client.widget.GlobalCollectionAddWidget) RuleModellerActionPlugin(org.drools.workbench.screens.guided.rule.client.editor.plugin.RuleModellerActionPlugin) ActionCallMethod(org.drools.workbench.models.datamodel.rule.ActionCallMethod) ActionInsertFactWidget(org.drools.workbench.screens.guided.rule.client.widget.ActionInsertFactWidget) ActionCallMethodWidget(org.drools.workbench.screens.guided.rule.client.widget.ActionCallMethodWidget) RuleModellerWidget(org.drools.workbench.screens.guided.rule.client.widget.RuleModellerWidget) DSLSentenceWidget(org.drools.workbench.screens.guided.rule.client.widget.DSLSentenceWidget) FreeFormLineWidget(org.drools.workbench.screens.guided.rule.client.widget.FreeFormLineWidget) ActionSetFieldWidget(org.drools.workbench.screens.guided.rule.client.widget.ActionSetFieldWidget) ActionRetractFact(org.drools.workbench.models.datamodel.rule.ActionRetractFact) ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) FreeFormLine(org.drools.workbench.models.datamodel.rule.FreeFormLine) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) ActionGlobalCollectionAdd(org.drools.workbench.models.datamodel.rule.ActionGlobalCollectionAdd) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence) ActionRetractFactWidget(org.drools.workbench.screens.guided.rule.client.widget.ActionRetractFactWidget)

Aggregations

ActionCallMethod (org.drools.workbench.models.datamodel.rule.ActionCallMethod)25 Test (org.junit.Test)19 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)17 List (java.util.List)12 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)11 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)9 MethodInfo (org.kie.soup.project.datamodel.oracle.MethodInfo)9 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)8 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)7 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)7 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)7 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)7 ActionFieldFunction (org.drools.workbench.models.datamodel.rule.ActionFieldFunction)5 FromEntryPointFactPattern (org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern)5 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)5 ActionSetField (org.drools.workbench.models.datamodel.rule.ActionSetField)3 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)3 IAction (org.drools.workbench.models.datamodel.rule.IAction)3 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)3