Search in sources :

Example 6 with ActionCallMethod

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

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

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

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

Example 10 with 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);
            }
        }
    }
}
Also used : PluggableIAction(org.drools.workbench.models.datamodel.rule.PluggableIAction) IAction(org.drools.workbench.models.datamodel.rule.IAction) PluggableIAction(org.drools.workbench.models.datamodel.rule.PluggableIAction) ArrayList(java.util.ArrayList) ActionCallMethod(org.drools.workbench.models.datamodel.rule.ActionCallMethod) FieldConstraint(org.drools.workbench.models.datamodel.rule.FieldConstraint) ConnectiveConstraint(org.drools.workbench.models.datamodel.rule.ConnectiveConstraint) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) ActionUpdateField(org.drools.workbench.models.datamodel.rule.ActionUpdateField) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ArrayList(java.util.ArrayList) StringUtils.splitArgumentsList(org.drools.core.util.StringUtils.splitArgumentsList) List(java.util.List) ActionFieldList(org.drools.workbench.models.datamodel.rule.ActionFieldList)

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