Search in sources :

Example 26 with EvaluationContext

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

Example 27 with EvaluationContext

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());
}
Also used : AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) DataInstance(org.javarosa.core.model.instance.DataInstance) FormDef(org.javarosa.core.model.FormDef) TreeReference(org.javarosa.core.model.instance.TreeReference) XPathPathExpr(org.javarosa.xpath.expr.XPathPathExpr) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext) Test(org.junit.Test)

Example 28 with EvaluationContext

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;
}
Also used : IExprDataType(org.javarosa.xpath.IExprDataType) IFunctionHandler(org.javarosa.core.model.condition.IFunctionHandler) Date(java.util.Date) ArrayList(java.util.ArrayList) List(java.util.List) EvaluationContext(org.javarosa.core.model.condition.EvaluationContext)

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