Search in sources :

Example 6 with ConnectiveConstraint

use of org.drools.workbench.models.datamodel.rule.ConnectiveConstraint in project drools by kiegroup.

the class RuleModelDRLPersistenceTest method testCompositeConstraints.

@Test
public void testCompositeConstraints() {
    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.setFieldType(DataType.TYPE_STRING);
    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].setFieldType(DataType.TYPE_STRING);
    X.getConnectives()[0].setOperator("|| ==");
    X.getConnectives()[0].setValue("bar");
    comp.addConstraint(X);
    final SingleFieldConstraint Y = new SingleFieldConstraint();
    Y.setFieldName("goo2");
    Y.setFieldType(DataType.TYPE_STRING);
    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.setFieldType(DataType.TYPE_STRING);
    Q1.setFieldName("goo");
    Q1.setOperator("==");
    Q1.setValue("whee");
    Q1.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
    comp2.addConstraint(Q1);
    final SingleFieldConstraint Q2 = new SingleFieldConstraint();
    Q2.setFieldType(DataType.TYPE_STRING);
    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.setFieldType(DataType.TYPE_STRING);
    Z.setValue("foo");
    Z.setOperator("==");
    p.addConstraint(Z);
    ActionInsertFact ass = new ActionInsertFact("Whee");
    m.addRhsItem(ass);
    String actual = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
    String expected = "rule \"with composite\"\n" + "\tdialect \"mvel\"\n" + "\twhen\n" + "\t\tp1 : Person( )\n" + "\t\tGoober( goo == \"foo\"  || == \"bar\" || goo2 == \"foo\" || ( goo == \"whee\" && gabba == \"whee\" ), goo3 == \"foo\" )\n" + "\tthen\n" + "\t\tinsert( new Whee() );\n" + "end\n";
    assertEqualsIgnoreWhitespace(expected, actual);
    checkMarshalling(expected, m);
}
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) 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)

Example 7 with ConnectiveConstraint

use of org.drools.workbench.models.datamodel.rule.ConnectiveConstraint in project drools by kiegroup.

the class RuleModelVisitor method visitSingleFieldConstraint.

private void visitSingleFieldConstraint(SingleFieldConstraint sfc) {
    InterpolationVariable var = new InterpolationVariable(sfc.getValue(), sfc.getFieldType(), (factPattern == null ? "" : factPattern.getFactType()), sfc.getFieldName());
    if (BaseSingleFieldConstraint.TYPE_TEMPLATE == sfc.getConstraintValueType() && !vars.containsKey(var)) {
        vars.put(var, vars.size());
    }
    // 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.containsKey(ccVar)) {
                vars.put(ccVar, vars.size());
            }
        }
    }
}
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 8 with ConnectiveConstraint

use of org.drools.workbench.models.datamodel.rule.ConnectiveConstraint in project drools by kiegroup.

the class RuleModelVisitor method visitSingleFieldConstraint.

private void visitSingleFieldConstraint(SingleFieldConstraintEBLeftSide sfexp) {
    String genericType = sfexp.getExpressionLeftSide().getGenericType();
    String factType = sfexp.getExpressionLeftSide().getPreviousClassType();
    if (factType == null) {
        factType = sfexp.getExpressionLeftSide().getClassType();
    }
    InterpolationVariable var = new InterpolationVariable(sfexp.getValue(), genericType, factType, sfexp.getFieldName());
    if (BaseSingleFieldConstraint.TYPE_TEMPLATE == sfexp.getConstraintValueType() && !vars.containsKey(var)) {
        vars.put(var, vars.size());
    }
    // Visit Connection constraints
    if (sfexp.getConnectives() != null) {
        for (int i = 0; i < sfexp.getConnectives().length; i++) {
            final ConnectiveConstraint cc = sfexp.getConnectives()[i];
            InterpolationVariable ccVar = new InterpolationVariable(cc.getValue(), genericType, factType, cc.getFieldName());
            if (BaseSingleFieldConstraint.TYPE_TEMPLATE == cc.getConstraintValueType() && !vars.containsKey(ccVar)) {
                vars.put(ccVar, vars.size());
            }
        }
    }
}
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 9 with ConnectiveConstraint

use of org.drools.workbench.models.datamodel.rule.ConnectiveConstraint in project drools by kiegroup.

the class RuleModelUpgradeHelper2 method fixConnectiveConstraints.

private void fixConnectiveConstraints(SingleFieldConstraint sfc) {
    if (sfc.getConnectives() == null) {
        return;
    }
    for (ConnectiveConstraint cc : sfc.getConnectives()) {
        if (cc.getFieldName() == null) {
            cc.setFieldName(sfc.getFieldName());
            cc.setFieldType(sfc.getFieldType());
        }
    }
}
Also used : ConnectiveConstraint(org.drools.workbench.models.datamodel.rule.ConnectiveConstraint)

Example 10 with ConnectiveConstraint

use of org.drools.workbench.models.datamodel.rule.ConnectiveConstraint in project drools by kiegroup.

the class RuleModelUpgradeHelper3 method fixConstraints.

private void fixConstraints(FactPattern fp, SingleFieldConstraint sfc) {
    final FieldConstraint parent = sfc.getParent();
    if (parent == null) {
        sfc.setFactType(fp.getFactType());
    } else if (parent instanceof SingleFieldConstraint) {
        sfc.setFactType(((SingleFieldConstraint) parent).getFieldType());
    }
    sfc.setFieldName(fixFieldName(sfc.getFieldName()));
    if (sfc.getConnectives() == null) {
        return;
    }
    for (ConnectiveConstraint cc : sfc.getConnectives()) {
        cc.setFactType(fp.getFactType());
        cc.setFieldName(fixFieldName(cc.getFieldName()));
    }
}
Also used : SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) 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