Search in sources :

Example 26 with TreeReference

use of org.javarosa.core.model.instance.TreeReference in project javarosa by opendatakit.

the class FormDef method isRepeatRelevant.

public boolean isRepeatRelevant(TreeReference repeatRef) {
    boolean relev = true;
    QuickTriggerable qc = dagImpl.getTriggerableForRepeatGroup(repeatRef.genericize());
    if (qc != null) {
        Condition c = (Condition) qc.t;
        relev = c.evalBool(mainInstance, new EvaluationContext(exprEvalContext, repeatRef));
    }
    // check the relevancy of the immediate parent
    if (relev) {
        TreeElement templNode = mainInstance.getTemplate(repeatRef);
        TreeReference parentPath = templNode.getParent().getRef().genericize();
        TreeElement parentNode = mainInstance.resolveReference(parentPath.contextualize(repeatRef));
        relev = parentNode.isRelevant();
    }
    return relev;
}
Also used : Condition(org.javarosa.core.model.condition.Condition) TreeReference(org.javarosa.core.model.instance.TreeReference) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext) TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 27 with TreeReference

use of org.javarosa.core.model.instance.TreeReference in project javarosa by opendatakit.

the class FormIndex method assignRefs.

public void assignRefs(FormDef f) {
    FormIndex cur = this;
    List<Integer> indexes = new ArrayList<Integer>();
    List<Integer> multiplicities = new ArrayList<Integer>();
    List<IFormElement> elements = new ArrayList<IFormElement>();
    f.collapseIndex(this, indexes, multiplicities, elements);
    List<Integer> curMults = new ArrayList<Integer>();
    List<IFormElement> curElems = new ArrayList<IFormElement>();
    int i = 0;
    while (cur != null) {
        curMults.add(multiplicities.get(i));
        curElems.add(elements.get(i));
        TreeReference ref = f.getChildInstanceRef(curElems, curMults);
        cur.reference = ref;
        cur = cur.getNextLevel();
        i++;
    }
}
Also used : TreeReference(org.javarosa.core.model.instance.TreeReference) ArrayList(java.util.ArrayList)

Example 28 with TreeReference

use of org.javarosa.core.model.instance.TreeReference in project javarosa by opendatakit.

the class IDag method reportDependencyCycles.

public final void reportDependencyCycles(XFormParserReporter reporter) {
    HashSet<TreeReference> vertices = new HashSet<TreeReference>();
    ArrayList<TreeReference[]> edges = new ArrayList<TreeReference[]>();
    // build graph
    ArrayList<TreeReference> targets = new ArrayList<TreeReference>();
    for (TreeReference trigger : triggerIndex.keySet()) {
        if (!vertices.contains(trigger))
            vertices.add(trigger);
        ArrayList<QuickTriggerable> triggered = triggerIndex.get(trigger);
        targets.clear();
        for (QuickTriggerable qt : triggered) {
            Triggerable t = qt.t;
            for (int j = 0; j < t.getTargets().size(); j++) {
                TreeReference target = t.getTargets().get(j);
                if (!targets.contains(target))
                    targets.add(target);
            }
        }
        for (int i = 0; i < targets.size(); i++) {
            TreeReference target = targets.get(i);
            if (!vertices.contains(target))
                vertices.add(target);
            TreeReference[] edge = { trigger, target };
            edges.add(edge);
        }
    }
    // find cycles
    boolean acyclic = true;
    HashSet<TreeReference> leaves = new HashSet<TreeReference>(vertices.size());
    while (vertices.size() > 0) {
        // determine leaf nodes
        leaves.clear();
        leaves.addAll(vertices);
        for (int i = 0; i < edges.size(); i++) {
            TreeReference[] edge = (TreeReference[]) edges.get(i);
            leaves.remove(edge[0]);
        }
        // if no leaf nodes while graph still has nodes, graph has cycles
        if (leaves.size() == 0) {
            acyclic = false;
            break;
        }
        // remove leaf nodes and edges pointing to them
        for (TreeReference leaf : leaves) {
            vertices.remove(leaf);
        }
        for (int i = edges.size() - 1; i >= 0; i--) {
            TreeReference[] edge = (TreeReference[]) edges.get(i);
            if (leaves.contains(edge[1]))
                edges.remove(i);
        }
    }
    if (!acyclic) {
        StringBuilder b = new StringBuilder();
        b.append("XPath Dependency Cycle:\n");
        for (int i = 0; i < edges.size(); i++) {
            TreeReference[] edge = edges.get(i);
            b.append(edge[0].toString()).append(" => ").append(edge[1].toString()).append("\n");
        }
        reporter.error(b.toString());
        throw new RuntimeException("Dependency cycles amongst the xpath expressions in relevant/calculate");
    }
}
Also used : ArrayList(java.util.ArrayList) Triggerable(org.javarosa.core.model.condition.Triggerable) TreeReference(org.javarosa.core.model.instance.TreeReference) HashSet(java.util.HashSet)

Example 29 with TreeReference

use of org.javarosa.core.model.instance.TreeReference in project javarosa by opendatakit.

the class LegacyDagImpl method evaluateTriggerable.

/**
 * Step 3 in DAG cascade. evaluate the individual triggerable expressions
 * against the anchor (the value that changed which triggered recomputation)
 *
 * @param qt
 *            The triggerable to be updated
 * @param anchorRef
 *            The reference to the value which was changed.
 */
private void evaluateTriggerable(FormInstance mainInstance, EvaluationContext evalContext, QuickTriggerable qt, TreeReference anchorRef) {
    // Contextualize the reference used by the triggerable against the
    // anchor
    TreeReference contextRef = qt.t.contextualizeContextRef(anchorRef);
    try {
        List<TreeReference> v = evalContext.expandReference(contextRef);
        for (int i = 0; i < v.size(); i++) {
            EvaluationContext ec = new EvaluationContext(evalContext, v.get(i));
            qt.t.apply(mainInstance, ec, v.get(i));
        }
    } catch (Exception e) {
        throw new RuntimeException("Error evaluating field '" + contextRef.getNameLast() + "': " + e.getMessage(), e);
    }
}
Also used : TreeReference(org.javarosa.core.model.instance.TreeReference) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext)

Example 30 with TreeReference

use of org.javarosa.core.model.instance.TreeReference in project javarosa by opendatakit.

the class LegacyDagImpl method triggerTriggerables.

/**
 * The entry point for the DAG cascade after a value is changed in the
 * model.
 *
 * @param ref
 *            The full contextualized unambiguous reference of the value
 *            that was changed.
 */
@Override
public Collection<QuickTriggerable> triggerTriggerables(FormInstance mainInstance, EvaluationContext evalContext, TreeReference ref, boolean midSurvey) {
    // turn unambiguous ref into a generic ref
    TreeReference genericRef = ref.genericize();
    // get conditions triggered by this node
    ArrayList<QuickTriggerable> triggered = triggerIndex.get(genericRef);
    if (triggered == null) {
        return Collections.emptySet();
    }
    ArrayList<QuickTriggerable> triggeredCopy = new ArrayList<QuickTriggerable>(triggered);
    // Evaluate all of the triggerables in our new List
    return evaluateTriggerables(mainInstance, evalContext, triggeredCopy, ref);
}
Also used : TreeReference(org.javarosa.core.model.instance.TreeReference) ArrayList(java.util.ArrayList)

Aggregations

TreeReference (org.javarosa.core.model.instance.TreeReference)85 ArrayList (java.util.ArrayList)30 TreeElement (org.javarosa.core.model.instance.TreeElement)29 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)16 Constraint (org.javarosa.core.model.condition.Constraint)12 HashSet (java.util.HashSet)11 Test (org.junit.Test)9 FormInstance (org.javarosa.core.model.instance.FormInstance)8 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)7 IFormElement (org.javarosa.core.model.IFormElement)6 Condition (org.javarosa.core.model.condition.Condition)6 IAnswerData (org.javarosa.core.model.data.IAnswerData)6 InstanceInitializationFactory (org.javarosa.core.model.instance.InstanceInitializationFactory)6 GroupDef (org.javarosa.core.model.GroupDef)5 XPathReference (org.javarosa.model.xform.XPathReference)5 DataBinding (org.javarosa.core.model.DataBinding)4 IDataReference (org.javarosa.core.model.IDataReference)4 DataInstance (org.javarosa.core.model.instance.DataInstance)4 EvaluationResult (org.javarosa.debug.EvaluationResult)4 XPathException (org.javarosa.xpath.XPathException)4