Search in sources :

Example 11 with TreeElement

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

the class RestoreUtils method mergeDataModel.

public static void mergeDataModel(FormInstance parent, FormInstance child, TreeReference parentRef) {
    TreeElement parentNode = parent.resolveReference(parentRef);
    // ugly
    if (parentNode == null) {
        parentRef = parent.addNode(parentRef);
        parentNode = parent.resolveReference(parentRef);
    }
    TreeElement childNode = child.getRoot();
    int mult = parentNode.getChildMultiplicity(childNode.getName());
    childNode.setMult(mult);
    parentNode.addChild(childNode);
}
Also used : TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 12 with TreeElement

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

the class ExtUtil method writeAttributes.

public static void writeAttributes(DataOutputStream out, List<TreeElement> attributes) throws IOException {
    ExtUtil.writeNumeric(out, attributes.size());
    for (TreeElement e : attributes) {
        ExtUtil.write(out, e.getNamespace());
        ExtUtil.write(out, e.getName());
        ExtUtil.write(out, e.getAttributeValue());
    }
}
Also used : TreeElement(org.javarosa.core.model.instance.TreeElement)

Example 13 with TreeElement

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

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

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

the class QuestionDataElementTests method setUp.

public void setUp() throws Exception {
    super.setUp();
    stringData = new StringData("Answer Value");
    integerData = new IntegerData(4);
    stringReference = new IDataReference() {

        String reference = "stringValue";

        public Object getReference() {
            return reference;
        }

        public void setReference(Object reference) {
            this.reference = (String) reference;
        }

        /*
            public boolean referenceMatches(IDataReference reference) {
                return this.reference.equals(reference.getReference());
            }


            public IDataReference clone()  {
                IDataReference newReference = null;
                try {
                    newReference = (IDataReference)this.getClass().newInstance();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                newReference.setReference(reference);
                return newReference;
            }
            */
        public void readExternal(DataInputStream in, PrototypeFactory pf) throws IOException, DeserializationException {
        }

        public void writeExternal(DataOutputStream out) throws IOException {
        }
    };
    integerReference = new IDataReference() {

        Integer intReference = new Integer(15);

        public Object getReference() {
            return intReference;
        }

        public void setReference(Object reference) {
            this.intReference = (Integer) reference;
        }

        /*
            public boolean referenceMatches(IDataReference reference) {
                return this.intReference.equals(reference.getReference());
            }


            public IDataReference clone()  {
                IDataReference newReference = null;
                try {
                    newReference = (IDataReference)this.getClass().newInstance();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                newReference.setReference(intReference);
                return newReference;
            }
            */
        public void readExternal(DataInputStream in, PrototypeFactory pf) throws IOException, DeserializationException {
        }

        public void writeExternal(DataOutputStream out) throws IOException {
        }
    };
    intElement = new TreeElement("intElement");
    intElement.setValue(integerData);
    stringElement = new TreeElement(stringElementName);
    stringElement.setValue(stringData);
}
Also used : PrototypeFactory(org.javarosa.core.util.externalizable.PrototypeFactory) IDataReference(org.javarosa.core.model.IDataReference) DataOutputStream(java.io.DataOutputStream) IntegerData(org.javarosa.core.model.data.IntegerData) IOException(java.io.IOException) StringData(org.javarosa.core.model.data.StringData) DataInputStream(java.io.DataInputStream) DeserializationException(org.javarosa.core.util.externalizable.DeserializationException) TreeElement(org.javarosa.core.model.instance.TreeElement) AbstractTreeElement(org.javarosa.core.model.instance.AbstractTreeElement)

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