use of org.drools.workbench.models.datamodel.rule.FactPattern 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.FactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testRHSTemplateKeys.
@Test
public // https://issues.jboss.org/browse/GUVNOR-2030
void testRHSTemplateKeys() throws Exception {
String drl = "package org.test;\n" + "rule \"MyRule\"\n" + "dialect \"mvel\"\n" + "when\n" + " $p : Person( name == \"Fred\" )\n" + "then\n" + " modify( $p ) { setName( \"@{k1}\" ) }\n" + "end";
addModelField("org.test.Person", "this", "org.test.Person", DataType.TYPE_THIS);
addModelField("org.test.Person", "name", String.class.getName(), 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("Person", fp0.getFactType());
assertEquals(1, fp0.getNumberOfConstraints());
assertTrue(fp0.getConstraint(0) instanceof SingleFieldConstraint);
final SingleFieldConstraint sfc1 = (SingleFieldConstraint) fp0.getConstraint(0);
assertEquals("Person", sfc1.getFactType());
assertEquals("name", sfc1.getFieldName());
assertEquals(DataType.TYPE_STRING, sfc1.getFieldType());
assertEquals(SingleFieldConstraint.TYPE_LITERAL, sfc1.getConstraintValueType());
assertEquals("Fred", sfc1.getValue());
assertEquals(1, m.rhs.length);
assertTrue(m.rhs[0] instanceof ActionUpdateField);
ActionUpdateField auf = (ActionUpdateField) m.rhs[0];
assertEquals("$p", auf.getVariable());
assertEquals(1, auf.getFieldValues().length);
ActionFieldValue afv = auf.getFieldValues()[0];
assertEquals("name", afv.getField());
assertEquals("k1", afv.getValue());
assertEquals(FieldNatureType.TYPE_TEMPLATE, afv.getNature());
}
use of org.drools.workbench.models.datamodel.rule.FactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testNewKeywordVariableNamePrefix3.
@Test
public // https://issues.jboss.org/browse/GUVNOR-2143
void testNewKeywordVariableNamePrefix3() {
String oldValue = System.getProperty("drools.dateformat");
try {
System.setProperty("drools.dateformat", "dd-MMM-yyyy");
String drl = "package org.test;\n" + "rule \"rule1\"\n" + " dialect \"java\"\n" + " when\n" + " $a : Applicant()\n" + " then\n" + " java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"dd-MMM-yyyy\");\n" + " java.util.Date newStartDate = new java.util.Date();\n" + " modify( $a ) \n" + " { setApplicantDate( newStartDate ) }\n" + "end\n";
addModelField("org.test.Applicant", "this", "org.test.Applicant", DataType.TYPE_THIS);
addModelField("org.test.Applicant", "applicantDate", Date.class.getName(), DataType.TYPE_DATE);
when(dmo.getPackageName()).thenReturn("org.test");
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals("rule1", m.name);
assertEquals(1, m.lhs.length);
IPattern p = m.lhs[0];
assertTrue(p instanceof FactPattern);
FactPattern fp = (FactPattern) p;
assertEquals("Applicant", fp.getFactType());
assertEquals("$a", fp.getBoundName());
assertNull(fp.getConstraintList());
assertEquals(2, m.rhs.length);
assertTrue(m.rhs[0] instanceof FreeFormLine);
FreeFormLine ffl = (FreeFormLine) m.rhs[0];
assertEquals("java.util.Date newStartDate = new java.util.Date();", ffl.getText());
assertTrue(m.rhs[1] instanceof ActionUpdateField);
ActionUpdateField auf = (ActionUpdateField) m.rhs[1];
assertEquals("$a", auf.getVariable());
assertEquals(1, auf.getFieldValues().length);
ActionFieldValue afv = auf.getFieldValues()[0];
assertEquals("applicantDate", afv.getField());
assertEquals("newStartDate", afv.getValue());
assertEquals(FieldNatureType.TYPE_FORMULA, afv.getNature());
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
} finally {
if (oldValue == null) {
System.clearProperty("drools.dateformat");
} else {
System.setProperty("drools.dateformat", oldValue);
}
}
}
use of org.drools.workbench.models.datamodel.rule.FactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testLHSMultipleAllOfTheFollowing.
@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1174360
void testLHSMultipleAllOfTheFollowing() throws Exception {
String drl = "package org.test;\n" + "rule \"test\"\n" + "dialect \"mvel\"\n" + "when\n" + "PhoneNumber(\n" + "( homePhone != null && homePhone matches \"\\\"+9199\\\"\" ) || \n" + "( personalPhone != null && personalPhone matches \"\\\"+9188\\\"\" ) || \n" + "( workPhone != null && workPhone matches \"\\\"+9177\\\"\") \n" + ")\n" + "then\n" + "end\n";
addModelField("org.test.PhoneNumber", "this", "org.test.PhoneNumber", DataType.TYPE_THIS);
addModelField("org.test.PhoneNumber", "homePhone", String.class.getName(), DataType.TYPE_STRING);
addModelField("org.test.PhoneNumber", "personalPhone", String.class.getName(), DataType.TYPE_STRING);
addModelField("org.test.PhoneNumber", "workPhone", String.class.getName(), 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);
assertEquals(0, m.rhs.length);
// Check Pattern
assertTrue(m.lhs[0] instanceof FactPattern);
final FactPattern fp = (FactPattern) m.lhs[0];
assertEquals("PhoneNumber", fp.getFactType());
assertEquals(1, fp.getNumberOfConstraints());
assertTrue(fp.getConstraint(0) instanceof CompositeFieldConstraint);
final CompositeFieldConstraint cfc = (CompositeFieldConstraint) fp.getConstraint(0);
assertEquals("||", cfc.getCompositeJunctionType());
assertEquals(3, cfc.getNumberOfConstraints());
assertTrue(cfc.getConstraint(0) instanceof CompositeFieldConstraint);
assertTrue(cfc.getConstraint(1) instanceof CompositeFieldConstraint);
assertTrue(cfc.getConstraint(2) instanceof CompositeFieldConstraint);
// Check first composite field constraint
final CompositeFieldConstraint cfc_0 = (CompositeFieldConstraint) cfc.getConstraint(0);
assertEquals("&&", cfc_0.getCompositeJunctionType());
assertEquals(2, cfc_0.getNumberOfConstraints());
assertTrue(cfc_0.getConstraint(0) instanceof SingleFieldConstraint);
assertTrue(cfc_0.getConstraint(1) instanceof SingleFieldConstraint);
final SingleFieldConstraint sfc_0_0 = (SingleFieldConstraint) cfc_0.getConstraint(0);
assertEquals("PhoneNumber", sfc_0_0.getFactType());
assertEquals("homePhone", sfc_0_0.getFieldName());
assertEquals(DataType.TYPE_STRING, sfc_0_0.getFieldType());
assertEquals("!= null", sfc_0_0.getOperator());
assertNull(sfc_0_0.getValue());
final SingleFieldConstraint sfc_0_1 = (SingleFieldConstraint) cfc_0.getConstraint(1);
assertEquals("PhoneNumber", sfc_0_1.getFactType());
assertEquals("homePhone", sfc_0_1.getFieldName());
assertEquals(DataType.TYPE_STRING, sfc_0_1.getFieldType());
assertEquals("matches", sfc_0_1.getOperator());
assertEquals("\\\"+9199\\\"", sfc_0_1.getValue());
// Check second composite field constraint
final CompositeFieldConstraint cfc_1 = (CompositeFieldConstraint) cfc.getConstraint(1);
assertEquals("&&", cfc_1.getCompositeJunctionType());
assertEquals(2, cfc_1.getNumberOfConstraints());
assertTrue(cfc_1.getConstraint(0) instanceof SingleFieldConstraint);
assertTrue(cfc_1.getConstraint(1) instanceof SingleFieldConstraint);
final SingleFieldConstraint sfc_1_0 = (SingleFieldConstraint) cfc_1.getConstraint(0);
assertEquals("PhoneNumber", sfc_1_0.getFactType());
assertEquals("personalPhone", sfc_1_0.getFieldName());
assertEquals(DataType.TYPE_STRING, sfc_1_0.getFieldType());
assertEquals("!= null", sfc_1_0.getOperator());
assertNull(sfc_1_0.getValue());
final SingleFieldConstraint sfc_1_1 = (SingleFieldConstraint) cfc_1.getConstraint(1);
assertEquals("PhoneNumber", sfc_1_1.getFactType());
assertEquals("personalPhone", sfc_1_1.getFieldName());
assertEquals(DataType.TYPE_STRING, sfc_1_1.getFieldType());
assertEquals("matches", sfc_1_1.getOperator());
assertEquals("\\\"+9188\\\"", sfc_1_1.getValue());
// Check third composite field constraint
final CompositeFieldConstraint cfc_2 = (CompositeFieldConstraint) cfc.getConstraint(2);
assertEquals("&&", cfc_2.getCompositeJunctionType());
assertEquals(2, cfc_2.getNumberOfConstraints());
assertTrue(cfc_2.getConstraint(0) instanceof SingleFieldConstraint);
assertTrue(cfc_2.getConstraint(1) instanceof SingleFieldConstraint);
final SingleFieldConstraint sfc_2_0 = (SingleFieldConstraint) cfc_2.getConstraint(0);
assertEquals("PhoneNumber", sfc_2_0.getFactType());
assertEquals("workPhone", sfc_2_0.getFieldName());
assertEquals(DataType.TYPE_STRING, sfc_2_0.getFieldType());
assertEquals("!= null", sfc_2_0.getOperator());
assertNull(sfc_2_0.getValue());
final SingleFieldConstraint sfc_2_1 = (SingleFieldConstraint) cfc_2.getConstraint(1);
assertEquals("PhoneNumber", sfc_2_1.getFactType());
assertEquals("workPhone", sfc_2_1.getFieldName());
assertEquals(DataType.TYPE_STRING, sfc_2_1.getFieldType());
assertEquals("matches", sfc_2_1.getOperator());
assertEquals("\\\"+9177\\\"", sfc_2_1.getValue());
// Check round-trip
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.FactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testDSLWithMultilineBracketsTwoFields.
@Test
public void testDSLWithMultilineBracketsTwoFields() {
String drl = "rule \"rule1\"\n" + "when\n" + "> Applicant( name == \n" + "> \"John\"\n" + "> , \n" + "> age > 18 )\n" + "then\n" + "end\n";
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshalUsingDSL(drl, Collections.emptyList(), dmo, "");
assertNotNull(m);
assertTrue(m.lhs[0] instanceof FactPattern);
FactPattern pattern = (FactPattern) m.lhs[0];
assertEquals("Applicant", pattern.getFactType());
assertEquals(2, pattern.getNumberOfConstraints());
assertEquals("name", ((SingleFieldConstraint) pattern.getConstraint(0)).getFieldName());
assertEquals("==", ((SingleFieldConstraint) pattern.getConstraint(0)).getOperator());
assertEquals("John", ((SingleFieldConstraint) pattern.getConstraint(0)).getValue());
assertEquals("age", ((SingleFieldConstraint) pattern.getConstraint(1)).getFieldName());
assertEquals(">", ((SingleFieldConstraint) pattern.getConstraint(1)).getOperator());
assertEquals("18", ((SingleFieldConstraint) pattern.getConstraint(1)).getValue());
}
Aggregations