aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/test/java/com/yahoo/document/annotation/SystemTestCase.java
blob: fa2e6108afb613e7dc4e44c3b066d8764b96ca43 (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
141
// 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.Document;
import com.yahoo.document.DocumentType;
import com.yahoo.document.DocumentTypeManager;
import com.yahoo.document.DocumentTypeManagerConfigurer;
import com.yahoo.document.StructDataType;
import com.yahoo.document.datatypes.StringFieldValue;
import com.yahoo.document.datatypes.Struct;
import com.yahoo.document.serialization.*;
import com.yahoo.io.GrowableByteBuffer;
import org.junit.Before;
import org.junit.Test;

import java.util.Iterator;

/**
 * @author Einar M R Rosenvinge
 */
public class SystemTestCase {

    DocumentTypeManager manager;

    private void annotate(Document document) {
        AnnotationTypeRegistry registry = manager.getAnnotationTypeRegistry();
        AnnotationType personType = registry.getType("person");
        AnnotationType artistType = registry.getType("artist");
        AnnotationType dateType = registry.getType("date");
        AnnotationType placeType = registry.getType("place");
        AnnotationType eventType = registry.getType("event");

        SpanList root = new SpanList();
        SpanTree tree = new SpanTree("meaningoflife", root);

        SpanNode personSpan = new Span(0,5);
        SpanNode artistSpan = new Span(5,10);
        SpanNode dateSpan = new Span(10,15);
        SpanNode placeSpan = new Span(15,20);

        root.add(personSpan);
        root.add(artistSpan);
        root.add(dateSpan);
        root.add(placeSpan);

        DocumentType docType = document.getDataType();
        Struct personValue = new Struct(personType.getDataType());
        personValue.setFieldValue("name", "george washington");
        Annotation person = new Annotation(personType, personValue);
        tree.annotate(personSpan, person);

        Struct artistValue = new Struct((StructDataType) artistType.getDataType());
        artistValue.setFieldValue("name", "elvis presley");
        artistValue.setFieldValue("instrument", 20);
        Annotation artist = new Annotation(artistType, artistValue);
        tree.annotate(artistSpan, artist);

        Struct dateValue = new Struct((StructDataType) dateType.getDataType());
        dateValue.setFieldValue("exacttime", 123456789L);
        Annotation date = new Annotation(dateType, dateValue);
        tree.annotate(dateSpan, date);

        Struct placeValue = new Struct((StructDataType) placeType.getDataType());
        placeValue.setFieldValue("lat", 1467L);
        placeValue.setFieldValue("lon", 789L);
        Annotation place = new Annotation(placeType, placeValue);
        tree.annotate(placeSpan, place);

        var eventStruct = (StructDataType) eventType.getDataType();
        Struct eventValue = new Struct(eventStruct);
        eventValue.setFieldValue("description", "Big concert");
        eventValue.setFieldValue("person", new AnnotationReference((AnnotationReferenceDataType) eventStruct.getField("person").getDataType(), person));
        eventValue.setFieldValue("date", new AnnotationReference((AnnotationReferenceDataType) eventStruct.getField("date").getDataType(), date));
        eventValue.setFieldValue("place", new AnnotationReference((AnnotationReferenceDataType) eventStruct.getField("place").getDataType(), place));
        Annotation event = new Annotation(eventType, eventValue);
        tree.annotate(root, event);

        StringFieldValue content = new StringFieldValue("This is the story of a big concert by Elvis and a special guest appearance by George Washington");
        content.setSpanTree(tree);

        document.setFieldValue(document.getDataType().getField("content"), content);
    }

    private void consume(Document document) {
        StringFieldValue content = (StringFieldValue) document.getFieldValue(document.getDataType().getField("content"));

        SpanTree tree = content.getSpanTree("meaningoflife");
        SpanList root = (SpanList) tree.getRoot();

        Iterator<SpanNode> childIterator = root.childIterator();
        SpanNode personSpan = childIterator.next();
        SpanNode artistSpan = childIterator.next();
        SpanNode dateSpan = childIterator.next();
        SpanNode placeSpan = childIterator.next();

        Annotation person = tree.iterator(personSpan).next();
        Struct personValue = (Struct) person.getFieldValue();
        System.err.println("Person is " + personValue.getField("name"));

        Annotation artist = tree.iterator(artistSpan).next();
        Struct artistValue = (Struct) artist.getFieldValue();
        System.err.println("Artist is " + artistValue.getFieldValue("name") + " who plays the " + artistValue.getFieldValue("instrument"));

        Annotation date = tree.iterator(dateSpan).next();
        Struct dateValue = (Struct) date.getFieldValue();
        System.err.println("Date is " + dateValue.getFieldValue("exacttime"));

        Annotation place = tree.iterator(placeSpan).next();
        Struct placeValue = (Struct) place.getFieldValue();
        System.err.println("Place is " + placeValue.getFieldValue("lat") + ";" + placeValue.getFieldValue("lon"));

        Annotation event = tree.iterator(root).next();
        Struct eventValue = (Struct) event.getFieldValue();
        System.err.println("Event is " + eventValue.getFieldValue("description") + " with " + eventValue.getFieldValue("person") + " and " + eventValue.getFieldValue("date") + " and " + eventValue.getFieldValue("place"));
    }

    @Before
    public void setUp() {
        manager = new DocumentTypeManager();
        var sub = DocumentTypeManagerConfigurer.configure
            (manager, "file:src/test/java/com/yahoo/document/annotation/documentmanager.systemtest.cfg");
        sub.close();
    }

    @Test
    public void testSystemTest() {
        DocumentType type = manager.getDocumentType("article");
        Document inDocument = new Document(type, "id:ns:article::boringarticle:longarticle");
        annotate(inDocument);

        GrowableByteBuffer buffer = new GrowableByteBuffer();
        DocumentSerializer serializer = DocumentSerializerFactory.create6(buffer);
        serializer.write(inDocument);
        buffer.flip();
        DocumentDeserializer deserializer = DocumentDeserializerFactory.create6(manager, buffer);

        Document outDocument = new Document(deserializer);
        consume(outDocument);
    }

}