Search in sources :

Example 11 with EvaluationContext

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

Example 12 with EvaluationContext

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

Example 13 with EvaluationContext

use of org.javarosa.core.model.condition.EvaluationContext in project collect by opendatakit.

the class ExternalAppsUtils method populateParameters.

public static void populateParameters(Intent intent, Map<String, String> exParams, TreeReference reference) throws ExternalParamsException {
    FormDef formDef = Collect.getInstance().getFormController().getFormDef();
    FormInstance formInstance = formDef.getInstance();
    EvaluationContext evaluationContext = new EvaluationContext(formDef.getEvaluationContext(), reference);
    if (exParams != null) {
        for (Map.Entry<String, String> paramEntry : exParams.entrySet()) {
            String paramEntryValue = paramEntry.getValue();
            try {
                Object result;
                if (paramEntryValue.startsWith("'")) {
                    // but not require an ending quote
                    if (paramEntryValue.endsWith("'")) {
                        result = paramEntryValue.substring(1, paramEntryValue.length() - 1);
                    } else {
                        result = paramEntryValue.substring(1, paramEntryValue.length());
                    }
                } else if (paramEntryValue.startsWith("/")) {
                    // treat this is an xpath
                    XPathPathExpr pathExpr = XPathReference.getPathExpr(paramEntryValue);
                    XPathNodeset xpathNodeset = pathExpr.eval(formInstance, evaluationContext);
                    result = XPathFuncExpr.unpack(xpathNodeset);
                } else if (paramEntryValue.equals("instanceProviderID()")) {
                    // instanceProviderID returns -1 if the current instance has not been
                    // saved to disk already
                    String path = Collect.getInstance().getFormController().getInstanceFile().getAbsolutePath();
                    String instanceProviderID = "-1";
                    Cursor c = new InstancesDao().getInstancesCursorForFilePath(path);
                    if (c != null && c.getCount() > 0) {
                        // should only ever be one
                        c.moveToFirst();
                        instanceProviderID = c.getString(c.getColumnIndex(InstanceColumns._ID));
                    }
                    if (c != null) {
                        c.close();
                    }
                    result = instanceProviderID;
                } else {
                    // treat this is a function
                    XPathExpression xpathExpression = XPathParseTool.parseXPath(paramEntryValue);
                    result = xpathExpression.eval(formInstance, evaluationContext);
                }
                if (result != null && result instanceof Serializable) {
                    intent.putExtra(paramEntry.getKey(), (Serializable) result);
                }
            } catch (Exception e) {
                throw new ExternalParamsException("Could not evaluate '" + paramEntryValue + "'", e);
            }
        }
    }
}
Also used : XPathExpression(org.javarosa.xpath.expr.XPathExpression) Serializable(java.io.Serializable) XPathPathExpr(org.javarosa.xpath.expr.XPathPathExpr) Cursor(android.database.Cursor) ExternalParamsException(org.odk.collect.android.exception.ExternalParamsException) ExternalParamsException(org.odk.collect.android.exception.ExternalParamsException) InstancesDao(org.odk.collect.android.dao.InstancesDao) FormDef(org.javarosa.core.model.FormDef) XPathNodeset(org.javarosa.xpath.XPathNodeset) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext) FormInstance(org.javarosa.core.model.instance.FormInstance) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 14 with EvaluationContext

use of org.javarosa.core.model.condition.EvaluationContext in project collect by opendatakit.

the class ExternalDataUtil method populateExternalChoices.

public static ArrayList<SelectChoice> populateExternalChoices(FormEntryPrompt formEntryPrompt, XPathFuncExpr xpathfuncexpr) {
    try {
        List<SelectChoice> selectChoices = formEntryPrompt.getSelectChoices();
        ArrayList<SelectChoice> returnedChoices = new ArrayList<SelectChoice>();
        for (SelectChoice selectChoice : selectChoices) {
            String value = selectChoice.getValue();
            if (isAnInteger(value)) {
                // treat this as a static choice
                returnedChoices.add(selectChoice);
            } else {
                String displayColumns = formEntryPrompt.getSelectChoiceText(selectChoice);
                String imageColumn = formEntryPrompt.getSpecialFormSelectChoiceText(selectChoice, FormEntryCaption.TEXT_FORM_IMAGE);
                if (imageColumn != null && imageColumn.startsWith(JR_IMAGES_PREFIX)) {
                    imageColumn = imageColumn.substring(JR_IMAGES_PREFIX.length());
                }
                // if (displayColumns == null || displayColumns.trim().length() == 0) {
                // throw new InvalidSyntaxException("The label column in the choices sheet
                // appears to be empty (or has been calculated as empty).");
                // }
                ExternalDataManager externalDataManager = Collect.getInstance().getExternalDataManager();
                FormInstance formInstance = Collect.getInstance().getFormController().getFormDef().getInstance();
                EvaluationContext baseEvaluationContext = new EvaluationContext(formInstance);
                EvaluationContext evaluationContext = new EvaluationContext(baseEvaluationContext, formEntryPrompt.getIndex().getReference());
                // we can only add only the appropriate by querying the xPathFuncExpr.id.name
                evaluationContext.addFunctionHandler(new ExternalDataHandlerSearch(externalDataManager, displayColumns, value, imageColumn));
                Object eval = xpathfuncexpr.eval(formInstance, evaluationContext);
                if (eval.getClass().isAssignableFrom(ArrayList.class)) {
                    @SuppressWarnings("unchecked") List<SelectChoice> dynamicChoices = (ArrayList<SelectChoice>) eval;
                    for (SelectChoice dynamicChoice : dynamicChoices) {
                        returnedChoices.add(dynamicChoice);
                    }
                } else {
                    throw new ExternalDataException(Collect.getInstance().getString(R.string.ext_search_return_error, eval.getClass().getName()));
                }
            }
        }
        return returnedChoices;
    } catch (Exception e) {
        throw new ExternalDataException(e.getMessage(), e);
    }
}
Also used : SelectChoice(org.javarosa.core.model.SelectChoice) ArrayList(java.util.ArrayList) ExternalDataException(org.odk.collect.android.exception.ExternalDataException) ExternalDataHandlerSearch(org.odk.collect.android.external.handler.ExternalDataHandlerSearch) XPathSyntaxException(org.javarosa.xpath.parser.XPathSyntaxException) ExternalDataException(org.odk.collect.android.exception.ExternalDataException) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext) FormInstance(org.javarosa.core.model.instance.FormInstance)

Example 15 with EvaluationContext

use of org.javarosa.core.model.condition.EvaluationContext in project javarosa by opendatakit.

the class FormInstanceParser method verifyBindings.

private void verifyBindings(FormInstance instance, String mainInstanceNodeName) {
    // check <bind>s (can't bind to '/', bound nodes actually exist)
    for (int i = 0; i < bindings.size(); i++) {
        DataBinding bind = bindings.get(i);
        TreeReference ref = FormInstance.unpackReference(bind.getReference());
        if (ref.size() == 0) {
            Std.out.println("Cannot bind to '/'; ignoring bind...");
            bindings.remove(i);
            i--;
        } else {
            List<TreeReference> nodes = new EvaluationContext(instance).expandReference(ref, true);
            if (nodes.size() == 0) {
                reporter.warning(XFormParserReporter.TYPE_ERROR_PRONE, "<bind> defined for a node that doesn't exist [" + ref.toString() + "]. The node's name was probably changed and the bind should be updated. ", null);
            }
        }
    }
    // check <repeat>s (can't bind to '/' or '/data')
    for (TreeReference ref : getRepeatableRefs()) {
        if (ref.size() <= 1) {
            throw new XFormParseException("Cannot bind repeat to '/' or '/" + mainInstanceNodeName + "'");
        }
    }
    // check control/group/repeat bindings (bound nodes exist, question can't bind to '/')
    List<String> bindErrors = new ArrayList<>();
    verifyControlBindings(formDef, instance, bindErrors);
    if (bindErrors.size() > 0) {
        String errorMsg = "";
        for (String bindError : bindErrors) {
            errorMsg += bindError + "\n";
        }
        throw new XFormParseException(errorMsg);
    }
    // check that repeat members bind to the proper scope (not above the binding of the parent repeat, and not within any sub-repeat (or outside repeat))
    verifyRepeatMemberBindings(formDef, instance, null);
    // check that label/copy/value refs are children of nodeset ref, and exist
    verifyItemsetBindings(instance);
    verifyItemsetSrcDstCompatibility(instance);
}
Also used : TreeReference(org.javarosa.core.model.instance.TreeReference) DataBinding(org.javarosa.core.model.DataBinding) ArrayList(java.util.ArrayList) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext) Constraint(org.javarosa.core.model.condition.Constraint)

Aggregations

EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)28 TreeReference (org.javarosa.core.model.instance.TreeReference)16 ArrayList (java.util.ArrayList)8 Constraint (org.javarosa.core.model.condition.Constraint)8 TreeElement (org.javarosa.core.model.instance.TreeElement)8 FormDef (org.javarosa.core.model.FormDef)4 XPathNodeset (org.javarosa.xpath.XPathNodeset)4 XPathExpression (org.javarosa.xpath.expr.XPathExpression)4 FormInstance (org.javarosa.core.model.instance.FormInstance)3 EvaluationResult (org.javarosa.debug.EvaluationResult)3 Event (org.javarosa.debug.Event)3 XPathException (org.javarosa.xpath.XPathException)3 XPathSyntaxException (org.javarosa.xpath.parser.XPathSyntaxException)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 DataBinding (org.javarosa.core.model.DataBinding)2 QuestionDef (org.javarosa.core.model.QuestionDef)2 SelectChoice (org.javarosa.core.model.SelectChoice)2 IFunctionHandler (org.javarosa.core.model.condition.IFunctionHandler)2