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