use of org.drools.workbench.models.datamodel.rule.ConnectiveConstraint in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testConnective.
@Test
public void testConnective() {
RuleModel m = new RuleModel();
m.name = "test literal strings";
FactPattern p = new FactPattern("Person");
SingleFieldConstraint con = new SingleFieldConstraint();
con.setFieldType(DataType.TYPE_STRING);
con.setFieldName("field1");
con.setOperator("==");
con.setValue("goo");
con.setConstraintValueType(SingleFieldConstraint.TYPE_VARIABLE);
p.addConstraint(con);
ConnectiveConstraint connective = new ConnectiveConstraint();
connective.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
connective.setFieldType(DataType.TYPE_STRING);
connective.setOperator("|| ==");
connective.setValue("blah");
con.setConnectives(new ConnectiveConstraint[1]);
con.getConnectives()[0] = connective;
m.addLhsItem(p);
String expected = "rule \"test literal strings\" " + "\tdialect \"mvel\"\n when " + "Person( field1 == goo || == \"blah\" )" + " then " + "end";
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.ConnectiveConstraint in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testSingleFieldConstraintConnectives3.
@Test
public // https://issues.jboss.org/browse/RHBRMS-2854
void testSingleFieldConstraintConnectives3() {
String drl = "package org.test;\n" + "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + " Applicant(age != 1 && < 10 && > 5)\n" + "then\n" + "end";
addModelField("org.test.Applicant", "this", "org.test.Applicant", DataType.TYPE_THIS);
addModelField("org.test.Applicant", "age", Integer.class.getName(), DataType.TYPE_NUMERIC_INTEGER);
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(1, fp.getConstraintList().getConstraints().length);
assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraint);
SingleFieldConstraint sfp = (SingleFieldConstraint) fp.getConstraint(0);
assertEquals("Applicant", sfp.getFactType());
assertEquals("age", sfp.getFieldName());
assertEquals("!=", sfp.getOperator());
assertEquals("1", sfp.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp.getConstraintValueType());
assertEquals(2, sfp.getConnectives().length);
ConnectiveConstraint cc1 = sfp.getConnectives()[0];
assertEquals("Applicant", cc1.getFactType());
assertEquals("age", cc1.getFieldName());
assertEquals("&& <", cc1.getOperator());
assertEquals("10", cc1.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cc1.getConstraintValueType());
ConnectiveConstraint cc2 = sfp.getConnectives()[1];
assertEquals("Applicant", cc2.getFactType());
assertEquals("age", cc2.getFieldName());
assertEquals("&& >", cc2.getOperator());
assertEquals("5", cc2.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cc2.getConstraintValueType());
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.ConnectiveConstraint in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testSingleFieldConstraintConnectives1.
@Test
public // https://issues.jboss.org/browse/GUVNOR-2141
void testSingleFieldConstraintConnectives1() {
String drl = "package org.test;\n" + "rule \"rule1\"\n" + "dialect \"mvel\"\n" + " when\n" + " Applicant( age < 55 || > 75 )\n" + " then\n" + "end";
addModelField("org.test.Applicant", "this", "org.test.Applicant", DataType.TYPE_THIS);
addModelField("org.test.Applicant", "age", Integer.class.getName(), DataType.TYPE_NUMERIC_INTEGER);
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(1, fp.getConstraintList().getConstraints().length);
assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraint);
SingleFieldConstraint sfp = (SingleFieldConstraint) fp.getConstraint(0);
assertEquals("Applicant", sfp.getFactType());
assertEquals("age", sfp.getFieldName());
assertEquals("<", sfp.getOperator());
assertEquals("55", sfp.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp.getConstraintValueType());
assertEquals(1, sfp.getConnectives().length);
ConnectiveConstraint cc = sfp.getConnectives()[0];
assertEquals("Applicant", cc.getFactType());
assertEquals("age", cc.getFieldName());
assertEquals("|| >", cc.getOperator());
assertEquals("75", cc.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cc.getConstraintValueType());
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.ConnectiveConstraint in project drools-wb by kiegroup.
the class RuleModelCloneVisitor method visitSingleFieldConstraint.
private SingleFieldConstraint visitSingleFieldConstraint(SingleFieldConstraint sfc) {
SingleFieldConstraint clone = new SingleFieldConstraint();
clone.setConstraintValueType(sfc.getConstraintValueType());
clone.setExpressionValue((ExpressionFormLine) visit(sfc.getExpressionValue()));
clone.setFieldBinding(sfc.getFieldBinding());
clone.setFactType(sfc.getFactType());
clone.setFieldName(sfc.getFieldName());
clone.setFieldType(sfc.getFieldType());
clone.setId(sfc.getId());
clone.setOperator(sfc.getOperator());
clone.setParent((FieldConstraint) visit(sfc.getParent()));
for (Map.Entry<String, String> entry : sfc.getParameters().entrySet()) {
clone.setParameter(entry.getKey(), entry.getValue());
}
clone.setValue(sfc.getValue());
if (sfc.getConnectives() != null) {
clone.setConnectives(new ConnectiveConstraint[sfc.getConnectives().length]);
for (int i = 0; i < sfc.getConnectives().length; i++) {
clone.getConnectives()[i] = (ConnectiveConstraint) visit(sfc.getConnectives()[i]);
}
}
return clone;
}
use of org.drools.workbench.models.datamodel.rule.ConnectiveConstraint in project drools-wb by kiegroup.
the class RuleModelCloneVisitor method visitSingleFieldConstraint.
private SingleFieldConstraintEBLeftSide visitSingleFieldConstraint(SingleFieldConstraintEBLeftSide sfexp) {
SingleFieldConstraintEBLeftSide clone = new SingleFieldConstraintEBLeftSide();
clone.setConstraintValueType(sfexp.getConstraintValueType());
clone.setExpressionLeftSide((ExpressionFormLine) visit(sfexp.getExpressionLeftSide()));
clone.setExpressionValue((ExpressionFormLine) visit(sfexp.getExpressionValue()));
clone.setFactType(sfexp.getFactType());
// skip setting fieldBinding, fieldName and fieldType, these are computed from expressionLeftSide
clone.setId(sfexp.getId());
clone.setOperator(sfexp.getOperator());
for (Map.Entry<String, String> entry : sfexp.getParameters().entrySet()) {
clone.setParameter(entry.getKey(), entry.getValue());
}
clone.setParent(sfexp.getParent());
clone.setValue(sfexp.getValue());
if (sfexp.getConnectives() != null) {
clone.setConnectives(new ConnectiveConstraint[sfexp.getConnectives().length]);
for (int i = 0; i < sfexp.getConnectives().length; i++) {
clone.getConnectives()[i] = (ConnectiveConstraint) visit(sfexp.getConnectives()[i]);
}
}
return clone;
}
Aggregations