Search in sources :

Example 81 with TreeElement

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

the class QuestionDataGroupTests method testAddTreeChild.

public void testAddTreeChild() {
    // TreeElement subTree = new TreeElement("subtree");
    // TreeElement firstRootTree = new TreeElement("firstRoot");
    // TreeElement secondRootTree = new TreeElement("secondRoot");
    TreeElement subElement = new TreeElement("SubElement");
    subElement.addChild(stringElement);
    subElement.addChild(intElement);
// assertTrue("Group did not add valid subtree group as a child",subTree.addChild(subElement));
// assertTrue("Group does not properly contain subtree group as a child",subTree.contains(subElement));
// Looks like these are not valid anymore after the last round of changes.
}
Also used : TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement)

Example 82 with TreeElement

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

the class Safe2014DagImplTest method deleteThirdRepeatGroup_evaluatesTriggerables_indirectlyDependentOnTheRepeatGroupsNumber.

/**
 * Indirectly means that the calculation - `concat(/rgwp/houseM/name)` - does not take the
 * `/rgwp/houseM` nodeset (the repeat group) as an argument
 * but since it takes one of its children (`name` children),
 * the calculation must re-evaluated once after a repeat group deletion because one of the children
 * has been deleted along with its parent (the repeat group instance).
 */
@Test
public void deleteThirdRepeatGroup_evaluatesTriggerables_indirectlyDependentOnTheRepeatGroupsNumber() throws Exception {
    // Given
    final FormDef formDef = parse(r("calculation-indirectly-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);
    assertThat(summaryNode.getValue().getDisplayText(), equalTo("ABCDE"));
    // Safe2014DagImplTest.deleteRepeatGroup is called by the below method
    formDef.deleteRepeat(indexToBeDeleted);
    // Then
    final List<TreeElement> repeats = mainInstance.getRoot().getChildrenWithName("houseM");
    assertThat(repeats.size(), equalTo(4));
    assertThat(repeats.get(0).getChildAt(0).getValue().getDisplayText(), equalTo("A"));
    assertThat(repeats.get(1).getChildAt(0).getValue().getDisplayText(), equalTo("B"));
    assertThat(repeats.get(2).getChildAt(0).getValue().getDisplayText(), equalTo("D"));
    assertThat(repeats.get(3).getChildAt(0).getValue().getDisplayText(), equalTo("E"));
    assertThat(summaryNode.getValue().getDisplayText(), equalTo("ABDE"));
    // check that correct calculations were triggered
    final String[] expectedMessages = { "Processing 'Deleted: houseM [3]: 0 triggerables were fired.' for ", "Processing 'Recalculate' for summary [1] (ABDE)", "Processing 'Deleted: name [3_1]: 1 triggerables were fired.' for ", "Processing 'Deleted: houseM [4]: 0 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 83 with TreeElement

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

the class Safe2014DagImplTest method deleteSecondRepeatGroup_evaluatesTriggerables_dependentOnTheParentPosition.

@Test
public void deleteSecondRepeatGroup_evaluatesTriggerables_dependentOnTheParentPosition() throws Exception {
    // Given
    final FormDef formDef = parse(r("repeat-group-with-children-calculations-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("A1"));
    assertThat(repeats.get(1).getChildAt(0).getValue().getDisplayText(), equalTo("2"));
    assertThat(repeats.get(1).getChildAt(2).getValue().getDisplayText(), equalTo("C2"));
    assertThat(repeats.get(2).getChildAt(0).getValue().getDisplayText(), equalTo("3"));
    assertThat(repeats.get(2).getChildAt(2).getValue().getDisplayText(), equalTo("D3"));
    assertThat(repeats.get(3).getChildAt(0).getValue().getDisplayText(), equalTo("4"));
    assertThat(repeats.get(3).getChildAt(2).getValue().getDisplayText(), equalTo("E4"));
    // check that correct calculations were triggered
    final String[] expectedMessages = { "Processing 'Recalculate' for no [2_1] (2.0)", "Processing 'Recalculate' for name_and_no [2_1] (C2)", "Processing 'Deleted: houseM [2]: 2 triggerables were fired.' for ", "Processing 'Deleted: no [2_1]: 0 triggerables were fired.' for ", "Processing 'Deleted: name [2_1]: 0 triggerables were fired.' for ", "Processing 'Deleted: name_and_no [2_1]: 2 triggerables were fired.' for ", "Processing 'Recalculate' for no [3_1] (3.0)", "Processing 'Recalculate' for name_and_no [3_1] (D3)", "Processing 'Deleted: houseM [3]: 2 triggerables were fired.' for ", "Processing 'Recalculate' for no [4_1] (4.0)", "Processing 'Recalculate' for name_and_no [4_1] (E4)", "Processing 'Deleted: houseM [4]: 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 84 with TreeElement

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

the class Safe2014DagImplTest method deleteSecondRepeatGroup_evaluatesTriggerables_dependentOnFollowingRepeatGroupSiblings.

@Test
public void deleteSecondRepeatGroup_evaluatesTriggerables_dependentOnFollowingRepeatGroupSiblings() throws Exception {
    // Given
    final FormDef formDef = parse(r("repeat-group-with-children-position-calculation.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(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"));
    // 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 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 85 with TreeElement

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

the class RestoreUtils method subDataModel.

public static FormInstance subDataModel(TreeElement top) {
    TreeElement newTop = top.shallowCopy();
    newTop.setMult(0);
    return new FormInstance(newTop);
}
Also used : FormInstance(org.javarosa.core.model.instance.FormInstance) 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