Search in sources :

Example 1 with NodeMatch

use of org.activityinfo.store.query.shared.NodeMatch 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();
    }
}
Also used : NodeMatch(org.activityinfo.store.query.shared.NodeMatch) FormulaException(org.activityinfo.model.formula.diagnostic.FormulaException)

Example 2 with NodeMatch

use of org.activityinfo.store.query.shared.NodeMatch in project activityinfo by bedatadriven.

the class SubFormSummaryTest method matchNodes.

@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());
}
Also used : SymbolNode(org.activityinfo.model.formula.SymbolNode) FormTree(org.activityinfo.model.formTree.FormTree) JoinNode(org.activityinfo.store.query.shared.join.JoinNode) NodeMatch(org.activityinfo.store.query.shared.NodeMatch) NodeMatcher(org.activityinfo.store.query.shared.NodeMatcher) Test(org.junit.Test)

Aggregations

NodeMatch (org.activityinfo.store.query.shared.NodeMatch)2 FormTree (org.activityinfo.model.formTree.FormTree)1 SymbolNode (org.activityinfo.model.formula.SymbolNode)1 FormulaException (org.activityinfo.model.formula.diagnostic.FormulaException)1 NodeMatcher (org.activityinfo.store.query.shared.NodeMatcher)1 JoinNode (org.activityinfo.store.query.shared.join.JoinNode)1 Test (org.junit.Test)1