List of usage examples for org.apache.poi.ss.formula TwoDEval getValue
ValueEval getValue(int rowIndex, int columnIndex);
From source file:nl.eur.ese.spreadsheettest.DStarRunner.java
License:Apache License
public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval database, ValueEval filterColumn, ValueEval conditionDatabase) {//from w w w .j av a2 s .c o m // Input processing and error checks. if (!(database instanceof TwoDEval) || !(conditionDatabase instanceof TwoDEval)) { return ErrorEval.VALUE_INVALID; } TwoDEval db = (TwoDEval) database; TwoDEval cdb = (TwoDEval) conditionDatabase; 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 = fac.create(); // Iterate over all DB entries. for (int row = 1; row < db.getHeight(); ++row) { boolean matches = true; try { matches = fullfillsConditions(db, row, cdb); } catch (EvaluationException e) { return ErrorEval.VALUE_INVALID; } // Filter each entry. if (matches) { try { ValueEval currentValueEval = solveReference(db.getValue(row, fc)); // Pass the match to the algorithm and conditionally abort the search. boolean shouldContinue = algorithm.processMatch(currentValueEval); if (!shouldContinue) { break; } } catch (EvaluationException e) { return e.getErrorEval(); } } } // Return the result of the algorithm. return algorithm.getResult(); }
From source file:nl.eur.ese.spreadsheettest.DStarRunner.java
License:Apache License
/** * For a given database returns the column number for a column heading. * * @param db Database.// ww w.j ava2 s . com * @param name Column heading. * @return Corresponding column number. * @throws EvaluationException If it's not possible to turn all headings into strings. */ private static int getColumnForString(TwoDEval db, String name) throws EvaluationException { int resultColumn = -1; for (int column = 0; column < db.getWidth(); ++column) { ValueEval columnNameValueEval = db.getValue(0, column); String columnName = getStringFromValueEval(columnNameValueEval); if (name.equals(columnName)) { resultColumn = column; break; } } return resultColumn; }
From source file:nl.eur.ese.spreadsheettest.DStarRunner.java
License:Apache License
/** * Checks a row in a database against a condition database. * * @param db Database.//from w w w .j a v a 2 s. c om * @param row The row in the database to check. * @param cdb The condition database to use for checking. * @return Whether the row matches the conditions. * @throws EvaluationException If references could not be resolved or comparison * operators and operands didn't match. */ private static boolean fullfillsConditions(TwoDEval db, int row, TwoDEval cdb) throws EvaluationException { // Only one row must match to accept the input, so rows are ORed. // Each row is made up of cells where each cell is a condition, // all have to match, so they are ANDed. for (int conditionRow = 1; conditionRow < cdb.getHeight(); ++conditionRow) { boolean matches = true; for (int column = 0; column < cdb.getWidth(); ++column) { // columns are ANDed // Whether the condition column matches a database column, if not it's a // special column that accepts formulas. boolean columnCondition = true; ValueEval condition = null; try { // The condition to apply. condition = solveReference(cdb.getValue(conditionRow, column)); } catch (java.lang.RuntimeException e) { // It might be a special formula, then it is ok if it fails. columnCondition = false; } // If the condition is empty it matches. if (condition instanceof BlankEval) continue; // The column in the DB to apply the condition to. ValueEval targetHeader = solveReference(cdb.getValue(0, column)); targetHeader = solveReference(targetHeader); if (!(targetHeader instanceof StringValueEval)) { throw new EvaluationException(ErrorEval.VALUE_INVALID); } if (getColumnForName(targetHeader, db) == -1) // No column found, it's again a special column that accepts formulas. columnCondition = false; if (columnCondition == true) { // normal column condition // Should not throw, checked above. ValueEval value = db.getValue(row, getColumnForName(targetHeader, db)); if (!testNormalCondition(value, condition)) { matches = false; break; } } else { // It's a special formula condition. if (getStringFromValueEval(condition).isEmpty()) { throw new EvaluationException(ErrorEval.VALUE_INVALID); } throw new NotImplementedException("D* function with formula conditions"); } } if (matches == true) { return true; } } return false; }