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();
}
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();
}
}
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))));
}
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);
}
}
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;
}
};
}
}
Aggregations