use of org.drools.workbench.models.datamodel.rule.IAction in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testMultipleFromKeywords.
@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1191737
void testMultipleFromKeywords() throws Exception {
String drl = "package org.test;\n" + "rule \"ToyWithoutName \"\n" + "dialect \"java\"\n" + "when\n" + " $father: Father()\n" + " ($kid: Kid() from $father.kids)\n" + " ($toy: Toy(name == null) from $kid.toys)\n" + "then\n" + " System.out.println(\"blabla\");\n" + "end";
addModelField("org.test.Father", "this", "org.test.Father", DataType.TYPE_THIS);
addModelField("org.test.Father", "kids", "org.test.Kid", DataType.TYPE_COLLECTION);
addModelField("org.test.Kid", "this", "org.test.Kid", DataType.TYPE_THIS);
addModelField("org.test.Kid", "toys", "org.test.Toy", DataType.TYPE_COLLECTION);
addModelField("org.test.Toy", "this", "org.test.Toy", DataType.TYPE_THIS);
addModelField("org.test.Toy", "name", "java.lang.String", DataType.TYPE_STRING);
when(dmo.getPackageName()).thenReturn("org.test");
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(3, m.lhs.length);
final IPattern p0 = m.lhs[0];
assertTrue(p0 instanceof FactPattern);
final FactPattern fp0 = (FactPattern) p0;
assertEquals("$father", fp0.getBoundName());
assertEquals("Father", fp0.getFactType());
final IPattern p1 = m.lhs[1];
assertTrue(p1 instanceof FromCompositeFactPattern);
final FromCompositeFactPattern fp1 = (FromCompositeFactPattern) p1;
assertEquals("$kid", fp1.getFactPattern().getBoundName());
assertEquals("Kid", fp1.getFactType());
final IPattern p2 = m.lhs[2];
assertTrue(p2 instanceof FromCompositeFactPattern);
final FromCompositeFactPattern fp2 = (FromCompositeFactPattern) p2;
assertEquals("$toy", fp2.getFactPattern().getBoundName());
assertEquals("Toy", fp2.getFactType());
assertEquals(1, m.rhs.length);
final IAction a = m.rhs[0];
assertTrue(a instanceof FreeFormLine);
final FreeFormLine affl = (FreeFormLine) a;
assertEquals("System.out.println(\"blabla\");", affl.getText());
// Check round-trip
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.IAction in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testRHSUpdateFactWithFormula.
@Test
public void testRHSUpdateFactWithFormula() {
// https://bugzilla.redhat.com/show_bug.cgi?id=1079253
String drl = "package org.mortgages;\n" + "import org.test.ShoppingCart\n" + "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "$sc : ShoppingCart( )\n" + "then\n" + "$sc.setCartItemPromoSavings( ($sc.cartItemPromoSavings == 0.0) ? 0.0 : $sc.cartItemPromoSavings * -1 );\n" + "update( $sc );\n" + "end\n";
addModelField("org.test.ShoppingCart", "cartItemPromoSavings", "java.lang.Double", DataType.TYPE_NUMERIC_DOUBLE);
when(dmo.getPackageName()).thenReturn("org.test");
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(1, m.lhs.length);
IPattern p = m.lhs[0];
assertTrue(p instanceof FactPattern);
FactPattern fp = (FactPattern) p;
assertEquals("ShoppingCart", fp.getFactType());
assertEquals("$sc", fp.getBoundName());
assertEquals(0, fp.getNumberOfConstraints());
assertEquals(1, m.rhs.length);
IAction a = m.rhs[0];
assertTrue(a instanceof ActionUpdateField);
ActionUpdateField ap = (ActionUpdateField) a;
assertEquals("$sc", ap.getVariable());
assertEquals(1, ap.getFieldValues().length);
ActionFieldValue afv = ap.getFieldValues()[0];
assertEquals("cartItemPromoSavings", afv.getField());
assertEquals("($sc.cartItemPromoSavings == 0.0) ? 0.0 : $sc.cartItemPromoSavings * -1", afv.getValue());
assertEquals(FieldNatureType.TYPE_FORMULA, afv.getNature());
}
use of org.drools.workbench.models.datamodel.rule.IAction in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testRHSInsertFactWithFieldAsLiteral.
@Test
public void testRHSInsertFactWithFieldAsLiteral() {
String drl = "package org.mortgages\n" + "import org.test.Person\n" + "rule \"variable\"\n" + "dialect \"mvel\"\n" + "when\n" + "Person( field1 == 44 )\n" + "then\n" + "Person fact0 = new Person();\n" + "fact0.setField1( 55 );\n" + "insert( fact0 );\n" + "end";
addModelField("org.test.Person", "field1", "java.lang.Integer", DataType.TYPE_NUMERIC_INTEGER);
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(1, m.lhs.length);
IPattern p = m.lhs[0];
assertTrue(p instanceof FactPattern);
FactPattern fp = (FactPattern) p;
assertEquals("Person", fp.getFactType());
assertEquals(1, fp.getConstraintList().getConstraints().length);
assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraint);
SingleFieldConstraint sfp = (SingleFieldConstraint) fp.getConstraint(0);
assertEquals("Person", sfp.getFactType());
assertEquals("field1", sfp.getFieldName());
assertEquals("==", sfp.getOperator());
assertEquals("44", sfp.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp.getConstraintValueType());
assertEquals(1, m.rhs.length);
IAction a = m.rhs[0];
assertTrue(a instanceof ActionInsertFact);
ActionInsertFact ap = (ActionInsertFact) a;
assertEquals("Person", ap.getFactType());
assertEquals("fact0", ap.getBoundName());
assertEquals(1, ap.getFieldValues().length);
ActionFieldValue afv = ap.getFieldValues()[0];
assertEquals("field1", afv.getField());
assertEquals(FieldNatureType.TYPE_LITERAL, afv.getNature());
assertEquals("55", afv.getValue());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, afv.getType());
}
use of org.drools.workbench.models.datamodel.rule.IAction in project drools-wb by kiegroup.
the class DefaultGuidedDecisionTableLinkManagerTest method fieldConstraintWithActionBRLFragmentFieldWithTemplateKey.
@Test
public void fieldConstraintWithActionBRLFragmentFieldWithTemplateKey() {
// Columns: Row#[0], Description[1], Action[2]
final GuidedDecisionTable52 dt1 = new GuidedDecisionTable52();
final BRLActionColumn brl = new BRLActionColumn();
final ActionInsertFact aif = new ActionInsertFact("Fact");
aif.addFieldValue(new ActionFieldValue() {
{
setField("field");
setValue("10");
setType(DataType.TYPE_STRING);
setNature(FieldNatureType.TYPE_TEMPLATE);
}
});
brl.setDefinition(new ArrayList<IAction>() {
{
add(aif);
}
});
brl.getChildColumns().add(new BRLActionVariableColumn("$f", DataType.TYPE_STRING, "Fact", "field"));
dt1.getActionCols().add(brl);
// Columns: Row#[0], Description[1], Condition[2]
final GuidedDecisionTable52 dt2 = new GuidedDecisionTable52();
final Pattern52 p2 = new Pattern52();
p2.setBoundName("$f");
p2.setFactType("Fact");
final ConditionCol52 p2c1 = new ConditionCol52();
p2c1.setFactField("field");
p2.getChildColumns().add(p2c1);
dt2.getConditions().add(p2);
manager.link(dt1, dt2, (s, t) -> {
assertEquals(2, s);
assertEquals(2, t);
});
}
use of org.drools.workbench.models.datamodel.rule.IAction in project drools-wb by kiegroup.
the class RuleModellerWidgetFactory method getWidget.
public RuleModellerWidget getWidget(RuleModeller ruleModeller, EventBus eventBus, IAction action, Boolean readOnly) {
if (action instanceof ActionCallMethod) {
return new ActionCallMethodWidget(ruleModeller, eventBus, (ActionCallMethod) action, readOnly);
}
if (action instanceof ActionSetField) {
return new ActionSetFieldWidget(ruleModeller, eventBus, (ActionSetField) action, readOnly);
}
if (action instanceof ActionInsertFact) {
return new ActionInsertFactWidget(ruleModeller, eventBus, (ActionInsertFact) action, readOnly);
}
if (action instanceof ActionRetractFact) {
return new ActionRetractFactWidget(ruleModeller, eventBus, (ActionRetractFact) action, readOnly);
}
if (action instanceof DSLSentence) {
RuleModellerWidget w = new DSLSentenceWidget(ruleModeller, eventBus, (DSLSentence) action, readOnly);
return w;
}
if (action instanceof FreeFormLine) {
return new FreeFormLineWidget(ruleModeller, eventBus, (FreeFormLine) action, readOnly);
}
if (action instanceof ActionGlobalCollectionAdd) {
return new GlobalCollectionAddWidget(ruleModeller, eventBus, (ActionGlobalCollectionAdd) action, readOnly);
}
// All hardcoded action widgets have been checked, perform a plugin lookup
List<RuleModellerActionPlugin> matchingActionPlugins = actionPlugins.stream().filter(p -> p.accept(action)).collect(Collectors.toList());
if (matchingActionPlugins.size() > 1) {
throw new IllegalStateException("Ambigious " + RuleModellerActionPlugin.class.getName() + " implementations for action " + action);
}
if (matchingActionPlugins.size() == 1) {
RuleModellerActionPlugin actionPlugin = matchingActionPlugins.get(0);
RuleModellerWidget ruleModellerWidget = actionPlugin.createWidget(ruleModeller, eventBus, action, readOnly);
return ruleModellerWidget;
}
// NON-NLS
throw new RuntimeException("I don't know what type of action is: " + action);
}
Aggregations