Search in sources :

Example 16 with SymbolNode

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

the class NodeMatch method forId.

public static NodeMatch forId(String idSymbol, FormClass formClass) {
    NodeMatch match = new NodeMatch();
    match.formClass = formClass;
    match.type = Type.ID;
    match.fieldExpr = new SymbolNode(idSymbol);
    match.joins = Lists.newLinkedList();
    return match;
}
Also used : SymbolNode(org.activityinfo.model.formula.SymbolNode)

Example 17 with SymbolNode

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

the class NodeMatch method joinsTo.

private static List<JoinNode> joinsTo(List<List<FormTree.Node>> partitions, Optional<StatFunction> aggregation) {
    /*
         *  Given a parent: "Site.Location.Territoire.District"
         *  This is represented as a tree of nodes:
         *      District -> Territoire -> Location -> Site
         *      
         *  We want to turn into a list of joins: 
         *      (site field -> form site), 
         *      (location field -> form school),
         *      (field territoire -> form Territoire)
         *      (field district -> form District)
         */
    LinkedList<JoinNode> joins = new LinkedList<>();
    for (int i = 0; i < partitions.size() - 1; i++) {
        // Reference field that functions as a foreign key
        List<FormTree.Node> left = partitions.get(i);
        FormField leftField = left.get(0).getField();
        ResourceId leftFormId = left.get(0).getDefiningFormClass().getId();
        FormulaNode leftFieldExpr = toExpr(left);
        // "RIGHT" side
        // Joining fom left to right using resource ids (primary key)
        List<FormTree.Node> right = partitions.get(i + 1);
        ResourceId rightFormId = right.get(0).getDefiningFormClass().getId();
        if (leftField.getType() instanceof ReferenceType) {
            // Join based on the (left) foreign key ==> (right) primary key
            joins.add(new JoinNode(JoinType.REFERENCE, leftFormId, leftFieldExpr, rightFormId, Optional.<StatFunction>absent()));
        } else if (leftField.getType() instanceof SubFormReferenceType) {
            joins.add(new JoinNode(JoinType.SUBFORM, leftFormId, new SymbolNode(ColumnModel.ID_SYMBOL), rightFormId, aggregation));
        } else {
            throw new IllegalStateException("Invalid field for joining: " + leftField.getType());
        }
    }
    return joins;
}
Also used : JoinNode(org.activityinfo.store.query.shared.join.JoinNode) SymbolNode(org.activityinfo.model.formula.SymbolNode) FormulaNode(org.activityinfo.model.formula.FormulaNode) JoinNode(org.activityinfo.store.query.shared.join.JoinNode) LinkedList(java.util.LinkedList) SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) ReferenceType(org.activityinfo.model.type.ReferenceType) SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) SymbolNode(org.activityinfo.model.formula.SymbolNode) FormulaNode(org.activityinfo.model.formula.FormulaNode) ResourceId(org.activityinfo.model.resource.ResourceId) StatFunction(org.activityinfo.model.formula.functions.StatFunction) FormField(org.activityinfo.model.form.FormField)

Example 18 with SymbolNode

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

the class NodeMatch method toExpr.

private static FormulaNode toExpr(List<FormTree.Node> partition) {
    Iterator<FormTree.Node> it = partition.iterator();
    FormulaNode expr = new SymbolNode(it.next().getFieldId());
    while (it.hasNext()) {
        expr = new CompoundExpr(expr, new SymbolNode(it.next().getFieldId()));
    }
    return expr;
}
Also used : CompoundExpr(org.activityinfo.model.formula.CompoundExpr) SymbolNode(org.activityinfo.model.formula.SymbolNode) FormulaNode(org.activityinfo.model.formula.FormulaNode) SymbolNode(org.activityinfo.model.formula.SymbolNode) FormulaNode(org.activityinfo.model.formula.FormulaNode) JoinNode(org.activityinfo.store.query.shared.join.JoinNode)

Example 19 with SymbolNode

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

the class QueryEvaluatorTest method circularReference.

@Test
public void circularReference() throws Exception {
    final FormClass formClass = new FormClass(ResourceId.valueOf("XYZ"));
    formClass.addField(ResourceId.valueOf("FA")).setCode("A").setLabel("Field A").setType(new CalculatedFieldType("B"));
    formClass.addField(ResourceId.valueOf("FB")).setCode("B").setLabel("Field B").setType(new CalculatedFieldType("A"));
    FormStorageProviderStub catalog = new FormStorageProviderStub();
    catalog.addForm(formClass).withRowCount(10);
    ColumnSetBuilder builder = new ColumnSetBuilder(catalog, new NullFormScanCache(), new NullFormSupervisor());
    FormScanBatch batch = builder.createNewBatch();
    QueryEvaluator evaluator = new QueryEvaluator(FilterLevel.BASE, catalog.getTree(formClass.getId()), batch);
    Slot<ColumnView> a = evaluator.evaluateExpression(new SymbolNode("A"));
    Slot<ColumnView> aPlusOne = evaluator.evaluateExpression(FormulaParser.parse("A+1"));
    builder.execute(batch);
    assertThat(a.get().numRows(), equalTo(10));
    assertThat(a.get().getString(0), nullValue());
    assertThat(aPlusOne.get().getString(0), nullValue());
    assertThat(aPlusOne.get().getDouble(0), equalTo(1d));
}
Also used : ColumnSetBuilder(org.activityinfo.store.query.server.ColumnSetBuilder) SymbolNode(org.activityinfo.model.formula.SymbolNode) CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) FormClass(org.activityinfo.model.form.FormClass) ColumnView(org.activityinfo.model.query.ColumnView) Test(org.junit.Test)

Example 20 with SymbolNode

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

the class EffectiveMapping method getRequiredColumns.

public List<ColumnModel> getRequiredColumns() {
    if (multiValued) {
        List<ColumnModel> columns = new ArrayList<>();
        EnumType enumType = (EnumType) this.formula.getResultType();
        for (EnumItem enumItem : enumType.getValues()) {
            ColumnModel columnModel = new ColumnModel();
            columnModel.setId(getColumnId(enumItem));
            columnModel.setFormula(new CompoundExpr(this.formula.getRootNode(), new SymbolNode(enumItem.getId())));
            columns.add(columnModel);
        }
        return columns;
    }
    if (this.mapping != null && this.formula.isValid()) {
        ColumnModel columnModel = new ColumnModel();
        columnModel.setId(getColumnId());
        columnModel.setFormula(this.formula.getFormula());
        return Collections.singletonList(columnModel);
    }
    return Collections.emptyList();
}
Also used : CompoundExpr(org.activityinfo.model.formula.CompoundExpr) SymbolNode(org.activityinfo.model.formula.SymbolNode) EnumType(org.activityinfo.model.type.enumerated.EnumType) ColumnModel(org.activityinfo.model.query.ColumnModel) EnumItem(org.activityinfo.model.type.enumerated.EnumItem)

Aggregations

SymbolNode (org.activityinfo.model.formula.SymbolNode)22 CompoundExpr (org.activityinfo.model.formula.CompoundExpr)12 FormulaNode (org.activityinfo.model.formula.FormulaNode)8 ColumnModel (org.activityinfo.model.query.ColumnModel)6 Test (org.junit.Test)5 FormTree (org.activityinfo.model.formTree.FormTree)4 ResourceId (org.activityinfo.model.resource.ResourceId)3 JoinNode (org.activityinfo.store.query.shared.join.JoinNode)3 ConstantNode (org.activityinfo.model.formula.ConstantNode)2 ColumnSet (org.activityinfo.model.query.ColumnSet)2 ColumnView (org.activityinfo.model.query.ColumnView)2 QueryModel (org.activityinfo.model.query.QueryModel)2 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Optional (com.google.common.base.Optional)1 Supplier (com.google.common.base.Supplier)1 HashMultimap (com.google.common.collect.HashMultimap)1 Multimap (com.google.common.collect.Multimap)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1