use of org.drools.workbench.models.datamodel.rule.ExpressionMethod in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testLHSReturnType.
@Test
public void testLHSReturnType() {
// https://bugzilla.redhat.com/show_bug.cgi?id=1087690
RuleModel m = new RuleModel();
m.name = "test";
FactPattern p = new FactPattern("Number");
m.addLhsItem(p);
SingleFieldConstraint con1 = new SingleFieldConstraint();
con1.setFieldType(DataType.TYPE_NUMERIC_INTEGER);
con1.setFieldName("this");
con1.setOperator("!= null");
con1.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
p.addConstraint(con1);
SingleFieldConstraintEBLeftSide con2 = new SingleFieldConstraintEBLeftSide();
con2.getExpressionLeftSide().appendPart(new ExpressionUnboundFact(p.getFactType()));
con2.getExpressionLeftSide().appendPart(new ExpressionMethod("intValue", "int", DataType.TYPE_NUMERIC_INTEGER));
con2.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
con2.setOperator("==");
con2.setValue("0");
p.addConstraint(con2);
String expected = "rule \"test\"\n" + "dialect \"mvel\"\n" + "when\n" + "Number( this != null, intValue() == 0 )\n" + "then\n" + "end";
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.ExpressionMethod in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testLHSNumberExpressionWithThisPrefix.
@Test
public void testLHSNumberExpressionWithThisPrefix() throws Exception {
String drl = "package org.mortgages;\n" + "import java.lang.Number\n" + "rule \"test\"\n" + " dialect \"mvel\"\n" + " when\n" + " Number( this.intValue() > 5 )\n" + " then\n" + "end";
final HashMap<String, List<MethodInfo>> map = new HashMap<>();
final ArrayList<MethodInfo> methodInfos = new ArrayList<>();
methodInfos.add(new MethodInfo("intValue", Collections.emptyList(), "int", null, DataType.TYPE_NUMERIC_INTEGER));
map.put("java.lang.Number", methodInfos);
when(dmo.getModuleMethodInformation()).thenReturn(map);
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(1, m.lhs.length);
assertTrue(m.lhs[0] instanceof FactPattern);
final FactPattern fp = (FactPattern) m.lhs[0];
assertEquals("Number", fp.getFactType());
assertEquals(1, fp.getNumberOfConstraints());
assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraintEBLeftSide);
final SingleFieldConstraintEBLeftSide exp = (SingleFieldConstraintEBLeftSide) fp.getConstraint(0);
assertEquals("int", exp.getFieldType());
assertEquals(">", exp.getOperator());
assertEquals("5", exp.getValue());
assertEquals(3, exp.getExpressionLeftSide().getParts().size());
assertTrue(exp.getExpressionLeftSide().getParts().get(0) instanceof ExpressionUnboundFact);
final ExpressionUnboundFact expPart0 = (ExpressionUnboundFact) exp.getExpressionLeftSide().getParts().get(0);
assertEquals("Number", expPart0.getFactType());
assertTrue(exp.getExpressionLeftSide().getParts().get(1) instanceof ExpressionField);
final ExpressionField expPart1 = (ExpressionField) exp.getExpressionLeftSide().getParts().get(1);
assertEquals("this", expPart1.getName());
assertTrue(exp.getExpressionLeftSide().getParts().get(2) instanceof ExpressionMethod);
final ExpressionMethod expPart2 = (ExpressionMethod) exp.getExpressionLeftSide().getParts().get(2);
assertEquals("intValue", expPart2.getName());
}
use of org.drools.workbench.models.datamodel.rule.ExpressionMethod in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testLHSNumberExpressionWithoutThisPrefix.
@Test
public void testLHSNumberExpressionWithoutThisPrefix() throws Exception {
String drl = "package org.mortgages;\n" + "import java.lang.Number\n" + "rule \"test\"\n" + " dialect \"mvel\"\n" + " when\n" + " Number( intValue() > 5 )\n" + " then\n" + "end";
final HashMap<String, List<MethodInfo>> map = new HashMap<>();
final ArrayList<MethodInfo> methodInfos = new ArrayList<>();
methodInfos.add(new MethodInfo("intValue", Collections.emptyList(), "int", null, DataType.TYPE_NUMERIC_INTEGER));
map.put("java.lang.Number", methodInfos);
when(dmo.getModuleMethodInformation()).thenReturn(map);
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(1, m.lhs.length);
assertTrue(m.lhs[0] instanceof FactPattern);
final FactPattern fp = (FactPattern) m.lhs[0];
assertEquals("Number", fp.getFactType());
assertEquals(1, fp.getNumberOfConstraints());
assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraintEBLeftSide);
final SingleFieldConstraintEBLeftSide exp = (SingleFieldConstraintEBLeftSide) fp.getConstraint(0);
assertEquals("int", exp.getFieldType());
assertEquals(">", exp.getOperator());
assertEquals("5", exp.getValue());
assertEquals(2, exp.getExpressionLeftSide().getParts().size());
assertTrue(exp.getExpressionLeftSide().getParts().get(0) instanceof ExpressionUnboundFact);
final ExpressionUnboundFact expPart0 = (ExpressionUnboundFact) exp.getExpressionLeftSide().getParts().get(0);
assertEquals("Number", expPart0.getFactType());
assertTrue(exp.getExpressionLeftSide().getParts().get(1) instanceof ExpressionMethod);
final ExpressionMethod expPart1 = (ExpressionMethod) exp.getExpressionLeftSide().getParts().get(1);
assertEquals("intValue", expPart1.getName());
}
use of org.drools.workbench.models.datamodel.rule.ExpressionMethod in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testStringReplaceExpression.
@Test
public void testStringReplaceExpression() throws Exception {
// https://bugzilla.redhat.com/show_bug.cgi?id=1264321
String drl = "rule \"Replace_condition_Issue\"\n" + "dialect \"mvel\"\n" + "when\n" + " MyType( myString.replace(\"a\",\"b\"))\n" + "then\n" + "end";
addModelField("org.test.MyType", "this", "org.test.MyType", DataType.TYPE_THIS);
addModelField("org.test.MyType", "myString", String.class.getName(), DataType.TYPE_STRING);
addMethodInformation("java.lang.String", "replace", new ArrayList<String>() {
{
add("String");
add("String");
}
}, "java.lang.String", null, DataType.TYPE_STRING);
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("MyType", fp0.getFactType());
assertEquals(1, fp0.getNumberOfConstraints());
assertTrue(fp0.getConstraint(0) instanceof SingleFieldConstraintEBLeftSide);
final SingleFieldConstraintEBLeftSide fp0sfc0 = (SingleFieldConstraintEBLeftSide) fp0.getConstraint(0);
assertEquals(3, fp0sfc0.getExpressionLeftSide().getParts().size());
assertTrue(fp0sfc0.getExpressionLeftSide().getParts().get(0) instanceof ExpressionUnboundFact);
final ExpressionUnboundFact ep0 = (ExpressionUnboundFact) fp0sfc0.getExpressionLeftSide().getParts().get(0);
assertEquals("MyType", ep0.getFactType());
assertTrue(fp0sfc0.getExpressionLeftSide().getParts().get(1) instanceof ExpressionField);
final ExpressionField ep1 = (ExpressionField) fp0sfc0.getExpressionLeftSide().getParts().get(1);
assertEquals("myString", ep1.getName());
assertTrue(fp0sfc0.getExpressionLeftSide().getParts().get(2) instanceof ExpressionMethod);
final ExpressionMethod ep2 = (ExpressionMethod) fp0sfc0.getExpressionLeftSide().getParts().get(2);
assertEquals("replace", ep2.getName());
assertEquals(2, ep2.getParams().size());
final ExpressionFormLine param0 = ep2.getParams().get(new ExpressionMethodParameterDefinition(0, "String"));
assertNotNull(param0);
assertEquals(1, param0.getParts().size());
assertNotNull(param0.getParts().get(0));
assertEquals("a", param0.getParts().get(0).getName());
assertEquals("String", param0.getParts().get(0).getClassType());
final ExpressionFormLine param1 = ep2.getParams().get(new ExpressionMethodParameterDefinition(1, "String"));
assertNotNull(param1);
assertEquals(1, param1.getParts().size());
assertNotNull(param1.getParts().get(0));
assertEquals("b", param1.getParts().get(0).getName());
assertEquals("String", param1.getParts().get(0).getClassType());
assertEquals(0, m.rhs.length);
// Check round-trip
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.ExpressionMethod in project drools by kiegroup.
the class CopyExpressionVisitor method visit.
public void visit(ExpressionMethod part) {
ExpressionMethod method = new ExpressionMethod(part.getName(), part.getClassType(), part.getGenericType(), part.getParametricType());
copyMethodParams(part, method);
add(method);
moveNext(part);
}
Aggregations