Search in sources :

Example 1 with DDTARule

use of org.kie.dmn.validation.dtanalysis.model.DDTARule in project drools by kiegroup.

the class DMNDTAnalyser method compileTableRules.

private void compileTableRules(DecisionTable dt, DDTATable ddtaTable) {
    for (int jRowIdx = 0; jRowIdx < dt.getRule().size(); jRowIdx++) {
        DecisionRule r = dt.getRule().get(jRowIdx);
        DDTARule ddtaRule = new DDTARule();
        int jColIdx = 0;
        for (UnaryTests ie : r.getInputEntry()) {
            ProcessedUnaryTest compileUnaryTests = (ProcessedUnaryTest) FEEL.compileUnaryTests(ie.getText(), FEEL.newCompilerContext());
            UnaryTestInterpretedExecutableExpression interpreted = compileUnaryTests.getInterpreted();
            UnaryTestListNode utln = (UnaryTestListNode) interpreted.getASTNode();
            DDTAInputClause ddtaInputClause = ddtaTable.getInputs().get(jColIdx);
            DDTAInputEntry ddtaInputEntry = new DDTAInputEntry(utln.getElements(), toIntervals(utln.getElements(), utln.isNegated(), ddtaInputClause.getDomainMinMax(), ddtaInputClause.getDiscreteValues(), jRowIdx + 1, jColIdx + 1));
            for (Interval interval : ddtaInputEntry.getIntervals()) {
                Interval domainMinMax = ddtaTable.getInputs().get(jColIdx).getDomainMinMax();
                if (!domainMinMax.includes(interval)) {
                    throw new IllegalStateException(MsgUtil.createMessage(Msg.DTANALYSIS_ERROR_RULE_OUTSIDE_DOMAIN, jRowIdx + 1, interval, domainMinMax, jColIdx + 1));
                }
            }
            ddtaRule.getInputEntry().add(ddtaInputEntry);
            jColIdx++;
        }
        for (LiteralExpression oe : r.getOutputEntry()) {
            ProcessedExpression compile = (ProcessedExpression) FEEL.compile(oe.getText(), FEEL.newCompilerContext());
            InterpretedExecutableExpression interpreted = compile.getInterpreted();
            BaseNode outputEntryNode = (BaseNode) interpreted.getASTNode();
            Comparable<?> value = valueFromNode(outputEntryNode, outputClauseVisitor);
            ddtaRule.getOutputEntry().add(value);
            jColIdx++;
        }
        ddtaTable.addRule(ddtaRule);
    }
}
Also used : DDTARule(org.kie.dmn.validation.dtanalysis.model.DDTARule) LiteralExpression(org.kie.dmn.model.api.LiteralExpression) BaseNode(org.kie.dmn.feel.lang.ast.BaseNode) ProcessedUnaryTest(org.kie.dmn.feel.codegen.feel11.ProcessedUnaryTest) ProcessedExpression(org.kie.dmn.feel.codegen.feel11.ProcessedExpression) UnaryTestInterpretedExecutableExpression(org.kie.dmn.feel.lang.impl.UnaryTestInterpretedExecutableExpression) InterpretedExecutableExpression(org.kie.dmn.feel.lang.impl.InterpretedExecutableExpression) DecisionRule(org.kie.dmn.model.api.DecisionRule) DDTAInputEntry(org.kie.dmn.validation.dtanalysis.model.DDTAInputEntry) UnaryTestInterpretedExecutableExpression(org.kie.dmn.feel.lang.impl.UnaryTestInterpretedExecutableExpression) UnaryTestListNode(org.kie.dmn.feel.lang.ast.UnaryTestListNode) DDTAInputClause(org.kie.dmn.validation.dtanalysis.model.DDTAInputClause) UnaryTests(org.kie.dmn.model.api.UnaryTests) Interval(org.kie.dmn.validation.dtanalysis.model.Interval)

Example 2 with DDTARule

use of org.kie.dmn.validation.dtanalysis.model.DDTARule in project drools by kiegroup.

the class MCDCAnalyser method calculateElseRuleIdx.

private void calculateElseRuleIdx() {
    if (dt.getHitPolicy() == HitPolicy.PRIORITY) {
        // calculate "else" rule if present.
        for (int ruleIdx = ddtaTable.getRule().size() - 1; ruleIdx >= 0 && !elseRuleIdx.isPresent(); ruleIdx--) {
            DDTARule rule = ddtaTable.getRule().get(ruleIdx);
            List<DDTAInputEntry> ie = rule.getInputEntry();
            boolean checkAll = true;
            for (int colIdx = 0; colIdx < ie.size() && checkAll; colIdx++) {
                DDTAInputEntry ieIDX = ie.get(colIdx);
                boolean idIDXsize1 = ieIDX.getIntervals().size() == 1;
                Interval ieIDXint0 = ieIDX.getIntervals().get(0);
                Interval domainMinMax = ddtaTable.getInputs().get(colIdx).getDomainMinMax();
                boolean equals = ieIDXint0.equals(domainMinMax);
                checkAll &= idIDXsize1 && equals;
            }
            if (checkAll) {
                LOG.debug("I believe P table with else rule: {}", ruleIdx + 1);
                elseRuleIdx = Optional.of(ruleIdx);
            }
        }
    }
}
Also used : DDTARule(org.kie.dmn.validation.dtanalysis.model.DDTARule) DDTAInputEntry(org.kie.dmn.validation.dtanalysis.model.DDTAInputEntry) Interval(org.kie.dmn.validation.dtanalysis.model.Interval)

Example 3 with DDTARule

use of org.kie.dmn.validation.dtanalysis.model.DDTARule in project drools by kiegroup.

the class MCDCAnalyser method calculatePosNegBlock.

private Optional<PosNegBlock> calculatePosNegBlock(Integer idx, Object value, Record posCandidate, List<List<?>> allEnumValues) {
    List<Comparable<?>> posOutput = posCandidate.output;
    List<?> enumValues = allEnumValues.get(idx);
    List<?> allOtherEnumValues = new ArrayList<>(enumValues);
    allOtherEnumValues.remove(value);
    List<Record> negativeRecords = new ArrayList<>();
    for (Object otherEnumValue : allOtherEnumValues) {
        Object[] negCandidate = Arrays.copyOf(posCandidate.enums, posCandidate.enums.length);
        negCandidate[idx] = otherEnumValue;
        Record negRecordForNegCandidate = null;
        for (int i = 0; negRecordForNegCandidate == null && i < ddtaTable.getRule().size(); i++) {
            DDTARule rule = ddtaTable.getRule().get(i);
            boolean ruleMatches = ruleMatches(rule, negCandidate);
            if (ruleMatches) {
                negRecordForNegCandidate = new Record(i, negCandidate, rule.getOutputEntry());
            }
        }
        if (negRecordForNegCandidate != null) {
            negativeRecords.add(negRecordForNegCandidate);
        }
    }
    boolean allNegValuesDiffer = true;
    for (Record record : negativeRecords) {
        allNegValuesDiffer &= !record.output.equals(posOutput);
    }
    if (allNegValuesDiffer) {
        PosNegBlock posNegBlock = new PosNegBlock(idx, posCandidate, negativeRecords);
        return Optional.of(posNegBlock);
    }
    LOG.trace("For In{}={} and candidate positive of {}, it cannot be a matching rule because some negative case had SAME output {}", idx + 1, value, posCandidate, negativeRecords);
    return Optional.empty();
}
Also used : DDTARule(org.kie.dmn.validation.dtanalysis.model.DDTARule) ArrayList(java.util.ArrayList)

Example 4 with DDTARule

use of org.kie.dmn.validation.dtanalysis.model.DDTARule in project drools by kiegroup.

the class MCDCAnalyser method ruleIndexesMatchingValues.

private List<Integer> ruleIndexesMatchingValues(Object[] values) {
    List<Integer> ruleIndexes = new ArrayList<>();
    for (int i = 0; i < ddtaTable.getRule().size(); i++) {
        DDTARule rule = ddtaTable.getRule().get(i);
        if (ruleMatches(rule, values)) {
            ruleIndexes.add(i);
        }
    }
    if (dt.getHitPolicy() == HitPolicy.PRIORITY) {
        List<List<Comparable<?>>> outputs = new ArrayList<>();
        for (Integer ruleIdx : ruleIndexes) {
            DDTARule rule = ddtaTable.getRule().get(ruleIdx);
            List<Comparable<?>> ruleOutput = rule.getOutputEntry();
            outputs.add(ruleOutput);
        }
        List<Comparable<?>> computedOutput = new ArrayList<>();
        for (int i = 0; i < ddtaTable.getOutputs().size(); i++) {
            List outputOrder = ddtaTable.getOutputs().get(i).getOutputOrder();
            int outputCursor = Integer.MAX_VALUE;
            for (List<Comparable<?>> outs : outputs) {
                Comparable<?> out = outs.get(i);
                if (outputOrder.indexOf(out) < outputCursor) {
                    outputCursor = outputOrder.indexOf(out);
                }
            }
            computedOutput.add((Comparable<?>) outputOrder.get(outputCursor));
        }
        List<Integer> pIndexes = new ArrayList<>();
        for (Integer ruleIdx : ruleIndexes) {
            DDTARule rule = ddtaTable.getRule().get(ruleIdx);
            List<Comparable<?>> ruleOutput = rule.getOutputEntry();
            if (ruleOutput.equals(computedOutput)) {
                pIndexes.add(ruleIdx);
            }
        }
        return pIndexes;
    }
    return ruleIndexes;
}
Also used : DDTARule(org.kie.dmn.validation.dtanalysis.model.DDTARule) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

DDTARule (org.kie.dmn.validation.dtanalysis.model.DDTARule)4 ArrayList (java.util.ArrayList)2 DDTAInputEntry (org.kie.dmn.validation.dtanalysis.model.DDTAInputEntry)2 Interval (org.kie.dmn.validation.dtanalysis.model.Interval)2 List (java.util.List)1 ProcessedExpression (org.kie.dmn.feel.codegen.feel11.ProcessedExpression)1 ProcessedUnaryTest (org.kie.dmn.feel.codegen.feel11.ProcessedUnaryTest)1 BaseNode (org.kie.dmn.feel.lang.ast.BaseNode)1 UnaryTestListNode (org.kie.dmn.feel.lang.ast.UnaryTestListNode)1 InterpretedExecutableExpression (org.kie.dmn.feel.lang.impl.InterpretedExecutableExpression)1 UnaryTestInterpretedExecutableExpression (org.kie.dmn.feel.lang.impl.UnaryTestInterpretedExecutableExpression)1 DecisionRule (org.kie.dmn.model.api.DecisionRule)1 LiteralExpression (org.kie.dmn.model.api.LiteralExpression)1 UnaryTests (org.kie.dmn.model.api.UnaryTests)1 DDTAInputClause (org.kie.dmn.validation.dtanalysis.model.DDTAInputClause)1