@Override public ColumnPlanNode visitSymbol(SymbolNode symbolNode) { NodeMatcher nodeMatcher = new NodeMatcher(formTree); Collection<NodeMatch> nodeMatches = nodeMatcher.resolveSymbol(symbolNode); return plan(nodeMatches); }
@Override public Slot<ColumnView> visitSymbol(SymbolNode symbolNode) { // Check for recursion: are we in the process of evaluating // this symbol? Trying to do so again will lead to an infinite // loop and a StackOverflowException. if(evaluationStack.contains(symbolNode)) { return batch.addEmptyColumn(filterLevel, rootFormClass); } evaluationStack.push(symbolNode); try { Collection<NodeMatch> nodes = resolver.resolveSymbol(symbolNode); LOGGER.finer(symbolNode + " matched to " + nodes); return addColumn(nodes); } catch (AmbiguousSymbolException | SymbolNotFoundException e) { // Ambiguous or unknown symbols should result in an empty column, not full failure LOGGER.log(Level.WARNING, e.getMessage(), e); return batch.addEmptyColumn(filterLevel, rootFormClass); } finally { evaluationStack.pop(); } }
@Override public Slot<ColumnView> visitFunctionCall(final FunctionCallNode call) { if(call.getFunction() instanceof ColumnFunction) { if(call.getArguments().isEmpty()) { return createNullaryFunctionCall(call); } else { return createFunctionCall(call); } } else if(call.getFunction() instanceof BoundingBoxFunction) { FormulaNode geometry = call.getArgument(0); Collection<NodeMatch> nodes; if(geometry instanceof SymbolNode) { nodes = resolver.resolveSymbol(((SymbolNode) geometry)); } else if(geometry instanceof CompoundExpr) { nodes = resolver.resolveCompoundExpr((CompoundExpr) geometry); } else { throw new QuerySyntaxException("Function " + call.getFunction().getId() + " can only be applied" + " to an argument of type GeoArea."); } return addColumn(nodes.stream().map(n -> n.withComponent(call.getFunction().getId())).collect(Collectors.toList())); } else { throw new UnsupportedOperationException("TODO: " + call.getFunction().getId()); } }
private Collection<String> resolve(String exprString) { FormTree tree = tree(); symbolTable = new NodeMatcher(tree); FormulaNode expr = FormulaParser.parse(exprString); Collection<NodeMatch> matches; if(expr instanceof SymbolNode) { matches = symbolTable.resolveSymbol((SymbolNode) expr); } else if(expr instanceof CompoundExpr) { matches = symbolTable.resolveCompoundExpr((CompoundExpr) expr); } else { throw new IllegalArgumentException(exprString); } // Create a string that we can match against easily List<String> strings = Lists.newArrayList(); for (NodeMatch match : matches) { strings.add(match.toDebugString()); } System.out.println("Resolved " + exprString + " => " + strings); return strings; }
try { if (formulaNode instanceof SymbolNode) { matches = nodeMatcher.resolveSymbol((SymbolNode) formulaNode); } else if (formulaNode instanceof CompoundExpr) { matches = nodeMatcher.resolveCompoundExpr((CompoundExpr) formulaNode);
@Test public void matchNodes() { TestingStorageProvider catalog = new TestingStorageProvider(); ClinicForm clinicForm = catalog.getClinicForm(); FormTree formTree = catalog.getFormTree(clinicForm.getFormId()); NodeMatcher nodeMatcher = new NodeMatcher(formTree); Collection<NodeMatch> nodeMatches = nodeMatcher.resolveSymbol(new SymbolNode("NUM_CONSULT")); assertThat(nodeMatches, hasSize(1)); NodeMatch nodeMatch = Iterables.getOnlyElement(nodeMatches); JoinNode joinNode = Iterables.getOnlyElement(nodeMatch.getJoins()); }