Search in sources :

Example 6 with ActionSetField

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

the class RuleModelDRLPersistenceTest method testRHSActionWorkItemSetFields.

@Test
public // Test that WorkItem Parameters can be used to set fields on existing Facts
void testRHSActionWorkItemSetFields() {
    RuleModel m = new RuleModel();
    m.name = "WorkItem";
    FactPattern fp1 = new FactPattern("Results");
    fp1.setBoundName("$r");
    m.addLhsItem(fp1);
    ActionExecuteWorkItem awi = new ActionExecuteWorkItem();
    PortableWorkDefinition pwd = new PortableWorkDefinition();
    pwd.setName("WorkItem");
    awi.setWorkDefinition(pwd);
    PortableBooleanParameterDefinition p1 = new PortableBooleanParameterDefinition();
    p1.setName("BooleanResult");
    pwd.addResult(p1);
    PortableFloatParameterDefinition p2 = new PortableFloatParameterDefinition();
    p2.setName("FloatResult");
    pwd.addResult(p2);
    PortableIntegerParameterDefinition p3 = new PortableIntegerParameterDefinition();
    p3.setName("IntegerResult");
    pwd.addResult(p3);
    PortableStringParameterDefinition p4 = new PortableStringParameterDefinition();
    p4.setName("StringResult");
    pwd.addResult(p4);
    m.addRhsItem(awi);
    ActionSetField asf = new ActionSetField();
    asf.setVariable("$r");
    ActionWorkItemFieldValue fv1 = new ActionWorkItemFieldValue("ResultsBooleanResult", DataType.TYPE_BOOLEAN, "WorkItem", "BooleanResult", Boolean.class.getName());
    asf.addFieldValue(fv1);
    ActionWorkItemFieldValue fv2 = new ActionWorkItemFieldValue("ResultsFloatResult", DataType.TYPE_NUMERIC_FLOAT, "WorkItem", "FloatResult", Float.class.getName());
    asf.addFieldValue(fv2);
    ActionWorkItemFieldValue fv3 = new ActionWorkItemFieldValue("ResultsIntegerResult", DataType.TYPE_NUMERIC_INTEGER, "WorkItem", "IntegerResult", Integer.class.getName());
    asf.addFieldValue(fv3);
    ActionWorkItemFieldValue fv4 = new ActionWorkItemFieldValue("ResultsStringResult", DataType.TYPE_STRING, "WorkItem", "StringResult", String.class.getName());
    asf.addFieldValue(fv4);
    m.addRhsItem(asf);
    String result = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
    assertTrue(result.indexOf("org.drools.core.process.instance.WorkItemManager wim = (org.drools.core.process.instance.WorkItemManager) drools.getWorkingMemory().getWorkItemManager();") != -1);
    assertTrue(result.indexOf("org.drools.core.process.instance.impl.WorkItemImpl wiWorkItem = new org.drools.core.process.instance.impl.WorkItemImpl();") != -1);
    assertTrue(result.indexOf("$r.setResultsBooleanResult( (java.lang.Boolean) wiWorkItem.getResult( \"BooleanResult\" ) );") != -1);
    assertTrue(result.indexOf("$r.setResultsFloatResult( (java.lang.Float) wiWorkItem.getResult( \"FloatResult\" ) );") != -1);
    assertTrue(result.indexOf("$r.setResultsIntegerResult( (java.lang.Integer) wiWorkItem.getResult( \"IntegerResult\" ) );") != -1);
    assertTrue(result.indexOf("$r.setResultsStringResult( (java.lang.String) wiWorkItem.getResult( \"StringResult\" ) );") != -1);
    assertTrue(result.indexOf("wim.internalExecuteWorkItem( wiWorkItem );") != -1);
    checkMarshalling(null, m);
}
Also used : PortableIntegerParameterDefinition(org.drools.workbench.models.datamodel.workitems.PortableIntegerParameterDefinition) ActionWorkItemFieldValue(org.drools.workbench.models.datamodel.rule.ActionWorkItemFieldValue) PortableFloatParameterDefinition(org.drools.workbench.models.datamodel.workitems.PortableFloatParameterDefinition) 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) PortableStringParameterDefinition(org.drools.workbench.models.datamodel.workitems.PortableStringParameterDefinition) ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) PortableWorkDefinition(org.drools.workbench.models.datamodel.workitems.PortableWorkDefinition) PortableBooleanParameterDefinition(org.drools.workbench.models.datamodel.workitems.PortableBooleanParameterDefinition) ActionExecuteWorkItem(org.drools.workbench.models.datamodel.rule.ActionExecuteWorkItem) Test(org.junit.Test)

Example 7 with ActionSetField

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

the class RuleModelDRLPersistenceTest method testRHSChangeMultipleFieldsBlockModify.

@Test
public void testRHSChangeMultipleFieldsBlockModify() {
    String expected = "" + "rule \"my rule\" \n" + "  dialect \"mvel\"\n" + "  when\n" + "    $p : Person()\n" + "  then\n" + "    modify( $p ) {\n" + "      setName( \"Fred\" ),\n" + "      setAge( 55 )\n" + "    }\n" + "    $p.setGender( \"X\" );" + "end\n";
    final RuleModel m = new RuleModel();
    FactPattern factPattern = new FactPattern();
    factPattern.setFactType("Person");
    factPattern.setBoundName("$p");
    m.lhs = new IPattern[] { factPattern };
    ActionUpdateField auf1 = new ActionUpdateField();
    auf1.setVariable("$p");
    ActionFieldValue afv1 = new ActionFieldValue();
    afv1.setField("name");
    afv1.setType(DataType.TYPE_STRING);
    afv1.setNature(FieldNatureType.TYPE_LITERAL);
    afv1.setValue("Fred");
    auf1.setFieldValues(new ActionFieldValue[] { afv1 });
    ActionSetField asf = new ActionSetField();
    asf.setVariable("$p");
    ActionFieldValue afv2 = new ActionFieldValue();
    afv2.setField("gender");
    afv2.setType(DataType.TYPE_STRING);
    afv2.setNature(FieldNatureType.TYPE_LITERAL);
    afv2.setValue("X");
    asf.setFieldValues(new ActionFieldValue[] { afv2 });
    ActionUpdateField auf2 = new ActionUpdateField();
    auf2.setVariable("$p");
    ActionFieldValue afv3 = new ActionFieldValue();
    afv3.setField("age");
    afv3.setType(DataType.TYPE_NUMERIC_INTEGER);
    afv3.setNature(FieldNatureType.TYPE_LITERAL);
    afv3.setValue("55");
    auf2.setFieldValues(new ActionFieldValue[] { afv3 });
    m.rhs = new IAction[] { auf1, asf, auf2 };
    m.name = "my rule";
    checkMarshalling(expected, m);
}
Also used : ActionUpdateField(org.drools.workbench.models.datamodel.rule.ActionUpdateField) ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) 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 8 with ActionSetField

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

the class RuleModelDRLPersistenceTest method testRHSChangeMultipleFieldsModifyOneUpdateOther.

@Test
public void testRHSChangeMultipleFieldsModifyOneUpdateOther() {
    String expected = "" + "rule \"my rule\" \n" + "  dialect \"mvel\"\n" + "  when\n" + "    $p : Person()\n" + "  then\n" + "    modify( $p ) {\n" + "      setName( \"Fred\" )\n" + "    }\n" + "  $p.setAge( 55 );\n" + "end\n";
    final RuleModel m = new RuleModel();
    FactPattern factPattern = new FactPattern();
    factPattern.setFactType("Person");
    factPattern.setBoundName("$p");
    m.lhs = new IPattern[] { factPattern };
    ActionUpdateField auf = new ActionUpdateField();
    auf.setVariable("$p");
    ActionFieldValue afv1 = new ActionFieldValue();
    afv1.setField("name");
    afv1.setType(DataType.TYPE_STRING);
    afv1.setNature(FieldNatureType.TYPE_LITERAL);
    afv1.setValue("Fred");
    auf.setFieldValues(new ActionFieldValue[] { afv1 });
    ActionSetField asf = new ActionSetField();
    asf.setVariable("$p");
    ActionFieldValue afv2 = new ActionFieldValue();
    afv2.setField("age");
    afv2.setType(DataType.TYPE_NUMERIC_INTEGER);
    afv2.setNature(FieldNatureType.TYPE_LITERAL);
    afv2.setValue("55");
    asf.setFieldValues(new ActionFieldValue[] { afv2 });
    m.rhs = new IAction[] { auf, asf };
    m.name = "my rule";
    checkMarshalling(expected, m);
}
Also used : ActionUpdateField(org.drools.workbench.models.datamodel.rule.ActionUpdateField) ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) 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 9 with ActionSetField

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

the class GuidedDTDRLPersistence method addAction.

private void addAction(IAction action, List<LabelledAction> actions) {
    String binding = null;
    if (action instanceof ActionInsertFact) {
        final ActionInsertFact af = (ActionInsertFact) action;
        binding = af.getBoundName();
    } else if (action instanceof ActionSetField) {
        final ActionSetField af = (ActionSetField) action;
        binding = af.getVariable();
    }
    // the binding use a unique identifier, in this case the Object itself.
    if (binding == null) {
        binding = action.toString();
    }
    final LabelledAction a = new LabelledAction();
    a.boundName = binding;
    a.action = action;
    actions.add(a);
}
Also used : ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact)

Example 10 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, ActionSetFieldCol52 sf, String cell) {
    LabelledAction a = findByLabelledAction(actions, sf.getBoundName(), sf.isUpdate());
    if (a == null) {
        a = new LabelledAction();
        a.boundName = sf.getBoundName();
        a.isUpdate = sf.isUpdate();
        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;
    ActionFieldValue val = new ActionFieldValue(sf.getFactField(), cell, sf.getType());
    asf.addFieldValue(val);
}
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)

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