Search in sources :

Example 1 with ActionSetField

use of org.drools.workbench.models.datamodel.rule.ActionSetField in project drools by kiegroup.

the class RuleModelTest method testAddItemRhsAtSpecificPosition.

@Test
public void testAddItemRhsAtSpecificPosition() {
    final RuleModel model = new RuleModel();
    final ActionSetField a = new ActionSetField();
    model.addRhsItem(a);
    assertEquals(1, model.rhs.length);
    final ActionSetField b = new ActionSetField();
    model.addRhsItem(b);
    assertEquals(2, model.rhs.length);
    final ActionSetField c = new ActionSetField();
    model.addRhsItem(c, true);
    assertEquals(3, model.rhs.length);
    assertEquals(a, model.rhs[0]);
    assertEquals(b, model.rhs[1]);
    assertEquals(c, model.rhs[2]);
    final ActionSetField d = new ActionSetField();
    model.addRhsItem(d, false);
    assertEquals(4, model.rhs.length);
    assertEquals(d, model.rhs[0]);
    assertEquals(a, model.rhs[1]);
    assertEquals(b, model.rhs[2]);
    assertEquals(c, model.rhs[3]);
    final ActionSetField e = new ActionSetField();
    model.addRhsItem(e, 2);
    assertEquals(5, model.rhs.length);
    assertEquals(d, model.rhs[0]);
    assertEquals(a, model.rhs[1]);
    assertEquals(e, model.rhs[2]);
    assertEquals(b, model.rhs[3]);
    assertEquals(c, model.rhs[4]);
    // test auto-bound
    final ActionSetField f = new ActionSetField();
    final ActionSetField g = new ActionSetField();
    model.addRhsItem(f, -1);
    model.addRhsItem(g, 100);
    assertEquals(7, model.rhs.length);
    assertEquals(f, model.rhs[0]);
    assertEquals(d, model.rhs[1]);
    assertEquals(a, model.rhs[2]);
    assertEquals(e, model.rhs[3]);
    assertEquals(b, model.rhs[4]);
    assertEquals(c, model.rhs[5]);
    assertEquals(g, model.rhs[6]);
    model.moveRhsItemDown(5);
    model.moveRhsItemUp(3);
    assertEquals(7, model.rhs.length);
    assertEquals(f, model.rhs[0]);
    assertEquals(d, model.rhs[1]);
    assertEquals(e, model.rhs[2]);
    assertEquals(a, model.rhs[3]);
    assertEquals(b, model.rhs[4]);
    assertEquals(g, model.rhs[5]);
    assertEquals(c, model.rhs[6]);
    model.moveRhsItemUp(0);
    model.moveRhsItemDown(6);
    assertEquals(7, model.rhs.length);
    assertEquals(f, model.rhs[0]);
    assertEquals(d, model.rhs[1]);
    assertEquals(e, model.rhs[2]);
    assertEquals(a, model.rhs[3]);
    assertEquals(b, model.rhs[4]);
    assertEquals(g, model.rhs[5]);
    assertEquals(c, model.rhs[6]);
}
Also used : ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 2 with ActionSetField

use of org.drools.workbench.models.datamodel.rule.ActionSetField in project drools by kiegroup.

the class RuleModelTest method testAddItemRhs.

@Test
public void testAddItemRhs() {
    final RuleModel model = new RuleModel();
    final IAction a0 = new ActionSetField();
    final IAction a1 = new ActionSetField();
    model.addRhsItem(a0);
    assertEquals(1, model.rhs.length);
    model.addRhsItem(a1);
    assertEquals(2, model.rhs.length);
    assertEquals(a0, model.rhs[0]);
    assertEquals(a1, model.rhs[1]);
}
Also used : ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) IAction(org.drools.workbench.models.datamodel.rule.IAction) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 3 with ActionSetField

use of org.drools.workbench.models.datamodel.rule.ActionSetField in project drools by kiegroup.

the class RuleModelTest method testIsDSLEnhanced.

@Test
public void testIsDSLEnhanced() throws Exception {
    RuleModel m = new RuleModel();
    assertFalse(m.hasDSLSentences());
    m.addLhsItem(new FactPattern());
    assertFalse(m.hasDSLSentences());
    m.addRhsItem(new ActionSetField("q"));
    assertFalse(m.hasDSLSentences());
    m.addLhsItem(new DSLSentence());
    assertTrue(m.hasDSLSentences());
    m.addRhsItem(new DSLSentence());
    assertTrue(m.hasDSLSentences());
    m = new RuleModel();
    m.addLhsItem(new DSLSentence());
    assertTrue(m.hasDSLSentences());
    m = new RuleModel();
    m.addRhsItem(new DSLSentence());
    assertTrue(m.hasDSLSentences());
}
Also used : ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence) Test(org.junit.Test)

Example 4 with ActionSetField

use of org.drools.workbench.models.datamodel.rule.ActionSetField in project drools by kiegroup.

the class RuleModelDRLPersistenceImpl method parseRhs.

private void parseRhs(final RuleModel m, final String rhs, final boolean isJavaDialect, final Map<String, String> boundParams, final ExpandedDRLInfo expandedDRLInfo, final PackageDataModelOracle dmo, final Collection<RuleModelIActionPersistenceExtension> extensions) throws RuleModelDRLPersistenceException {
    PortableWorkDefinition pwd = null;
    Map<String, List<String>> setStatements = new HashMap<String, List<String>>();
    Map<String, Integer> setStatementsPosition = new HashMap<String, Integer>();
    Map<String, String> factsType = new HashMap<String, String>();
    String modifiedVariable = null;
    String modifiers = null;
    int lineCounter = -1;
    String[] lines = rhs.split("\n");
    for (String line : lines) {
        lineCounter++;
        line = line.trim();
        List<RuleModelIActionPersistenceExtension> matchingExtensions = getMatchingExtensionsForLine(line, extensions);
        if (matchingExtensions.isEmpty()) {
        // Continue with hardcoded parsers
        } else if (matchingExtensions.size() > 1) {
            throw new RuleModelDRLPersistenceException("Ambiguous RuleModelIActionPersistenceExtension implementations (" + matchingExtensions + ") found for line " + line);
        } else {
            unmarshalUsingExtension(m, matchingExtensions.get(0), line);
            continue;
        }
        if (expandedDRLInfo.hasDsl) {
            String dslLine = expandedDRLInfo.dslStatementsInRhs.get(lineCounter);
            while (dslLine != null) {
                List<RuleModelIActionPersistenceExtension> matchingExtensionsDslLine = getMatchingExtensionsForLine(dslLine, extensions);
                if (matchingExtensionsDslLine.isEmpty()) {
                    m.addRhsItem(toDSLSentence(expandedDRLInfo.rhsDslPatterns, dslLine));
                } else if (matchingExtensionsDslLine.size() > 1) {
                    throw new RuleModelDRLPersistenceException("Ambiguous RuleModelIActionPersistenceExtension implementations (" + matchingExtensionsDslLine + ") found for line " + line);
                } else {
                    unmarshalUsingExtension(m, matchingExtensionsDslLine.get(0), dslLine);
                }
                dslLine = expandedDRLInfo.dslStatementsInRhs.get(++lineCounter);
            }
        }
        if (modifiedVariable != null) {
            int modifyBlockEnd = line.lastIndexOf('}');
            if (modifiers == null) {
                modifiers = modifyBlockEnd > 0 ? line.substring(line.indexOf('{') + 1, modifyBlockEnd).trim() : line.substring(line.indexOf('{') + 1).trim();
            } else if (modifyBlockEnd != 0) {
                modifiers += modifyBlockEnd > 0 ? line.substring(0, modifyBlockEnd).trim() : line;
            }
            if (modifyBlockEnd >= 0) {
                ActionUpdateField action = new ActionUpdateField();
                action.setVariable(modifiedVariable);
                m.addRhsItem(action);
                addModifiersToAction(modifiers, action, modifiedVariable, boundParams, dmo, m, isJavaDialect);
                modifiedVariable = null;
                modifiers = null;
            }
        } else if (line.startsWith("insertLogical")) {
            String fact = unwrapParenthesis(line);
            String type = getStatementType(fact, factsType);
            if (type != null) {
                boundParams.put(fact, type);
                ActionInsertLogicalFact action = new ActionInsertLogicalFact(type);
                m.addRhsItem(action);
                if (factsType.containsKey(fact)) {
                    action.setBoundName(fact);
                    addSettersToAction(setStatements, fact, action, boundParams, dmo, m, isJavaDialect);
                }
            }
        } else if (line.startsWith("insert")) {
            String fact = unwrapParenthesis(line);
            String type = getStatementType(fact, factsType);
            if (type != null) {
                boundParams.put(fact, type);
                ActionInsertFact action = new ActionInsertFact(type);
                m.addRhsItem(action);
                if (factsType.containsKey(fact)) {
                    action.setBoundName(fact);
                    addSettersToAction(setStatements, fact, action, boundParams, dmo, m, isJavaDialect);
                }
            }
        } else if (line.startsWith("update")) {
            String variable = unwrapParenthesis(line);
            ActionUpdateField action = new ActionUpdateField();
            action.setVariable(variable);
            m.addRhsItem(action);
            addSettersToAction(setStatements, variable, action, boundParams, dmo, m, isJavaDialect);
        } else if (line.startsWith("modify")) {
            int modifyBlockEnd = line.lastIndexOf('}');
            if (modifyBlockEnd > 0) {
                String variable = line.substring(line.indexOf('(') + 1, line.indexOf(')')).trim();
                ActionUpdateField action = new ActionUpdateField();
                action.setVariable(variable);
                m.addRhsItem(action);
                addModifiersToAction(line.substring(line.indexOf('{') + 1, modifyBlockEnd).trim(), action, variable, boundParams, dmo, m, isJavaDialect);
            } else {
                modifiedVariable = line.substring(line.indexOf('(') + 1, line.indexOf(')')).trim();
                int modifyBlockStart = line.indexOf('{');
                if (modifyBlockStart > 0) {
                    modifiers = line.substring(modifyBlockStart + 1).trim();
                }
            }
        } else if (line.startsWith("retract") || line.startsWith("delete")) {
            String variable = unwrapParenthesis(line);
            m.addRhsItem(new ActionRetractFact(variable));
        } else if (line.startsWith("org.drools.core.process.instance.impl.WorkItemImpl wiWorkItem")) {
            ActionExecuteWorkItem awi = new ActionExecuteWorkItem();
            pwd = new PortableWorkDefinition();
            pwd.setName("WorkItem");
            awi.setWorkDefinition(pwd);
            m.addRhsItem(awi);
        } else if (line.startsWith("wiWorkItem.getParameters().put")) {
            String statement = line.substring("wiWorkItem.getParameters().put".length());
            statement = unwrapParenthesis(statement);
            int commaPos = statement.indexOf(',');
            String name = statement.substring(0, commaPos).trim();
            String value = statement.substring(commaPos + 1).trim();
            pwd.addParameter(buildPortableParameterDefinition(name, value, boundParams));
        } else if (line.startsWith("wim.internalExecuteWorkItem") || line.startsWith("wiWorkItem.setName")) {
        // ignore
        } else {
            int dotPos = line.indexOf('.');
            int argStart = line.indexOf('(');
            if (dotPos > 0 && argStart > dotPos) {
                String variable = line.substring(0, dotPos).trim();
                if (boundParams.containsKey(variable) || factsType.containsKey(variable) || expandedDRLInfo.hasGlobal(variable)) {
                    if (isJavaIdentifier(variable)) {
                        String methodName = line.substring(dotPos + 1, argStart).trim();
                        if (isJavaIdentifier(methodName)) {
                            if (getSettedField(m, methodName, boundParams.get(variable), dmo) != null) {
                                List<String> setters = setStatements.get(variable);
                                if (setters == null) {
                                    setters = new ArrayList<String>();
                                    setStatements.put(variable, setters);
                                }
                                if (!setStatementsPosition.containsKey(variable)) {
                                    setStatementsPosition.put(variable, lineCounter);
                                }
                                setters.add(line);
                            } else if (methodName.equals("add") && expandedDRLInfo.hasGlobal(variable)) {
                                String factName = line.substring(argStart + 1, line.lastIndexOf(')')).trim();
                                ActionGlobalCollectionAdd actionGlobalCollectionAdd = new ActionGlobalCollectionAdd();
                                actionGlobalCollectionAdd.setGlobalName(variable);
                                actionGlobalCollectionAdd.setFactName(factName);
                                m.addRhsItem(actionGlobalCollectionAdd);
                            } else {
                                m.addRhsItem(getActionCallMethod(m, isJavaDialect, boundParams, dmo, line, variable, methodName));
                            }
                            continue;
                        }
                    }
                }
            }
            int eqPos = line.indexOf('=');
            boolean addFreeFormLine = line.trim().length() > 0;
            if (eqPos > 0) {
                String field = line.substring(0, eqPos).trim();
                if ("java.text.SimpleDateFormat sdf".equals(field) || "org.drools.core.process.instance.WorkItemManager wim".equals(field)) {
                    addFreeFormLine = false;
                }
                String[] split = field.split(" ");
                if (split.length == 2) {
                    factsType.put(split[1], split[0]);
                    addFreeFormLine &= !isInsertedFact(lines, lineCounter, split[1]);
                }
            }
            if (addFreeFormLine) {
                FreeFormLine ffl = new FreeFormLine();
                ffl.setText(line);
                m.addRhsItem(ffl);
            }
        }
    }
    // variable they are modifying was recorded as Free Format DRL and hence the "sets" need to be Free Format DRL too.
    for (Map.Entry<String, List<String>> entry : setStatements.entrySet()) {
        if (boundParams.containsKey(entry.getKey())) {
            ActionSetField action = new ActionSetField(entry.getKey());
            addSettersToAction(entry.getValue(), action, entry.getKey(), boundParams, dmo, m, isJavaDialect);
            m.addRhsItem(action, setStatementsPosition.get(entry.getKey()));
        } else {
            FreeFormLine action = new FreeFormLine();
            StringBuilder sb = new StringBuilder();
            for (String setter : entry.getValue()) {
                sb.append(setter).append("\n");
            }
            action.setText(sb.toString());
            m.addRhsItem(action, setStatementsPosition.get(entry.getKey()));
        }
    }
    if (expandedDRLInfo.hasDsl) {
        String dslLine = expandedDRLInfo.dslStatementsInRhs.get(++lineCounter);
        while (dslLine != null) {
            m.addRhsItem(toDSLSentence(expandedDRLInfo.rhsDslPatterns, dslLine));
            dslLine = expandedDRLInfo.dslStatementsInRhs.get(++lineCounter);
        }
    }
}
Also used : HashMap(java.util.HashMap) ActionRetractFact(org.drools.workbench.models.datamodel.rule.ActionRetractFact) ActionUpdateField(org.drools.workbench.models.datamodel.rule.ActionUpdateField) PortableWorkDefinition(org.drools.workbench.models.datamodel.workitems.PortableWorkDefinition) ArrayList(java.util.ArrayList) StringUtils.splitArgumentsList(org.drools.core.util.StringUtils.splitArgumentsList) List(java.util.List) ActionFieldList(org.drools.workbench.models.datamodel.rule.ActionFieldList) ActionExecuteWorkItem(org.drools.workbench.models.datamodel.rule.ActionExecuteWorkItem) RuleModelDRLPersistenceException(org.drools.workbench.models.commons.backend.rule.exception.RuleModelDRLPersistenceException) 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) FreeFormLine(org.drools.workbench.models.datamodel.rule.FreeFormLine) ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) ActionGlobalCollectionAdd(org.drools.workbench.models.datamodel.rule.ActionGlobalCollectionAdd) Map(java.util.Map) HashMap(java.util.HashMap) ActionInsertLogicalFact(org.drools.workbench.models.datamodel.rule.ActionInsertLogicalFact)

Example 5 with ActionSetField

use of org.drools.workbench.models.datamodel.rule.ActionSetField 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

ActionSetField (org.drools.workbench.models.datamodel.rule.ActionSetField)39 Test (org.junit.Test)27 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)23 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)21 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)21 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)17 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)13 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)12 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)11 ArrayList (java.util.ArrayList)10 IAction (org.drools.workbench.models.datamodel.rule.IAction)10 ActionInsertFact (org.drools.workbench.models.datamodel.rule.ActionInsertFact)9 ActionUpdateField (org.drools.workbench.models.datamodel.rule.ActionUpdateField)9 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)9 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)8 BRLActionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn)7 GuidedDecisionTable52 (org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52)7 ActionRetractFact (org.drools.workbench.models.datamodel.rule.ActionRetractFact)6 FromEntryPointFactPattern (org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern)6 BRLActionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn)6