use of org.javarosa.core.model.condition.EvaluationContext 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.condition.EvaluationContext in project javarosa by opendatakit.
the class XFormParserTest method parsesExternalSecondaryInstanceForm.
@Test
public void parsesExternalSecondaryInstanceForm() throws IOException, XPathSyntaxException {
FormDef formDef = parse(EXTERNAL_SECONDARY_INSTANCE_XML).formDef;
assertEquals("Form with external secondary instance", formDef.getTitle());
TreeReference treeReference = ((XPathPathExpr) parseXPath("instance('towns')/data_set")).getReference();
EvaluationContext evaluationContext = formDef.getEvaluationContext();
List<TreeReference> treeReferences = evaluationContext.expandReference(treeReference);
assertEquals(1, treeReferences.size());
DataInstance townInstance = formDef.getNonMainInstance("towns");
AbstractTreeElement tiRoot = townInstance.getRoot();
assertEquals("towndata", tiRoot.getName());
assertEquals(1, tiRoot.getNumChildren());
AbstractTreeElement dataSetChild = tiRoot.getChild("data_set", 0);
assertEquals("us_east", dataSetChild.getValue().getDisplayText());
}
use of org.javarosa.core.model.condition.EvaluationContext in project javarosa by opendatakit.
the class XPathEvalTest method getFunctionHandlers.
private EvaluationContext getFunctionHandlers() {
EvaluationContext ec = new EvaluationContext(null);
final Class[][] allPrototypes = { { Double.class, Double.class }, { Double.class }, { String.class, String.class }, { Double.class, String.class, Boolean.class }, { Boolean.class }, { Boolean.class, Double.class, String.class, Date.class, CustomType.class } };
ec.addFunctionHandler(new IFunctionHandler() {
@Override
public String getName() {
return "testfunc";
}
@Override
public List<Class[]> getPrototypes() {
List<Class[]> p = new ArrayList<Class[]>();
p.add(new Class[0]);
return p;
}
@Override
public boolean rawArgs() {
return false;
}
@Override
public boolean realTime() {
return false;
}
@Override
public Object eval(Object[] args, EvaluationContext ec) {
return TRUE;
}
});
ec.addFunctionHandler(new IFunctionHandler() {
@Override
public String getName() {
return "regex";
}
@Override
public Object eval(Object[] args, EvaluationContext ec) {
System.out.println("EVAL REGEX TESTS:");
for (Object arg : args) {
System.out.println("REGEX ARGS: " + arg.toString());
}
// String.re args[0].
return TRUE;
}
@Override
public List<Class[]> getPrototypes() {
List<Class[]> p = new ArrayList<Class[]>();
p.add(allPrototypes[2]);
return p;
}
@Override
public boolean rawArgs() {
return false;
}
@Override
public boolean realTime() {
return false;
}
});
ec.addFunctionHandler(new IFunctionHandler() {
@Override
public String getName() {
return "add";
}
@Override
public List<Class[]> getPrototypes() {
List<Class[]> p = new ArrayList<Class[]>();
p.add(allPrototypes[0]);
return p;
}
@Override
public boolean rawArgs() {
return false;
}
@Override
public boolean realTime() {
return false;
}
@Override
public Object eval(Object[] args, EvaluationContext ec) {
return (Double) args[0] + (Double) args[1];
}
});
ec.addFunctionHandler(new IFunctionHandler() {
@Override
public String getName() {
return "proto";
}
@Override
public List<Class[]> getPrototypes() {
List<Class[]> p = new ArrayList<Class[]>();
p.add(allPrototypes[0]);
p.add(allPrototypes[1]);
p.add(allPrototypes[2]);
p.add(allPrototypes[3]);
return p;
}
@Override
public boolean rawArgs() {
return false;
}
@Override
public boolean realTime() {
return false;
}
@Override
public Object eval(Object[] args, EvaluationContext ec) {
return printArgs(args);
}
});
ec.addFunctionHandler(new IFunctionHandler() {
@Override
public String getName() {
return "raw";
}
@Override
public List<Class[]> getPrototypes() {
List<Class[]> p = new ArrayList<Class[]>();
p.add(allPrototypes[3]);
return p;
}
@Override
public boolean rawArgs() {
return true;
}
@Override
public boolean realTime() {
return false;
}
@Override
public Object eval(Object[] args, EvaluationContext ec) {
return printArgs(args);
}
});
ec.addFunctionHandler(new IFunctionHandler() {
@Override
public String getName() {
return "null-proto";
}
@Override
public List<Class[]> getPrototypes() {
return null;
}
@Override
public boolean rawArgs() {
return false;
}
@Override
public boolean realTime() {
return false;
}
@Override
public Object eval(Object[] args, EvaluationContext ec) {
return FALSE;
}
});
ec.addFunctionHandler(new IFunctionHandler() {
@Override
public String getName() {
return "concat";
}
@Override
public List<Class[]> getPrototypes() {
return new ArrayList<Class[]>();
}
@Override
public boolean rawArgs() {
return true;
}
@Override
public boolean realTime() {
return false;
}
@Override
public Object eval(Object[] args, EvaluationContext ec) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < args.length; i++) sb.append(XPathFuncExpr.toString(args[i]));
return sb.toString();
}
});
ec.addFunctionHandler(new IFunctionHandler() {
@Override
public String getName() {
return "convertible";
}
@Override
public List<Class[]> getPrototypes() {
List<Class[]> p = new ArrayList<Class[]>();
p.add(new Class[0]);
return p;
}
@Override
public boolean rawArgs() {
return false;
}
@Override
public boolean realTime() {
return false;
}
@Override
public Object eval(Object[] args, EvaluationContext ec) {
return new IExprDataType() {
@Override
public Boolean toBoolean() {
return TRUE;
}
@Override
public Double toNumeric() {
return 5.0;
}
public String toString() {
return "hi";
}
};
}
});
ec.addFunctionHandler(new IFunctionHandler() {
@Override
public String getName() {
return "inconvertible";
}
@Override
public List<Class[]> getPrototypes() {
List<Class[]> p = new ArrayList<Class[]>();
p.add(new Class[0]);
return p;
}
@Override
public boolean rawArgs() {
return false;
}
@Override
public boolean realTime() {
return false;
}
@Override
public Object eval(Object[] args, EvaluationContext ec) {
return new Object();
}
});
ec.addFunctionHandler(new IFunctionHandler() {
@Override
public String getName() {
return "get-custom";
}
@Override
public List<Class[]> getPrototypes() {
List<Class[]> p = new ArrayList<Class[]>();
p.add(allPrototypes[4]);
return p;
}
@Override
public boolean rawArgs() {
return false;
}
@Override
public boolean realTime() {
return false;
}
@Override
public Object eval(Object[] args, EvaluationContext ec) {
return (Boolean) args[0] ? new CustomSubType() : new CustomType();
}
});
ec.addFunctionHandler(new IFunctionHandler() {
@Override
public String getName() {
return "check-types";
}
@Override
public List<Class[]> getPrototypes() {
List<Class[]> p = new ArrayList<Class[]>();
p.add(allPrototypes[5]);
return p;
}
@Override
public boolean rawArgs() {
return false;
}
@Override
public boolean realTime() {
return false;
}
@Override
public Object eval(Object[] args, EvaluationContext ec) {
if (args.length != 5 || !(args[0] instanceof Boolean) || !(args[1] instanceof Double) || !(args[2] instanceof String) || !(args[3] instanceof Date) || !(args[4] instanceof CustomType))
fail("Types in custom function handler not converted properly/prototype not matched properly");
return TRUE;
}
});
return ec;
}
Aggregations