Search in sources :

Example 26 with ActionSetField

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

the class RuleModelDRLPersistenceUnmarshallingTest method testMVELInlineList.

@Test
public void testMVELInlineList() throws Exception {
    String drl = "" + "rule \"Borked\"\n" + "  dialect \"mvel\"\n" + "  when\n" + "    c : Company( )\n" + "  then\n" + "    c.setEmps( [\"item1\", \"item2\"] );\n" + "end";
    addModelField("Company", "emps", "java.util.List", DataType.TYPE_COLLECTION);
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertEquals(1, m.rhs.length);
    assertTrue(m.rhs[0] instanceof ActionSetField);
    ActionSetField actionSetField = (ActionSetField) m.rhs[0];
    assertEquals("c", actionSetField.getVariable());
    assertEquals(1, actionSetField.getFieldValues().length);
    ActionFieldValue actionFieldValue = actionSetField.getFieldValues()[0];
    assertEquals("[\"item1\", \"item2\"]", actionFieldValue.getValue());
    assertEquals("emps", actionFieldValue.getField());
    assertEquals(FieldNatureType.TYPE_FORMULA, actionFieldValue.getNature());
    assertEquals(DataType.TYPE_COLLECTION, actionFieldValue.getType());
}
Also used : ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 27 with ActionSetField

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

the class RuleModelDRLPersistenceUnmarshallingTest method actionSetFieldWithFormula.

@Test
public void actionSetFieldWithFormula() throws Exception {
    String drl = "package org.mortgages;\n" + "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "$a : Application()\n" + "then\n" + "$a.setName( \"Pupa\" + 20 + \"Smurf\" );" + "end";
    addModelField("org.mortgages.Applicant", "this", "org.mortgages.Applicant", DataType.TYPE_THIS);
    addModelField("org.mortgages.Applicant", "name", String.class.getName(), DataType.TYPE_STRING);
    when(dmo.getPackageName()).thenReturn("org.mortgages");
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals("r1", m.name);
    // LHS Pattern
    assertEquals(1, m.lhs.length);
    IPattern p = m.lhs[0];
    assertTrue(p instanceof FactPattern);
    FactPattern fp = (FactPattern) p;
    assertEquals("Application", fp.getFactType());
    assertEquals(0, fp.getNumberOfConstraints());
    assertEquals(1, m.rhs.length);
    assertTrue(m.rhs[0] instanceof ActionSetField);
    ActionSetField asf = (ActionSetField) m.rhs[0];
    assertEquals("$a", asf.getVariable());
    assertEquals(1, asf.getFieldValues().length);
    ActionFieldValue afv0 = asf.getFieldValues()[0];
    assertEquals("name", afv0.getField());
    assertEquals("\"Pupa\" + 20 + \"Smurf\"", afv0.getValue());
    assertEquals(FieldNatureType.TYPE_FORMULA, afv0.getNature());
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) 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 28 with ActionSetField

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

the class RuleModelDRLPersistenceUnmarshallingTest method testRHSOrder.

@Test
public void testRHSOrder() {
    String drl = "rule \"Low Down Payment based on Appraisal\"\n" + "  dialect \"mvel\"\n" + "  ruleflow-group \"apr-calculation\"\n" + "  salience -3\n" + "  no-loop true\n" + " when\n" + "    appraised : Appraisal( )\n" + "    application : Application( mortgageAmount > ( appraised.value * 8 / 10 ) )\n" + " then\n" + "    double ratio = application.getMortgageAmount().doubleValue() / appraised.getValue().doubleValue();\n" + "    int brackets = (int)((ratio - 0.8) / 0.05);\n" + "    brackets++;\n" + "    double aprSurcharge = 0.75 * brackets;\n" + "    System.out.println( \"aprSurcharge added is \" + aprSurcharge );\n" + "    application.setApr(  application.getApr() + aprSurcharge );\n" + "    System.out.println(\"Executed Rule: \" + drools.getRule().getName() );\n" + "end";
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals(7, m.rhs.length);
    assertTrue(m.rhs[0] instanceof FreeFormLine);
    assertTrue(m.rhs[1] instanceof FreeFormLine);
    assertTrue(m.rhs[2] instanceof FreeFormLine);
    assertTrue(m.rhs[3] instanceof FreeFormLine);
    assertTrue(m.rhs[4] instanceof FreeFormLine);
    assertTrue(m.rhs[5] instanceof ActionSetField);
    assertTrue(m.rhs[6] instanceof FreeFormLine);
}
Also used : FreeFormLine(org.drools.workbench.models.datamodel.rule.FreeFormLine) ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 29 with ActionSetField

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

the class RuleModelTest method testBoundFromCompositeFactFinder.

@Test
public void testBoundFromCompositeFactFinder() {
    final RuleModel model = new RuleModel();
    model.lhs = new IPattern[1];
    final FromCompositeFactPattern fcfp = new FromCompositeFactPattern();
    final FactPattern x = new FactPattern("Car");
    x.setBoundName("x");
    final SingleFieldConstraint a = new SingleFieldConstraint("name");
    a.setFieldBinding("a");
    a.setFieldType("String");
    x.addConstraint(a);
    fcfp.setFactPattern(x);
    model.lhs[0] = fcfp;
    assertEquals(x, model.getLHSBoundFact("x"));
    assertEquals(1, model.getLHSBoundFacts().size());
    assertEquals("x", model.getLHSBoundFacts().get(0));
    assertEquals(a, model.getLHSBoundField("a"));
    assertEquals("Car", model.getLHSBindingType("x"));
    assertEquals("String", model.getLHSBindingType("a"));
    assertEquals(x, model.getLHSParentFactPatternForBinding("a"));
    assertEquals(2, model.getAllLHSVariables().size());
    assertTrue(model.getAllLHSVariables().contains("x"));
    assertTrue(model.getAllLHSVariables().contains("a"));
    model.rhs = new IAction[1];
    final ActionSetField set = new ActionSetField();
    set.setVariable("x");
    model.rhs[0] = set;
    assertTrue(model.isBoundFactUsed("x"));
    assertEquals(1, model.lhs.length);
    assertFalse(model.removeLhsItem(0));
    assertEquals(1, model.lhs.length);
}
Also used : SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) 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) Test(org.junit.Test)

Example 30 with ActionSetField

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

the class RuleModelTest method testBoundFactFinder.

@Test
public void testBoundFactFinder() {
    final RuleModel model = new RuleModel();
    assertNull(model.getLHSBoundFact("x"));
    model.lhs = new IPattern[3];
    final FactPattern x = new FactPattern("Car");
    model.lhs[0] = x;
    x.setBoundName("x");
    assertNotNull(model.getLHSBoundFact("x"));
    assertEquals(x, model.getLHSBoundFact("x"));
    final FactPattern y = new FactPattern("Car");
    model.lhs[1] = y;
    y.setBoundName("y");
    final FactPattern other = new FactPattern("House");
    model.lhs[2] = other;
    assertEquals(y, model.getLHSBoundFact("y"));
    assertEquals(x, model.getLHSBoundFact("x"));
    model.rhs = new IAction[1];
    final ActionSetField set = new ActionSetField();
    set.setVariable("x");
    model.rhs[0] = set;
    assertTrue(model.isBoundFactUsed("x"));
    assertFalse(model.isBoundFactUsed("y"));
    assertEquals(3, model.lhs.length);
    assertFalse(model.removeLhsItem(0));
    assertEquals(3, model.lhs.length);
    final ActionRetractFact fact = new ActionRetractFact("q");
    model.rhs[0] = fact;
    assertTrue(model.isBoundFactUsed("q"));
    assertFalse(model.isBoundFactUsed("x"));
    final XStream xt = XStreamUtils.createTrustingXStream();
    xt.alias("rule", RuleModel.class);
    xt.alias("fact", FactPattern.class);
    xt.alias("retract", ActionRetractFact.class);
    // See https://issues.jboss.org/browse/GUVNOR-1115
    xt.aliasPackage("org.drools.guvnor.client", "org.drools.ide.common.client");
    final String brl = xt.toXML(model);
    logger.debug(brl);
}
Also used : ActionRetractFact(org.drools.workbench.models.datamodel.rule.ActionRetractFact) ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) XStream(com.thoughtworks.xstream.XStream) 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) Test(org.junit.Test)

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