use of org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testNestedFieldExpressions.
@Test
public void testNestedFieldExpressions() {
String drl = "rule rule1\n" + "when\n" + "Person( address.postalCode == 12345 )\n" + "then\n" + "end";
addModelField("org.test.Person", "address", "org.test.Address", "Address");
addModelField("org.test.Address", "postalCode", "java.lang.Integer", "Integer");
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(1, m.lhs.length);
assertTrue(m.lhs[0] instanceof FactPattern);
assertTrue(((FactPattern) m.lhs[0]).getFieldConstraints()[0] instanceof SingleFieldConstraintEBLeftSide);
SingleFieldConstraintEBLeftSide ebLeftSide = (SingleFieldConstraintEBLeftSide) ((FactPattern) m.lhs[0]).getFieldConstraints()[0];
assertEquals("postalCode", ebLeftSide.getFieldName());
assertEquals("java.lang.Integer", ebLeftSide.getFieldType());
assertEquals("==", ebLeftSide.getOperator());
assertEquals("12345", ebLeftSide.getValue());
assertEquals(3, ebLeftSide.getExpressionLeftSide().getParts().size());
assertTrue(ebLeftSide.getExpressionLeftSide().getParts().get(0) instanceof ExpressionUnboundFact);
ExpressionUnboundFact expressionUnboundFact = ((ExpressionUnboundFact) ebLeftSide.getExpressionLeftSide().getParts().get(0));
assertEquals("Person", expressionUnboundFact.getName());
assertEquals("Person", expressionUnboundFact.getClassType());
assertEquals("Person", expressionUnboundFact.getGenericType());
assertEquals(((FactPattern) m.lhs[0]).getFactType(), expressionUnboundFact.getFactType());
assertNull(expressionUnboundFact.getPrevious());
assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(1), expressionUnboundFact.getNext());
assertTrue(ebLeftSide.getExpressionLeftSide().getParts().get(1) instanceof ExpressionField);
ExpressionField expressionField1 = (ExpressionField) ebLeftSide.getExpressionLeftSide().getParts().get(1);
assertEquals("address", expressionField1.getName());
assertEquals("org.test.Address", expressionField1.getClassType());
assertEquals("Address", expressionField1.getGenericType());
assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(0), expressionField1.getPrevious());
assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(2), expressionField1.getNext());
assertTrue(ebLeftSide.getExpressionLeftSide().getParts().get(2) instanceof ExpressionField);
ExpressionField expressionField2 = (ExpressionField) ebLeftSide.getExpressionLeftSide().getParts().get(2);
assertEquals("postalCode", expressionField2.getName());
assertEquals("java.lang.Integer", expressionField2.getClassType());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, expressionField2.getGenericType());
assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(1), expressionField2.getPrevious());
assertNull(expressionField2.getNext());
}
use of org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testExpressionWithListSize.
@Test
public void testExpressionWithListSize() throws Exception {
String drl = "" + "rule \"Borked\"\n" + " dialect \"mvel\"\n" + " when\n" + " Company( emps.size() == 0 )\n" + " then\n" + "end";
addModelField("Company", "emps", "java.util.List", "List");
addMethodInformation("java.util.List", "size", Collections.emptyList(), "int", null, DataType.TYPE_NUMERIC_INTEGER);
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertEquals(1, m.lhs.length);
assertTrue(m.lhs[0] instanceof FactPattern);
FactPattern factPattern = (FactPattern) m.lhs[0];
assertEquals(1, factPattern.getConstraintList().getConstraints().length);
assertTrue(factPattern.getConstraintList().getConstraints()[0] instanceof SingleFieldConstraintEBLeftSide);
SingleFieldConstraintEBLeftSide constraint = (SingleFieldConstraintEBLeftSide) factPattern.getConstraintList().getConstraints()[0];
assertEquals("size", constraint.getFieldName());
assertEquals("int", constraint.getFieldType());
assertEquals("0", constraint.getValue());
assertEquals("==", constraint.getOperator());
assertEquals(1, constraint.getConstraintValueType());
}
use of org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide 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.SingleFieldConstraintEBLeftSide 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.SingleFieldConstraintEBLeftSide in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testEnumerationNestedClasses.
@Test
public void testEnumerationNestedClasses() throws Exception {
// https://bugzilla.redhat.com/show_bug.cgi?id=1047879
String drl = "import org.drools.workbench.models.commons.backend.rule.TestEnum;\n" + "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "OuterClassWithEnums( innerClass.innerField == TestEnum.VALUE1 )\n" + "then\n" + "end";
addModelField("OuterClassWithEnums", "innerClass", "InnerClassWithEnums", "InnerClassWithEnums");
addModelField("InnerClassWithEnums", "innerField", TestEnum.class.getSimpleName(), DataType.TYPE_COMPARABLE);
addJavaEnumDefinition("InnerClassWithEnums", "innerField", new String[] { "TestEnum.VALUE1=TestEnum.VALUE1", "TestEnum.VALUE2=TestEnum.VALUE2" });
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
FactPattern pattern = (FactPattern) m.lhs[0];
assertEquals(1, pattern.getNumberOfConstraints());
assertTrue(pattern.getConstraint(0) instanceof SingleFieldConstraintEBLeftSide);
final SingleFieldConstraintEBLeftSide constraint = (SingleFieldConstraintEBLeftSide) pattern.getConstraint(0);
assertEquals(3, constraint.getExpressionLeftSide().getParts().size());
assertTrue(constraint.getExpressionLeftSide().getParts().get(0) instanceof ExpressionUnboundFact);
final ExpressionUnboundFact eubf = ((ExpressionUnboundFact) constraint.getExpressionLeftSide().getParts().get(0));
assertEquals("OuterClassWithEnums", eubf.getName());
assertEquals("OuterClassWithEnums", eubf.getClassType());
assertEquals("OuterClassWithEnums", eubf.getGenericType());
assertTrue(constraint.getExpressionLeftSide().getParts().get(1) instanceof ExpressionField);
final ExpressionField ef1 = ((ExpressionField) constraint.getExpressionLeftSide().getParts().get(1));
assertEquals("innerClass", ef1.getName());
assertEquals("InnerClassWithEnums", ef1.getClassType());
assertEquals("InnerClassWithEnums", ef1.getGenericType());
assertTrue(constraint.getExpressionLeftSide().getParts().get(2) instanceof ExpressionField);
final ExpressionField ef2 = ((ExpressionField) constraint.getExpressionLeftSide().getParts().get(2));
assertEquals("innerField", ef2.getName());
assertEquals("TestEnum", ef2.getClassType());
assertEquals(DataType.TYPE_COMPARABLE, ef2.getGenericType());
assertEquals("OuterClassWithEnums", constraint.getFactType());
assertEquals("innerField", constraint.getFieldName());
assertEquals("TestEnum", constraint.getFieldType());
assertEquals("==", constraint.getOperator());
assertEquals("TestEnum.VALUE1", constraint.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_ENUM, constraint.getConstraintValueType());
final String drl2 = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
assertEqualsIgnoreWhitespace(drl, drl2);
}
Aggregations