use of org.drools.workbench.models.datamodel.rule.ConnectiveConstraint in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testSingleFieldConstraintConnectives4.
@Test
public // https://issues.jboss.org/browse/RHBRMS-2854
void testSingleFieldConstraintConnectives4() {
String drl = "package org.test;\n" + "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + " Applicant(age != null && != 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("!= null", sfp.getOperator());
assertNull(sfp.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_UNDEFINED, sfp.getConstraintValueType());
assertEquals(3, sfp.getConnectives().length);
ConnectiveConstraint cc1 = sfp.getConnectives()[0];
assertEquals("Applicant", cc1.getFactType());
assertEquals("age", cc1.getFieldName());
assertEquals("&& !=", cc1.getOperator());
assertEquals("1", 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("10", cc2.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cc2.getConstraintValueType());
ConnectiveConstraint cc3 = sfp.getConnectives()[2];
assertEquals("Applicant", cc3.getFactType());
assertEquals("age", cc3.getFieldName());
assertEquals("&& >", cc3.getOperator());
assertEquals("5", cc3.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cc3.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 testSingleFieldConstraintConnectives2.
@Test
public // https://issues.jboss.org/browse/GUVNOR-2141
void testSingleFieldConstraintConnectives2() {
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 by kiegroup.
the class RuleModelDRLPersistenceImpl method findOperator.
private static String findOperator(String expr) {
// only need to try to find the first operator before the ConnectiveConstraint separator.
if (expr.contains("&&")) {
expr = expr.substring(0, expr.indexOf("&&")).trim();
}
if (expr.contains("||")) {
expr = expr.substring(0, expr.indexOf("||")).trim();
}
final Set<String> potentialOperators = new HashSet<String>();
for (Operator op : Operator.getAllOperators()) {
String opString = op.getOperatorString();
if (op.isNegated()) {
if (expr.contains(" not " + opString)) {
return "not " + opString;
}
}
int opPos = expr.indexOf(opString);
if (opPos >= 0 && !isInQuote(expr, opPos) && !(Character.isLetter(opString.charAt(0)) && (expr.length() == opPos + opString.length() || Character.isLetter(expr.charAt(opPos + opString.length())) || (opPos > 0 && Character.isLetter(expr.charAt(opPos - 1)))))) {
potentialOperators.add(opString);
}
}
String operator = "";
if (!potentialOperators.isEmpty()) {
for (String potentialOperator : potentialOperators) {
if (potentialOperator.length() > operator.length()) {
operator = potentialOperator;
}
}
}
if (!operator.isEmpty()) {
return operator;
}
if (expr.contains(" not in ")) {
return " not in ";
}
if (expr.contains(" in ")) {
return " in ";
}
return null;
}
use of org.drools.workbench.models.datamodel.rule.ConnectiveConstraint in project drools by kiegroup.
the class GeneratorContextRuleModelVisitor method visitSingleFieldConstraint.
private void visitSingleFieldConstraint(final SingleFieldConstraint sfc) {
final InterpolationVariable var = new InterpolationVariable(sfc.getValue(), sfc.getFieldType(), (factPattern == null ? "" : factPattern.getFactType()), sfc.getFieldName());
if (BaseSingleFieldConstraint.TYPE_TEMPLATE == sfc.getConstraintValueType() && !vars.contains(var)) {
vars.add(var);
} else {
hasNonTemplateOutput = true;
}
// Visit Connection constraints
if (sfc.getConnectives() != null) {
for (int i = 0; i < sfc.getConnectives().length; i++) {
final ConnectiveConstraint cc = sfc.getConnectives()[i];
InterpolationVariable ccVar = new InterpolationVariable(cc.getValue(), cc.getFieldType(), (factPattern == null ? "" : factPattern.getFactType()), cc.getFieldName());
if (BaseSingleFieldConstraint.TYPE_TEMPLATE == cc.getConstraintValueType() && !vars.contains(ccVar)) {
vars.add(ccVar);
} else {
hasNonTemplateOutput = true;
}
}
}
}
use of org.drools.workbench.models.datamodel.rule.ConnectiveConstraint in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testCompositeConstraintsRoundTrip.
@Test
public void testCompositeConstraintsRoundTrip() throws Exception {
RuleModel m = new RuleModel();
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 drl = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
PackageDataModelOracle dmo = mock(PackageDataModelOracle.class);
RuleModel m2 = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.EMPTY_LIST, dmo);
assertNotNull(m2);
assertEquals("with composite", m2.name);
assertEquals(m2.lhs.length, m.lhs.length);
assertEquals(m2.rhs.length, m.rhs.length);
}
Aggregations