use of org.drools.workbench.models.datamodel.rule.SingleFieldConstraint in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testCompositeFactPatternWithOr.
@Test
public void testCompositeFactPatternWithOr() {
String drl = "rule \"rule1\"\n" + "when\n" + "( Person( age == 42 ) or Person( age == 43 ) )\n" + "then\n" + "end";
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals("rule1", m.name);
// LHS Pattern
assertEquals(1, m.lhs.length);
IPattern p = m.lhs[0];
assertTrue(p instanceof CompositeFactPattern);
CompositeFactPattern cfp = (CompositeFactPattern) p;
assertEquals(CompositeFactPattern.COMPOSITE_TYPE_OR, cfp.getType());
// LHS sub-patterns
assertEquals(2, cfp.getPatterns().length);
IPattern cfp_p1 = cfp.getPatterns()[0];
assertTrue(cfp_p1 instanceof FactPattern);
FactPattern fp1 = (FactPattern) cfp_p1;
assertEquals("Person", fp1.getFactType());
assertEquals(1, fp1.getConstraintList().getConstraints().length);
assertTrue(fp1.getConstraint(0) instanceof SingleFieldConstraint);
SingleFieldConstraint cfp_sfp1 = (SingleFieldConstraint) fp1.getConstraint(0);
assertEquals("Person", cfp_sfp1.getFactType());
assertEquals("age", cfp_sfp1.getFieldName());
assertEquals("==", cfp_sfp1.getOperator());
assertEquals("42", cfp_sfp1.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cfp_sfp1.getConstraintValueType());
IPattern cfp_p2 = cfp.getPatterns()[1];
assertTrue(cfp_p2 instanceof FactPattern);
FactPattern fp2 = (FactPattern) cfp_p2;
assertEquals("Person", fp2.getFactType());
assertTrue(fp2.getConstraint(0) instanceof SingleFieldConstraint);
SingleFieldConstraint cfp_sfp2 = (SingleFieldConstraint) fp2.getConstraint(0);
assertEquals("Person", cfp_sfp2.getFactType());
assertEquals("age", cfp_sfp2.getFieldName());
assertEquals("==", cfp_sfp2.getOperator());
assertEquals("43", cfp_sfp2.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cfp_sfp2.getConstraintValueType());
}
use of org.drools.workbench.models.datamodel.rule.SingleFieldConstraint in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method compositeFieldConstraintWithTwoPredicates.
@Test
public void compositeFieldConstraintWithTwoPredicates() {
String drl = "rule \"rule1\"\n" + "when\n" + "Person( eval( age > 18 ) && eval(age < 45) )\n" + "then\n" + "end";
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals("rule1", m.name);
// LHS Pattern
assertEquals(1, m.lhs.length);
IPattern p = m.lhs[0];
assertTrue(p instanceof FactPattern);
FactPattern fp = (FactPattern) p;
// LHS sub-patterns
assertEquals(1, fp.getNumberOfConstraints());
assertTrue(fp.getConstraint(0) instanceof CompositeFieldConstraint);
CompositeFieldConstraint fp_cfp = (CompositeFieldConstraint) fp.getConstraint(0);
assertEquals("&&", fp_cfp.getCompositeJunctionType());
assertEquals(2, fp_cfp.getNumberOfConstraints());
assertTrue(fp_cfp.getConstraint(0) instanceof SingleFieldConstraint);
assertTrue(fp_cfp.getConstraint(1) instanceof SingleFieldConstraint);
SingleFieldConstraint fp_cfp_sfp1 = (SingleFieldConstraint) fp_cfp.getConstraint(0);
assertEquals("age > 18", fp_cfp_sfp1.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_PREDICATE, fp_cfp_sfp1.getConstraintValueType());
SingleFieldConstraint fp_cfp_sfp2 = (SingleFieldConstraint) fp_cfp.getConstraint(1);
assertEquals("age < 45", fp_cfp_sfp2.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_PREDICATE, fp_cfp_sfp2.getConstraintValueType());
}
use of org.drools.workbench.models.datamodel.rule.SingleFieldConstraint 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.SingleFieldConstraint 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.SingleFieldConstraint in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testNewKeywordVariableNamePrefix1.
@Test
public // https://issues.jboss.org/browse/GUVNOR-2143
void testNewKeywordVariableNamePrefix1() {
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" + " $bundle : Bundle( $treatmentEffectiveDt : treatmentEffectiveDt )\n" + " then\n" + " java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"dd-MMM-yyyy\");\n" + " DateTime newStartDate = new DateTime();\n" + " modify( $bundle ) {\n" + " setTreatmentEffectiveDt( newStartDate.toDate() )" + " }\n" + "end\n";
addModelField("org.test.Bundle", "this", "org.test.Bundle", DataType.TYPE_THIS);
addModelField("org.test.Bundle", "treatmentEffectiveDt", 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("Bundle", fp.getFactType());
assertEquals("$bundle", fp.getBoundName());
assertEquals(1, fp.getConstraintList().getConstraints().length);
assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraint);
SingleFieldConstraint sfp = (SingleFieldConstraint) fp.getConstraint(0);
assertEquals("Bundle", sfp.getFactType());
assertEquals("treatmentEffectiveDt", sfp.getFieldName());
assertEquals("$treatmentEffectiveDt", sfp.getFieldBinding());
assertNull(sfp.getOperator());
assertNull(sfp.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_UNDEFINED, sfp.getConstraintValueType());
assertEquals(2, m.rhs.length);
assertTrue(m.rhs[0] instanceof FreeFormLine);
FreeFormLine ffl = (FreeFormLine) m.rhs[0];
assertEquals("DateTime newStartDate = new DateTime();", ffl.getText());
assertTrue(m.rhs[1] instanceof ActionUpdateField);
ActionUpdateField auf = (ActionUpdateField) m.rhs[1];
assertEquals("$bundle", auf.getVariable());
assertEquals(1, auf.getFieldValues().length);
ActionFieldValue afv = auf.getFieldValues()[0];
assertEquals("treatmentEffectiveDt", afv.getField());
assertEquals("newStartDate.toDate()", 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);
}
}
}
Aggregations