Search in sources :

Example 31 with Struct

use of com.yahoo.document.datatypes.Struct in project vespa by vespa-engine.

the class PositionTypeTestCase method requireThatPositionFactoryWorks.

@Test
public void requireThatPositionFactoryWorks() {
    Struct val = PositionDataType.valueOf(6, 9);
    assertEquals(new IntegerFieldValue(6), val.getFieldValue(PositionDataType.FIELD_X));
    assertEquals(new IntegerFieldValue(9), val.getFieldValue(PositionDataType.FIELD_Y));
    assertEquals(2, val.getFieldCount());
    val = PositionDataType.fromLong((6L << 32) + 9);
    assertEquals(new IntegerFieldValue(6), val.getFieldValue(PositionDataType.FIELD_X));
    assertEquals(new IntegerFieldValue(9), val.getFieldValue(PositionDataType.FIELD_Y));
    assertEquals(2, val.getFieldCount());
}
Also used : IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) Struct(com.yahoo.document.datatypes.Struct) Test(org.junit.Test)

Example 32 with Struct

use of com.yahoo.document.datatypes.Struct in project vespa by vespa-engine.

the class PositionTypeTestCase method requireFixedPointFormat.

@Test
public void requireFixedPointFormat() {
    assertEquals("0.0", PositionDataType.fmtD(0));
    assertEquals("123.0", PositionDataType.fmtD(123));
    assertEquals("0.000123", PositionDataType.fmtD(0.000123));
    assertEquals("0.000001", PositionDataType.fmtD(0.000001));
    assertEquals("0.0", PositionDataType.fmtD(0.0000004));
    assertEquals("999.123456", PositionDataType.fmtD(999.1234564));
    Struct val1 = PositionDataType.valueOf(0, 0);
    assertEquals("N0.0;E0.0", PositionDataType.renderAsString(val1));
    Struct val2 = PositionDataType.valueOf(-1, -1);
    assertEquals("S0.000001;W0.000001", PositionDataType.renderAsString(val2));
    Struct val3 = PositionDataType.valueOf(123456789, 87654321);
    assertEquals("N87.654321;E123.456789", PositionDataType.renderAsString(val3));
    Struct val4 = PositionDataType.valueOf(Integer.MIN_VALUE, Integer.MIN_VALUE);
    assertEquals("S2147.483648;W2147.483648", PositionDataType.renderAsString(val4));
    Struct val5 = PositionDataType.valueOf(Integer.MAX_VALUE, Integer.MAX_VALUE);
    assertEquals("N2147.483647;E2147.483647", PositionDataType.renderAsString(val5));
}
Also used : Struct(com.yahoo.document.datatypes.Struct) Test(org.junit.Test)

Example 33 with Struct

use of com.yahoo.document.datatypes.Struct in project vespa by vespa-engine.

the class Bug4261985TestCase method annotate.

public void annotate(Document document, DocumentTypeManager manager) {
    AnnotationTypeRegistry registry = manager.getAnnotationTypeRegistry();
    AnnotationType company = registry.getType("company");
    AnnotationType industry = registry.getType("industry");
    AnnotationType person = registry.getType("person");
    AnnotationType location = registry.getType("location");
    AnnotationType bigshots = registry.getType("bigshots");
    if (company.inherits(industry)) {
        System.out.println("Company Inherits Industry");
    } else {
        System.out.println("FAIL: COMPANY DOES NOT INHERIT INDUSTRY");
        throw new RuntimeException("FAIL: COMPANY DOES NOT INHERIT INDUSTRY, though it does in SD file");
    }
    SpanTree tree = new SpanTree("testannotations");
    SpanList root = (SpanList) tree.getRoot();
    SpanNode span1 = new Span(0, 5);
    SpanNode span2 = new Span(5, 10);
    SpanNode span3 = new Span(10, 15);
    SpanNode span4 = new Span(15, 20);
    SpanNode span5 = new Span(6, 10);
    SpanNode span6 = new Span(8, 4);
    SpanNode span7 = new Span(4, 2);
    SpanNode span8 = new Span(12, 6);
    root.add(span1);
    root.add(span2);
    // root.add(span3);
    root.add(span4);
    root.add(span5);
    root.add(span6);
    // root.add(span7);
    root.add(span8);
    AlternateSpanList aspl = new AlternateSpanList();
    aspl.add(span7);
    List<SpanNode> subtree1 = new ArrayList<SpanNode>();
    subtree1.add(span3);
    aspl.addChildren(1, subtree1, 33.0d);
    root.add(aspl);
    Struct personValue = (Struct) person.getDataType().createFieldValue();
    personValue.setFieldValue("name", "Richard Bair");
    Annotation personAn = new Annotation(person, personValue);
    tree.annotate(span1, personAn);
    Struct companyValue = (Struct) company.getDataType().createFieldValue();
    companyValue.setFieldValue("name", "Sun");
    Struct locationVal = new Struct(manager.getDataType("annotation.location"));
    locationVal.setFieldValue("lat", 37.774929);
    locationVal.setFieldValue("lon", -122.419415);
    Annotation locAnnotation = new Annotation(location, locationVal);
    Field compLocField = ((StructDataType) company.getDataType()).getField("place");
    // FieldValue compLocFieldVal = new FieldValue(compLocField.getDataType());
    AnnotationReferenceDataType annType = (AnnotationReferenceDataType) compLocField.getDataType();
    FieldValue compLocFieldVal = null;
    // if (scenario.equals("createFieldValue")) {
    // compLocFieldVal = annType.createFieldValue(new AnnotationReference(annType, locAnnotation));
    // } else {
    compLocFieldVal = new AnnotationReference(annType, locAnnotation);
    // }
    companyValue.setFieldValue(compLocField, compLocFieldVal);
    companyValue.setFieldValue("vertical", "software");
    Struct dirValue1 = new Struct(manager.getDataType("annotation.person"));
    dirValue1.setFieldValue("name", "Jonathan Schwartz");
    Annotation dirAnnotation1 = new Annotation(person, dirValue1);
    Struct dirValue2 = new Struct(manager.getDataType("annotation.person"));
    dirValue2.setFieldValue("name", "Scott Mcnealy");
    Annotation dirAnnotation2 = new Annotation(person, dirValue2);
    Field dirField = ((StructDataType) company.getDataType()).getField("directors");
    Array<FieldValue> dirFieldVal = new Array<FieldValue>(dirField.getDataType());
    AnnotationReferenceDataType annRefType = (AnnotationReferenceDataType) ((ArrayDataType) dirField.getDataType()).getNestedType();
    dirFieldVal.add(new AnnotationReference(annRefType, dirAnnotation1));
    dirFieldVal.add(new AnnotationReference(annRefType, dirAnnotation2));
    companyValue.setFieldValue(dirField, dirFieldVal);
    Annotation compAn = new Annotation(company, companyValue);
    tree.annotate(span2, compAn);
    Struct bigshotsValue = (Struct) bigshots.getDataType().createFieldValue();
    Field ceosField = ((StructDataType) bigshots.getDataType()).getField("ceos");
    // FieldValue compLocFieldVal = new FieldValue(compLocField.getDataType());
    AnnotationReferenceDataType annType1 = (AnnotationReferenceDataType) ceosField.getDataType();
    FieldValue ceosFieldVal = new AnnotationReference(annType1, compAn);
    bigshotsValue.setFieldValue(ceosField, ceosFieldVal);
    Annotation bigshotsAn = new Annotation(bigshots, bigshotsValue);
    tree.annotate(span8, bigshotsAn);
    Field selfField = ((StructDataType) bigshots.getDataType()).getField("self");
    AnnotationReferenceDataType annType2 = (AnnotationReferenceDataType) selfField.getDataType();
    FieldValue selfFieldVal = new AnnotationReference(annType2, bigshotsAn);
    bigshotsValue.setFieldValue(selfField, selfFieldVal);
    bigshotsAn = new Annotation(bigshots, bigshotsValue);
    tree.annotate(span8, bigshotsAn);
    tree.annotate(span3, locAnnotation);
    tree.annotate(span5, dirAnnotation1);
    tree.annotate(span6, dirAnnotation2);
    Struct indValue = new Struct(manager.getDataType("annotation.industry"));
    indValue.setFieldValue("vertical", "Manufacturing");
    Annotation indAn = new Annotation(industry, indValue);
    tree.annotate(span4, indAn);
    StringFieldValue body = (StringFieldValue) document.getFieldValue(document.getDataType().getField("body"));
    body.setSpanTree(tree);
    document.setFieldValue(document.getDataType().getField("body"), body);
}
Also used : ArrayList(java.util.ArrayList) Struct(com.yahoo.document.datatypes.Struct) Array(com.yahoo.document.datatypes.Array) Field(com.yahoo.document.Field) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) StructDataType(com.yahoo.document.StructDataType) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue)

Example 34 with Struct

use of com.yahoo.document.datatypes.Struct in project vespa by vespa-engine.

the class DocTestCase method simple5.

public void simple5() {
    // the following two lines work inside process(Document, Arguments, Processing) in a DocumentProcessor
    DocumentTypeManager dtm = processing.getService().getDocumentTypeManager();
    AnnotationTypeRegistry atr = dtm.getAnnotationTypeRegistry();
    StringFieldValue text = new StringFieldValue("<body><p>I live in San </p>Francisco</body>");
    // 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
    SpanList root = new SpanList();
    SpanTree tree = new SpanTree("html", root);
    StructDataType positionType = (StructDataType) dtm.getDataType("position");
    AnnotationType textType = atr.getType("text");
    AnnotationType beginTag = atr.getType("begintag");
    AnnotationType endTag = atr.getType("endtag");
    AnnotationType bodyType = atr.getType("body");
    AnnotationType paragraphType = atr.getType("paragraph");
    AnnotationType cityType = atr.getType("city");
    Struct position = new Struct(positionType);
    position.setFieldValue("latitude", 37.774929);
    position.setFieldValue("longitude", -122.419415);
    Annotation sanAnnotation = new Annotation(textType);
    Annotation franciscoAnnotation = new Annotation(textType);
    Struct positionWithRef = (Struct) cityType.getDataType().createFieldValue();
    positionWithRef.setFieldValue("position", position);
    Field referencesField = ((StructDataType) cityType.getDataType()).getField("references");
    Array<FieldValue> refList = new Array<FieldValue>(referencesField.getDataType());
    AnnotationReferenceDataType annRefType = (AnnotationReferenceDataType) ((ArrayDataType) referencesField.getDataType()).getNestedType();
    refList.add(new AnnotationReference(annRefType, sanAnnotation));
    refList.add(new AnnotationReference(annRefType, franciscoAnnotation));
    positionWithRef.setFieldValue(referencesField, refList);
    Annotation city = new Annotation(cityType, positionWithRef);
    SpanList paragraph = new SpanList();
    {
        Span span1 = new Span(6, 3);
        Span span2 = new Span(9, 10);
        Span span3 = new Span(19, 4);
        Span span4 = new Span(23, 4);
        paragraph.add(span1).add(span2).add(span3).add(span4);
        tree.annotate(span1, beginTag).annotate(span2, textType).annotate(span3, sanAnnotation).annotate(span4, endTag).annotate(paragraph, paragraphType);
    }
    {
        Span span1 = new Span(0, 6);
        Span span2 = new Span(27, 9);
        Span span3 = new Span(36, 8);
        root.add(span1).add(paragraph).add(span2).add(span3);
        tree.annotate(span1, beginTag).annotate(span2, franciscoAnnotation).annotate(span3, endTag).annotate(root, bodyType).annotate(city);
    }
    text.setSpanTree(tree);
}
Also used : Struct(com.yahoo.document.datatypes.Struct) Array(com.yahoo.document.datatypes.Array) Field(com.yahoo.document.Field) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) StructDataType(com.yahoo.document.StructDataType) DocumentTypeManager(com.yahoo.document.DocumentTypeManager) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue)

Example 35 with Struct

use of com.yahoo.document.datatypes.Struct in project vespa by vespa-engine.

the class Bug4259784TestCase method annotate.

private void annotate(Document document, DocumentTypeManager manager) {
    AnnotationTypeRegistry registry = manager.getAnnotationTypeRegistry();
    AnnotationType company = registry.getType("company");
    AnnotationType industry = registry.getType("industry");
    AnnotationType person = registry.getType("person");
    AnnotationType location = registry.getType("location");
    SpanTree tree = new SpanTree("testannotations");
    SpanList root = (SpanList) tree.getRoot();
    SpanNode span1 = new Span(0, 5);
    SpanNode span2 = new Span(5, 10);
    SpanNode span3 = new Span(10, 15);
    SpanNode span4 = new Span(15, 20);
    SpanNode span5 = new Span(6, 10);
    SpanNode span6 = new Span(8, 4);
    SpanNode span7 = new Span(4, 2);
    root.add(span1);
    root.add(span2);
    root.add(span4);
    root.add(span5);
    root.add(span6);
    AlternateSpanList aspl = new AlternateSpanList();
    aspl.add(span7);
    List<SpanNode> subtree1 = new ArrayList<SpanNode>();
    subtree1.add(span3);
    aspl.addChildren(1, subtree1, 33.0d);
    root.add(aspl);
    Struct personValue = (Struct) person.getDataType().createFieldValue();
    personValue.setFieldValue("name", "Richard Bair");
    Annotation personAn = new Annotation(person, personValue);
    tree.annotate(span1, personAn);
    Struct companyValue = (Struct) company.getDataType().createFieldValue();
    companyValue.setFieldValue("name", "Sun");
    Annotation compAn = new Annotation(company, companyValue);
    tree.annotate(span2, compAn);
    Struct locationVal = new Struct(manager.getDataType("annotation.location"));
    locationVal.setFieldValue("lat", 37.774929);
    locationVal.setFieldValue("lon", -122.419415);
    Annotation locAnnotation = new Annotation(location, locationVal);
    tree.annotate(span3, locAnnotation);
    Struct dirValue1 = new Struct(manager.getDataType("annotation.person"));
    dirValue1.setFieldValue("name", "Jonathan Schwartz");
    Annotation dirAnnotation1 = new Annotation(person, dirValue1);
    tree.annotate(span5, dirAnnotation1);
    Struct dirValue2 = new Struct(manager.getDataType("annotation.person"));
    dirValue2.setFieldValue("name", "Scott Mcnealy");
    Annotation dirAnnotation2 = new Annotation(person, dirValue2);
    tree.annotate(span6, dirAnnotation2);
    Struct indValue = new Struct(manager.getDataType("annotation.industry"));
    indValue.setFieldValue("vertical", "Manufacturing");
    Annotation indAn = new Annotation(industry, indValue);
    tree.annotate(span4, indAn);
    Field compLocField = ((StructDataType) company.getDataType()).getField("place");
    AnnotationReferenceDataType annType = (AnnotationReferenceDataType) compLocField.getDataType();
    FieldValue compLocFieldVal = new AnnotationReference(annType, locAnnotation);
    companyValue.setFieldValue(compLocField, compLocFieldVal);
    companyValue.setFieldValue("vertical", "software");
    Field dirField = ((StructDataType) company.getDataType()).getField("directors");
    Array<FieldValue> dirFieldVal = new Array<FieldValue>(dirField.getDataType());
    AnnotationReferenceDataType annRefType = (AnnotationReferenceDataType) ((ArrayDataType) dirField.getDataType()).getNestedType();
    dirFieldVal.add(new AnnotationReference(annRefType, dirAnnotation1));
    dirFieldVal.add(new AnnotationReference(annRefType, dirAnnotation2));
    companyValue.setFieldValue(dirField, dirFieldVal);
    tree.clearAnnotations(span3);
    StringFieldValue body = (StringFieldValue) document.getFieldValue(document.getDataType().getField("body"));
    body.setSpanTree(tree);
    document.setFieldValue(document.getDataType().getField("body"), body);
}
Also used : ArrayList(java.util.ArrayList) Struct(com.yahoo.document.datatypes.Struct) Array(com.yahoo.document.datatypes.Array) Field(com.yahoo.document.Field) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) StructDataType(com.yahoo.document.StructDataType) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue)

Aggregations

Struct (com.yahoo.document.datatypes.Struct)38 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)25 Test (org.junit.Test)21 FieldValue (com.yahoo.document.datatypes.FieldValue)13 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)11 StructDataType (com.yahoo.document.StructDataType)9 Array (com.yahoo.document.datatypes.Array)9 Field (com.yahoo.document.Field)7 Document (com.yahoo.document.Document)6 MapFieldValue (com.yahoo.document.datatypes.MapFieldValue)5 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)4 DocumentType (com.yahoo.document.DocumentType)3 DoubleFieldValue (com.yahoo.document.datatypes.DoubleFieldValue)3 FloatFieldValue (com.yahoo.document.datatypes.FloatFieldValue)3 LongFieldValue (com.yahoo.document.datatypes.LongFieldValue)3 WeightedSet (com.yahoo.document.datatypes.WeightedSet)3 AddValueUpdate (com.yahoo.document.update.AddValueUpdate)3 FieldUpdate (com.yahoo.document.update.FieldUpdate)3 MapValueUpdate (com.yahoo.document.update.MapValueUpdate)3 ValueUpdate (com.yahoo.document.update.ValueUpdate)3