Search in sources :

Example 1 with ConnectiveConstraint

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));
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) ConnectiveConstraint(org.drools.workbench.models.datamodel.rule.ConnectiveConstraint) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 2 with ConnectiveConstraint

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));
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) ConnectiveConstraint(org.drools.workbench.models.datamodel.rule.ConnectiveConstraint) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 3 with ConnectiveConstraint

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;
}
Also used : Operator(org.drools.core.base.evaluators.Operator) FieldConstraint(org.drools.workbench.models.datamodel.rule.FieldConstraint) ConnectiveConstraint(org.drools.workbench.models.datamodel.rule.ConnectiveConstraint) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) HashSet(java.util.HashSet)

Example 4 with ConnectiveConstraint

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;
            }
        }
    }
}
Also used : InterpolationVariable(org.drools.workbench.models.datamodel.rule.InterpolationVariable) ConnectiveConstraint(org.drools.workbench.models.datamodel.rule.ConnectiveConstraint) ConnectiveConstraint(org.drools.workbench.models.datamodel.rule.ConnectiveConstraint) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) FieldConstraint(org.drools.workbench.models.datamodel.rule.FieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)

Example 5 with ConnectiveConstraint

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);
}
Also used : SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) PackageDataModelOracle(org.kie.soup.project.datamodel.oracle.PackageDataModelOracle) FromEntryPointFactPattern(org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) ConnectiveConstraint(org.drools.workbench.models.datamodel.rule.ConnectiveConstraint) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) Test(org.junit.Test)

Aggregations

ConnectiveConstraint (org.drools.workbench.models.datamodel.rule.ConnectiveConstraint)28 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)25 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)21 CompositeFieldConstraint (org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint)15 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)15 Test (org.junit.Test)15 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)14 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)13 FieldConstraint (org.drools.workbench.models.datamodel.rule.FieldConstraint)9 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)9 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)7 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)7 TemplateModel (org.drools.workbench.models.guided.template.shared.TemplateModel)7 HashMap (java.util.HashMap)4 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)4 InterpolationVariable (org.drools.workbench.models.datamodel.rule.InterpolationVariable)4 Map (java.util.Map)3 ActionInsertFact (org.drools.workbench.models.datamodel.rule.ActionInsertFact)3 FromEntryPointFactPattern (org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern)3 HashSet (java.util.HashSet)2