use of org.drools.workbench.models.datamodel.rule.ActionCallMethod in project drools by kiegroup.
the class RuleModelUpgradeHelper1 method updateMethodCall.
// The way method calls are done changed after 5.0.0.CR1 so every rule done
// before that needs to be updated.
private RuleModel updateMethodCall(RuleModel model) {
for (int i = 0; i < model.rhs.length; i++) {
if (model.rhs[i] instanceof ActionCallMethod) {
ActionCallMethod action = (ActionCallMethod) model.rhs[i];
// Check if method name is filled, if not this was made with an older Guvnor version
if (action.getMethodName() == null || "".equals(action.getMethodName())) {
if (action.getFieldValues() != null && action.getFieldValues().length >= 1) {
action.setMethodName(action.getFieldValues()[0].getField());
action.setFieldValues(new ActionFieldValue[0]);
action.setState(ActionCallMethod.TYPE_DEFINED);
}
}
}
}
return model;
}
use of org.drools.workbench.models.datamodel.rule.ActionCallMethod in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testCallFunction.
@Test
public void testCallFunction() throws Exception {
String expected = "" + "package org.mortgages;\n" + "import org.mortgages.LoanApplication;\n" + "\n" + "rule \"my rule\"\n" + " dialect \"mvel\"\n" + " when\n" + " a : LoanApplication( )\n" + " then\n" + " keke.clear( );\n" + "end\n";
final RuleModel m = new RuleModel();
m.setPackageName("org.mortgages");
m.getImports().addImport(new Import("org.mortgages.LoanApplication"));
m.name = "my rule";
FactPattern factPattern = new FactPattern();
factPattern.setFactType("LoanApplication");
factPattern.setBoundName("a");
m.lhs = new IPattern[] { factPattern };
ActionCallMethod actionCallMethod = new ActionCallMethod();
actionCallMethod.setState(1);
actionCallMethod.setMethodName("clear");
actionCallMethod.setVariable("keke");
m.rhs = new IAction[] { actionCallMethod };
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.ActionCallMethod in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testRHSSetMethodCallsMethodJava.
@Test
public void testRHSSetMethodCallsMethodJava() {
String oldValue = System.getProperty("drools.dateformat");
try {
System.setProperty("drools.dateformat", "dd-MMM-yyyy");
RuleModel m = new RuleModel();
m.name = "RHS SetMethodCallsMethod";
m.addAttribute(new RuleAttribute("dialect", "java"));
FactPattern p = new FactPattern("Person");
p.setBoundName("$p");
m.addLhsItem(p);
ActionCallMethod acm = new ActionCallMethod();
acm.setMethodName("method");
acm.setVariable("$p");
acm.addFieldValue(new ActionFieldFunction("f1", "String", DataType.TYPE_STRING));
acm.addFieldValue(new ActionFieldFunction("f2", "true", DataType.TYPE_BOOLEAN));
acm.addFieldValue(new ActionFieldFunction("f3", "31-Jan-2012", DataType.TYPE_DATE));
acm.addFieldValue(new ActionFieldFunction("f4", "100", DataType.TYPE_NUMERIC_INTEGER));
acm.addFieldValue(new ActionFieldFunction("f5", "100", DataType.TYPE_NUMERIC_BIGDECIMAL));
m.addRhsItem(acm);
String result = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
assertTrue(result.indexOf("java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"dd-MMM-yyyy\");") != -1);
assertTrue(result.indexOf("$p.method( \"String\", true, sdf.parse(\"31-Jan-2012\"), 100, new java.math.BigDecimal(\"100\") );") != -1);
checkMarshalling(null, m);
} finally {
if (oldValue == null) {
System.clearProperty("drools.dateformat");
} else {
System.setProperty("drools.dateformat", oldValue);
}
}
}
use of org.drools.workbench.models.datamodel.rule.ActionCallMethod in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testFunctionCall.
@Test
public void testFunctionCall() {
// BZ-1013682
String drl = "" + "package org.mortgages;\n" + "import org.mortgages.LoanApplication;\n" + "\n" + "rule \"my rule\"\n" + " dialect \"mvel\"\n" + " when\n" + " a : LoanApplication( )\n" + " then\n" + " keke.clear( );\n" + "end\n";
HashMap<String, String> globals = new HashMap<>();
globals.put("keke", "java.util.ArrayList");
when(dmo.getPackageGlobals()).thenReturn(globals);
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertTrue(m.rhs[0] instanceof ActionCallMethod);
ActionCallMethod actionGlobalCollectionAdd = (ActionCallMethod) m.rhs[0];
assertEquals("clear", actionGlobalCollectionAdd.getMethodName());
assertEquals("keke", actionGlobalCollectionAdd.getVariable());
assertEquals(1, actionGlobalCollectionAdd.getState());
assertEquals(0, actionGlobalCollectionAdd.getFieldValues().length);
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.ActionCallMethod in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testRHSAppendToList.
@Test
public void testRHSAppendToList() throws Exception {
// https://issues.jboss.org/browse/GUVNOR-2286
String drl = "package org.test;\n" + "import java.lang.Number;\n" + "rule \"RuleCheckEmail\"\n" + "dialect \"mvel\"\n" + "when\n" + "incomeData : IncomeData( email == \"myemail\" , $list : list != null )\n" + "then\n" + "Element element = new Element();\n" + "element.setId( 2 );\n" + "insertLogical( element );\n" + "$list.add( element );\n" + "end";
addModelField("org.test.IncomeData", "this", "org.test.IncomeData", DataType.TYPE_THIS);
addModelField("org.test.IncomeData", "email", String.class.getName(), DataType.TYPE_STRING);
addModelField("org.test.IncomeData", "list", List.class.getName(), DataType.TYPE_COLLECTION);
addModelField("java.util.List", "this", "java.util.List", DataType.TYPE_THIS);
addMethodInformation("java.util.List", "add", new ArrayList<String>() {
{
add("java.lang.Object");
}
}, DataType.TYPE_BOOLEAN, null, DataType.TYPE_BOOLEAN);
addMethodInformation("java.util.List", "add", new ArrayList<String>() {
{
add("java.lang.Integer");
add("java.lang.Object");
}
}, DataType.TYPE_BOOLEAN, null, DataType.TYPE_BOOLEAN);
when(dmo.getPackageName()).thenReturn("org.test");
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(1, m.lhs.length);
final IPattern p0 = m.lhs[0];
assertTrue(p0 instanceof FactPattern);
final FactPattern fp0 = (FactPattern) p0;
assertEquals("IncomeData", fp0.getFactType());
assertEquals(2, fp0.getNumberOfConstraints());
assertTrue(fp0.getConstraint(0) instanceof SingleFieldConstraint);
final SingleFieldConstraint fp0sfc0 = (SingleFieldConstraint) fp0.getConstraint(0);
assertEquals("IncomeData", fp0sfc0.getFactType());
assertEquals("email", fp0sfc0.getFieldName());
assertEquals(DataType.TYPE_STRING, fp0sfc0.getFieldType());
assertEquals("==", fp0sfc0.getOperator());
assertEquals("myemail", fp0sfc0.getValue());
assertTrue(fp0.getConstraint(1) instanceof SingleFieldConstraint);
final SingleFieldConstraint fp0sfc1 = (SingleFieldConstraint) fp0.getConstraint(1);
assertEquals("IncomeData", fp0sfc1.getFactType());
assertEquals("list", fp0sfc1.getFieldName());
assertEquals(DataType.TYPE_COLLECTION, fp0sfc1.getFieldType());
assertEquals("!= null", fp0sfc1.getOperator());
assertNull(fp0sfc1.getValue());
assertEquals(2, m.rhs.length);
assertTrue(m.rhs[0] instanceof ActionInsertLogicalFact);
final ActionInsertLogicalFact a0 = (ActionInsertLogicalFact) m.rhs[0];
assertEquals("Element", a0.getFactType());
assertEquals("element", a0.getBoundName());
assertEquals(1, a0.getFieldValues().length);
final ActionFieldValue a0f0 = a0.getFieldValues()[0];
assertEquals("id", a0f0.getField());
assertEquals("2", a0f0.getValue());
assertEquals(DataType.TYPE_NUMERIC, a0f0.getType());
assertEquals(FieldNatureType.TYPE_LITERAL, a0f0.getNature());
assertTrue(m.rhs[1] instanceof ActionCallMethod);
final ActionCallMethod a1 = (ActionCallMethod) m.rhs[1];
assertEquals("add", a1.getMethodName());
assertEquals("$list", a1.getVariable());
assertEquals(1, a1.getFieldValues().length);
final ActionFieldValue a1f0 = a1.getFieldValues()[0];
assertEquals("add", a1f0.getField());
assertEquals("element", a1f0.getValue());
assertEquals(FieldNatureType.TYPE_VARIABLE, a1f0.getNature());
assertEquals("java.lang.Object", a1f0.getType());
// Check round-trip
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
Aggregations