/** * Adds an Annotation. * Convenience shorthand for <code>annotate(node,new Annotation(type,value)</code> * * @param node the node to annotate * @param type the type of the Annotation to add * @param value the value of the Annotation to add * @return this, for chaining * @see com.yahoo.document.annotation.Annotation */ public SpanTree annotate(SpanNode node, AnnotationType type,FieldValue value) { return annotate(node, new Annotation(type, value)); }
private void removeAnnotationReferencesThatPointToRemovedAnnotations() { if (hasAnyFieldValues()) { Map<Annotation, Annotation> annotationsStillPresent = new IdentityHashMap<Annotation, Annotation>(); for (Annotation a : this) { annotationsStillPresent.put(a, a); } for (Annotation a : this) { if (!a.hasFieldValue()) { continue; } //do we have any references to annotations that are NOT in this global list?? removeObsoleteReferencesFromFieldValue(a.getFieldValue(), annotationsStillPresent, false); } } }
@Override void annotate(Annotation a) { if (a.getSpanNode() == null) { spanNode2Annotation.put(DummySpanNode.INSTANCE, a); } else { spanNode2Annotation.put(a.getSpanNode(), a); } }
@SuppressWarnings("unchecked") private Map<Annotation, Annotation> removeAnnotationsThatPointToInvalidSpanNodes() { Map<Annotation, Annotation> removedAnnotations = new IdentityHashMap<Annotation, Annotation>(); Iterator<Annotation> annotationIt = iterator(); while (annotationIt.hasNext()) { Annotation a = annotationIt.next(); if (a.hasSpanNode() && !a.isSpanNodeValid()) { a.setSpanNode(null); a.setFieldValue(null); removedAnnotations.put(a, a); annotationIt.remove(); } } return removedAnnotations; }
public void write(Annotation annotation) { buf.putInt(annotation.getType().getId()); //name hash if (annotation.isSpanNodeValid()) { features |= ((byte) 1); if (annotation.hasFieldValue()) { features |= ((byte) 2); if (annotation.isSpanNodeValid()) { int spanNodeId = annotation.getSpanNode().getScratchId(); if (spanNodeId >= 0) { buf.putInt1_2_4Bytes(spanNodeId); if (annotation.hasFieldValue()) { buf.putInt(annotation.getType().getDataType().getId()); annotation.getFieldValue().serialize(this);
newAnnotations.add(new Annotation(otherAnnotationToCopy)); if (!originalAnnotation.isSpanNodeValid()) { //returns false also if spanNode is null! continue; Integer indexOfOriginalSpanNode = originalSpanNodes.get(originalAnnotation.getSpanNode()); if (indexOfOriginalSpanNode == null) { throw new IllegalStateException("Could not clone tree, SpanNode of " + originalAnnotation + " not found."); newAnnotations.get(i).setSpanNode(copySpanNodes.get(indexOfOriginalSpanNode)); if (!a.hasFieldValue()) { continue; setCorrectAnnotationReference(a.getFieldValue(), originalAnnotations, newAnnotations);
Map<Integer, List<String>> map = new HashMap<>(); for (Annotation anno : tree) { SpanNode span = anno.getSpanNode(); if (span == null) { continue; if (anno.getType() != AnnotationTypes.TERM) { continue; FieldValue val = anno.getFieldValue(); String str; if (val instanceof StringFieldValue) {
while (annotationIt.hasNext()) { Annotation a = annotationIt.next(); if (!a.hasFieldValue()) { continue; FieldValue value = a.getFieldValue(); a.setFieldValue(null); if (!a.isSpanNodeValid()) { if (!a.hasFieldValue()) { continue; removeObsoleteReferencesFromFieldValue(a.getFieldValue(), removedAnnotations, true);
annotation.setType(type); try { SpanNode node = spanNodes.get(spanNodeId); annotation.setSpanNode(node); } catch (IndexOutOfBoundsException ioobe) { throw new DeserializationException("Could not deserialize annotation, associated span node not found ", ioobe); FieldValue value = type.getDataType().createFieldValue(); value.deserialize(this); annotation.setFieldValue(value);
public boolean isValueCompatible(Annotation structValue) { if (structValue.getType().inherits(this)) { //the value is of this type; or the supertype of the value is of this type, etc.... return true; } return false; }
public void write(FieldBase field, AnnotationReference value) { int annotationId = value.getReference().getScratchId(); if (annotationId >= 0) { buf.putInt1_2_4Bytes(annotationId); } else { throw new SerializationException("Could not serialize AnnotationReference value, reference not found (" + value + ")"); } }
/** * Constructs a new annotation of the specified type, and having the specified value. * * @param type the type of the new annotation * @param value the value of the new annotation * @throws UnsupportedOperationException if the annotation type does not allow this annotation to have values. */ public Annotation(AnnotationType type, FieldValue value) { this(type); setFieldValue(value); }
private boolean hasAnyFieldValues() { for (Annotation a : this) { if (a.hasFieldValue()) { return true; } } return false; } private void removeAnnotationReferencesThatPointToRemovedAnnotations() {
@Override public boolean hasNext() { if (next != null) { return true; } while (annotationIt.hasNext()) { Annotation tmp = annotationIt.next(); if (nodes.containsKey(tmp.getSpanNodeFast())) { next = tmp; return true; } } next = null; return false; }
public void write(Annotation annotation) { buf.putInt(annotation.getType().getId()); //name hash if (annotation.isSpanNodeValid()) { features |= ((byte) 1); if (annotation.hasFieldValue()) { features |= ((byte) 2); if (annotation.isSpanNodeValid()) { int spanNodeId = annotation.getSpanNode().getScratchId(); if (spanNodeId >= 0) { buf.putInt1_2_4Bytes(spanNodeId); if (annotation.hasFieldValue()) { buf.putInt(annotation.getType().getDataType().getId()); annotation.getFieldValue().serialize(this);
annotation.setType(type); try { SpanNode node = spanNodes.get(spanNodeId); annotation.setSpanNode(node); } catch (IndexOutOfBoundsException ioobe) { throw new DeserializationException("Could not deserialize annotation, associated span node not found ", ioobe); FieldValue value = type.getDataType().createFieldValue(); value.deserialize(this); annotation.setFieldValue(value);
@Override void annotate(Annotation annotation) { annotationType2Annotation.put(annotation.getType(), annotation); }
public void write(FieldBase field, AnnotationReference value) { int annotationId = value.getReference().getScratchId(); if (annotationId >= 0) { buf.putInt1_2_4Bytes(annotationId); } else { throw new SerializationException("Could not serialize AnnotationReference value, reference not found (" + value + ")"); } }
/** * Creates an Annotation based on the given AnnotationType, and adds it to the internal list of * annotations for this SpanTree (convenience method). Use this when * adding an Annotation (that does not have a FieldValue) that shall annotate a SpanNode. * Upon return, Annotation.getSpanNode() * returns the given node. * * @param node the node to annotate * @param type the AnnotationType to create an Annotation from * @return this, for chaining * @see com.yahoo.document.annotation.Annotation * @see com.yahoo.document.annotation.AnnotationType */ public SpanTree annotate(SpanNode node, AnnotationType type) { Annotation a = new Annotation(type); return annotate(node, a); }
/** * Set an {@link Annotation} that this AnnotationReference shall refer to. * * @param reference an Annotation that this AnnotationReference shall refer to. * @throws IllegalArgumentException if the given annotation has a type that is not compatible with this reference */ public void setReference(Annotation reference) { if (reference == null) { this.reference = null; return; } AnnotationReferenceDataType type = getDataType(); if (type.getAnnotationType().isValueCompatible(reference) // The case if concrete annotation type || reference.getType() instanceof AnnotationType) { this.reference = reference; } else { throw new IllegalArgumentException("Cannot set reference, must be of type " + type + " (was of type " + reference.getType() + ")"); } }