use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.
the class FormDef method evaluateConstraint.
public boolean evaluateConstraint(TreeReference ref, IAnswerData data) {
if (data == null) {
return true;
}
TreeElement node = mainInstance.resolveReference(ref);
Constraint c = node.getConstraint();
if (c == null) {
return true;
}
EvaluationContext ec = new EvaluationContext(exprEvalContext, ref);
ec.isConstraint = true;
ec.candidateValue = data;
boolean result = c.constraint.eval(mainInstance, ec);
getEventNotifier().publishEvent(new Event("Constraint", new EvaluationResult(ref, result)));
return result;
}
use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.
the class FormDef method createNewRepeat.
public void createNewRepeat(FormIndex index) throws InvalidReferenceException {
TreeReference destRef = getChildInstanceRef(index);
TreeElement template = mainInstance.getTemplate(destRef);
mainInstance.copyNode(template, destRef);
TreeElement newNode = mainInstance.resolveReference(destRef);
preloadInstance(newNode);
// 2013-05-14 - ctsims - Events should get fired _before_ calculate stuff
// is fired, moved
// this above triggering triggerables
// Grab any actions listening to this event
List<Action> listeners = getEventListeners(Action.EVENT_JR_INSERT);
for (Action a : listeners) {
a.processAction(this, destRef);
}
TreeReference parentRef = destRef.getParentRef();
TreeElement parentElement = mainInstance.resolveReference(parentRef);
dagImpl.createRepeatGroup(getMainInstance(), getEvaluationContext(), destRef, parentElement, newNode);
}
use of org.javarosa.core.model.instance.TreeElement in project javarosa by opendatakit.
the class FormDef method canCreateRepeat.
public boolean canCreateRepeat(TreeReference repeatRef, FormIndex repeatIndex) {
GroupDef repeat = (GroupDef) this.getChild(repeatIndex);
// Check to see if this repeat can have children added by the user
if (repeat.noAddRemove) {
// should have
if (repeat.getCountReference() != null) {
int currentMultiplicity = repeatIndex.getElementMultiplicity();
// Lu Gram: the count XPath needs to be contextualized for nested
// repeat groups...
TreeReference countRef = FormInstance.unpackReference(repeat.getCountReference());
TreeElement countNode = this.getMainInstance().resolveReference(countRef.contextualize(repeatRef));
if (countNode == null) {
throw new RuntimeException("Could not locate the repeat count value expected at " + repeat.getCountReference().getReference().toString());
}
// get the total multiplicity possible
IAnswerData count = countNode.getValue();
long fullcount = count == null ? 0 : (Integer) count.getValue();
if (fullcount <= currentMultiplicity) {
return false;
}
} else {
// Otherwise the user can never add repeat instances
return false;
}
}
return true;
}
use of org.javarosa.core.model.instance.TreeElement 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.instance.TreeElement in project javarosa by opendatakit.
the class EvaluationContext method expandReferenceAccumulator.
/**
* Recursively performs the search for all repeated nodes that match the pattern of the 'ref' argument.
*
* @param sourceRef original path we're matching against
* @param sourceInstance original node obtained from sourceRef
* @param workingRef explicit path that refers to the current node
* @param refs accumulator List to collect matching paths.
*/
private void expandReferenceAccumulator(TreeReference sourceRef, DataInstance sourceInstance, TreeReference workingRef, List<TreeReference> refs, boolean includeTemplates) {
final int depth = workingRef.size();
// check to see if we've matched fully
if (depth == sourceRef.size()) {
// TODO: Do we need to clone these references?
refs.add(workingRef);
return;
}
// Get the next set of matching references
final String name = sourceRef.getName(depth);
List<XPathExpression> predicates = sourceRef.getPredicate(depth);
// Copy predicates for batch fetch
if (predicates != null) {
List<XPathExpression> predCopy = new ArrayList<XPathExpression>(predicates.size());
for (XPathExpression xpe : predicates) {
predCopy.add(xpe);
}
predicates = predCopy;
}
// ETHERTON: Is this where we should test for predicates?
final int mult = sourceRef.getMultiplicity(depth);
final List<TreeReference> treeReferences = new ArrayList<>(1);
final AbstractTreeElement node = sourceInstance.resolveReference(workingRef);
if (node.getNumChildren() > 0) {
if (mult == TreeReference.INDEX_UNBOUND) {
final List<TreeElement> childrenWithName = node.getChildrenWithName(name);
final int count = childrenWithName.size();
for (int i = 0; i < count; i++) {
TreeElement child = childrenWithName.get(i);
if (child.getMultiplicity() != i) {
throw new IllegalStateException("Unexpected multiplicity mismatch");
}
treeReferences.add(child.getRef());
}
if (includeTemplates) {
AbstractTreeElement template = node.getChild(name, TreeReference.INDEX_TEMPLATE);
if (template != null) {
treeReferences.add(template.getRef());
}
}
} else if (mult != TreeReference.INDEX_ATTRIBUTE) {
// TODO: Make this test mult >= 0?
// If the multiplicity is a simple integer, just get
// the appropriate child
AbstractTreeElement child = node.getChild(name, mult);
if (child != null) {
treeReferences.add(child.getRef());
}
}
}
if (mult == TreeReference.INDEX_ATTRIBUTE) {
AbstractTreeElement attribute = node.getAttribute(null, name);
if (attribute != null) {
treeReferences.add(attribute.getRef());
}
}
if (predicates != null && predicateEvaluationProgress != null) {
predicateEvaluationProgress[1] += treeReferences.size();
}
if (predicates != null) {
boolean firstTime = true;
List<TreeReference> passed = new ArrayList<TreeReference>(treeReferences.size());
for (XPathExpression xpe : predicates) {
for (int i = 0; i < treeReferences.size(); ++i) {
// if there are predicates then we need to see if e.nextElement meets the standard of the predicate
TreeReference treeRef = treeReferences.get(i);
// test the predicate on the treeElement
EvaluationContext evalContext = rescope(treeRef, (firstTime ? treeRef.getMultLast() : i));
Object o = xpe.eval(sourceInstance, evalContext);
if (o instanceof Boolean) {
boolean testOutcome = (Boolean) o;
if (testOutcome) {
passed.add(treeRef);
}
}
}
firstTime = false;
treeReferences.clear();
treeReferences.addAll(passed);
passed.clear();
if (predicateEvaluationProgress != null) {
predicateEvaluationProgress[0]++;
}
}
}
for (TreeReference treeRef : treeReferences) {
expandReferenceAccumulator(sourceRef, sourceInstance, treeRef, refs, includeTemplates);
}
}
Aggregations