use of com.yahoo.document.datatypes.Array in project vespa by vespa-engine.
the class SerializeAnnotationsTestCase method testSerializeAdvancedTree.
public void testSerializeAdvancedTree() throws IOException {
SpanList root = new SpanList();
SpanTree tree = new SpanTree("html", root);
DataType positionType = docMan.getDataType("myposition");
StructDataType cityDataType = (StructDataType) docMan.getDataType("annotation.city");
AnnotationTypeRegistry registry = docMan.getAnnotationTypeRegistry();
AnnotationType textType = registry.getType("text");
AnnotationType beginTag = registry.getType("begintag");
AnnotationType endTag = registry.getType("endtag");
AnnotationType bodyType = registry.getType("body");
AnnotationType paragraphType = registry.getType("paragraph");
AnnotationType cityType = registry.getType("city");
AnnotationReferenceDataType annRefType = (AnnotationReferenceDataType) docMan.getDataType("annotationreference<text>");
Struct position = new Struct(positionType);
position.setFieldValue("latitude", new DoubleFieldValue(37.774929));
position.setFieldValue("longitude", new DoubleFieldValue(-122.419415));
Annotation sanAnnotation = new Annotation(textType);
Annotation franciscoAnnotation = new Annotation(textType);
Struct positionWithRef = cityDataType.createFieldValue();
positionWithRef.setFieldValue("position", position);
Field referencesField = cityDataType.getField("references");
Array<FieldValue> refList = new Array<FieldValue>(referencesField.getDataType());
refList.add(new AnnotationReference(annRefType, sanAnnotation));
refList.add(new AnnotationReference(annRefType, franciscoAnnotation));
positionWithRef.setFieldValue(referencesField, refList);
Annotation city = new Annotation(cityType, positionWithRef);
AlternateSpanList paragraph = new AlternateSpanList();
paragraph.addChildren(new ArrayList<SpanNode>(), 0);
paragraph.setProbability(0, 0.9);
paragraph.setProbability(1, 0.1);
{
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(0, span1).add(0, span2).add(0, span3).add(0, span4);
Span alt_span1 = new Span(6, 13);
Span alt_span2 = new Span(19, 8);
paragraph.add(1, alt_span1).add(1, alt_span2);
tree.annotate(span1, beginTag).annotate(span2, textType).annotate(span3, sanAnnotation).annotate(span4, endTag).annotate(alt_span1, textType).annotate(alt_span2, bodyType).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);
}
StringFieldValue value = new StringFieldValue("lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj " + "lkj lkj lkj lkj lkj lkj lkj lkj lkj lkj l jlkj lkj lkj " + "lkjoijoij oij oij oij oij oij oijoijoij oij oij oij oij oij " + "oijoijoijoijoijoijoijoijoijoijoijoijoij oij oij oij oij " + "oijaosdifjoai fdoais jdoasi jai os oafoai ai dfojsfoa dfoi dsf" + "aosifjasofija sodfij oasdifj aosdiosifjsi ooai oais osi");
value.setSpanTree(tree);
// important! call readFile() before writeFile()!
ByteBuffer serializedFromFile = readFile("test_data_serialized_advanced");
ByteBuffer serialized = writeFile(value, "test_data_serialized_advanced");
assertEquals(serialized.limit(), serializedFromFile.limit());
StringFieldValue valueFromFile = new StringFieldValue();
DocumentDeserializer deserializer = DocumentDeserializerFactory.create42(docMan, new GrowableByteBuffer(serializedFromFile));
deserializer.read(null, valueFromFile);
assertEquals(value, valueFromFile);
}
use of com.yahoo.document.datatypes.Array in project vespa by vespa-engine.
the class UriParserTestCase method requireThatUriFieldsCanBeParsed.
@Test
public void requireThatUriFieldsCanBeParsed() throws Exception {
DocumentTypeManager mgr = new DocumentTypeManager();
DocumentType docType = new DocumentType("my_doc");
docType.addField("my_uri", DataType.URI);
docType.addField("my_arr", DataType.getArray(DataType.URI));
mgr.registerDocumentType(docType);
VespaXMLFeedReader parser = new VespaXMLFeedReader("src/test/vespaxmlparser/test_uri.xml", mgr);
Iterator<VespaXMLFeedReader.Operation> it = parser.readAll().iterator();
Document doc = nextDocument(it);
assertNotNull(doc);
assertEquals(new StringFieldValue("scheme://host"), doc.getFieldValue("my_uri"));
assertNull(doc.getFieldValue("my_arr"));
assertNotNull(doc = nextDocument(it));
assertNull(doc.getFieldValue("my_uri"));
FieldValue val = doc.getFieldValue("my_arr");
assertNotNull(val);
assertTrue(val instanceof Array);
Array arr = (Array) val;
assertEquals(1, arr.size());
assertEquals(new StringFieldValue("scheme://host"), arr.get(0));
DocumentUpdate upd = nextUpdate(it);
assertNotNull(upd);
assertEquals(1, upd.getFieldUpdates().size());
FieldUpdate fieldUpd = upd.getFieldUpdate(0);
assertNotNull(fieldUpd);
assertEquals(docType.getField("my_arr"), fieldUpd.getField());
assertEquals(1, fieldUpd.getValueUpdates().size());
ValueUpdate valueUpd = fieldUpd.getValueUpdate(0);
assertNotNull(valueUpd);
assertTrue(valueUpd instanceof AddValueUpdate);
assertEquals(new StringFieldValue("scheme://host"), valueUpd.getValue());
assertFalse(it.hasNext());
}
use of com.yahoo.document.datatypes.Array in project vespa by vespa-engine.
the class ForEachExpression method doExecute.
@Override
protected void doExecute(final ExecutionContext ctx) {
FieldValue input = ctx.getValue();
if (input instanceof Array || input instanceof WeightedSet) {
FieldValue next = new MyConverter(ctx, exp).convert(input);
if (next == null) {
VerificationContext vctx = new VerificationContext(ctx);
vctx.setValue(input.getDataType()).execute(this);
next = vctx.getValue().createFieldValue();
}
ctx.setValue(next);
} else if (input instanceof Struct) {
ctx.setValue(new MyConverter(ctx, exp).convert(input));
} else {
throw new IllegalArgumentException("Expected Array, Struct or WeightedSet input, got " + input.getDataType().getName() + ".");
}
}
use of com.yahoo.document.datatypes.Array in project vespa by vespa-engine.
the class SplitTestCase method requireThatEmptyInputProducesEmptyArray.
@Test
public void requireThatEmptyInputProducesEmptyArray() {
ExecutionContext ctx = new ExecutionContext(new SimpleTestAdapter());
ctx.setValue(new StringFieldValue(""));
new SplitExpression(";").execute(ctx);
FieldValue val = ctx.getValue();
assertTrue(val.getDataType().equals(DataType.getArray(DataType.STRING)));
assertTrue(val instanceof Array);
assertEquals(0, ((Array) val).size());
}
use of com.yahoo.document.datatypes.Array in project vespa by vespa-engine.
the class ToArrayTestCase method requireThatValueIsConverted.
@Test
public void requireThatValueIsConverted() {
ExecutionContext ctx = new ExecutionContext(new SimpleTestAdapter());
ctx.setValue(new StringFieldValue("69")).execute(new ToArrayExpression());
FieldValue val = ctx.getValue();
assertEquals(Array.class, val.getClass());
Array arr = (Array) val;
ArrayDataType type = arr.getDataType();
assertEquals(DataType.STRING, type.getNestedType());
assertEquals(1, arr.size());
assertEquals(new StringFieldValue("69"), arr.get(0));
}
Aggregations