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