use of org.javarosa.core.model.instance.FormInstance 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.FormInstance 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);
}
use of org.javarosa.core.model.instance.FormInstance in project javarosa by opendatakit.
the class Safe2014DagImplTest method deleteThirdRepeatGroup_evaluatesTriggerables_dependentOnTheRepeatGroupsNumber.
@Test
public void deleteThirdRepeatGroup_evaluatesTriggerables_dependentOnTheRepeatGroupsNumber() throws Exception {
// Given
final FormDef formDef = parse(r("calculation-dependent-on-the-repeat-groups-number.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, 2, elementToBeDeletedRef);
// When
TreeElement summaryNode = mainInstance.getRoot().getChildrenWithName("summary").get(0);
// check the calculation result for 10 repeat groups
assertThat(summaryNode.getValue().getDisplayText(), equalTo("55"));
// 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(1).getChildAt(0).getValue().getDisplayText(), equalTo("2"));
assertThat(repeats.get(2).getChildAt(0).getValue().getDisplayText(), equalTo("3"));
assertThat(repeats.get(3).getChildAt(0).getValue().getDisplayText(), equalTo("4"));
assertThat(repeats.get(4).getChildAt(0).getValue().getDisplayText(), equalTo("5"));
assertThat(repeats.get(5).getChildAt(0).getValue().getDisplayText(), equalTo("6"));
assertThat(repeats.get(6).getChildAt(0).getValue().getDisplayText(), equalTo("7"));
assertThat(repeats.get(7).getChildAt(0).getValue().getDisplayText(), equalTo("8"));
assertThat(repeats.get(8).getChildAt(0).getValue().getDisplayText(), equalTo("9"));
assertThat(summaryNode.getValue().getDisplayText(), equalTo("45"));
// check that correct calculations were triggered
final String[] expectedMessages = { "Processing 'Recalculate' for no [3_1] (3.0)", "Processing 'Recalculate' for summary [1] (51.0)", "Processing 'Deleted: houseM [3]: 2 triggerables were fired.' for ", "Processing 'Deleted: no [3_1]: 0 triggerables were fired.' for ", "Processing 'Recalculate' for no [4_1] (4.0)", "Processing 'Recalculate' for summary [1] (50.0)", "Processing 'Deleted: houseM [4]: 2 triggerables were fired.' for ", "Processing 'Recalculate' for no [5_1] (5.0)", "Processing 'Recalculate' for summary [1] (49.0)", "Processing 'Deleted: houseM [5]: 2 triggerables were fired.' for ", "Processing 'Recalculate' for no [6_1] (6.0)", "Processing 'Recalculate' for summary [1] (48.0)", "Processing 'Deleted: houseM [6]: 2 triggerables were fired.' for ", "Processing 'Recalculate' for no [7_1] (7.0)", "Processing 'Recalculate' for summary [1] (47.0)", "Processing 'Deleted: houseM [7]: 2 triggerables were fired.' for ", "Processing 'Recalculate' for no [8_1] (8.0)", "Processing 'Recalculate' for summary [1] (46.0)", "Processing 'Deleted: houseM [8]: 2 triggerables were fired.' for ", "Processing 'Recalculate' for no [9_1] (9.0)", "Processing 'Recalculate' for summary [1] (45.0)", "Processing 'Deleted: houseM [9]: 2 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.FormInstance in project javarosa by opendatakit.
the class XPathEvalTest method createTestInstance.
public FormInstance createTestInstance() {
TreeElement data = new TreeElement("data");
data.addChild(new TreeElement("path"));
return new FormInstance(data);
}
use of org.javarosa.core.model.instance.FormInstance in project javarosa by opendatakit.
the class XPathEvalTest method createCountNonEmptyTestInstance.
public FormInstance createCountNonEmptyTestInstance() {
TreeElement data = new TreeElement("data");
TreeElement path = new TreeElement("path", 0);
path.addChild(new TreeElement("child", 0));
path.addChild(new TreeElement("child", 1));
data.addChild(path);
data.addChild(new TreeElement("path", 1));
path = new TreeElement("path", 2);
path.setValue(new StringData("some value"));
data.addChild(path);
path = new TreeElement("path", 3);
path.addChild(new TreeElement("child", 0));
data.addChild(path);
data.addChild(new TreeElement("path", 4));
return new FormInstance(data);
}
Aggregations