use of org.javarosa.core.model.instance.TreeReference in project javarosa by opendatakit.
the class SetValueAction method processAction.
public void processAction(FormDef model, TreeReference contextRef) {
// Qualify the reference if necessary
TreeReference qualifiedReference = contextRef == null ? target : target.contextualize(contextRef);
// insert events should only trigger on the right nodes
if (contextRef != null) {
// strategy
if (!contextRef.isParentOf(qualifiedReference, false)) {
return;
}
}
// TODO: either the target or the value's node might not exist here, catch and throw
// reasonably
EvaluationContext context = new EvaluationContext(model.getEvaluationContext(), qualifiedReference);
Object result;
if (explicitValue != null) {
result = explicitValue;
} else {
result = XPathFuncExpr.unpack(value.eval(model.getMainInstance(), context));
}
AbstractTreeElement node = context.resolveReference(qualifiedReference);
if (node == null) {
throw new NullPointerException("Target of TreeReference " + qualifiedReference.toString(true) + " could not be resolved!");
}
int dataType = node.getDataType();
IAnswerData val = Recalculate.wrapData(result, dataType);
model.setValue(val == null ? null : AnswerDataFactory.templateByDataType(dataType).cast(val.uncast()), qualifiedReference, true);
}
use of org.javarosa.core.model.instance.TreeReference in project javarosa by opendatakit.
the class April2014DagImpl 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
// 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 Collections.emptySet();
}
ArrayList<QuickTriggerable> triggeredCopy = new ArrayList<QuickTriggerable>(triggered);
// Evaluate all of the triggerables in our new list
return evaluateTriggerables(mainInstance, evalContext, triggeredCopy, ref);
}
use of org.javarosa.core.model.instance.TreeReference in project javarosa by opendatakit.
the class April2014DagImpl 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 {
// Now identify all of the fully qualified nodes which this
// triggerable updates. (Multiple nodes can be updated by the
// same trigger)
List<TreeReference> v = evalContext.expandReference(contextRef);
// Go through each one and evaluate the trigger expresion
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 Triggerable method apply.
/**
* Not for re-implementation, dispatches all of the evaluation
* @param mainInstance
* @param parentContext
* @param context
*/
public final List<EvaluationResult> apply(FormInstance mainInstance, EvaluationContext parentContext, TreeReference context) {
// The triggeringRoot is the highest level of actual data we can inquire about, but it _isn't_ necessarily the basis
// for the actual expressions, so we need genericize that ref against the current context
TreeReference ungenericised = originalContextRef.contextualize(context);
EvaluationContext ec = new EvaluationContext(parentContext, ungenericised);
Object result = eval(mainInstance, ec);
List<EvaluationResult> affectedNodes = new ArrayList<EvaluationResult>(0);
for (TreeReference target : targets) {
TreeReference targetRef = target.contextualize(ec.getContextRef());
List<TreeReference> v = ec.expandReference(targetRef);
for (TreeReference affectedRef : v) {
apply(affectedRef, result, mainInstance);
affectedNodes.add(new EvaluationResult(affectedRef, result));
}
}
return affectedNodes;
}
use of org.javarosa.core.model.instance.TreeReference in project javarosa by opendatakit.
the class Triggerable method getTriggers.
public Set<TreeReference> getTriggers() {
// / should this be originalContextRef???
Set<TreeReference> relTriggers = expr.getTriggers(null);
Set<TreeReference> absTriggers = new HashSet<TreeReference>();
for (TreeReference r : relTriggers) {
absTriggers.add(r.anchor(originalContextRef));
}
return absTriggers;
}
Aggregations