Search in sources :

Example 71 with TreeReference

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);
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) TreeReference(org.javarosa.core.model.instance.TreeReference) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext)

Example 72 with TreeReference

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);
}
Also used : TreeReference(org.javarosa.core.model.instance.TreeReference) ArrayList(java.util.ArrayList)

Example 73 with TreeReference

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);
    }
}
Also used : TreeReference(org.javarosa.core.model.instance.TreeReference) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext)

Example 74 with TreeReference

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;
}
Also used : TreeReference(org.javarosa.core.model.instance.TreeReference) ArrayList(java.util.ArrayList) EvaluationResult(org.javarosa.debug.EvaluationResult)

Example 75 with TreeReference

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;
}
Also used : TreeReference(org.javarosa.core.model.instance.TreeReference) HashSet(java.util.HashSet)

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