Search in sources :

Example 21 with ConnectiveConstraint

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

Example 22 with ConnectiveConstraint

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));
}
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 23 with ConnectiveConstraint

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));
}
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 24 with ConnectiveConstraint

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;
}
Also used : SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) HashMap(java.util.HashMap) Map(java.util.Map) 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)

Example 25 with ConnectiveConstraint

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;
}
Also used : SingleFieldConstraintEBLeftSide(org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide) HashMap(java.util.HashMap) Map(java.util.Map) 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)

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