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());
}
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());
}
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);
}
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);
}
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);
}
Aggregations