use of org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact 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.ExpressionUnboundFact 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.ExpressionUnboundFact 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());
}
use of org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testLHSExpressionString2.
@Test
public void testLHSExpressionString2() {
RuleModel m = new RuleModel();
m.name = "test expressionsString2";
FactPattern p = new FactPattern("Person");
SingleFieldConstraintEBLeftSide con = new SingleFieldConstraintEBLeftSide();
con.getExpressionLeftSide().appendPart(new ExpressionUnboundFact(p.getFactType()));
con.getExpressionLeftSide().appendPart(new ExpressionField("field1", "java.lang.String", DataType.TYPE_STRING));
con.setOperator("==");
con.setValue("Cheddar");
con.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
p.addConstraint(con);
m.addLhsItem(p);
String result = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
String expected = "rule \"test expressionsString2\"" + "\tdialect \"mvel\"\n when " + " Person( field1 == \"Cheddar\" )" + " then " + "end";
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testLHSExpressionJavaEnum.
@Test
public void testLHSExpressionJavaEnum() {
RuleModel m = new RuleModel();
m.name = "test expressionsJavaEnum";
FactPattern p = new FactPattern("Person");
SingleFieldConstraintEBLeftSide con = new SingleFieldConstraintEBLeftSide();
con.getExpressionLeftSide().appendPart(new ExpressionUnboundFact(p.getFactType()));
con.getExpressionLeftSide().appendPart(new ExpressionField("field1", "CHEESE", DataType.TYPE_COMPARABLE));
con.setOperator("==");
con.setValue("CHEESE.Cheddar");
con.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
p.addConstraint(con);
m.addLhsItem(p);
String expected = "rule \"test expressionsJavaEnum\"" + "\tdialect \"mvel\"\n when " + " Person( field1 == CHEESE.Cheddar )" + " then " + "end";
checkMarshalling(expected, m);
}
Aggregations