use of org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testNestedFieldConstraintsOnlyLeafOperator.
@Test
public void testNestedFieldConstraintsOnlyLeafOperator() {
String drl = "rule \"rule1\"\n" + "when\n" + "ParentType( parentChildField.childField == \"hello\" )\n" + "then\n" + "end";
addModelField("org.test.ParentType", "parentChildField", "org.test.ChildType", "ChildType");
addModelField("org.test.ChildType", "childField", "java.lang.String", "String");
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("childField", ebLeftSide.getFieldName());
assertEquals("java.lang.String", ebLeftSide.getFieldType());
assertEquals("==", ebLeftSide.getOperator());
assertEquals("hello", 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("ParentType", expressionUnboundFact.getName());
assertEquals("ParentType", expressionUnboundFact.getClassType());
assertEquals("ParentType", 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("parentChildField", expressionField1.getName());
assertEquals("org.test.ChildType", expressionField1.getClassType());
assertEquals("ChildType", 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("childField", expressionField2.getName());
assertEquals("java.lang.String", expressionField2.getClassType());
assertEquals("String", 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 testListSize.
@Test
public void testListSize() throws Exception {
String drl = "" + "package org.mortgages;\n" + "import java.lang.Number;\n" + "rule \"Test\"\n" + " dialect \"mvel\"\n" + " when\n" + " Person( addresses.size() == 0 )\n" + " then\n" + "end\n";
addModelField("Person", "addresses", "java.util.List", DataType.TYPE_COLLECTION);
addMethodInformation("java.util.List", "size", Collections.emptyList(), "int", null, DataType.TYPE_NUMERIC_INTEGER);
HashMap<String, String> map = new HashMap<>();
map.put("Person#addresses", "Address");
when(dmo.getModuleFieldParametersType()).thenReturn(map);
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertEquals(1, m.lhs.length);
FactPattern pattern = (FactPattern) m.lhs[0];
assertEquals(1, pattern.getConstraintList().getConstraints().length);
SingleFieldConstraintEBLeftSide constraint = (SingleFieldConstraintEBLeftSide) pattern.getConstraint(0);
assertEquals("size", constraint.getFieldName());
assertEquals("int", constraint.getFieldType());
assertEquals("0", constraint.getValue());
assertEquals("==", constraint.getOperator());
assertEquals(1, constraint.getConstraintValueType());
assertTrue(constraint.getExpressionLeftSide().getParts().get(0) instanceof ExpressionUnboundFact);
assertTrue(constraint.getExpressionLeftSide().getParts().get(1) instanceof ExpressionCollection);
assertTrue(constraint.getExpressionLeftSide().getParts().get(2) instanceof ExpressionMethod);
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide in project drools by kiegroup.
the class RuleModelTest method testAllVariableBindings2.
@Test
public void testAllVariableBindings2() {
final RuleModel model = new RuleModel();
model.lhs = new IPattern[1];
final FactPattern fp = new FactPattern("Car");
model.lhs[0] = fp;
fp.setBoundName("$c");
SingleFieldConstraint sfc = new SingleFieldConstraintEBLeftSide("make");
sfc.getExpressionValue().appendPart(new ExpressionField("make", "java.lang.String", "String"));
sfc.setFieldBinding("$m");
fp.addConstraint(sfc);
List<String> vars = model.getAllVariables();
assertEquals(2, vars.size());
assertEquals("$c", vars.get(0));
assertEquals("$m", vars.get(1));
}
use of org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testSingleFieldConstraintEBLeftSide.
@Test
public void testSingleFieldConstraintEBLeftSide() throws Exception {
String drl = "" + "rule \" broken \"\n" + "dialect \"mvel\"\n" + " when\n" + " Customer( contact != null , contact.tel1 > \"15\" )\n" + " then\n" + "end";
addModelField("Customer", "contact", "Contact", "Contact");
addModelField("Contact", "tel1", "String", "String");
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
FactPattern pattern = (FactPattern) m.lhs[0];
SingleFieldConstraint constraint = (SingleFieldConstraint) pattern.getFieldConstraints()[0];
assertEquals("Customer", constraint.getFactType());
assertEquals("contact", constraint.getFieldName());
assertEquals("Contact", constraint.getFieldType());
SingleFieldConstraintEBLeftSide constraint2 = (SingleFieldConstraintEBLeftSide) pattern.getFieldConstraints()[1];
assertEquals("tel1", constraint2.getFieldName());
assertEquals("String", constraint2.getFieldType());
assertEquals("15", constraint2.getValue());
assertEquals(">", constraint2.getOperator());
assertEquals(3, constraint2.getExpressionLeftSide().getParts().size());
ExpressionPart part1 = constraint2.getExpressionLeftSide().getParts().get(0);
assertEquals("Customer", part1.getName());
assertEquals("Customer", part1.getClassType());
assertEquals("Customer", part1.getGenericType());
ExpressionPart part2 = constraint2.getExpressionLeftSide().getParts().get(1);
assertEquals("contact", part2.getName());
assertEquals("Contact", part2.getClassType());
assertEquals("Contact", part2.getGenericType());
ExpressionPart part3 = constraint2.getExpressionLeftSide().getParts().get(2);
assertEquals("tel1", part3.getName());
assertEquals("String", part3.getClassType());
assertEquals("String", part3.getGenericType());
}
use of org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testNestedFieldExpressionsWithAnotherExpression.
@Test
public void testNestedFieldExpressionsWithAnotherExpression() {
String drl = "rule rule1\n" + "when\n" + "p : Person( address.postalCode == p.address.postalCode )\n" + "then\n" + "end";
addModelField("org.test.Person", "address", "org.test.Address", "Address");
addModelField("org.test.Person", "this", "org.test.Person", DataType.TYPE_THIS);
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("", ebLeftSide.getValue());
assertEquals(3, ebLeftSide.getExpressionValue().getParts().size());
assertTrue(ebLeftSide.getExpressionValue().getParts().get(0) instanceof ExpressionVariable);
ExpressionVariable expressionVariable = (ExpressionVariable) ebLeftSide.getExpressionValue().getParts().get(0);
assertEquals("p", expressionVariable.getName());
assertEquals("org.test.Person", expressionVariable.getClassType());
assertEquals(DataType.TYPE_THIS, expressionVariable.getGenericType());
assertTrue(ebLeftSide.getExpressionValue().getParts().get(1) instanceof ExpressionField);
ExpressionField ef1 = (ExpressionField) ebLeftSide.getExpressionValue().getParts().get(1);
assertEquals("address", ef1.getName());
assertEquals("org.test.Address", ef1.getClassType());
assertEquals("Address", ef1.getGenericType());
assertTrue(ebLeftSide.getExpressionValue().getParts().get(2) instanceof ExpressionField);
ExpressionField ef2 = (ExpressionField) ebLeftSide.getExpressionValue().getParts().get(2);
assertEquals("postalCode", ef2.getName());
assertEquals("java.lang.Integer", ef2.getClassType());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, ef2.getGenericType());
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());
}
Aggregations