Search in sources :

Example 11 with ActionUpdateField

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

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

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

the class RuleModelDRLPersistenceTest method testRHSChangeMultipleFieldsModifyBoth.

@Test
public void testRHSChangeMultipleFieldsModifyBoth() {
    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" + "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");
    ActionFieldValue afv2 = new ActionFieldValue();
    afv2.setField("age");
    afv2.setType(DataType.TYPE_NUMERIC_INTEGER);
    afv2.setNature(FieldNatureType.TYPE_LITERAL);
    afv2.setValue("55");
    auf.setFieldValues(new ActionFieldValue[] { afv1, afv2 });
    m.rhs = new IAction[] { auf };
    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 14 with ActionUpdateField

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

the class RuleModelDRLPersistenceTest method getComplexModel.

private RuleModel getComplexModel(boolean useDsl) {
    final RuleModel m = new RuleModel();
    m.name = "Complex Rule";
    m.setPackageName("org.test");
    m.addAttribute(new RuleAttribute("no-loop", "true"));
    m.addAttribute(new RuleAttribute("salience", "-10"));
    m.addAttribute(new RuleAttribute("agenda-group", "aGroup"));
    final FactPattern pat = new FactPattern("Person");
    pat.setBoundName("p1");
    final SingleFieldConstraint con = new SingleFieldConstraint();
    con.setFieldBinding("f1");
    con.setFieldName("age");
    con.setOperator("<");
    con.setValue("42");
    pat.addConstraint(con);
    m.addLhsItem(pat);
    final CompositeFactPattern comp = new CompositeFactPattern("not");
    comp.addFactPattern(new FactPattern("Cancel"));
    m.addLhsItem(comp);
    final ActionUpdateField upd1 = new ActionUpdateField();
    upd1.setVariable("p1");
    upd1.addFieldValue(new ActionFieldValue("status", "rejected", DataType.TYPE_STRING));
    upd1.addFieldValue(new ActionFieldValue("name", "Fred", DataType.TYPE_STRING));
    m.addRhsItem(upd1);
    final ActionRetractFact ret = new ActionRetractFact("p1");
    m.addRhsItem(ret);
    if (useDsl) {
        final DSLSentence sen = new DSLSentence();
        sen.setDefinition("Send an email to {administrator}");
        m.addRhsItem(sen);
    }
    addModelField("org.test.Person", "this", "org.test.Person", DataType.TYPE_THIS);
    addModelField("org.test.Person", "age", Integer.class.getName(), DataType.TYPE_NUMERIC_INTEGER);
    addModelField("org.test.Person", "status", String.class.getName(), DataType.TYPE_STRING);
    return m;
}
Also used : SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) ActionRetractFact(org.drools.workbench.models.datamodel.rule.ActionRetractFact) ActionUpdateField(org.drools.workbench.models.datamodel.rule.ActionUpdateField) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) RuleAttribute(org.drools.workbench.models.datamodel.rule.RuleAttribute) 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) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence)

Example 15 with ActionUpdateField

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

ActionUpdateField (org.drools.workbench.models.datamodel.rule.ActionUpdateField)58 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)53 Test (org.junit.Test)47 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)37 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)37 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)36 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)27 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)22 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)22 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)16 TemplateModel (org.drools.workbench.models.guided.template.shared.TemplateModel)16 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)11 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)11 ActionSetField (org.drools.workbench.models.datamodel.rule.ActionSetField)10 IAction (org.drools.workbench.models.datamodel.rule.IAction)8 BRLActionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn)8 BRLActionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn)8 GuidedDecisionTable52 (org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52)8 Pattern52 (org.drools.workbench.models.guided.dtable.shared.model.Pattern52)7 ActionInsertFact (org.drools.workbench.models.datamodel.rule.ActionInsertFact)6