Search in sources :

Example 71 with TreeElement

use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.

the class IDag method getConditionExpressionForTrueAction.

/**
 * Pull this in from FormOverview so that we can make fields private.
 *
 * @param instanceNode
 * @param action
 * @return
 */
public final IConditionExpr getConditionExpressionForTrueAction(FormInstance mainInstance, TreeElement instanceNode, int action) {
    IConditionExpr expr = null;
    for (int i = 0; i < triggerablesDAG.size() && expr == null; i++) {
        // Clayton Sims - Jun 1, 2009 : Not sure how legitimate this
        // cast is. It might work now, but break later.
        // Clayton Sims - Jun 24, 2009 : Yeah, that change broke things.
        // For now, we won't bother to print out anything that isn't
        // a condition.
        QuickTriggerable qt = triggerablesDAG.get(i);
        if (qt.t instanceof Condition) {
            Condition c = (Condition) qt.t;
            if (c.trueAction == action) {
                List<TreeReference> targets = c.getTargets();
                for (int j = 0; j < targets.size() && expr == null; j++) {
                    TreeReference target = targets.get(j);
                    TreeReference tr = (TreeReference) (new XPathReference(target)).getReference();
                    TreeElement element = mainInstance.getTemplatePath(tr);
                    if (instanceNode == element) {
                        expr = c.getExpr();
                    }
                }
            }
        }
    }
    return expr;
}
Also used : Condition(org.javarosa.core.model.condition.Condition) IConditionExpr(org.javarosa.core.model.condition.IConditionExpr) TreeReference(org.javarosa.core.model.instance.TreeReference) XPathReference(org.javarosa.model.xform.XPathReference) TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 72 with TreeElement

use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.

the class Safe2014DagImpl method deleteRepeatGroup.

@Override
public void deleteRepeatGroup(FormInstance mainInstance, EvaluationContext evalContext, TreeReference deleteRef, TreeElement parentElement, TreeElement deletedElement) {
    /* After a repeat group has been deleted, the following repeat groups position has changed.
         Evaluate triggerables which depend on the repeat group reference. Directly or indirectly.
       */
    final String repeatGroupName = deletedElement.getName();
    for (int i = deletedElement.getMultiplicity(); i < parentElement.getChildMultiplicity(repeatGroupName); i++) {
        final TreeElement repeatGroup = parentElement.getChild(repeatGroupName, i);
        Set<QuickTriggerable> alreadyEvaluated = triggerTriggerables(mainInstance, evalContext, repeatGroup.getRef(), new HashSet<QuickTriggerable>(0));
        publishSummary("Deleted", repeatGroup.getRef(), alreadyEvaluated);
        if (repeatGroup.getRef().equals(deleteRef)) {
            /* Evaluate the children triggerables only once, for the deleted repeat group.
               Only children of the deleted repeat group have actually changed (they're gone) and thus calculations depend
               on the must be re-evaluated, the following repeat groups have been shifted along with their children.
               If there are calculations - regardless if inside the repeat or outside - that depend on the following
               repeat group positions, they will fired cascade by the above code anyway.
               Unit test for this scenario:
               Safe2014DagImplTest#deleteThirdRepeatGroup_evaluatesTriggerables_indirectlyDependentOnTheRepeatGroupsNumber
             */
            evaluateChildrenTriggerables(mainInstance, evalContext, repeatGroup, false, alreadyEvaluated);
        }
    }
}
Also used : TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement)

Example 73 with TreeElement

use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.

the class Safe2014DagImpl method addChildrenOfElement.

// Recursive step of utility method
private void addChildrenOfElement(FormInstance mainInstance, EvaluationContext evalContext, AbstractTreeElement el, Set<TreeReference> toAdd, boolean expandRepeatables) {
    TreeElement repeatTemplate = expandRepeatables ? mainInstance.getTemplatePath(el.getRef()) : null;
    if (repeatTemplate != null) {
        for (int i = 0; i < repeatTemplate.getNumChildren(); ++i) {
            TreeElement child = repeatTemplate.getChildAt(i);
            toAdd.add(child.getRef().genericize());
            addChildrenOfElement(mainInstance, evalContext, child, toAdd, expandRepeatables);
        }
    } else {
        for (int i = 0; i < el.getNumChildren(); ++i) {
            AbstractTreeElement child = el.getChildAt(i);
            toAdd.add(child.getRef().genericize());
            addChildrenOfElement(mainInstance, evalContext, child, toAdd, expandRepeatables);
        }
    }
}
Also used : AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement) TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement)

Example 74 with TreeElement

use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.

the class Safe2014DagImpl method addChildrenOfReference.

/**
 * This is a utility method to get all of the references of a node. It can
 * be replaced when we support dependent XPath Steps (IE: /path/to//)
 */
private void addChildrenOfReference(FormInstance mainInstance, EvaluationContext evalContext, TreeReference original, Set<TreeReference> toAdd, boolean expandRepeatables) {
    // original has already been added to the 'toAdd' list.
    TreeElement repeatTemplate = expandRepeatables ? mainInstance.getTemplatePath(original) : null;
    if (repeatTemplate != null) {
        for (int i = 0; i < repeatTemplate.getNumChildren(); ++i) {
            TreeElement child = repeatTemplate.getChildAt(i);
            toAdd.add(child.getRef().genericize());
            addChildrenOfElement(mainInstance, evalContext, child, toAdd, expandRepeatables);
        }
    } else {
        List<TreeReference> refSet = evalContext.expandReference(original);
        for (TreeReference ref : refSet) {
            addChildrenOfElement(mainInstance, evalContext, evalContext.resolveReference(ref), toAdd, expandRepeatables);
        }
    }
}
Also used : TreeReference(org.javarosa.core.model.instance.TreeReference) TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement)

Example 75 with TreeElement

use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.

the class CompactInstanceWrapper method writeTreeElement.

/**
 * recursively write out a node of the instance
 * @param out
 * @param e
 * @param ref
 * @throws IOException
 */
private void writeTreeElement(DataOutputStream out, TreeElement e) throws IOException {
    TreeElement templ = instance.getTemplatePath(e.getRef());
    boolean isGroup = !templ.isLeaf();
    if (isGroup) {
        List<String> childTypesHandled = new ArrayList<String>(templ.getNumChildren());
        for (int i = 0; i < templ.getNumChildren(); i++) {
            String childName = templ.getChildAt(i).getName();
            if (!childTypesHandled.contains(childName)) {
                childTypesHandled.add(childName);
                int mult = e.getChildMultiplicity(childName);
                if (mult > 0 && !e.getChild(childName, 0).isRelevant()) {
                    mult = 0;
                }
                ExtUtil.writeNumeric(out, mult);
                for (int j = 0; j < mult; j++) {
                    writeTreeElement(out, e.getChild(childName, j));
                }
            }
        }
    } else {
        ExtUtil.write(out, new ExtWrapAnswerData(e.getDataType(), e.getValue()));
    }
}
Also used : ArrayList(java.util.ArrayList) TreeElement(org.javarosa.core.model.instance.TreeElement)

Aggregations

TreeElement (org.javarosa.core.model.instance.TreeElement)86 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)33 TreeReference (org.javarosa.core.model.instance.TreeReference)28 FormInstance (org.javarosa.core.model.instance.FormInstance)16 ArrayList (java.util.ArrayList)15 Constraint (org.javarosa.core.model.condition.Constraint)11 Test (org.junit.Test)10 Element (org.kxml2.kdom.Element)9 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)8 FormDef (org.javarosa.core.model.FormDef)7 InstanceInitializationFactory (org.javarosa.core.model.instance.InstanceInitializationFactory)7 IOException (java.io.IOException)6 IFormElement (org.javarosa.core.model.IFormElement)6 StringData (org.javarosa.core.model.data.StringData)6 HashMap (java.util.HashMap)5 IAnswerData (org.javarosa.core.model.data.IAnswerData)4 File (java.io.File)3 GroupDef (org.javarosa.core.model.GroupDef)3 IDataReference (org.javarosa.core.model.IDataReference)3 QuestionDef (org.javarosa.core.model.QuestionDef)3