Search in sources :

Example 1 with FormulaException

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

the class FormulaValidator method findCalculatedFieldType.

private FieldType findCalculatedFieldType(FormTree.Node fieldNode) {
    CalculatedFieldType fieldType = (CalculatedFieldType) fieldNode.getType();
    FormulaNode rootNode;
    try {
        rootNode = FormulaParser.parse(fieldType.getExpression());
    } catch (FormulaException e) {
        throw new ValidationFailed();
    }
    FormClass formClass = fieldNode.getDefiningFormClass();
    FormTree subTree = formTree.subTree(formClass.getId());
    FormulaValidator validator = new FormulaValidator(subTree);
    if (!validator.validate(rootNode)) {
        throw new ValidationFailed();
    }
    return validator.getResultType();
}
Also used : CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) FormTree(org.activityinfo.model.formTree.FormTree) FormClass(org.activityinfo.model.form.FormClass) FormulaException(org.activityinfo.model.formula.diagnostic.FormulaException)

Example 2 with FormulaException

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

the class FormulaValidator method validateReference.

private FieldType validateReference(FormulaNode formulaNode) {
    Collection<NodeMatch> matches;
    try {
        if (formulaNode instanceof SymbolNode) {
            matches = nodeMatcher.resolveSymbol((SymbolNode) formulaNode);
        } else if (formulaNode instanceof CompoundExpr) {
            matches = nodeMatcher.resolveCompoundExpr((CompoundExpr) formulaNode);
        } else {
            throw new IllegalArgumentException();
        }
    } catch (FormulaException e) {
        errors.add(new FormulaError(formulaNode, e.getMessage()));
        throw new ValidationFailed();
    }
    if (matches.isEmpty()) {
        errors.add(new FormulaError(formulaNode, "Invalid field reference"));
        throw new ValidationFailed();
    }
    for (NodeMatch match : matches) {
        references.add(new FieldReference(formulaNode.getSourceRange(), match));
    }
    NodeMatch match = matches.iterator().next();
    if (match.isEnumBoolean()) {
        return BooleanType.INSTANCE;
    } else if (match.isCalculated()) {
        return findCalculatedFieldType(match.getFieldNode());
    } else if (match.isRootId()) {
        return TextType.SIMPLE;
    } else {
        return match.getFieldNode().getType();
    }
}
Also used : NodeMatch(org.activityinfo.store.query.shared.NodeMatch) FormulaException(org.activityinfo.model.formula.diagnostic.FormulaException)

Example 3 with FormulaException

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

the class FormulaParserTest method invalidNumber.

@Test
public void invalidNumber() {
    FormulaException exception = null;
    try {
        FormulaParser.parse("A*B+\nC + 13.342342.2343");
    } catch (FormulaException e) {
        exception = e;
    }
    assertNotNull(exception);
    assertThat(exception.getSourceRange(), equalTo(new SourceRange(new SourcePos(1, 4), new SourcePos(1, 18))));
}
Also used : FormulaException(org.activityinfo.model.formula.diagnostic.FormulaException) Test(org.junit.Test)

Example 4 with FormulaException

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

the class FunctionCallSlot method get.

@Override
public ColumnView get() {
    List<ColumnView> arguments = Lists.newArrayList();
    for (Slot<ColumnView> argument : argumentSlots) {
        ColumnView view = argument.get();
        if (view == null) {
            throw new IllegalStateException();
        }
        arguments.add(view);
    }
    try {
        return function.columnApply(arguments.get(0).numRows(), arguments);
    } catch (FormulaException e) {
        int numRows = arguments.get(0).numRows();
        return new EmptyColumnView(ColumnType.STRING, numRows);
    }
}
Also used : ColumnView(org.activityinfo.model.query.ColumnView) EmptyColumnView(org.activityinfo.model.query.EmptyColumnView) EmptyColumnView(org.activityinfo.model.query.EmptyColumnView) FormulaException(org.activityinfo.model.formula.diagnostic.FormulaException)

Example 5 with FormulaException

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

the class QueryEvaluator method evaluate.

public Slot<ColumnSet> evaluate(final QueryModel model) {
    Slot<TableFilter> filter = filter(model.getFilter());
    final HashMap<String, Slot<ColumnView>> columnViews = Maps.newHashMap();
    for (ColumnModel column : model.getColumns()) {
        Slot<ColumnView> view;
        try {
            view = evaluateExpression(column.getFormula());
        } catch (FormulaException e) {
            throw new QuerySyntaxException("Syntax error in column " + column.getId() + " '" + column.getFormula() + "' : " + e.getMessage(), e);
        }
        columnViews.put(column.getId(), new FilteredSlot(filter, view));
    }
    if (columnViews.isEmpty()) {
        return new MemoizedSlot<>(batch.addRowCount(FilterLevel.PERMISSIONS, tree.getRootFormId()), new Function<Integer, ColumnSet>() {

            @Override
            public ColumnSet apply(Integer rowCount) {
                return new ColumnSet(rowCount, Collections.<String, ColumnView>emptyMap());
            }
        });
    } else {
        return new Slot<ColumnSet>() {

            private ColumnSet result = null;

            @Override
            public ColumnSet get() {
                if (result == null) {
                    // result
                    Map<String, ColumnView> dataMap = Maps.newHashMap();
                    for (Map.Entry<String, Slot<ColumnView>> entry : columnViews.entrySet()) {
                        dataMap.put(entry.getKey(), entry.getValue().get());
                    }
                    ColumnSet dataset = new ColumnSet(commonLength(dataMap), dataMap);
                    if (model.getSortModels().isEmpty()) {
                        result = dataset;
                    } else {
                        result = sort(dataset, model.getSortModels());
                    }
                }
                return result;
            }
        };
    }
}
Also used : FilteredSlot(org.activityinfo.store.query.shared.columns.FilteredSlot) FilteredSlot(org.activityinfo.store.query.shared.columns.FilteredSlot) FormulaException(org.activityinfo.model.formula.diagnostic.FormulaException)

Aggregations

FormulaException (org.activityinfo.model.formula.diagnostic.FormulaException)5 FormClass (org.activityinfo.model.form.FormClass)1 FormTree (org.activityinfo.model.formTree.FormTree)1 ColumnView (org.activityinfo.model.query.ColumnView)1 EmptyColumnView (org.activityinfo.model.query.EmptyColumnView)1 CalculatedFieldType (org.activityinfo.model.type.expr.CalculatedFieldType)1 NodeMatch (org.activityinfo.store.query.shared.NodeMatch)1 FilteredSlot (org.activityinfo.store.query.shared.columns.FilteredSlot)1 Test (org.junit.Test)1