Search in sources :

Example 16 with FormulaNode

use of org.activityinfo.model.formula.FormulaNode in project activityinfo by bedatadriven.

the class SimpleConditionListTest method existing.

@Test
public void existing() throws IOException {
    URL resource = Resources.getResource(SimpleConditionList.class, "formulas.txt");
    List<String> formulas = Resources.readLines(resource, Charsets.UTF_8);
    for (String formula : formulas) {
        FormulaNode parsed = FormulaParser.parse(formula);
        SimpleConditionList conditionList = SimpleConditionParser.parse(parsed);
        String reformula = conditionList.toFormula().asExpression();
        FormulaNode reparsed;
        try {
            reparsed = FormulaParser.parse(reformula);
        } catch (Exception e) {
            System.err.println("Original formula: " + formula);
            System.err.println("conditionList: " + conditionList);
            System.err.println("conditionList.toFormula(): " + reformula);
            throw e;
        }
        SimpleConditionList reparsedConditionList = SimpleConditionParser.parse(reparsed);
        if (!conditionList.equals(reparsedConditionList)) {
            System.err.println("Original formula: " + formula);
            System.err.println("conditionList: " + conditionList);
            System.err.println("conditionList.toFormula(): " + reformula);
            System.err.println("reparsedConditionList: " + reparsedConditionList);
            throw new AssertionError("Round trip failed");
        }
    }
}
Also used : FormulaNode(org.activityinfo.model.formula.FormulaNode) URL(java.net.URL) IOException(java.io.IOException) Test(org.junit.Test)

Example 17 with FormulaNode

use of org.activityinfo.model.formula.FormulaNode in project activityinfo by bedatadriven.

the class SimpleConditionListTest method singleSelectEqual.

@Test
public void singleSelectEqual() {
    SimpleConditionList conditionList = SimpleConditionParser.parse(FormulaParser.parse("{Q1717540673}=={t1717155924}"));
    FormulaNode formulaRoot = conditionList.toFormula();
    System.out.println(formulaRoot.asExpression());
}
Also used : FormulaNode(org.activityinfo.model.formula.FormulaNode) Test(org.junit.Test)

Example 18 with FormulaNode

use of org.activityinfo.model.formula.FormulaNode in project activityinfo by bedatadriven.

the class SimpleConditionListTest method testReparsing.

private void testReparsing(String expression, String errMessage) {
    SimpleConditionList conditionList = SimpleConditionParser.parse(FormulaParser.parse(expression));
    String reformula = conditionList.toFormula().asExpression();
    FormulaNode reparsed = FormulaParser.parse(reformula);
    SimpleConditionList reparsedConditionList = SimpleConditionParser.parse(reparsed);
    if (!conditionList.equals(reparsedConditionList)) {
        System.err.println(expression);
        System.err.println("conditionList: " + conditionList);
        System.err.println("conditionList.toFormula(): " + reformula);
        System.err.println("reparsedConditionList: " + reparsedConditionList);
        throw new AssertionError(errMessage);
    }
}
Also used : FormulaNode(org.activityinfo.model.formula.FormulaNode)

Example 19 with FormulaNode

use of org.activityinfo.model.formula.FormulaNode in project activityinfo by bedatadriven.

the class FormScan method getValuesToCache.

public Map<String, Object> getValuesToCache() {
    Map<String, Object> toPut = new HashMap<>();
    for (Map.Entry<FormulaNode, PendingSlot<ColumnView>> column : columnMap.entrySet()) {
        ColumnView value;
        try {
            value = column.getValue().get();
        } catch (IllegalStateException e) {
            throw new IllegalStateException(column.getKey().toString(), e);
        }
        toPut.put(fieldCacheKey(column.getKey()), value);
    }
    for (Map.Entry<ForeignKeyId, PendingSlot<ForeignKey>> fk : foreignKeyMap.entrySet()) {
        toPut.put(fkCacheKey(fk.getKey()), fk.getValue().get());
    }
    if (!columnMap.isEmpty()) {
        toPut.put(rowCountKey(), rowCountFromColumn(columnMap));
    } else if (rowCount != null) {
        toPut.put(rowCountKey(), rowCount.get());
    }
    return toPut;
}
Also used : FormulaNode(org.activityinfo.model.formula.FormulaNode) ForeignKeyId(org.activityinfo.store.query.shared.join.ForeignKeyId) ColumnView(org.activityinfo.model.query.ColumnView)

Example 20 with FormulaNode

use of org.activityinfo.model.formula.FormulaNode in project activityinfo by bedatadriven.

the class FormScan method updateFromCache.

public void updateFromCache(Map<String, Object> cached) {
    // See which columns we could retrieve from cache
    for (FormulaNode fieldId : Lists.newArrayList(columnMap.keySet())) {
        ColumnView view = (ColumnView) cached.get(fieldCacheKey(fieldId));
        if (view != null) {
            // populate the pending result slot with the view from the cache
            columnMap.get(fieldId).set(view);
            // remove this column from the list of columns to fetch
            columnMap.remove(fieldId);
            // resolve the rowCount slot if still needed
            if (rowCount != null) {
                rowCount.set(view.numRows());
                rowCount = null;
            }
        }
    }
    // And which foreign keys...
    for (ForeignKeyId keyId : Lists.newArrayList(foreignKeyMap.keySet())) {
        ForeignKey map = (ForeignKey) cached.get(fkCacheKey(keyId));
        if (map != null) {
            foreignKeyMap.get(keyId).set(map);
            foreignKeyMap.remove(keyId);
        }
    }
    // Do we need a row count?
    if (rowCount != null) {
        Integer count = (Integer) cached.get(rowCountKey());
        if (count != null) {
            rowCount.set(count);
        }
    }
}
Also used : FormulaNode(org.activityinfo.model.formula.FormulaNode) ForeignKeyId(org.activityinfo.store.query.shared.join.ForeignKeyId) ColumnView(org.activityinfo.model.query.ColumnView) ForeignKey(org.activityinfo.store.query.shared.columns.ForeignKey)

Aggregations

FormulaNode (org.activityinfo.model.formula.FormulaNode)27 SymbolNode (org.activityinfo.model.formula.SymbolNode)8 FormTree (org.activityinfo.model.formTree.FormTree)7 CompoundExpr (org.activityinfo.model.formula.CompoundExpr)7 Test (org.junit.Test)7 LookupKey (org.activityinfo.model.formTree.LookupKey)5 LookupKeySet (org.activityinfo.model.formTree.LookupKeySet)5 ColumnView (org.activityinfo.model.query.ColumnView)4 FormulaParser (org.activityinfo.model.formula.FormulaParser)3 ResourceId (org.activityinfo.model.resource.ResourceId)3 ForeignKeyId (org.activityinfo.store.query.shared.join.ForeignKeyId)3 FormEvalContext (org.activityinfo.model.form.FormEvalContext)2 FormField (org.activityinfo.model.form.FormField)2 FormPermissions (org.activityinfo.model.form.FormPermissions)2 FormulaLexer (org.activityinfo.model.formula.FormulaLexer)2 FieldValue (org.activityinfo.model.type.FieldValue)2 JoinNode (org.activityinfo.store.query.shared.join.JoinNode)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Optional (com.google.common.base.Optional)1 HashMultimap (com.google.common.collect.HashMultimap)1