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