Search in sources :

Example 11 with ActionSetField

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

the class GuidedDTDRLPersistence method doAction.

private void doAction(List<LabelledAction> actions, ActionWorkItemSetFieldCol52 sf, String cell) {
    if (Boolean.TRUE.equals(Boolean.parseBoolean(cell))) {
        LabelledAction a = findByLabelledAction(actions, sf.getBoundName());
        if (a == null) {
            a = new LabelledAction();
            a.boundName = sf.getBoundName();
            if (!sf.isUpdate()) {
                a.action = new ActionSetField(sf.getBoundName());
            } else {
                a.action = new ActionUpdateField(sf.getBoundName());
            }
            actions.add(a);
        } else if (sf.isUpdate() && !(a.action instanceof ActionUpdateField)) {
            // lets swap it out for an update as the user has asked for it.
            ActionSetField old = (ActionSetField) a.action;
            ActionUpdateField update = new ActionUpdateField(sf.getBoundName());
            update.setFieldValues(old.getFieldValues());
            a.action = update;
        }
        ActionSetField asf = (ActionSetField) a.action;
        ActionWorkItemFieldValue val = new ActionWorkItemFieldValue(sf.getFactField(), sf.getType(), sf.getWorkItemName(), sf.getWorkItemResultParameterName(), sf.getParameterClassName());
        asf.addFieldValue(val);
    }
}
Also used : ActionWorkItemFieldValue(org.drools.workbench.models.datamodel.rule.ActionWorkItemFieldValue) ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) ActionUpdateField(org.drools.workbench.models.datamodel.rule.ActionUpdateField)

Example 12 with ActionSetField

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

the class BRLRuleModelTest method testRuleModelWithRHSBoundFactsUsageWithBRLActionColumn.

@Test
public void testRuleModelWithRHSBoundFactsUsageWithBRLActionColumn() {
    final GuidedDecisionTable52 dt = new GuidedDecisionTable52();
    // Setup Decision Table columns
    final Pattern52 p1 = new Pattern52();
    p1.setFactType("Driver");
    p1.setBoundName("$d");
    dt.getConditions().add(p1);
    final BRLActionColumn brl = new BRLActionColumn();
    brl.setDefinition(Collections.singletonList(new ActionSetField() {

        {
            setVariable("$d");
        }
    }));
    dt.getActionCols().add(brl);
    final BRLRuleModel model = new BRLRuleModel(dt);
    // Checks
    assertTrue(model.isBoundFactUsed("$d"));
    assertFalse(model.isBoundFactUsed("$cheese"));
}
Also used : BRLActionColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn) ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) GuidedDecisionTable52(org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52) Pattern52(org.drools.workbench.models.guided.dtable.shared.model.Pattern52) BRLRuleModel(org.drools.workbench.models.guided.dtable.shared.model.BRLRuleModel) Test(org.junit.Test)

Example 13 with ActionSetField

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

the class GuidedDecisionTreeModelUnmarshallingVisitor method visit.

private List<Node> visit(final IAction a, final List<TypeNode> types, final GuidedDecisionTree model, final PackageDataModelOracle dmo, final List<ParserMessage> messages) {
    final List<Node> nodes = new ArrayList<Node>();
    if (a instanceof ActionRetractFact) {
        final ActionRetractFact arf = (ActionRetractFact) a;
        final String binding = arf.getVariableName();
        for (TypeNode tn : types) {
            if (tn.isBound()) {
                if (tn.getBinding().equals(binding)) {
                    final ActionRetractNode arn = new ActionRetractNodeImpl(tn);
                    nodes.add(arn);
                    return nodes;
                }
            }
        }
        messages.add(new BindingNotFoundParserMessage(binding));
        return nodes;
    } else if (a instanceof ActionInsertLogicalFact) {
        final ActionInsertLogicalFact aif = (ActionInsertLogicalFact) a;
        final ActionInsertNode aun = new ActionInsertNodeImpl(aif.getFactType());
        aun.setLogicalInsertion(true);
        for (org.drools.workbench.models.datamodel.rule.ActionFieldValue afv : aif.getFieldValues()) {
            if (afv.getNature() != FieldNatureType.TYPE_LITERAL) {
                messages.add(new UnsupportedFieldNatureTypeParserMessage());
                return nodes;
            }
            final String fieldName = afv.getField();
            final Value value = getValue(aif.getFactType(), afv.getField(), model, dmo, messages, afv.getValue());
            if (value != null) {
                final ActionFieldValue _afv = new ActionFieldValueImpl(fieldName, value);
                aun.getFieldValues().add(_afv);
            }
        }
        nodes.add(aun);
        return nodes;
    } else if (a instanceof ActionInsertFact) {
        final ActionInsertFact aif = (ActionInsertFact) a;
        final ActionInsertNode aun = new ActionInsertNodeImpl(aif.getFactType());
        aun.setLogicalInsertion(false);
        for (org.drools.workbench.models.datamodel.rule.ActionFieldValue afv : aif.getFieldValues()) {
            if (afv.getNature() != FieldNatureType.TYPE_LITERAL) {
                messages.add(new UnsupportedFieldNatureTypeParserMessage());
                return nodes;
            }
            final String fieldName = afv.getField();
            final Value value = getValue(aif.getFactType(), afv.getField(), model, dmo, messages, afv.getValue());
            if (value != null) {
                final ActionFieldValue _afv = new ActionFieldValueImpl(fieldName, value);
                aun.getFieldValues().add(_afv);
            }
        }
        nodes.add(aun);
        return nodes;
    } else if (a instanceof ActionUpdateField) {
        final ActionUpdateField auf = (ActionUpdateField) a;
        final String binding = auf.getVariable();
        for (TypeNode tn : types) {
            if (tn.isBound()) {
                if (tn.getBinding().equals(binding)) {
                    final ActionUpdateNode aun = new ActionUpdateNodeImpl(tn);
                    aun.setModify(true);
                    for (org.drools.workbench.models.datamodel.rule.ActionFieldValue afv : auf.getFieldValues()) {
                        if (afv.getNature() != FieldNatureType.TYPE_LITERAL) {
                            messages.add(new UnsupportedFieldNatureTypeParserMessage());
                            return nodes;
                        }
                        final String fieldName = afv.getField();
                        final Value value = getValue(tn.getClassName(), afv.getField(), model, dmo, messages, afv.getValue());
                        if (value != null) {
                            final ActionFieldValue _afv = new ActionFieldValueImpl(fieldName, value);
                            aun.getFieldValues().add(_afv);
                        }
                    }
                    nodes.add(aun);
                    return nodes;
                }
            }
        }
        messages.add(new BindingNotFoundParserMessage(binding));
        return nodes;
    } else if (a instanceof ActionSetField) {
        final ActionSetField asf = (ActionSetField) a;
        final String binding = asf.getVariable();
        for (TypeNode tn : types) {
            if (tn.isBound()) {
                if (tn.getBinding().equals(binding)) {
                    final ActionUpdateNode aun = new ActionUpdateNodeImpl(tn);
                    for (org.drools.workbench.models.datamodel.rule.ActionFieldValue afv : asf.getFieldValues()) {
                        if (afv.getNature() != FieldNatureType.TYPE_LITERAL) {
                            messages.add(new UnsupportedFieldNatureTypeParserMessage());
                            return nodes;
                        }
                        final String fieldName = afv.getField();
                        final Value value = getValue(tn.getClassName(), afv.getField(), model, dmo, messages, afv.getValue());
                        if (value != null) {
                            final ActionFieldValue _afv = new ActionFieldValueImpl(fieldName, value);
                            aun.getFieldValues().add(_afv);
                        }
                    }
                    nodes.add(aun);
                    return nodes;
                }
            }
        }
        messages.add(new BindingNotFoundParserMessage(binding));
        return nodes;
    } else {
        messages.add(new UnsupportedIActionParserMessage());
        return nodes;
    }
}
Also used : ActionInsertNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionInsertNode) ActionRetractNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionRetractNode) TypeNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.TypeNode) Node(org.drools.workbench.models.guided.dtree.shared.model.nodes.Node) ActionUpdateNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionUpdateNode) ActionInsertNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionInsertNode) ConstraintNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ConstraintNode) ArrayList(java.util.ArrayList) ActionRetractNodeImpl(org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.ActionRetractNodeImpl) ActionInsertNodeImpl(org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.ActionInsertNodeImpl) ActionRetractFact(org.drools.workbench.models.datamodel.rule.ActionRetractFact) ActionUpdateField(org.drools.workbench.models.datamodel.rule.ActionUpdateField) BindingNotFoundParserMessage(org.drools.workbench.models.guided.dtree.shared.model.parser.messages.BindingNotFoundParserMessage) ActionFieldValue(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionFieldValue) ActionRetractNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionRetractNode) ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) UnsupportedFieldNatureTypeParserMessage(org.drools.workbench.models.guided.dtree.shared.model.parser.messages.UnsupportedFieldNatureTypeParserMessage) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) ActionUpdateNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionUpdateNode) ShortValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.ShortValue) StringValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.StringValue) FloatValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.FloatValue) BigDecimalValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.BigDecimalValue) ByteValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.ByteValue) Value(org.drools.workbench.models.guided.dtree.shared.model.values.Value) DoubleValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.DoubleValue) IntegerValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.IntegerValue) DateValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.DateValue) BooleanValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.BooleanValue) BigIntegerValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.BigIntegerValue) LongValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.LongValue) ActionFieldValue(org.drools.workbench.models.guided.dtree.shared.model.nodes.ActionFieldValue) EnumValue(org.drools.workbench.models.guided.dtree.shared.model.values.impl.EnumValue) ActionUpdateNodeImpl(org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.ActionUpdateNodeImpl) TypeNode(org.drools.workbench.models.guided.dtree.shared.model.nodes.TypeNode) ActionFieldValueImpl(org.drools.workbench.models.guided.dtree.shared.model.nodes.impl.ActionFieldValueImpl) UnsupportedIActionParserMessage(org.drools.workbench.models.guided.dtree.shared.model.parser.messages.UnsupportedIActionParserMessage) ActionInsertLogicalFact(org.drools.workbench.models.datamodel.rule.ActionInsertLogicalFact)

Example 14 with ActionSetField

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

the class RuleTemplateModelDRLPersistenceTest method testActionUpdateFactFirstValue.

@Test
public void testActionUpdateFactFirstValue() {
    TemplateModel m = new TemplateModel();
    m.name = "r1";
    FactPattern fp = new FactPattern("Person");
    fp.setBoundName("$p");
    m.addLhsItem(fp);
    ActionSetField asf = new ActionSetField("$p");
    ActionFieldValue afv0 = new ActionFieldValue();
    afv0.setNature(FieldNatureType.TYPE_TEMPLATE);
    afv0.setField("field1");
    afv0.setValue("$f1");
    asf.addFieldValue(afv0);
    ActionFieldValue afv1 = new ActionFieldValue();
    afv1.setNature(FieldNatureType.TYPE_TEMPLATE);
    afv1.setField("field2");
    afv1.setValue("$f2");
    asf.addFieldValue(afv1);
    m.addRhsItem(asf);
    String expected = "rule \"r1_0\"\n" + "dialect \"mvel\"\n" + "when\n" + "$p : Person()\n" + "then\n" + "$p.setField1(\"foo\");\n" + "end";
    m.addRow(new String[] { "foo", null });
    checkMarshall(expected, m);
}
Also used : ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) TemplateModel(org.drools.workbench.models.guided.template.shared.TemplateModel) Test(org.junit.Test)

Example 15 with ActionSetField

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

the class GuidedRuleTemplateFactory method makeModelWithActions.

public static TemplateModel makeModelWithActions(final String packageName, final Collection<Import> imports, final String name) {
    final TemplateModel model = new TemplateModel();
    model.getImports().getImports().addAll(imports);
    model.setPackageName(packageName);
    model.name = name;
    final ActionInsertFact ifc1 = new ActionInsertFact();
    ifc1.setFactType("Applicant");
    ifc1.setBoundName("$a");
    final ActionFieldValue afv1 = new ActionFieldValue();
    afv1.setNature(FieldNatureType.TYPE_TEMPLATE);
    afv1.setField("age");
    afv1.setValue("f1");
    ifc1.addFieldValue(afv1);
    model.addRhsItem(ifc1);
    final ActionInsertFact ifc2 = new ActionInsertFact();
    ifc2.setFactType("Mortgage");
    ifc2.setBoundName("$m");
    final ActionFieldValue afv2 = new ActionFieldValue();
    afv2.setNature(FieldNatureType.TYPE_TEMPLATE);
    afv2.setField("amount");
    afv2.setValue("f2");
    ifc2.addFieldValue(afv2);
    model.addRhsItem(ifc2);
    final ActionSetField asf = new ActionSetField();
    asf.setVariable("$a");
    asf.addFieldValue(new ActionFieldValue("age", "33", DataType.TYPE_NUMERIC_INTEGER));
    model.addRhsItem(asf);
    final ActionUpdateField auf = new ActionUpdateField();
    asf.setVariable("$m");
    asf.addFieldValue(new ActionFieldValue("amount", "10000", DataType.TYPE_NUMERIC_INTEGER));
    model.addRhsItem(auf);
    model.addRow(new String[] { "33", null });
    return model;
}
Also used : ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) ActionUpdateField(org.drools.workbench.models.datamodel.rule.ActionUpdateField) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) TemplateModel(org.drools.workbench.models.guided.template.shared.TemplateModel)

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