Search in sources :

Example 1 with FormulaSyntaxException

use of org.activityinfo.model.formula.diagnostic.FormulaSyntaxException in project activityinfo by bedatadriven.

the class FormulaParser method number.

private FormulaNode number() {
    Token token = lexer.next();
    double value;
    try {
        value = Double.parseDouble(token.getString());
    } catch (NumberFormatException e) {
        throw new FormulaSyntaxException(new SourceRange(token), "Invalid number '" + token.getString() + "': " + e.getMessage());
    }
    return new ConstantNode(value, new SourceRange(token));
}
Also used : FormulaSyntaxException(org.activityinfo.model.formula.diagnostic.FormulaSyntaxException)

Example 2 with FormulaSyntaxException

use of org.activityinfo.model.formula.diagnostic.FormulaSyntaxException in project activityinfo by bedatadriven.

the class FormulaParser method call.

private FormulaNode call(Token functionToken) {
    FormulaFunction function = function(functionToken);
    expect(TokenType.PAREN_START);
    List<FormulaNode> arguments = new ArrayList<>();
    while (true) {
        if (!lexer.hasNext()) {
            throw new FormulaSyntaxException("Unexpected end of formula");
        }
        TokenType nextToken = lexer.peek().getType();
        if (nextToken == TokenType.COMMA) {
            // Consume comma and parse next argument
            lexer.next();
            continue;
        }
        if (nextToken == TokenType.PAREN_END) {
            // consume paren and complete argument list
            Token closingParen = lexer.next();
            return new FunctionCallNode(function, arguments, new SourceRange(functionToken, closingParen));
        }
        // Otherwise parse the next argument
        arguments.add(expression());
    }
}
Also used : FormulaFunction(org.activityinfo.model.formula.functions.FormulaFunction) FormulaSyntaxException(org.activityinfo.model.formula.diagnostic.FormulaSyntaxException) ArrayList(java.util.ArrayList)

Example 3 with FormulaSyntaxException

use of org.activityinfo.model.formula.diagnostic.FormulaSyntaxException in project activityinfo by bedatadriven.

the class BinaryBooleanOperator method columnApply.

@Override
public ColumnView columnApply(int numRows, List<ColumnView> arguments) {
    checkArity(arguments, 2);
    ColumnView a = arguments.get(0);
    ColumnView b = arguments.get(1);
    if (a.numRows() != b.numRows()) {
        throw new FormulaSyntaxException("Arguments must have the same number of rows");
    }
    int[] result = new int[a.numRows()];
    for (int i = 0; i < result.length; i++) {
        int ai = a.getBoolean(i);
        int bi = b.getBoolean(i);
        result[i] = apply(ai, bi);
    }
    return new BooleanColumnView(result);
}
Also used : FormulaSyntaxException(org.activityinfo.model.formula.diagnostic.FormulaSyntaxException) ColumnView(org.activityinfo.model.query.ColumnView) BooleanColumnView(org.activityinfo.model.query.BooleanColumnView) BooleanColumnView(org.activityinfo.model.query.BooleanColumnView)

Example 4 with FormulaSyntaxException

use of org.activityinfo.model.formula.diagnostic.FormulaSyntaxException in project activityinfo by bedatadriven.

the class YearFracFunction method apply.

@Override
public FieldValue apply(List<FieldValue> arguments) {
    if (arguments.size() != 2) {
        throw new FormulaSyntaxException("YEARFRAC() requires two arguments");
    }
    LocalDate startDate = (LocalDate) arguments.get(0);
    LocalDate endDate = (LocalDate) arguments.get(1);
    if (startDate == null || endDate == null) {
        return null;
    }
    return new Quantity(compute(startDate, endDate));
}
Also used : FormulaSyntaxException(org.activityinfo.model.formula.diagnostic.FormulaSyntaxException) Quantity(org.activityinfo.model.type.number.Quantity) LocalDate(org.activityinfo.model.type.time.LocalDate)

Example 5 with FormulaSyntaxException

use of org.activityinfo.model.formula.diagnostic.FormulaSyntaxException in project activityinfo by bedatadriven.

the class YearFracFunction method columnApply.

@Override
public ColumnView columnApply(int numRows, List<ColumnView> arguments) {
    if (arguments.size() != 2) {
        throw new FormulaSyntaxException("YEARFRAC() requires two arguments");
    }
    ColumnView startView = arguments.get(0);
    ColumnView endView = arguments.get(1);
    double[] result = new double[numRows];
    for (int i = 0; i < numRows; i++) {
        String start = startView.getString(i);
        String end = endView.getString(i);
        if (start == null || end == null) {
            result[i] = Double.NaN;
        } else {
            result[i] = compute(LocalDate.parse(start), LocalDate.parse(end));
        }
    }
    return new DoubleArrayColumnView(result);
}
Also used : FormulaSyntaxException(org.activityinfo.model.formula.diagnostic.FormulaSyntaxException) ColumnView(org.activityinfo.model.query.ColumnView) DoubleArrayColumnView(org.activityinfo.model.query.DoubleArrayColumnView) DoubleArrayColumnView(org.activityinfo.model.query.DoubleArrayColumnView)

Aggregations

FormulaSyntaxException (org.activityinfo.model.formula.diagnostic.FormulaSyntaxException)9 FormulaFunction (org.activityinfo.model.formula.functions.FormulaFunction)3 ColumnView (org.activityinfo.model.query.ColumnView)3 BooleanColumnView (org.activityinfo.model.query.BooleanColumnView)2 Quantity (org.activityinfo.model.type.number.Quantity)2 LocalDate (org.activityinfo.model.type.time.LocalDate)2 ArrayList (java.util.ArrayList)1 DoubleArrayColumnView (org.activityinfo.model.query.DoubleArrayColumnView)1 EnumColumnView (org.activityinfo.model.query.EnumColumnView)1 FieldValue (org.activityinfo.model.type.FieldValue)1