Search in sources :

Example 11 with FieldConstraint

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

the class GuidedDTDRLPersistence method doCondition.

private void doCondition(List<BaseColumn> allColumns, Pattern52 pattern, List<IPattern> patterns, List<DTCellValue52> row, List<List<DTCellValue52>> data, RuleModel rm) {
    List<ConditionCol52> cols = pattern.getChildColumns();
    for (ConditionCol52 c : cols) {
        int index = allColumns.indexOf(c);
        DTCellValue52 dcv = row.get(index);
        String cell = "";
        if (c instanceof LimitedEntryCol) {
            if (Boolean.TRUE.equals(dcv.getBooleanValue())) {
                LimitedEntryCol lec = (LimitedEntryCol) c;
                DTCellValue52 value = lec.getValue();
                if (value != null) {
                    cell = GuidedDTDRLUtilities.convertDTCellValueToString(value);
                }
            }
        } else {
            cell = GuidedDTDRLUtilities.convertDTCellValueToString(dcv);
        }
        boolean isOtherwise = dcv.isOtherwise();
        boolean isValid = isOtherwise;
        // Otherwise values are automatically valid as they're constructed from the other rules
        if (!isOtherwise) {
            isValid = validCell(cell, dcv.getDataType());
        }
        // If operator is "== null" or "!= null" add constraint if table value is true
        if (c.getOperator() != null && (c.getOperator().equals("== null") || c.getOperator().equals("!= null"))) {
            isValid = Boolean.TRUE.equals(dcv.getBooleanValue());
        }
        if (isValid) {
            // get or create the pattern it belongs too
            IPattern ifp = findByFactPattern(patterns, pattern);
            // If the pattern does not exist create one suitable
            if (ifp == null) {
                FactPattern fp = new FactPattern(pattern.getFactType());
                fp.setBoundName(pattern.getBoundName());
                fp.setNegated(pattern.isNegated());
                fp.setWindow(pattern.getWindow());
                if (pattern.getEntryPointName() != null && pattern.getEntryPointName().length() > 0) {
                    FromEntryPointFactPattern fep = new FromEntryPointFactPattern();
                    fep.setEntryPointName(pattern.getEntryPointName());
                    fep.setFactPattern(fp);
                    patterns.add(fep);
                    ifp = fep;
                } else {
                    patterns.add(fp);
                    ifp = fp;
                }
            }
            // Extract the FactPattern from the IFactPattern
            FactPattern fp;
            if (ifp instanceof FactPattern) {
                fp = (FactPattern) ifp;
            } else if (ifp instanceof FromEntryPointFactPattern) {
                FromEntryPointFactPattern fep = (FromEntryPointFactPattern) ifp;
                fp = fep.getFactPattern();
            } else {
                throw new IllegalArgumentException("Inexpected IFactPattern implementation found.");
            }
            // Add the constraint from this cell
            switch(c.getConstraintValueType()) {
                case BaseSingleFieldConstraint.TYPE_LITERAL:
                case BaseSingleFieldConstraint.TYPE_RET_VALUE:
                    if (!isOtherwise) {
                        FieldConstraint fc = makeSingleFieldConstraint(c, cell);
                        fp.addConstraint(fc);
                    } else {
                        FieldConstraint fc = makeSingleFieldConstraint(c, allColumns, data);
                        fp.addConstraint(fc);
                    }
                    break;
                case BaseSingleFieldConstraint.TYPE_PREDICATE:
                    SingleFieldConstraint pred = new SingleFieldConstraint();
                    pred.setConstraintValueType(c.getConstraintValueType());
                    if (c.getFactField() != null && c.getFactField().indexOf("$param") > -1) {
                        // handle interpolation
                        pred.setValue(c.getFactField().replace("$param", cell));
                    } else {
                        pred.setValue(cell);
                    }
                    fp.addConstraint(pred);
                    break;
                default:
                    throw new IllegalArgumentException("Unknown constraintValueType: " + c.getConstraintValueType());
            }
        }
    }
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) FromEntryPointFactPattern(org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern) FromEntryPointFactPattern(org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) DTCellValue52(org.drools.workbench.models.guided.dtable.shared.model.DTCellValue52) FieldConstraint(org.drools.workbench.models.datamodel.rule.FieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) LimitedEntryCol(org.drools.workbench.models.guided.dtable.shared.model.LimitedEntryCol) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) ConditionCol52(org.drools.workbench.models.guided.dtable.shared.model.ConditionCol52) FieldConstraint(org.drools.workbench.models.datamodel.rule.FieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)

Example 12 with FieldConstraint

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

the class GuidedDTDRLPersistence method makeSingleFieldConstraint.

// Build a normal SingleFieldConstraint for a non-otherwise cell value
private FieldConstraint makeSingleFieldConstraint(ConditionCol52 c, String cell) {
    SingleFieldConstraint sfc = new SingleFieldConstraint(c.getFactField());
    // expansion and contraction of decision table columns.... this might have to go.
    if (no(c.getOperator())) {
        String[] a = cell.split("\\s");
        if (a.length > 1) {
            // Operator might be 1 part (e.g. "==") or two parts (e.g. "not in")
            StringBuilder operator = new StringBuilder(a[0]);
            for (int i = 1; i < a.length - 1; i++) {
                operator.append(a[i]);
            }
            sfc.setOperator(operator.toString());
            sfc.setValue(a[a.length - 1]);
        } else {
            sfc.setValue(cell);
        }
    } else {
        sfc.setOperator(c.getOperator());
        if (OperatorsOracle.operatorRequiresList(c.getOperator())) {
            sfc.setValue(makeInList(cell));
        } else {
            if (!c.getOperator().equals("== null") && !c.getOperator().equals("!= null")) {
                sfc.setValue(cell);
            }
        }
    }
    final int constraintValueType = c.getConstraintValueType();
    if ((constraintValueType == BaseSingleFieldConstraint.TYPE_LITERAL || constraintValueType == BaseSingleFieldConstraint.TYPE_RET_VALUE) && c.isBound()) {
        sfc.setFieldBinding(c.getBinding());
    }
    sfc.setParameters(c.getParameters());
    sfc.setConstraintValueType(c.getConstraintValueType());
    sfc.setFieldType(c.getFieldType());
    return sfc;
}
Also used : SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) FieldConstraint(org.drools.workbench.models.datamodel.rule.FieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)

Example 13 with FieldConstraint

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

the class BRLRuleModelTest method testDecisionTableColumnsWithLHSBoundFieldsInConditionCol52.

@Test
public void testDecisionTableColumnsWithLHSBoundFieldsInConditionCol52() {
    GuidedDecisionTable52 dt = new GuidedDecisionTable52();
    Pattern52 p1 = new Pattern52();
    p1.setFactType("Driver");
    p1.setBoundName("$p1");
    ConditionCol52 c1 = new ConditionCol52();
    c1.setFactField("name");
    c1.setFieldType(DataType.TYPE_STRING);
    c1.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
    c1.setBinding("$c1");
    p1.getChildColumns().add(c1);
    dt.getConditions().add(p1);
    BRLRuleModel model = new BRLRuleModel(dt);
    FieldConstraint fcr1 = model.getLHSBoundField("$c1");
    assertNotNull(fcr1);
    assertTrue(fcr1 instanceof ConditionCol52FieldConstraintAdaptor);
    ConditionCol52FieldConstraintAdaptor fcr1sfc = (ConditionCol52FieldConstraintAdaptor) fcr1;
    assertEquals("Driver", fcr1sfc.getFactType());
    assertEquals("name", fcr1sfc.getFieldName());
    assertEquals(DataType.TYPE_STRING, fcr1sfc.getFieldType());
}
Also used : ConditionCol52(org.drools.workbench.models.guided.dtable.shared.model.ConditionCol52) GuidedDecisionTable52(org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52) Pattern52(org.drools.workbench.models.guided.dtable.shared.model.Pattern52) BRLRuleModel(org.drools.workbench.models.guided.dtable.shared.model.BRLRuleModel) ConditionCol52FieldConstraintAdaptor(org.drools.workbench.models.guided.dtable.shared.model.adaptors.ConditionCol52FieldConstraintAdaptor) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) FieldConstraint(org.drools.workbench.models.datamodel.rule.FieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) Test(org.junit.Test)

Example 14 with FieldConstraint

use of org.drools.workbench.models.datamodel.rule.FieldConstraint 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 15 with FieldConstraint

use of org.drools.workbench.models.datamodel.rule.FieldConstraint in project drools-wb by kiegroup.

the class RuleModelCloneVisitor method visitFactPattern.

private FactPattern visitFactPattern(FactPattern pattern) {
    FactPattern clone = new FactPattern();
    clone.setBoundName(pattern.getBoundName());
    clone.setFactType(pattern.getFactType());
    clone.setNegated(pattern.isNegated());
    CEPWindow cloneCEPWindow = new CEPWindow();
    cloneCEPWindow.setOperator(pattern.getWindow().getOperator());
    cloneCEPWindow.setParameters(cloneCEPWindowParameters(pattern.getWindow()));
    clone.setWindow(cloneCEPWindow);
    for (FieldConstraint fc : pattern.getFieldConstraints()) {
        clone.addConstraint((FieldConstraint) visit(fc));
    }
    return clone;
}
Also used : CEPWindow(org.drools.workbench.models.datamodel.rule.CEPWindow) IFactPattern(org.drools.workbench.models.datamodel.rule.IFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) 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

FieldConstraint (org.drools.workbench.models.datamodel.rule.FieldConstraint)23 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)23 CompositeFieldConstraint (org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint)18 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)10 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)7 ConnectiveConstraint (org.drools.workbench.models.datamodel.rule.ConnectiveConstraint)5 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)3 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)3 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)3 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)3 IFactPattern (org.drools.workbench.models.datamodel.rule.IFactPattern)3 ConditionCol52 (org.drools.workbench.models.guided.dtable.shared.model.ConditionCol52)3 FlexTable (com.google.gwt.user.client.ui.FlexTable)2 Image (com.google.gwt.user.client.ui.Image)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LHSGeneratorContextFactory (org.drools.workbench.models.commons.backend.rule.context.LHSGeneratorContextFactory)2 RHSGeneratorContextFactory (org.drools.workbench.models.commons.backend.rule.context.RHSGeneratorContextFactory)2