Search in sources :

Example 1 with AreaEval

use of org.apache.poi.ss.formula.eval.AreaEval in project poi by apache.

the class DStarRunner method evaluate.

public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval database, ValueEval filterColumn, ValueEval conditionDatabase) {
    // Input processing and error checks.
    if (!(database instanceof AreaEval) || !(conditionDatabase instanceof AreaEval)) {
        return ErrorEval.VALUE_INVALID;
    }
    AreaEval db = (AreaEval) database;
    AreaEval cdb = (AreaEval) conditionDatabase;
    try {
        filterColumn = OperandResolver.getSingleValue(filterColumn, srcRowIndex, srcColumnIndex);
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
    int fc;
    try {
        fc = getColumnForName(filterColumn, db);
    } catch (EvaluationException e) {
        return ErrorEval.VALUE_INVALID;
    }
    if (fc == -1) {
        // column not found
        return ErrorEval.VALUE_INVALID;
    }
    // Create an algorithm runner.
    IDStarAlgorithm algorithm = null;
    switch(algoType) {
        case DGET:
            algorithm = new DGet();
            break;
        case DMIN:
            algorithm = new DMin();
            break;
        default:
            throw new IllegalStateException("Unexpected algorithm type " + algoType + " encountered.");
    }
    // Iterate over all DB entries.
    final int height = db.getHeight();
    for (int row = 1; row < height; ++row) {
        boolean matches = true;
        try {
            matches = fullfillsConditions(db, row, cdb);
        } catch (EvaluationException e) {
            return ErrorEval.VALUE_INVALID;
        }
        // Filter each entry.
        if (matches) {
            ValueEval currentValueEval = resolveReference(db, row, fc);
            // Pass the match to the algorithm and conditionally abort the search.
            boolean shouldContinue = algorithm.processMatch(currentValueEval);
            if (!shouldContinue) {
                break;
            }
        }
    }
    // Return the result of the algorithm.
    return algorithm.getResult();
}
Also used : NumericValueEval(org.apache.poi.ss.formula.eval.NumericValueEval) ValueEval(org.apache.poi.ss.formula.eval.ValueEval) StringValueEval(org.apache.poi.ss.formula.eval.StringValueEval) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException) AreaEval(org.apache.poi.ss.formula.eval.AreaEval)

Example 2 with AreaEval

use of org.apache.poi.ss.formula.eval.AreaEval in project poi by apache.

the class Baseifs method validateCriteriaRanges.

/**
     * Verify that each <code>criteriaRanges</code> argument contains the same number of rows and columns
     * including the <code>sumRange</code> argument if present
     * @param sumRange if used, it must match the shape of the criteriaRanges
     * @param criteriaRanges to check
     * @throws EvaluationException if the ranges do not match.
     */
private static void validateCriteriaRanges(AreaEval sumRange, AreaEval[] criteriaRanges) throws EvaluationException {
    int h = criteriaRanges[0].getHeight();
    int w = criteriaRanges[0].getWidth();
    if (sumRange != null && (sumRange.getHeight() != h || sumRange.getWidth() != w)) {
        throw EvaluationException.invalidValue();
    }
    for (AreaEval r : criteriaRanges) {
        if (r.getHeight() != h || r.getWidth() != w) {
            throw EvaluationException.invalidValue();
        }
    }
}
Also used : AreaEval(org.apache.poi.ss.formula.eval.AreaEval)

Example 3 with AreaEval

use of org.apache.poi.ss.formula.eval.AreaEval in project poi by apache.

the class Baseifs method evaluate.

public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
    final boolean hasInitialRange = hasInitialRange();
    final int firstCriteria = hasInitialRange ? 1 : 0;
    if (args.length < (2 + firstCriteria) || args.length % 2 != firstCriteria) {
        return ErrorEval.VALUE_INVALID;
    }
    try {
        AreaEval sumRange = null;
        if (hasInitialRange) {
            sumRange = convertRangeArg(args[0]);
        }
        // collect pairs of ranges and criteria
        AreaEval[] ae = new AreaEval[(args.length - firstCriteria) / 2];
        I_MatchPredicate[] mp = new I_MatchPredicate[ae.length];
        for (int i = firstCriteria, k = 0; i < args.length; i += 2, k++) {
            ae[k] = convertRangeArg(args[i]);
            mp[k] = Countif.createCriteriaPredicate(args[i + 1], ec.getRowIndex(), ec.getColumnIndex());
        }
        validateCriteriaRanges(sumRange, ae);
        validateCriteria(mp);
        double result = aggregateMatchingCells(sumRange, ae, mp);
        return new NumberEval(result);
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
}
Also used : I_MatchPredicate(org.apache.poi.ss.formula.functions.CountUtils.I_MatchPredicate) EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException) AreaEval(org.apache.poi.ss.formula.eval.AreaEval) NumberEval(org.apache.poi.ss.formula.eval.NumberEval)

Example 4 with AreaEval

use of org.apache.poi.ss.formula.eval.AreaEval in project poi by apache.

the class Baseifs method aggregateMatchingCells.

/**
     * @param sumRange  the range to sum, if used (uses 1 for each match if not present)
     * @param ranges  criteria ranges
     * @param predicates  array of predicates, a predicate for each value in <code>ranges</code>
     * @return the computed value
     */
private static double aggregateMatchingCells(AreaEval sumRange, AreaEval[] ranges, I_MatchPredicate[] predicates) {
    int height = ranges[0].getHeight();
    int width = ranges[0].getWidth();
    double result = 0.0;
    for (int r = 0; r < height; r++) {
        for (int c = 0; c < width; c++) {
            boolean matches = true;
            for (int i = 0; i < ranges.length; i++) {
                AreaEval aeRange = ranges[i];
                I_MatchPredicate mp = predicates[i];
                // Bugs 60858 and 56420 show predicate can be null
                if (mp == null || !mp.matches(aeRange.getRelativeValue(r, c))) {
                    matches = false;
                    break;
                }
            }
            if (matches) {
                // sum only if all of the corresponding criteria specified are true for that cell.
                result += accumulate(sumRange, r, c);
            }
        }
    }
    return result;
}
Also used : I_MatchPredicate(org.apache.poi.ss.formula.functions.CountUtils.I_MatchPredicate) AreaEval(org.apache.poi.ss.formula.eval.AreaEval)

Example 5 with AreaEval

use of org.apache.poi.ss.formula.eval.AreaEval in project poi by apache.

the class Sumif method evaluate.

public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1, ValueEval arg2) {
    AreaEval aeRange;
    AreaEval aeSum;
    try {
        aeRange = convertRangeArg(arg0);
        aeSum = createSumRange(arg2, aeRange);
    } catch (EvaluationException e) {
        return e.getErrorEval();
    }
    return eval(srcRowIndex, srcColumnIndex, arg1, aeRange, aeSum);
}
Also used : EvaluationException(org.apache.poi.ss.formula.eval.EvaluationException) AreaEval(org.apache.poi.ss.formula.eval.AreaEval)

Aggregations

AreaEval (org.apache.poi.ss.formula.eval.AreaEval)35 ValueEval (org.apache.poi.ss.formula.eval.ValueEval)29 NumberEval (org.apache.poi.ss.formula.eval.NumberEval)21 NumericValueEval (org.apache.poi.ss.formula.eval.NumericValueEval)17 StringEval (org.apache.poi.ss.formula.eval.StringEval)13 EvaluationException (org.apache.poi.ss.formula.eval.EvaluationException)4 I_MatchPredicate (org.apache.poi.ss.formula.functions.CountUtils.I_MatchPredicate)4 AssertionFailedError (junit.framework.AssertionFailedError)3 RefEval (org.apache.poi.ss.formula.eval.RefEval)3 ErrorEval (org.apache.poi.ss.formula.eval.ErrorEval)1 StringValueEval (org.apache.poi.ss.formula.eval.StringValueEval)1 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)1