Search in sources :

Example 81 with TreeReference

use of org.javarosa.core.model.instance.TreeReference 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 82 with TreeReference

use of org.javarosa.core.model.instance.TreeReference 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 83 with TreeReference

use of org.javarosa.core.model.instance.TreeReference 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 84 with TreeReference

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

the class FormIndexSerializationTest method testLocalAndInstanceNonNullReference.

@Test
public void testLocalAndInstanceNonNullReference() throws IOException, ClassNotFoundException {
    TreeReference treeReference = TreeReference.rootRef();
    FormIndex formIndexToSerialize = new FormIndex(1, 2, treeReference);
    byte[] serializedObject = serializeObject(formIndexToSerialize);
    FormIndex formIndexDeserialized = deserializeFormIndex(serializedObject);
    assertFormIndex(formIndexToSerialize, formIndexDeserialized);
}
Also used : TreeReference(org.javarosa.core.model.instance.TreeReference) FormIndex(org.javarosa.core.model.FormIndex) Test(org.junit.Test)

Example 85 with TreeReference

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

the class RestoreUtils method addData.

public static void addData(FormInstance dm, String xpath, Object data, int dataType) {
    if (data == null) {
        dataType = -1;
    }
    IAnswerData val;
    switch(dataType) {
        case -1:
            val = null;
            break;
        case Constants.DATATYPE_TEXT:
            val = new StringData((String) data);
            break;
        case Constants.DATATYPE_INTEGER:
            val = new IntegerData((Integer) data);
            break;
        case Constants.DATATYPE_LONG:
            val = new LongData((Long) data);
            break;
        case Constants.DATATYPE_DECIMAL:
            val = new DecimalData((Double) data);
            break;
        case Constants.DATATYPE_BOOLEAN:
            val = new StringData(((Boolean) data).booleanValue() ? "t" : "f");
            break;
        case Constants.DATATYPE_DATE:
            val = new DateData((Date) data);
            break;
        case Constants.DATATYPE_DATE_TIME:
            val = new DateTimeData((Date) data);
            break;
        case Constants.DATATYPE_TIME:
            val = new TimeData((Date) data);
            break;
        case Constants.DATATYPE_CHOICE_LIST:
            val = (SelectMultiData) data;
            break;
        default:
            throw new IllegalArgumentException("Don't know how to handle data type [" + dataType + "]");
    }
    TreeReference ref = absRef(xpath, dm);
    if (dm.addNode(ref, val, dataType) == null) {
        throw new RuntimeException("error setting value during object backup [" + xpath + "]");
    }
}
Also used : IAnswerData(org.javarosa.core.model.data.IAnswerData) IntegerData(org.javarosa.core.model.data.IntegerData) LongData(org.javarosa.core.model.data.LongData) Date(java.util.Date) DecimalData(org.javarosa.core.model.data.DecimalData) DateData(org.javarosa.core.model.data.DateData) TreeReference(org.javarosa.core.model.instance.TreeReference) DateTimeData(org.javarosa.core.model.data.DateTimeData) TimeData(org.javarosa.core.model.data.TimeData) DateTimeData(org.javarosa.core.model.data.DateTimeData) StringData(org.javarosa.core.model.data.StringData)

Aggregations

TreeReference (org.javarosa.core.model.instance.TreeReference)85 ArrayList (java.util.ArrayList)30 TreeElement (org.javarosa.core.model.instance.TreeElement)29 EvaluationContext (org.javarosa.core.model.condition.EvaluationContext)16 Constraint (org.javarosa.core.model.condition.Constraint)12 HashSet (java.util.HashSet)11 Test (org.junit.Test)9 FormInstance (org.javarosa.core.model.instance.FormInstance)8 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)7 IFormElement (org.javarosa.core.model.IFormElement)6 Condition (org.javarosa.core.model.condition.Condition)6 IAnswerData (org.javarosa.core.model.data.IAnswerData)6 InstanceInitializationFactory (org.javarosa.core.model.instance.InstanceInitializationFactory)6 GroupDef (org.javarosa.core.model.GroupDef)5 XPathReference (org.javarosa.model.xform.XPathReference)5 DataBinding (org.javarosa.core.model.DataBinding)4 IDataReference (org.javarosa.core.model.IDataReference)4 DataInstance (org.javarosa.core.model.instance.DataInstance)4 EvaluationResult (org.javarosa.debug.EvaluationResult)4 XPathException (org.javarosa.xpath.XPathException)4