Search in sources :

Example 31 with TreeReference

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

the class Safe2014DagImpl method triggerTriggerables.

private Set<QuickTriggerable> triggerTriggerables(FormInstance mainInstance, EvaluationContext evalContext, TreeReference ref, Set<QuickTriggerable> alreadyEvaluated) {
    // turn unambiguous ref into a generic ref
    // to identify what nodes should be triggered by this
    // reference changing
    TreeReference genericRef = ref.genericize();
    // get triggerables which are activated by the generic reference
    ArrayList<QuickTriggerable> triggered = triggerIndex.get(genericRef);
    if (triggered == null) {
        return alreadyEvaluated;
    }
    Set<QuickTriggerable> triggeredCopy = new HashSet<QuickTriggerable>(triggered);
    // Evaluate all of the triggerables in our new set
    return evaluateTriggerables(mainInstance, evalContext, triggeredCopy, ref, alreadyEvaluated);
}
Also used : TreeReference(org.javarosa.core.model.instance.TreeReference) HashSet(java.util.HashSet)

Example 32 with TreeReference

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

the class Safe2014DagImpl method initializeTriggerables.

private Set<QuickTriggerable> initializeTriggerables(FormInstance mainInstance, EvaluationContext evalContext, TreeReference rootRef, Set<QuickTriggerable> alreadyEvaluated) {
    TreeReference genericRoot = rootRef.genericize();
    Set<QuickTriggerable> applicable = new HashSet<QuickTriggerable>();
    for (int i = 0; i < triggerablesDAG.size(); i++) {
        QuickTriggerable qt = triggerablesDAG.get(i);
        for (int j = 0; j < qt.t.getTargets().size(); j++) {
            TreeReference target = qt.t.getTargets().get(j);
            if (genericRoot.isParentOf(target, false)) {
                applicable.add(qt);
                break;
            }
        }
    }
    return evaluateTriggerables(mainInstance, evalContext, applicable, rootRef, alreadyEvaluated);
}
Also used : TreeReference(org.javarosa.core.model.instance.TreeReference) HashSet(java.util.HashSet)

Example 33 with TreeReference

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

the class Safe2014DagImpl method copyItemsetAnswer.

@Override
public void copyItemsetAnswer(FormInstance mainInstance, EvaluationContext evalContext, TreeReference copyRef, TreeElement copyToElement, boolean midSurvey) {
    TreeReference targetRef = copyToElement.getRef();
    Set<QuickTriggerable> qtSet1 = triggerTriggerables(mainInstance, evalContext, copyRef, // trigger conditions that
    new HashSet<QuickTriggerable>(0));
    // depend on the creation of
    // these new nodes
    publishSummary("Copied itemset answer (phase 1)", targetRef, qtSet1);
    Set<QuickTriggerable> qtSet2 = initializeTriggerables(mainInstance, evalContext, copyRef, // initialize conditions for
    new HashSet<QuickTriggerable>(0));
    // the node (and sub-nodes)
    publishSummary("Copied itemset answer (phase 2)", targetRef, qtSet2);
// not 100% sure this will work since destRef is ambiguous as the last
// step, but i think it's supposed to work
}
Also used : TreeReference(org.javarosa.core.model.instance.TreeReference)

Example 34 with TreeReference

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

the class April2014DagImpl method initializeTriggerables.

/**
 * Walks the current set of conditions, and evaluates each of them with the
 * current context.
 */
@Override
public Collection<QuickTriggerable> initializeTriggerables(FormInstance mainInstance, EvaluationContext evalContext, TreeReference rootRef, boolean midSurvey) {
    TreeReference genericRoot = rootRef.genericize();
    ArrayList<QuickTriggerable> applicable = new ArrayList<QuickTriggerable>();
    for (int i = 0; i < triggerablesDAG.size(); i++) {
        QuickTriggerable qt = triggerablesDAG.get(i);
        for (int j = 0; j < qt.t.getTargets().size(); j++) {
            TreeReference target = qt.t.getTargets().get(j);
            if (genericRoot.isParentOf(target, false)) {
                applicable.add(qt);
                break;
            }
        }
    }
    return evaluateTriggerables(mainInstance, evalContext, applicable, rootRef);
}
Also used : TreeReference(org.javarosa.core.model.instance.TreeReference) ArrayList(java.util.ArrayList)

Example 35 with TreeReference

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

the class April2014DagImpl method finalizeTriggerables.

/**
 * Finalize the DAG associated with the form's triggered conditions. This
 * will create the appropriate ordering and dependencies to ensure the
 * conditions will be evaluated in the appropriate orders.
 *
 * @throws IllegalStateException
 *            - If the trigger ordering contains an illegal cycle and the
 *            triggers can't be laid out appropriately
 */
@Override
public void finalizeTriggerables(FormInstance mainInstance, EvaluationContext evalContext) throws IllegalStateException {
    // 
    // DAGify the triggerables based on dependencies and sort them so that
    // triggerables come only after the triggerables they depend on
    // 
    triggerablesDAG.clear();
    ArrayList<QuickTriggerable[]> partialOrdering = new ArrayList<QuickTriggerable[]>();
    for (int i = 0; i < unorderedTriggerables.size(); i++) {
        QuickTriggerable qt = unorderedTriggerables.get(i);
        ArrayList<QuickTriggerable> deps = new ArrayList<QuickTriggerable>();
        fillTriggeredElements(evalContext, qt, deps);
        for (int j = 0; j < deps.size(); j++) {
            QuickTriggerable qu = deps.get(j);
            QuickTriggerable[] edge = { qt, qu };
            partialOrdering.add(edge);
        }
    }
    ArrayList<QuickTriggerable> vertices = new ArrayList<QuickTriggerable>(unorderedTriggerables);
    ArrayList<QuickTriggerable> roots = new ArrayList<QuickTriggerable>(unorderedTriggerables.size());
    while (vertices.size() > 0) {
        // determine root nodes
        roots.clear();
        roots.addAll(vertices);
        for (int i = 0; i < partialOrdering.size(); i++) {
            QuickTriggerable[] edge = partialOrdering.get(i);
            roots.remove(edge[1]);
        }
        // if no root nodes while graph still has nodes, graph has cycles
        if (roots.size() == 0) {
            String hints = "";
            for (QuickTriggerable qt : vertices) {
                for (TreeReference r : qt.t.getTargets()) {
                    hints += "\n" + r.toString(true);
                }
            }
            String message = "Cycle detected in form's relevant and calculation logic!";
            if (!hints.equals("")) {
                message += "\nThe following nodes are likely involved in the loop:" + hints;
            }
            throw new IllegalStateException(message);
        }
        // remove root nodes and edges originating from them
        for (int i = 0; i < roots.size(); i++) {
            QuickTriggerable root = roots.get(i);
            triggerablesDAG.add(root);
            vertices.remove(root);
        }
        for (int i = partialOrdering.size() - 1; i >= 0; i--) {
            QuickTriggerable[] edge = partialOrdering.get(i);
            if (roots.contains(edge[0]))
                partialOrdering.remove(i);
        }
    }
    // 
    // build the condition index for repeatable nodes
    // 
    conditionRepeatTargetIndex.clear();
    for (QuickTriggerable qt : triggerablesDAG) {
        if (qt.t instanceof Condition) {
            List<TreeReference> targets = qt.t.getTargets();
            for (TreeReference target : targets) {
                if (mainInstance.getTemplate(target) != null) {
                    conditionRepeatTargetIndex.put(target, qt);
                }
            }
        }
    }
// printTriggerables();
}
Also used : Condition(org.javarosa.core.model.condition.Condition) 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