use of org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint in project drools by kiegroup.
the class RuleTemplateModelXMLPersistenceTest method testCompositeConstraintsRoundTrip.
@Test
public void testCompositeConstraintsRoundTrip() throws Exception {
TemplateModel m = new TemplateModel();
m.name = "with composite";
FactPattern p1 = new FactPattern("Person");
p1.setBoundName("p1");
m.addLhsItem(p1);
FactPattern p = new FactPattern("Goober");
m.addLhsItem(p);
CompositeFieldConstraint comp = new CompositeFieldConstraint();
comp.setCompositeJunctionType(CompositeFieldConstraint.COMPOSITE_TYPE_OR);
p.addConstraint(comp);
final SingleFieldConstraint X = new SingleFieldConstraint();
X.setFieldName("goo");
X.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
X.setValue("foo");
X.setOperator("==");
X.setConnectives(new ConnectiveConstraint[1]);
X.getConnectives()[0] = new ConnectiveConstraint();
X.getConnectives()[0].setConstraintValueType(ConnectiveConstraint.TYPE_LITERAL);
X.getConnectives()[0].setOperator("|| ==");
X.getConnectives()[0].setValue("bar");
comp.addConstraint(X);
final SingleFieldConstraint Y = new SingleFieldConstraint();
Y.setFieldName("goo2");
Y.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
Y.setValue("foo");
Y.setOperator("==");
comp.addConstraint(Y);
CompositeFieldConstraint comp2 = new CompositeFieldConstraint();
comp2.setCompositeJunctionType(CompositeFieldConstraint.COMPOSITE_TYPE_AND);
final SingleFieldConstraint Q1 = new SingleFieldConstraint();
Q1.setFieldName("goo");
Q1.setOperator("==");
Q1.setValue("whee");
Q1.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
comp2.addConstraint(Q1);
final SingleFieldConstraint Q2 = new SingleFieldConstraint();
Q2.setFieldName("gabba");
Q2.setOperator("==");
Q2.setValue("whee");
Q2.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
comp2.addConstraint(Q2);
// now nest it
comp.addConstraint(comp2);
final SingleFieldConstraint Z = new SingleFieldConstraint();
Z.setFieldName("goo3");
Z.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
Z.setValue("foo");
Z.setOperator("==");
p.addConstraint(Z);
ActionInsertFact ass = new ActionInsertFact("Whee");
m.addRhsItem(ass);
String xml = RuleTemplateModelXMLPersistenceImpl.getInstance().marshal(m);
// System.err.println(xml);
RuleModel m2 = RuleTemplateModelXMLPersistenceImpl.getInstance().unmarshal(xml);
assertNotNull(m2);
assertEquals("with composite", m2.name);
assertEquals(m2.lhs.length, m.lhs.length);
assertEquals(m2.rhs.length, m.rhs.length);
}
use of org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testCompositeAndConstraintsComplex.
@Test
public void testCompositeAndConstraintsComplex() {
RuleModel m = new RuleModel();
m.name = "and composite complex";
FactPattern p = new FactPattern("Goober");
m.addLhsItem(p);
CompositeFieldConstraint comp = new CompositeFieldConstraint();
comp.setCompositeJunctionType(CompositeFieldConstraint.COMPOSITE_TYPE_AND);
p.addConstraint(comp);
final SingleFieldConstraint sfc1 = new SingleFieldConstraint();
sfc1.setFactType("Goober");
sfc1.setFieldName("gooField");
sfc1.setFieldType(DataType.TYPE_STRING);
sfc1.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
sfc1.setValue("gooValue");
sfc1.setOperator("==");
comp.addConstraint(sfc1);
final SingleFieldConstraint sfc2 = new SingleFieldConstraint();
sfc2.setFactType("Goober");
sfc2.setFieldName("fooField");
sfc2.setFieldType(DataType.TYPE_OBJECT);
sfc2.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
sfc2.setOperator("!= null");
comp.addConstraint(sfc2);
final SingleFieldConstraint sfc3 = new SingleFieldConstraint();
sfc1.setFactType("Bar");
sfc3.setFieldName("barField");
sfc3.setParent(sfc2);
sfc3.setFieldType(DataType.TYPE_STRING);
sfc3.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
sfc3.setValue("barValue");
sfc3.setOperator("==");
comp.addConstraint(sfc3);
final SingleFieldConstraint sfc4 = new SingleFieldConstraint();
sfc4.setFactType("Goober");
sfc4.setFieldName("zooField");
sfc4.setFieldType(DataType.TYPE_STRING);
sfc4.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
sfc4.setValue("zooValue");
sfc4.setOperator("==");
p.addConstraint(sfc4);
ActionInsertFact ass = new ActionInsertFact("Whee");
m.addRhsItem(ass);
String actual = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
String expected = "rule \"and composite complex\"" + "dialect \"mvel\"\n" + "when\n" + "Goober( gooField == \"gooValue\" && fooField != null && fooField.barField == \"barValue\", zooField == \"zooValue\" )\n" + "then\n" + "insert( new Whee() );\n" + "end";
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testCompositeFactPatternWithOrAndCompositeFieldConstraint.
@Test
public void testCompositeFactPatternWithOrAndCompositeFieldConstraint() {
String drl = "rule \"rule1\"\n" + "when\n" + "( Person( age == 42 ) or Person( age == 43 || age == 44) )\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(1, fp2.getConstraintList().getConstraints().length);
assertTrue(fp2.getConstraint(0) instanceof CompositeFieldConstraint);
CompositeFieldConstraint cfp_p2_cfp = (CompositeFieldConstraint) fp2.getConstraint(0);
assertEquals("||", cfp_p2_cfp.getCompositeJunctionType());
assertEquals(2, cfp_p2_cfp.getNumberOfConstraints());
assertTrue(cfp_p2_cfp.getConstraint(0) instanceof SingleFieldConstraint);
assertTrue(cfp_p2_cfp.getConstraint(1) instanceof SingleFieldConstraint);
SingleFieldConstraint cfp_p2_sfp1 = (SingleFieldConstraint) cfp_p2_cfp.getConstraint(0);
assertEquals("Person", cfp_p2_sfp1.getFactType());
assertEquals("age", cfp_p2_sfp1.getFieldName());
assertEquals("==", cfp_p2_sfp1.getOperator());
assertEquals("43", cfp_p2_sfp1.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cfp_p2_sfp1.getConstraintValueType());
SingleFieldConstraint cfp_p2_sfp2 = (SingleFieldConstraint) cfp_p2_cfp.getConstraint(1);
assertEquals("Person", cfp_p2_sfp2.getFactType());
assertEquals("age", cfp_p2_sfp2.getFieldName());
assertEquals("==", cfp_p2_sfp2.getOperator());
assertEquals("44", cfp_p2_sfp2.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cfp_p2_sfp2.getConstraintValueType());
}
use of org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testCompositeFieldConstraint.
@Test
public void testCompositeFieldConstraint() {
String drl = "rule \"rule1\"\n" + "when\n" + "Applicant( age < 55 || age > 70 )\n" + "then\n" + "end";
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(1, fp.getConstraintList().getConstraints().length);
assertTrue(fp.getConstraint(0) instanceof CompositeFieldConstraint);
CompositeFieldConstraint cfp = (CompositeFieldConstraint) fp.getConstraint(0);
assertEquals("||", cfp.getCompositeJunctionType());
assertEquals(2, cfp.getNumberOfConstraints());
assertTrue(cfp.getConstraint(0) instanceof SingleFieldConstraint);
assertTrue(cfp.getConstraint(1) instanceof SingleFieldConstraint);
SingleFieldConstraint sfp1 = (SingleFieldConstraint) cfp.getConstraint(0);
assertEquals("Applicant", sfp1.getFactType());
assertEquals("age", sfp1.getFieldName());
assertEquals("<", sfp1.getOperator());
assertEquals("55", sfp1.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp1.getConstraintValueType());
SingleFieldConstraint sfp2 = (SingleFieldConstraint) cfp.getConstraint(1);
assertEquals("Applicant", sfp2.getFactType());
assertEquals("age", sfp2.getFieldName());
assertEquals(">", sfp2.getOperator());
assertEquals("70", sfp2.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp2.getConstraintValueType());
}
use of org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testCompositeFieldConstraintWithNotNullOperator.
@Test
public void testCompositeFieldConstraintWithNotNullOperator() {
String drl = "rule \"rule1\"\n" + "when\n" + "Applicant( age != null && age > 70 )\n" + "then\n" + "end";
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(1, fp.getConstraintList().getConstraints().length);
assertTrue(fp.getConstraint(0) instanceof CompositeFieldConstraint);
CompositeFieldConstraint cfp = (CompositeFieldConstraint) fp.getConstraint(0);
assertEquals("&&", cfp.getCompositeJunctionType());
assertEquals(2, cfp.getNumberOfConstraints());
assertTrue(cfp.getConstraint(0) instanceof SingleFieldConstraint);
assertTrue(cfp.getConstraint(1) instanceof SingleFieldConstraint);
SingleFieldConstraint sfp1 = (SingleFieldConstraint) cfp.getConstraint(0);
assertEquals("Applicant", sfp1.getFactType());
assertEquals("age", sfp1.getFieldName());
assertEquals("!= null", sfp1.getOperator());
assertNull(sfp1.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_UNDEFINED, sfp1.getConstraintValueType());
SingleFieldConstraint sfp2 = (SingleFieldConstraint) cfp.getConstraint(1);
assertEquals("Applicant", sfp2.getFactType());
assertEquals("age", sfp2.getFieldName());
assertEquals(">", sfp2.getOperator());
assertEquals("70", sfp2.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp2.getConstraintValueType());
}
Aggregations