Search in sources :

Example 1 with InstanceInitializationFactory

use of org.javarosa.core.model.instance.InstanceInitializationFactory 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));
    }
}
Also used : InstanceInitializationFactory(org.javarosa.core.model.instance.InstanceInitializationFactory) TreeReference(org.javarosa.core.model.instance.TreeReference) FormInstance(org.javarosa.core.model.instance.FormInstance) TreeElement(org.javarosa.core.model.instance.TreeElement) Test(org.junit.Test)

Example 2 with InstanceInitializationFactory

use of org.javarosa.core.model.instance.InstanceInitializationFactory 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);
}
Also used : InstanceInitializationFactory(org.javarosa.core.model.instance.InstanceInitializationFactory) TreeReference(org.javarosa.core.model.instance.TreeReference) FormInstance(org.javarosa.core.model.instance.FormInstance) TreeElement(org.javarosa.core.model.instance.TreeElement) Test(org.junit.Test)

Example 3 with InstanceInitializationFactory

use of org.javarosa.core.model.instance.InstanceInitializationFactory 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));
    }
}
Also used : InstanceInitializationFactory(org.javarosa.core.model.instance.InstanceInitializationFactory) TreeReference(org.javarosa.core.model.instance.TreeReference) FormInstance(org.javarosa.core.model.instance.FormInstance) TreeElement(org.javarosa.core.model.instance.TreeElement) Test(org.junit.Test)

Example 4 with InstanceInitializationFactory

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

the class FormLoaderTask method initializeForm.

private boolean initializeForm(FormDef formDef, FormEntryController fec) {
    final InstanceInitializationFactory instanceInit = new InstanceInitializationFactory();
    boolean usedSavepoint = false;
    if (instancePath != null) {
        File instanceXml = new File(instancePath);
        // Use the savepoint file only if it's newer than the last manual save
        final File savepointFile = SaveToDiskTask.getSavepointFile(instanceXml.getName());
        if (savepointFile.exists() && savepointFile.lastModified() > instanceXml.lastModified()) {
            usedSavepoint = true;
            instanceXml = savepointFile;
            Timber.w("Loading instance from savepoint file: %s", savepointFile.getAbsolutePath());
        }
        if (instanceXml.exists()) {
            // This order is important. Import data, then initialize.
            try {
                Timber.i("Importing data");
                publishProgress(Collect.getInstance().getString(R.string.survey_loading_reading_data_message));
                importData(instanceXml, fec);
                formDef.initialize(false, instanceInit);
            } catch (RuntimeException e) {
                Timber.e(e);
                // Skip a savepoint file that is corrupted or 0-sized
                if (usedSavepoint && !(e.getCause() instanceof XPathTypeMismatchException)) {
                    usedSavepoint = false;
                    instancePath = null;
                    formDef.initialize(true, instanceInit);
                } else {
                    // The saved instance is corrupted.
                    throw e;
                }
            }
        } else {
            formDef.initialize(true, instanceInit);
        }
    } else {
        formDef.initialize(true, instanceInit);
    }
    return usedSavepoint;
}
Also used : InstanceInitializationFactory(org.javarosa.core.model.instance.InstanceInitializationFactory) XPathTypeMismatchException(org.javarosa.xpath.XPathTypeMismatchException) File(java.io.File)

Example 5 with InstanceInitializationFactory

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

the class RecalculateTest method setUp.

@Before
public void setUp() {
    FormParseInit fpi = new FormParseInit();
    fpi.setFormToParse(r("calculate-now.xml").toString());
    formDef = fpi.getFormDef();
    formDef.initialize(true, new InstanceInitializationFactory());
}
Also used : InstanceInitializationFactory(org.javarosa.core.model.instance.InstanceInitializationFactory) FormParseInit(org.javarosa.core.test.FormParseInit) Before(org.junit.Before)

Aggregations

InstanceInitializationFactory (org.javarosa.core.model.instance.InstanceInitializationFactory)10 TreeElement (org.javarosa.core.model.instance.TreeElement)7 Test (org.junit.Test)7 FormInstance (org.javarosa.core.model.instance.FormInstance)6 TreeReference (org.javarosa.core.model.instance.TreeReference)6 File (java.io.File)2 FormDef (org.javarosa.core.model.FormDef)2 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 IAnswerData (org.javarosa.core.model.data.IAnswerData)1 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)1 FormParseInit (org.javarosa.core.test.FormParseInit)1 ParseResult (org.javarosa.xform.parse.FormParserHelper.ParseResult)1 XPathTypeMismatchException (org.javarosa.xpath.XPathTypeMismatchException)1 Before (org.junit.Before)1