Search in sources :

Example 16 with ActionFieldValue

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

Example 17 with ActionFieldValue

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

the class RuleModelDRLPersistenceTest method testRHSDateInsertActionWithoutSystemProperty.

@Test
public void testRHSDateInsertActionWithoutSystemProperty() {
    // RHBRMS-3034
    String oldValue = System.getProperty("drools.dateformat");
    try {
        System.clearProperty("drools.dateformat");
        RuleModel m = new RuleModel();
        m.name = "RHS Date";
        FactPattern p = new FactPattern("Person");
        SingleFieldConstraint con = new SingleFieldConstraint();
        con.setFieldType(DataType.TYPE_DATE);
        con.setFieldName("dateOfBirth");
        con.setOperator("==");
        con.setValue("31-Jan-2000");
        con.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
        p.addConstraint(con);
        m.addLhsItem(p);
        ActionInsertFact ai = new ActionInsertFact("Birthday");
        ai.addFieldValue(new ActionFieldValue("dob", "31-Jan-2000", DataType.TYPE_DATE));
        m.addRhsItem(ai);
        String result = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
        assertTrue("result DRL : " + result, result.indexOf("java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"dd-MMM-yyyy\");") != -1);
        assertTrue(result.indexOf("fact0.setDob( sdf.parse(\"31-Jan-2000\"") != -1);
        checkMarshalling(null, m);
    } finally {
        if (oldValue == null) {
            System.clearProperty("drools.dateformat");
        } else {
            System.setProperty("drools.dateformat", oldValue);
        }
    }
}
Also used : SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) FromEntryPointFactPattern(org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 18 with ActionFieldValue

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

the class RuleModelDRLPersistenceTest method testActionCallMethod.

@Test
public void testActionCallMethod() {
    final String drl = "rule \"r0\"\n" + "dialect \"mvel\"\n" + "when\n" + "$a : Applicant( )\n" + "then\n" + "$a.addName( \"Michael\" );\n" + "end\n";
    PackageDataModelOracle dmo = mock(PackageDataModelOracle.class);
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.EMPTY_LIST, dmo);
    assertNotNull(m);
    // LHS
    assertEquals(1, m.lhs.length);
    assertTrue(m.lhs[0] instanceof FactPattern);
    final FactPattern p = (FactPattern) m.lhs[0];
    assertEquals("$a", p.getBoundName());
    assertEquals("Applicant", p.getFactType());
    // RHS
    assertEquals(1, m.rhs.length);
    assertTrue(m.rhs[0] instanceof ActionCallMethod);
    final ActionCallMethod a = (ActionCallMethod) m.rhs[0];
    assertEquals("$a", a.getVariable());
    assertEquals("addName", a.getMethodName());
    assertEquals(1, a.getFieldValues().length);
    final ActionFieldValue fv = a.getFieldValue(0);
    assertEquals("Michael", fv.getValue());
}
Also used : ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) PackageDataModelOracle(org.kie.soup.project.datamodel.oracle.PackageDataModelOracle) ActionCallMethod(org.drools.workbench.models.datamodel.rule.ActionCallMethod) FromEntryPointFactPattern(org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 19 with ActionFieldValue

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

the class RuleModelDRLPersistenceTest method testSumAsGivenValue.

@Test
public void testSumAsGivenValue() {
    // BZ-1013682
    String expected = "" + "rule \"my rule\" \n" + "  dialect \"mvel\"\n" + "  when\n" + "    m:Message()\n" + "  then\n" + "    modify( m ) {\n" + "      setText( \"Hello \" + \"world\" )\n" + "    }\n" + "end\n";
    final RuleModel m = new RuleModel();
    FactPattern factPattern = new FactPattern();
    factPattern.setFactType("Message");
    factPattern.setBoundName("m");
    m.lhs = new IPattern[] { factPattern };
    ActionUpdateField actionUpdateField = new ActionUpdateField();
    actionUpdateField.setVariable("m");
    ActionFieldValue actionFieldValue = new ActionFieldValue();
    actionFieldValue.setField("text");
    actionFieldValue.setType("String");
    actionFieldValue.setNature(FieldNatureType.TYPE_FORMULA);
    actionFieldValue.setValue("\"Hello \" + \"world\"");
    actionUpdateField.setFieldValues(new ActionFieldValue[] { actionFieldValue });
    m.rhs = new IAction[] { actionUpdateField };
    m.name = "my rule";
    checkMarshalling(expected, m);
}
Also used : ActionUpdateField(org.drools.workbench.models.datamodel.rule.ActionUpdateField) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) FromEntryPointFactPattern(org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 20 with ActionFieldValue

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

the class RuleModelDRLPersistenceTest method testRHSDateModifyAction.

@Test
public void testRHSDateModifyAction() {
    String oldValue = System.getProperty("drools.dateformat");
    try {
        System.setProperty("drools.dateformat", "dd-MMM-yyyy");
        RuleModel m = new RuleModel();
        m.name = "RHS Date";
        FactPattern p = new FactPattern("Person");
        p.setBoundName("$p");
        SingleFieldConstraint con = new SingleFieldConstraint();
        con.setFieldType(DataType.TYPE_DATE);
        con.setFieldName("dateOfBirth");
        con.setOperator("==");
        con.setValue("31-Jan-2000");
        con.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
        p.addConstraint(con);
        m.addLhsItem(p);
        ActionUpdateField am = new ActionUpdateField("$p");
        am.addFieldValue(new ActionFieldValue("dob", "31-Jan-2000", DataType.TYPE_DATE));
        m.addRhsItem(am);
        String result = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
        assertTrue(result.indexOf("java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"dd-MMM-yyyy\");") != -1);
        assertTrue(result.indexOf("setDob( sdf.parse(\"31-Jan-2000\"") != -1);
        assertTrue(result.indexOf("modify( $p ) {") != -1);
        checkMarshalling(null, m);
    } finally {
        if (oldValue == null) {
            System.clearProperty("drools.dateformat");
        } else {
            System.setProperty("drools.dateformat", oldValue);
        }
    }
}
Also used : SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) ActionUpdateField(org.drools.workbench.models.datamodel.rule.ActionUpdateField) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) FromEntryPointFactPattern(org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Aggregations

ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)136 Test (org.junit.Test)113 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)71 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)71 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)65 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)63 ActionUpdateField (org.drools.workbench.models.datamodel.rule.ActionUpdateField)53 ActionInsertFact (org.drools.workbench.models.datamodel.rule.ActionInsertFact)48 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)41 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)41 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)33 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)30 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)28 TemplateModel (org.drools.workbench.models.guided.template.shared.TemplateModel)26 IAction (org.drools.workbench.models.datamodel.rule.IAction)25 BRLActionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn)24 ActionSetField (org.drools.workbench.models.datamodel.rule.ActionSetField)23 GuidedDecisionTable52 (org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52)23 BRLActionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn)21 ArrayList (java.util.ArrayList)19