Search in sources :

Example 1 with Interval

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

the class DMNDTAnalyser method compileTableInputClauses.

private void compileTableInputClauses(DMNModel model, DecisionTable dt, DDTATable ddtaTable) {
    for (int jColIdx = 0; jColIdx < dt.getInput().size(); jColIdx++) {
        InputClause ie = dt.getInput().get(jColIdx);
        Interval infDomain = new Interval(RangeBoundary.CLOSED, Interval.NEG_INF, Interval.POS_INF, RangeBoundary.CLOSED, 0, jColIdx + 1);
        String allowedValues;
        if (ie.getInputValues() != null) {
            allowedValues = ie.getInputValues().getText();
        } else {
            QName typeRef = DMNCompilerImpl.getNamespaceAndName(dt, ((DMNModelImpl) model).getImportAliasesForNS(), ie.getInputExpression().getTypeRef(), model.getNamespace());
            allowedValues = findAllowedValues(model, typeRef);
        }
        if (allowedValues != null) {
            ProcessedUnaryTest compileUnaryTests = (ProcessedUnaryTest) FEEL.compileUnaryTests(allowedValues, FEEL.newCompilerContext());
            UnaryTestInterpretedExecutableExpression interpreted = compileUnaryTests.getInterpreted();
            UnaryTestListNode utln = (UnaryTestListNode) interpreted.getASTNode();
            if (utln.getElements().size() != 1) {
                verifyUnaryTestsAllEQ(utln, dt);
                List<Comparable<?>> discreteValues = getDiscreteValues(utln);
                Collections.sort((List) discreteValues);
                Interval discreteDomainMinMax = new Interval(RangeBoundary.CLOSED, discreteValues.get(0), discreteValues.get(discreteValues.size() - 1), RangeBoundary.CLOSED, 0, jColIdx + 1);
                DDTAInputClause ic = new DDTAInputClause(discreteDomainMinMax, discreteValues, getDiscreteValues(utln));
                ddtaTable.getInputs().add(ic);
            } else if (utln.getElements().size() == 1) {
                UnaryTestNode utn0 = (UnaryTestNode) utln.getElements().get(0);
                Interval interval = utnToInterval(utn0, infDomain, null, 0, jColIdx + 1);
                DDTAInputClause ic = new DDTAInputClause(interval);
                ddtaTable.getInputs().add(ic);
            } else {
                throw new IllegalStateException("inputValues not null but utln: " + utln);
            }
        } else {
            DDTAInputClause ic = new DDTAInputClause(infDomain);
            ddtaTable.getInputs().add(ic);
        }
    }
}
Also used : UnaryTestNode(org.kie.dmn.feel.lang.ast.UnaryTestNode) QName(javax.xml.namespace.QName) ProcessedUnaryTest(org.kie.dmn.feel.codegen.feel11.ProcessedUnaryTest) UnaryTestInterpretedExecutableExpression(org.kie.dmn.feel.lang.impl.UnaryTestInterpretedExecutableExpression) UnaryTestListNode(org.kie.dmn.feel.lang.ast.UnaryTestListNode) DDTAInputClause(org.kie.dmn.validation.dtanalysis.model.DDTAInputClause) DDTAInputClause(org.kie.dmn.validation.dtanalysis.model.DDTAInputClause) InputClause(org.kie.dmn.model.api.InputClause) Interval(org.kie.dmn.validation.dtanalysis.model.Interval)

Example 2 with Interval

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

the class DMNDTAnalyser method findGaps.

private static void findGaps(DTAnalysis analysis, DDTATable ddtaTable, int jColIdx, Interval[] currentIntervals, Collection<Integer> activeRules) {
    LOG.debug("findGaps jColIdx {}, currentIntervals {}, activeRules {}", jColIdx, currentIntervals, activeRules);
    if (jColIdx < ddtaTable.inputCols()) {
        findBoundsSorted(ddtaTable, jColIdx, activeRules);
        List<Bound> bounds = findBoundsSorted(ddtaTable, jColIdx, activeRules);
        Interval domainRange = ddtaTable.getInputs().get(jColIdx).getDomainMinMax();
        // from domain start to the 1st bound
        if (!domainRange.getLowerBound().equals(bounds.get(0))) {
            currentIntervals[jColIdx] = lastDimensionUncoveredInterval(domainRange.getLowerBound(), bounds.get(0), domainRange);
            Hyperrectangle gap = new Hyperrectangle(ddtaTable.inputCols(), buildEdgesForHyperrectangleFromIntervals(currentIntervals, jColIdx));
            analysis.addGap(gap);
            LOG.debug("STARTLEFT GAP DETECTED {}", gap);
        }
        // cycle rule's interval bounds
        List<Interval> activeIntervals = new ArrayList<>();
        Bound<?> lastBound = NullBoundImpl.NULL;
        for (Bound<?> currentBound : bounds) {
            LOG.debug("lastBound {} currentBound {}      activeIntervals {} == rules {}", lastBound, currentBound, activeIntervals, activeIntervalsToRules(activeIntervals));
            if (activeIntervals.isEmpty() && lastBound != NullBoundImpl.NULL && !Bound.adOrOver(lastBound, currentBound)) {
                currentIntervals[jColIdx] = lastDimensionUncoveredInterval(lastBound, currentBound, domainRange);
                Hyperrectangle gap = new Hyperrectangle(ddtaTable.inputCols(), buildEdgesForHyperrectangleFromIntervals(currentIntervals, jColIdx));
                LOG.debug("GAP DETECTED {}", gap);
                analysis.addGap(gap);
            }
            if (!activeIntervals.isEmpty() && canBeNewCurrInterval(lastBound, currentBound)) {
                Interval missingInterval = new Interval(lastBound.isUpperBound() ? Interval.invertBoundary(lastBound.getBoundaryType()) : lastBound.getBoundaryType(), lastBound.getValue(), currentBound.getValue(), currentBound.isLowerBound() ? Interval.invertBoundary(currentBound.getBoundaryType()) : currentBound.getBoundaryType(), 0, 0);
                currentIntervals[jColIdx] = missingInterval;
                findGaps(analysis, ddtaTable, jColIdx + 1, currentIntervals, activeIntervalsToRules(activeIntervals));
            }
            if (currentBound.isLowerBound()) {
                activeIntervals.add(currentBound.getParent());
            } else {
                activeIntervals.remove(currentBound.getParent());
            }
            lastBound = currentBound;
        }
        // from last Nth bound, to domain end.
        if (!lastBound.equals(domainRange.getUpperBound())) {
            currentIntervals[jColIdx] = lastDimensionUncoveredInterval(lastBound, domainRange.getUpperBound(), domainRange);
            Hyperrectangle gap = new Hyperrectangle(ddtaTable.inputCols(), buildEdgesForHyperrectangleFromIntervals(currentIntervals, jColIdx));
            LOG.debug("ENDRIGHT GAP DETECTED {}", gap);
            analysis.addGap(gap);
        }
        // facilitate debugging.
        currentIntervals[jColIdx] = null;
    }
    LOG.debug(".");
}
Also used : Bound(org.kie.dmn.validation.dtanalysis.model.Bound) Hyperrectangle(org.kie.dmn.validation.dtanalysis.model.Hyperrectangle) ArrayList(java.util.ArrayList) Interval(org.kie.dmn.validation.dtanalysis.model.Interval)

Example 3 with Interval

use of org.kie.dmn.validation.dtanalysis.model.Interval 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 4 with Interval

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

the class DMNDTAnalyser method compileTableOutputClauses.

private void compileTableOutputClauses(DMNModel model, DecisionTable dt, DDTATable ddtaTable) {
    for (int jColIdx = 0; jColIdx < dt.getOutput().size(); jColIdx++) {
        OutputClause oe = dt.getOutput().get(jColIdx);
        Interval infDomain = new Interval(RangeBoundary.CLOSED, Interval.NEG_INF, Interval.POS_INF, RangeBoundary.CLOSED, 0, jColIdx + 1);
        String allowedValues = null;
        if (oe.getOutputValues() != null) {
            allowedValues = oe.getOutputValues().getText();
        } else {
            QName outputTypeRef = (oe.getTypeRef() == null && dt.getOutput().size() == 1) ? dt.getTypeRef() : oe.getTypeRef();
            if (outputTypeRef != null) {
                QName typeRef = DMNCompilerImpl.getNamespaceAndName(dt, ((DMNModelImpl) model).getImportAliasesForNS(), outputTypeRef, model.getNamespace());
                allowedValues = findAllowedValues(model, typeRef);
            }
        }
        if (allowedValues != null) {
            ProcessedUnaryTest compileUnaryTests = (ProcessedUnaryTest) FEEL.compileUnaryTests(allowedValues, FEEL.newCompilerContext());
            UnaryTestInterpretedExecutableExpression interpreted = compileUnaryTests.getInterpreted();
            UnaryTestListNode utln = (UnaryTestListNode) interpreted.getASTNode();
            if (utln.getElements().size() != 1) {
                verifyUnaryTestsAllEQ(utln, dt);
                List<Comparable<?>> discreteValues = getDiscreteValues(utln);
                List<Comparable<?>> outputOrder = new ArrayList<>(discreteValues);
                Collections.sort((List) discreteValues);
                Interval discreteDomainMinMax = new Interval(RangeBoundary.CLOSED, discreteValues.get(0), discreteValues.get(discreteValues.size() - 1), RangeBoundary.CLOSED, 0, jColIdx + 1);
                DDTAOutputClause ic = new DDTAOutputClause(discreteDomainMinMax, discreteValues, outputOrder);
                ddtaTable.getOutputs().add(ic);
            } else if (utln.getElements().size() == 1) {
                UnaryTestNode utn0 = (UnaryTestNode) utln.getElements().get(0);
                Interval interval = utnToInterval(utn0, infDomain, null, 0, jColIdx + 1);
                DDTAOutputClause ic = new DDTAOutputClause(interval);
                ddtaTable.getOutputs().add(ic);
            } else {
                throw new IllegalStateException("inputValues not null but utln: " + utln);
            }
        } else {
            DDTAOutputClause ic = new DDTAOutputClause(infDomain);
            ddtaTable.getOutputs().add(ic);
        }
    }
}
Also used : UnaryTestNode(org.kie.dmn.feel.lang.ast.UnaryTestNode) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) ProcessedUnaryTest(org.kie.dmn.feel.codegen.feel11.ProcessedUnaryTest) DDTAOutputClause(org.kie.dmn.validation.dtanalysis.model.DDTAOutputClause) OutputClause(org.kie.dmn.model.api.OutputClause) DDTAOutputClause(org.kie.dmn.validation.dtanalysis.model.DDTAOutputClause) UnaryTestInterpretedExecutableExpression(org.kie.dmn.feel.lang.impl.UnaryTestInterpretedExecutableExpression) UnaryTestListNode(org.kie.dmn.feel.lang.ast.UnaryTestListNode) Interval(org.kie.dmn.validation.dtanalysis.model.Interval)

Example 5 with Interval

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

the class DTAnalysisMeta method hrAsExpression.

private static Expression hrAsExpression(Hyperrectangle gap) {
    int dimensions = gap.getDimensions();
    MethodCallExpr edgesExpression = parseExpression("Arrays.asList()");
    for (Interval edge : gap.getEdges()) {
        Expression intervalAsExpression = intervalAsExpression(edge);
        edgesExpression.addArgument(intervalAsExpression);
    }
    ObjectCreationExpr newExpression = parseExpression("new Hyperrectangle()");
    newExpression.addArgument(new IntegerLiteralExpr(dimensions));
    newExpression.addArgument(edgesExpression);
    return newExpression;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) StaticJavaParser.parseExpression(com.github.javaparser.StaticJavaParser.parseExpression) Expression(com.github.javaparser.ast.expr.Expression) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Interval(org.kie.dmn.validation.dtanalysis.model.Interval)

Aggregations

Interval (org.kie.dmn.validation.dtanalysis.model.Interval)20 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)4 QName (javax.xml.namespace.QName)3 ProcessedUnaryTest (org.kie.dmn.feel.codegen.feel11.ProcessedUnaryTest)3 UnaryTestListNode (org.kie.dmn.feel.lang.ast.UnaryTestListNode)3 UnaryTestNode (org.kie.dmn.feel.lang.ast.UnaryTestNode)3 UnaryTestInterpretedExecutableExpression (org.kie.dmn.feel.lang.impl.UnaryTestInterpretedExecutableExpression)3 DDTAInputClause (org.kie.dmn.validation.dtanalysis.model.DDTAInputClause)3 DDTAInputEntry (org.kie.dmn.validation.dtanalysis.model.DDTAInputEntry)3 BaseNode (org.kie.dmn.feel.lang.ast.BaseNode)2 NullNode (org.kie.dmn.feel.lang.ast.NullNode)2 InputClause (org.kie.dmn.model.api.InputClause)2 Bound (org.kie.dmn.validation.dtanalysis.model.Bound)2 DDTARule (org.kie.dmn.validation.dtanalysis.model.DDTARule)2 Hyperrectangle (org.kie.dmn.validation.dtanalysis.model.Hyperrectangle)2 StaticJavaParser.parseExpression (com.github.javaparser.StaticJavaParser.parseExpression)1 Expression (com.github.javaparser.ast.expr.Expression)1 IntegerLiteralExpr (com.github.javaparser.ast.expr.IntegerLiteralExpr)1 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1