Search in sources :

Example 1 with EvaluationResult

use of org.javarosa.debug.EvaluationResult in project javarosa by opendatakit.

the class FormDef method evaluateConstraint.

public boolean evaluateConstraint(TreeReference ref, IAnswerData data) {
    if (data == null) {
        return true;
    }
    TreeElement node = mainInstance.resolveReference(ref);
    Constraint c = node.getConstraint();
    if (c == null) {
        return true;
    }
    EvaluationContext ec = new EvaluationContext(exprEvalContext, ref);
    ec.isConstraint = true;
    ec.candidateValue = data;
    boolean result = c.constraint.eval(mainInstance, ec);
    getEventNotifier().publishEvent(new Event("Constraint", new EvaluationResult(ref, result)));
    return result;
}
Also used : Constraint(org.javarosa.core.model.condition.Constraint) Event(org.javarosa.debug.Event) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext) EvaluationResult(org.javarosa.debug.EvaluationResult) TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 2 with EvaluationResult

use of org.javarosa.debug.EvaluationResult in project javarosa by opendatakit.

the class FormDef method populateDynamicChoices.

/**
 * Identify the itemset in the backend model, and create a set of
 * SelectChoice objects at the current question reference based on the data
 * in the model.
 * <p/>
 * Will modify the itemset binding to contain the relevant choices
 *
 * @param itemset The binding for an itemset, where the choices will be populated
 * @param curQRef A reference to the current question's element, which will be
 *                used to determine the values to be chosen from.
 */
public void populateDynamicChoices(ItemsetBinding itemset, TreeReference curQRef) {
    getEventNotifier().publishEvent(new Event("Dynamic choices", new EvaluationResult(curQRef, null)));
    List<SelectChoice> choices = new ArrayList<SelectChoice>();
    List<TreeReference> matches = itemset.nodesetExpr.evalNodeset(this.getMainInstance(), new EvaluationContext(exprEvalContext, itemset.contextRef.contextualize(curQRef)));
    DataInstance fi = null;
    if (// We're not dealing
    itemset.nodesetRef.getInstanceName() != null) // with the default
    // instance
    {
        fi = getNonMainInstance(itemset.nodesetRef.getInstanceName());
        if (fi == null) {
            throw new XPathException("Instance " + itemset.nodesetRef.getInstanceName() + " not found");
        }
    } else {
        fi = getMainInstance();
    }
    if (matches == null) {
        throw new XPathException("Could not find references depended on by" + itemset.nodesetRef.getInstanceName());
    }
    for (int i = 0; i < matches.size(); i++) {
        TreeReference item = matches.get(i);
        // String label =
        // itemset.labelExpr.evalReadable(this.getMainInstance(), new
        // EvaluationContext(exprEvalContext, item));
        String label = itemset.labelExpr.evalReadable(fi, new EvaluationContext(exprEvalContext, item));
        String value = null;
        TreeElement copyNode = null;
        if (itemset.copyMode) {
            copyNode = this.getMainInstance().resolveReference(itemset.copyRef.contextualize(item));
        }
        if (itemset.valueRef != null) {
            // value = itemset.valueExpr.evalReadable(this.getMainInstance(),
            // new EvaluationContext(exprEvalContext, item));
            value = itemset.valueExpr.evalReadable(fi, new EvaluationContext(exprEvalContext, item));
        }
        // SelectChoice choice = new
        // SelectChoice(labelID,labelInnerText,value,isLocalizable);
        SelectChoice choice = new SelectChoice(label, value != null ? value : "dynamic:" + i, itemset.labelIsItext);
        choice.setIndex(i);
        if (itemset.copyMode)
            choice.copyNode = copyNode;
        choices.add(choice);
    }
    if (choices.size() == 0) {
        // throw new
        // RuntimeException("dynamic select question has no choices! [" +
        // itemset.nodesetRef + "]");
        // When you exit a survey mid way through and want to save it, it seems
        // that Collect wants to
        // go through all the questions. Well of course not all the questions
        // are going to have answers
        // to chose from if the user hasn't filled them out. So I'm just going
        // to make a note of this
        // and not throw an exception.
        Std.out.println("Dynamic select question has no choices! [" + itemset.nodesetRef + "]. If this occurs while filling out a form (and not while saving an incomplete form), the filter condition may have eliminated all the choices. Is that what you intended?\n");
    }
    itemset.clearChoices();
    itemset.setChoices(choices, this.getLocalizer());
}
Also used : DataInstance(org.javarosa.core.model.instance.DataInstance) XPathException(org.javarosa.xpath.XPathException) ArrayList(java.util.ArrayList) EvaluationResult(org.javarosa.debug.EvaluationResult) Constraint(org.javarosa.core.model.condition.Constraint) TreeElement(org.javarosa.core.model.instance.TreeElement) TreeReference(org.javarosa.core.model.instance.TreeReference) Event(org.javarosa.debug.Event) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext)

Example 3 with EvaluationResult

use of org.javarosa.debug.EvaluationResult in project javarosa by opendatakit.

the class LatestDagBase method doEvaluateTriggerables.

protected Set<QuickTriggerable> doEvaluateTriggerables(FormInstance mainInstance, EvaluationContext evalContext, Set<QuickTriggerable> tv, TreeReference anchorRef, Set<QuickTriggerable> alreadyEvaluated) {
    // tv should now contain all of the triggerable components which are
    // going
    // to need to be addressed
    // by this update.
    // 'triggerables' is topologically-ordered by dependencies, so evaluate
    // the triggerables in 'tv'
    // in the order they appear in 'triggerables'
    Set<QuickTriggerable> fired = new HashSet<QuickTriggerable>();
    Map<TreeReference, List<TreeReference>> firedAnchors = new LinkedHashMap<TreeReference, List<TreeReference>>();
    for (QuickTriggerable qt : triggerablesDAG) {
        if (tv.contains(qt) && !alreadyEvaluated.contains(qt)) {
            List<TreeReference> affectedTriggers = qt.t.findAffectedTriggers(firedAnchors);
            if (affectedTriggers.isEmpty()) {
                affectedTriggers.add(anchorRef);
            }
            List<EvaluationResult> evaluationResults = evaluateTriggerable(mainInstance, evalContext, qt, affectedTriggers);
            if (evaluationResults.size() > 0) {
                fired.add(qt);
                for (EvaluationResult evaluationResult : evaluationResults) {
                    TreeReference affectedRef = evaluationResult.getAffectedRef();
                    TreeReference key = affectedRef.genericize();
                    List<TreeReference> values = firedAnchors.get(key);
                    if (values == null) {
                        values = new ArrayList<TreeReference>();
                        firedAnchors.put(key, values);
                    }
                    values.add(affectedRef);
                }
            }
            fired.add(qt);
        }
    }
    return fired;
}
Also used : TreeReference(org.javarosa.core.model.instance.TreeReference) List(java.util.List) ArrayList(java.util.ArrayList) EvaluationResult(org.javarosa.debug.EvaluationResult) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with EvaluationResult

use of org.javarosa.debug.EvaluationResult in project javarosa by opendatakit.

the class LatestDagBase 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 anchorRefs
 */
private List<EvaluationResult> evaluateTriggerable(FormInstance mainInstance, EvaluationContext evalContext, QuickTriggerable qt, List<TreeReference> anchorRefs) {
    List<EvaluationResult> evaluationResults = new ArrayList<EvaluationResult>(0);
    // Contextualize the reference used by the triggerable against the
    // anchor
    Set<TreeReference> updatedContextRef = new HashSet<TreeReference>();
    for (TreeReference anchorRef : anchorRefs) {
        TreeReference contextRef = qt.t.contextualizeContextRef(anchorRef);
        if (updatedContextRef.contains(contextRef)) {
            continue;
        }
        try {
            // Now identify all of the fully qualified nodes which this
            // triggerable
            // updates. (Multiple nodes can be updated by the same trigger)
            List<TreeReference> qualifiedList = evalContext.expandReference(contextRef);
            // Go through each one and evaluate the trigger expression
            for (TreeReference qualified : qualifiedList) {
                EvaluationContext ec = new EvaluationContext(evalContext, qualified);
                evaluationResults.addAll(qt.t.apply(mainInstance, ec, qualified));
            }
            boolean fired = evaluationResults.size() > 0;
            if (fired) {
                accessor.getEventNotifier().publishEvent(new Event(qt.t.getClass().getSimpleName(), evaluationResults));
            }
            updatedContextRef.add(contextRef);
        } catch (Exception e) {
            throw new RuntimeException("Error evaluating field '" + contextRef.getNameLast() + "': " + e.getMessage(), e);
        }
    }
    return evaluationResults;
}
Also used : TreeReference(org.javarosa.core.model.instance.TreeReference) ArrayList(java.util.ArrayList) Event(org.javarosa.debug.Event) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext) EvaluationResult(org.javarosa.debug.EvaluationResult) HashSet(java.util.HashSet)

Example 5 with EvaluationResult

use of org.javarosa.debug.EvaluationResult 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)

Aggregations

EvaluationResult (org.javarosa.debug.EvaluationResult)5 ArrayList (java.util.ArrayList)4 TreeReference (org.javarosa.core.model.instance.TreeReference)4 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)3 Event (org.javarosa.debug.Event)3 HashSet (java.util.HashSet)2 Constraint (org.javarosa.core.model.condition.Constraint)2 TreeElement (org.javarosa.core.model.instance.TreeElement)2 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 DataInstance (org.javarosa.core.model.instance.DataInstance)1 XPathException (org.javarosa.xpath.XPathException)1