Search in sources :

Example 1 with NullNode

use of org.kie.dmn.feel.lang.ast.NullNode in project drools by kiegroup.

the class DMNDTAnalyser method getDiscreteValues.

/**
 * Transform a UnaryTestListNode into a List of discrete values for input/output clause enumeration
 */
private List<Comparable<?>> getDiscreteValues(UnaryTestListNode utln) {
    List<Comparable<?>> discreteValues = new ArrayList<>();
    for (BaseNode e : utln.getElements()) {
        BaseNode value = ((UnaryTestNode) e).getValue();
        if (!(value instanceof NullNode)) {
            // to retrieve value from input/output clause enumeration, null is ignored.
            Comparable<?> v = valueFromNode(value);
            discreteValues.add(v);
        }
    }
    return discreteValues;
}
Also used : UnaryTestNode(org.kie.dmn.feel.lang.ast.UnaryTestNode) ArrayList(java.util.ArrayList) BaseNode(org.kie.dmn.feel.lang.ast.BaseNode) NullNode(org.kie.dmn.feel.lang.ast.NullNode)

Example 2 with NullNode

use of org.kie.dmn.feel.lang.ast.NullNode in project drools by kiegroup.

the class DMNDTAnalyser method toIntervals.

private List<Interval> toIntervals(List<BaseNode> elements, boolean isNegated, Interval minMax, List discreteValues, int rule, int col) {
    List<Interval> results = new ArrayList<>();
    if (elements.size() == 1 && elements.get(0) instanceof UnaryTestNode && ((UnaryTestNode) elements.get(0)).getValue() instanceof NullNode) {
        return Collections.emptyList();
    }
    if (discreteValues != null && !discreteValues.isEmpty() && areAllEQUnaryTest(elements) && elements.size() > 1) {
        // JDK BitSet size will always be larger.
        int bitsetLogicalSize = discreteValues.size();
        BitSet hitValues = new BitSet(bitsetLogicalSize);
        for (BaseNode n : elements) {
            Comparable<?> thisValue = valueFromNode(((UnaryTestNode) n).getValue());
            int indexOf = discreteValues.indexOf(thisValue);
            if (indexOf < 0) {
                throw new IllegalStateException("Unable to determine discreteValue index for: " + n);
            }
            hitValues.set(indexOf);
        }
        if (isNegated) {
            hitValues.flip(0, bitsetLogicalSize);
        }
        int lowerBoundIdx = -1;
        int upperBoundIdx = -1;
        for (int i = 0; i < hitValues.length(); i++) {
            boolean curValue = hitValues.get(i);
            if (curValue) {
                if (lowerBoundIdx < 0) {
                    lowerBoundIdx = i;
                    upperBoundIdx = i;
                } else {
                    upperBoundIdx = i;
                }
            } else {
                if (lowerBoundIdx >= 0 && upperBoundIdx >= 0) {
                    results.add(createIntervalOfRule(discreteValues, rule, col, lowerBoundIdx, upperBoundIdx));
                    lowerBoundIdx = -1;
                    upperBoundIdx = -1;
                }
            }
        }
        if (lowerBoundIdx >= 0 && upperBoundIdx >= 0) {
            results.add(createIntervalOfRule(discreteValues, rule, col, lowerBoundIdx, upperBoundIdx));
        }
    } else {
        for (BaseNode n : elements) {
            if (n instanceof DashNode) {
                results.add(new Interval(minMax.getLowerBound().getBoundaryType(), minMax.getLowerBound().getValue(), minMax.getUpperBound().getValue(), minMax.getUpperBound().getBoundaryType(), rule, col));
                continue;
            }
            UnaryTestNode ut = (UnaryTestNode) n;
            Interval interval = utnToInterval(ut, minMax, discreteValues, rule, col);
            if (isNegated) {
                results.addAll(Interval.invertOverDomain(interval, minMax));
            } else {
                results.add(interval);
            }
        }
    }
    return results;
}
Also used : UnaryTestNode(org.kie.dmn.feel.lang.ast.UnaryTestNode) ArrayList(java.util.ArrayList) BitSet(java.util.BitSet) BaseNode(org.kie.dmn.feel.lang.ast.BaseNode) DashNode(org.kie.dmn.feel.lang.ast.DashNode) NullNode(org.kie.dmn.feel.lang.ast.NullNode) Interval(org.kie.dmn.validation.dtanalysis.model.Interval)

Example 3 with NullNode

use of org.kie.dmn.feel.lang.ast.NullNode in project drools by kiegroup.

the class DMNDTAnalyser method utnToInterval.

private Interval utnToInterval(UnaryTestNode ut, Interval minMax, List discreteValues, int rule, int col) {
    if (ut.getOperator() == UnaryOperator.EQ) {
        if (discreteValues == null || discreteValues.isEmpty()) {
            return new Interval(RangeBoundary.CLOSED, valueFromNode(ut.getValue()), valueFromNode(ut.getValue()), RangeBoundary.CLOSED, rule, col);
        } else {
            Comparable<?> thisValue = valueFromNode(ut.getValue());
            int indexOf = discreteValues.indexOf(thisValue);
            if (indexOf < 0) {
                throw new IllegalStateException("Unable to determine discreteValue index for: " + ut);
            }
            if (indexOf + 1 == discreteValues.size()) {
                return new Interval(RangeBoundary.CLOSED, thisValue, thisValue, RangeBoundary.CLOSED, rule, col);
            }
            return new Interval(RangeBoundary.CLOSED, thisValue, (Comparable<?>) discreteValues.get(indexOf + 1), RangeBoundary.OPEN, rule, col);
        }
    } else if (ut.getOperator() == UnaryOperator.LTE) {
        return new Interval(minMax.getLowerBound().getBoundaryType(), minMax.getLowerBound().getValue(), valueFromNode(ut.getValue()), RangeBoundary.CLOSED, rule, col);
    } else if (ut.getOperator() == UnaryOperator.LT) {
        return new Interval(minMax.getLowerBound().getBoundaryType(), minMax.getLowerBound().getValue(), valueFromNode(ut.getValue()), RangeBoundary.OPEN, rule, col);
    } else if (ut.getOperator() == UnaryOperator.GT) {
        return new Interval(RangeBoundary.OPEN, valueFromNode(ut.getValue()), minMax.getUpperBound().getValue(), minMax.getUpperBound().getBoundaryType(), rule, col);
    } else if (ut.getOperator() == UnaryOperator.GTE) {
        return new Interval(RangeBoundary.CLOSED, valueFromNode(ut.getValue()), minMax.getUpperBound().getValue(), minMax.getUpperBound().getBoundaryType(), rule, col);
    } else if (ut.getValue() instanceof RangeNode) {
        RangeNode rangeNode = (RangeNode) ut.getValue();
        if (!(rangeNode.getStart() instanceof NullNode || rangeNode.getEnd() instanceof NullNode)) {
            return new Interval(rangeNode.getLowerBound() == IntervalBoundary.OPEN ? RangeBoundary.OPEN : RangeBoundary.CLOSED, valueFromNode(rangeNode.getStart()), valueFromNode(rangeNode.getEnd()), rangeNode.getUpperBound() == IntervalBoundary.OPEN ? RangeBoundary.OPEN : RangeBoundary.CLOSED, rule, col);
        } else if (rangeNode.getStart() instanceof NullNode) {
            return new Interval(minMax.getLowerBound().getBoundaryType(), minMax.getLowerBound().getValue(), valueFromNode(rangeNode.getEnd()), rangeNode.getUpperBound() == IntervalBoundary.OPEN ? RangeBoundary.OPEN : RangeBoundary.CLOSED, rule, col);
        } else {
            return new Interval(rangeNode.getLowerBound() == IntervalBoundary.OPEN ? RangeBoundary.OPEN : RangeBoundary.CLOSED, valueFromNode(rangeNode.getStart()), minMax.getUpperBound().getValue(), minMax.getUpperBound().getBoundaryType(), rule, col);
        }
    } else {
        throw new UnsupportedOperationException("UnaryTest type: " + ut);
    }
}
Also used : RangeNode(org.kie.dmn.feel.lang.ast.RangeNode) NullNode(org.kie.dmn.feel.lang.ast.NullNode) Interval(org.kie.dmn.validation.dtanalysis.model.Interval)

Aggregations

NullNode (org.kie.dmn.feel.lang.ast.NullNode)3 ArrayList (java.util.ArrayList)2 BaseNode (org.kie.dmn.feel.lang.ast.BaseNode)2 UnaryTestNode (org.kie.dmn.feel.lang.ast.UnaryTestNode)2 Interval (org.kie.dmn.validation.dtanalysis.model.Interval)2 BitSet (java.util.BitSet)1 DashNode (org.kie.dmn.feel.lang.ast.DashNode)1 RangeNode (org.kie.dmn.feel.lang.ast.RangeNode)1