aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/test/java/com/yahoo/document/annotation/Bug4164299TestCase.java
blob: 812ed2a9f8441d9998b2ea7d76614dabaa981fe9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.annotation;

import com.yahoo.document.datatypes.StringFieldValue;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import static org.junit.Assert.assertEquals;

/**
 * @author <a href="mailto:mpraveen@yahoo-inc.com">Praveen Mohan</a>
 *
 * This test checks if sub-trees are sorted appropriately by probability
 * within an alternate span list and getFrom() and getTo() are updated properly
 * when the trees are sorted.
 *
 */
public class Bug4164299TestCase {

    private SpanTree tree;

    @Before
    public void buildTree() {
        SpanList root = new SpanList();
        AlternateSpanList branch = new AlternateSpanList();
        tree = new SpanTree("test", root);

        SpanNode span1 = new Span(0, 2);
        SpanNode span2 = new Span(2, 2);
        SpanNode span3 = new Span(4, 2);

        SpanNode span11 = new Span(10, 2);
        SpanNode span22 = new Span(12, 2);
        SpanNode span33 = new Span(14, 2);

        branch.add(span3);
        branch.add(span2);
        branch.add(span1);

        List<SpanNode> subtreeList = new ArrayList<SpanNode>();
        subtreeList.add(span11);
        subtreeList.add(span22);
        subtreeList.add(span33);
        branch.addChildren(1, subtreeList, 50.0d);
        root.add(branch);

    }

    @Test
    public void assertTree() {
        final AlternateSpanList branch =  (AlternateSpanList)((SpanList)tree.getRoot()).children().get(0);
        assertEquals(0, branch.getFrom());
        assertEquals(6, branch.getTo());
        assertEquals(10, branch.getFrom(1));
        assertEquals(16, branch.getTo(1));
        branch.sortSubTreesByProbability();

        assertEquals(10, branch.getFrom());
        assertEquals(16, branch.getTo());
        assertEquals(0, branch.getFrom(1));
        assertEquals(6, branch.getTo(1));
    }

    @After
    public void removeTree() {
        tree = null;
    }

    public void consumeAnnotations(SpanTree tree, SpanList root) {
        System.out.println("\n\nSpanList: [" + root.getFrom() + ", " + root.getTo() + "] num Children: " + root.numChildren());
        System.out.println("-------------------");
        Iterator<SpanNode> childIterator = root.childIterator();
        while (childIterator.hasNext()) {
            SpanNode node = childIterator.next();
            System.out.println("\n\nSpan Node (" + node + "): [" + node.getFrom() + ", " + node.getTo() + "] ");
            if (node instanceof AlternateSpanList) {
                parseAlternateLists(tree, (AlternateSpanList)node);
                System.out.println("---- Alternate SpanList complete ---");
            } else if (node instanceof SpanList) {
                System.out.println("Encountered another span list");
                SpanList spl = (SpanList) node;
                ListIterator<SpanNode> lli = spl.childIterator();
                while (lli.hasNext()) System.out.print(" " + lli.next() + " ");
                consumeAnnotations(tree, (SpanList) node);
            } else {
                   System.out.println("\nGetting annotations for this span node: [" + node.getFrom() + ", " + node.getTo() + "] ");
                   getAnnotationsForNode(tree, node);
            }
        }
        System.out.println("\nGetting annotations for the SpanList itself : [" + root.getFrom() + ", " + root.getTo() + "] ");
        getAnnotationsForNode(tree, root);
    }

    public void parseAlternateLists(SpanTree tree, AlternateSpanList aspl) {
        int no = aspl.getNumSubTrees();
        System.out.println("Parsing Alternate span list. No of subtrees: " + no);
        int ctr = 0;
        while (ctr < no) {
            System.out.println("\nSubTree: " + ctr);
            ListIterator<SpanNode> lIter = aspl.childIteratorRecursive(ctr);
            while (lIter.hasNext()) {
                SpanNode spnNode = lIter.next();
                System.out.println("Parsing span node: [" + spnNode.getFrom() + ", " + spnNode.getTo() + "] ");
                if (spnNode instanceof AlternateSpanList) {
                    System.out.println("A child alternate span list found. Recursing");
                    parseAlternateLists(tree, (AlternateSpanList)spnNode);
                }

                getAnnotationsForNode(tree, spnNode);
            }
            ctr ++;
        }
    }

    public void getAnnotationsForNode(SpanTree tree, SpanNode node) {
        Iterator<Annotation> iter = tree.iterator(node);
        boolean annotationPresent = false;
        while (iter.hasNext()) {
            annotationPresent = true;
            Annotation xx = iter.next();
            StringFieldValue fValue = (StringFieldValue) xx.getFieldValue();
            System.out.println("Annotation: " + xx);
            if (fValue == null) {
                System.out.println("Field Value is null");
                return;
            } else {
                System.out.println("Field Value: " + fValue.getString());
            }
        }
        if (!annotationPresent) {
            System.out.println("****No annotations found for the span node: [" + node.getFrom() + ", " + node.getTo() + "] ");
        }
    }
}