use of org.javarosa.core.model.instance.TreeReference in project javarosa by opendatakit.
the class CompactInstanceWrapper method readTreeElement.
/**
* recursively read in a node of the instance, by filling out the template instance
* @param e
* @param ref
* @param in
* @param pf
* @throws IOException
* @throws DeserializationException
*/
private void readTreeElement(TreeElement e, DataInputStream in, PrototypeFactory pf) throws IOException, DeserializationException {
TreeElement templ = instance.getTemplatePath(e.getRef());
boolean isGroup = !templ.isLeaf();
if (isGroup) {
List<String> childTypes = new ArrayList<String>(templ.getNumChildren());
for (int i = 0; i < templ.getNumChildren(); i++) {
String childName = templ.getChildAt(i).getName();
if (!childTypes.contains(childName)) {
childTypes.add(childName);
}
}
for (int i = 0; i < childTypes.size(); i++) {
String childName = childTypes.get(i);
TreeReference childTemplRef = e.getRef().extendRef(childName, 0);
TreeElement childTempl = instance.getTemplatePath(childTemplRef);
boolean repeatable = childTempl.isRepeatable();
int n = ExtUtil.readInt(in);
boolean relevant = (n > 0);
if (!repeatable && n > 1) {
throw new DeserializationException("Detected repeated instances of a non-repeatable node");
}
if (repeatable) {
int mult = e.getChildMultiplicity(childName);
for (int j = mult - 1; j >= 0; j--) {
e.removeChild(childName, j);
}
for (int j = 0; j < n; j++) {
TreeReference dstRef = e.getRef().extendRef(childName, j);
try {
instance.copyNode(childTempl, dstRef);
} catch (InvalidReferenceException ire) {
// If there is an invalid reference, this is a malformed instance,
// so we'll throw a Deserialization exception.
TreeReference r = ire.getInvalidReference();
if (r == null) {
throw new DeserializationException("Null Reference while attempting to deserialize! " + ire.getMessage());
} else {
throw new DeserializationException("Invalid Reference while attemtping to deserialize! Reference: " + r.toString(true) + " | " + ire.getMessage());
}
}
TreeElement child = e.getChild(childName, j);
child.setRelevant(true);
readTreeElement(child, in, pf);
}
} else {
TreeElement child = e.getChild(childName, 0);
child.setRelevant(relevant);
if (relevant) {
readTreeElement(child, in, pf);
}
}
}
} else {
e.setValue((IAnswerData) ExtUtil.read(in, new ExtWrapAnswerData(e.getDataType())));
}
}
use of org.javarosa.core.model.instance.TreeReference in project javarosa by opendatakit.
the class RestoreUtils method applyDataType.
public static void applyDataType(FormInstance dm, String path, TreeReference parent, int dataType) {
TreeReference ref = childRef(path, parent);
List<TreeReference> v = new EvaluationContext(dm).expandReference(ref);
for (int i = 0; i < v.size(); i++) {
TreeElement e = dm.resolveReference(v.get(i));
e.setDataType(dataType);
}
}
use of org.javarosa.core.model.instance.TreeReference in project javarosa by opendatakit.
the class RestoreUtils method templateChild.
public static void templateChild(FormInstance dm, String prefixPath, TreeReference parent, Restorable r) {
TreeReference childRef = (prefixPath == null ? parent : RestoreUtils.childRef(prefixPath, parent));
childRef = childRef(r.getRestorableType(), childRef);
templateData(r, dm, childRef);
}
use of org.javarosa.core.model.instance.TreeReference in project javarosa by opendatakit.
the class Safe2014DagImplTest method deleteSecondRepeatGroup_doesNotEvaluateTriggerables_notDependentOnTheParentPosition.
@Test
public void deleteSecondRepeatGroup_doesNotEvaluateTriggerables_notDependentOnTheParentPosition() throws Exception {
// Given
final FormDef formDef = parse(r("repeat-group-with-children-calculations-not-dependent-on-the-parent.xml")).formDef;
assertIDagImplUnderTest(formDef);
// trigger all calculations
formDef.initialize(false, new InstanceInitializationFactory());
// it's important to set the test event notifier now to avoid storing events from the above initialization
formDef.setEventNotifier(eventNotifier);
final FormInstance mainInstance = formDef.getMainInstance();
final TreeElement elementToBeDeleted = mainInstance.getRoot().getChildAt(2);
final TreeReference elementToBeDeletedRef = elementToBeDeleted.getRef();
// Index pointing to the second repeat group
final FormIndex indexToBeDeleted = new FormIndex(0, 1, elementToBeDeletedRef);
// When
// Safe2014DagImplTest.deleteRepeatGroup is called by the below method
formDef.deleteRepeat(indexToBeDeleted);
// Then
final List<TreeElement> repeats = mainInstance.getRoot().getChildrenWithName("houseM");
// check the values based on the position of the parents
assertThat(repeats.get(0).getChildAt(0).getValue().getDisplayText(), equalTo("1"));
assertThat(repeats.get(0).getChildAt(2).getValue().getDisplayText(), equalTo("AX"));
assertThat(repeats.get(1).getChildAt(0).getValue().getDisplayText(), equalTo("2"));
assertThat(repeats.get(1).getChildAt(2).getValue().getDisplayText(), equalTo("CX"));
assertThat(repeats.get(2).getChildAt(0).getValue().getDisplayText(), equalTo("3"));
assertThat(repeats.get(2).getChildAt(2).getValue().getDisplayText(), equalTo("DX"));
assertThat(repeats.get(3).getChildAt(0).getValue().getDisplayText(), equalTo("4"));
assertThat(repeats.get(3).getChildAt(2).getValue().getDisplayText(), equalTo("EX"));
// check that correct calculations were triggered
final String[] expectedMessages = { "Processing 'Recalculate' for no [2_1] (2.0)", "Processing 'Deleted: houseM [2]: 1 triggerables were fired.' for ", "Processing 'Deleted: no [2_1]: 1 triggerables were fired.' for ", "Processing 'Recalculate' for name_concat [2_1] (CX)", "Processing 'Deleted: name [2_1]: 1 triggerables were fired.' for ", "Processing 'Deleted: name_concat [2_1]: 1 triggerables were fired.' for ", "Processing 'Recalculate' for no [3_1] (3.0)", "Processing 'Deleted: houseM [3]: 1 triggerables were fired.' for ", "Processing 'Recalculate' for no [4_1] (4.0)", "Processing 'Deleted: houseM [4]: 1 triggerables were fired.' for " };
assertThat(dagEvents.size(), equalTo(expectedMessages.length));
int messageIndex = 0;
for (String expectedMessage : expectedMessages) {
assertThat(dagEvents.get(messageIndex++).getDisplayMessage(), equalTo(expectedMessage));
}
}
use of org.javarosa.core.model.instance.TreeReference in project javarosa by opendatakit.
the class Safe2014DagImplTest method deleteRepeatGroupWithCalculationsTimingTest.
@Test
public void deleteRepeatGroupWithCalculationsTimingTest() throws Exception {
// Given
final FormDef formDef = parse(r("delete-repeat-group-with-calculations-timing-test.xml")).formDef;
assertIDagImplUnderTest(formDef);
// trigger all calculations
formDef.initialize(false, new InstanceInitializationFactory());
final FormInstance mainInstance = formDef.getMainInstance();
// Construct the required amount of repeats
final TreeElement templateRepeat = mainInstance.getRoot().getChildAt(0);
// Raise this value to really measure
final int numberOfRepeats = 200;
for (int i = 0; i < numberOfRepeats; i++) {
final TreeReference refToNewRepeat = templateRepeat.getRef();
// set the correct multiplicity
refToNewRepeat.setMultiplicity(1, i);
final FormIndex indexOfNewRepeat = new FormIndex(0, i, refToNewRepeat);
formDef.createNewRepeat(indexOfNewRepeat);
}
final TreeElement firstRepeat = mainInstance.getRoot().getChildAt(1);
final TreeReference firstRepeatRef = firstRepeat.getRef();
final FormIndex firstRepeatIndex = new FormIndex(0, 0, firstRepeatRef);
// When
long startMs = System.currentTimeMillis();
for (int i = 0; i < numberOfRepeats; i++) {
long currentIterationStart = System.nanoTime();
formDef.deleteRepeat(firstRepeatIndex);
double tookMs = (System.nanoTime() - currentIterationStart) / 1000000D;
System.out.printf("%d\t%.3f\n", i, tookMs);
}
// Then
final String elapsedFormatted = LocalTime.fromMillisOfDay(System.currentTimeMillis() - startMs).toString();
System.out.println("Deletion of " + numberOfRepeats + " repeats took " + elapsedFormatted);
}
Aggregations