gate.annotation
Class AnnotationSetImpl

java.lang.Object
  extended by java.util.AbstractCollection<E>
      extended by java.util.AbstractSet<Annotation>
          extended by gate.annotation.AnnotationSetImpl
All Implemented Interfaces:
AnnotationSet, SimpleAnnotationSet, Serializable, Cloneable, Iterable<Annotation>, Collection<Annotation>, Set<Annotation>
Direct Known Subclasses:
DatabaseAnnotationSetImpl, ImmutableAnnotationSetImpl

public class AnnotationSetImpl
extends AbstractSet<Annotation>
implements AnnotationSet

Implementation of AnnotationSet. Has a number of indices, all bar one of which are null by default and are only constructed when asked for. Has lots of get methods with various selection criteria; these return views into the set, which are nonetheless valid sets in their own right (but will not necesarily be fully indexed). Has a name, which is null by default; clients of Document can request named AnnotationSets if they so desire. Has a reference to the Document it is attached to. Contrary to Collections convention, there is no no-arg constructor, as this would leave the set in an inconsistent state.

There are four indices: annotation by id, annotations by type, annotations by start node and nodes by offset. The last two jointly provide positional indexing; construction of these is triggered by indexByStart(), or by calling a get method that selects on offset. The type index is triggered by indexByType(), or calling a get method that selects on type. The id index is always present.

See Also:
Serialized Form

Nested Class Summary
(package private)  class AnnotationSetImpl.AnnotationSetIterator
          This inner class serves as the return value from the iterator() method.
 
Field Summary
protected static AnnotationFactory annFactory
           
private  Annotation[] annotations
          This field is used temporarily during serialisation to store all the annotations that need to be saved.
protected  Vector<AnnotationSetListener> annotationSetListeners
           
protected  HashMap<Integer,Annotation> annotsById
          Maps annotation ids (Integers) to Annotations
(package private)  Map<Integer,Object> annotsByStartNode
          Maps node ids (Integers) to Annotations or a Collection of Annotations that start from that node
(package private)  Map<String,AnnotationSet> annotsByType
          Maps annotation types (Strings) to AnnotationSets
(package private)  DocumentImpl doc
          The document this set belongs to
static AnnotationSet emptyAnnotationSet
           
private  Vector<GateListener> gateListeners
           
private static org.apache.log4j.Logger log
           
protected  Long longestAnnot
          A caching value that greatly improves the performance of get methods that have a defined beginning and end.
(package private)  String name
          The name of this set
(package private)  RBTreeMap nodesByOffset
          Maps offsets (Longs) to nodes
(package private) static long serialVersionUID
          Freeze the serialization UID.
 
Constructor Summary
AnnotationSetImpl(AnnotationSet c)
          Construction from an existing AnnotationSet
AnnotationSetImpl(Document doc)
          Construction from Document.
AnnotationSetImpl(Document doc, String name)
          Construction from Document and name.
 
Method Summary
 boolean add(Annotation a)
          Add an existing annotation.
 void add(Integer id, Long start, Long end, String type, FeatureMap features)
          Create and add an annotation from database read data In this case the id is already known being previously fetched from the database
 Integer add(Long start, Long end, String type, FeatureMap features)
          Create and add an annotation and return its id
 Integer add(Node start, Node end, String type, FeatureMap features)
          Create and add an annotation with pre-existing nodes, and return its id.
 boolean addAll(Collection<? extends Annotation> c)
          Adds multiple annotations to this set in one go.
protected  boolean addAllKeepIDs(Collection<? extends Annotation> c)
          Adds multiple annotations to this set in one go.
 void addAnnotationSetListener(AnnotationSetListener l)
           
 void addGateListener(GateListener l)
           
(package private)  void addToStartOffsetIndex(Annotation a)
          Add an annotation to the start offset index.
(package private)  void addToTypeIndex(Annotation a)
          Add an annotation to the type index.
 Object clone()
           
 void edit(Long start, Long end, DocumentContent replacement)
          Propagate changes to the document content.
protected  void fireAnnotationAdded(AnnotationSetEvent e)
           
protected  void fireAnnotationRemoved(AnnotationSetEvent e)
           
protected  void fireGateEvent(GateEvent e)
           
 Node firstNode()
          Get the node with the smallest offset
 AnnotationSet get()
          Get all annotations.
 Annotation get(Integer id)
          Find annotations by id
 AnnotationSet get(Long offset)
          Select annotations by offset.
 AnnotationSet get(Long startOffset, Long endOffset)
          Select annotations by offset.
 AnnotationSet get(Set<String> types)
          Select annotations by a set of types.
 AnnotationSet get(String type)
          Select annotations by type
 AnnotationSet get(String type, FeatureMap constraints)
          Select annotations by type and features This will return an annotation set containing just those annotations of a particular type (i.e. with a particular name) and which have features with specific names and values.
 AnnotationSet get(String type, FeatureMap constraints, Long offset)
          Select annotations by type, features and offset
 AnnotationSet get(String neededType, Long startOffset, Long endOffset)
          Select annotations by offset.
 AnnotationSet get(String type, Set featureNames)
          Select annotations by type and feature names
 Set<String> getAllTypes()
          Get a set of java.lang.String objects representing all the annotation types present in this annotation set.
private  Collection<Annotation> getAnnotsByStartNode(Integer id)
          Returns a set of annotations starting at that position This intermediate method is used to simplify the code as the values of the annotsByStartNode hashmap can be Annotations or a Collection of Annotations.
 AnnotationSet getContained(Long startOffset, Long endOffset)
          Select annotations contained within an interval, i.e.
 AnnotationSet getCovering(String neededType, Long startOffset, Long endOffset)
          Select annotations of the given type that completely span the range.
 Document getDocument()
          Get the document this set is attached to.
 String getName()
          Get the name of this set.
private  Node[] getNodes(Long start, Long end)
          Returns the nodes corresponding to the Longs.
 AnnotationSet getStrict(Long startOffset, Long endOffset)
          Select annotations by offset.
protected  void indexByStartOffset()
          Construct the positional indices for annotation start
protected  void indexByType()
          Construct the positional index.
 Iterator<Annotation> iterator()
          Get an iterator for this set
 Node lastNode()
          Get the node with the largest offset
 Node nextNode(Node node)
          Get the first node that is relevant for this annotation set and which has the offset larger than the one of the node provided.
private  void readObject(ObjectInputStream in)
           
 boolean remove(Object o)
          Remove an element from this set.
 void removeAnnotationSetListener(AnnotationSetListener l)
           
protected  boolean removeFromIdIndex(Annotation a)
          Remove from the ID index.
protected  void removeFromOffsetIndex(Annotation a)
          Remove from the offset indices.
protected  void removeFromTypeIndex(Annotation a)
          Remove from the type index.
 void removeGateListener(GateListener l)
           
static void setAnnotationFactory(AnnotationFactory newFactory)
          Set the annotation factory used to create annotation objects.
 int size()
          The size of this set
private  void writeObject(ObjectOutputStream out)
           
 
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
 
Methods inherited from class java.util.AbstractCollection
clear, contains, containsAll, isEmpty, retainAll, toArray, toArray, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Set
clear, contains, containsAll, equals, hashCode, isEmpty, removeAll, retainAll, toArray, toArray
 

Field Detail

log

private static final org.apache.log4j.Logger log

serialVersionUID

static final long serialVersionUID
Freeze the serialization UID.

See Also:
Constant Field Values

name

String name
The name of this set


doc

DocumentImpl doc
The document this set belongs to


annotsById

protected transient HashMap<Integer,Annotation> annotsById
Maps annotation ids (Integers) to Annotations


nodesByOffset

transient RBTreeMap nodesByOffset
Maps offsets (Longs) to nodes


annotations

private Annotation[] annotations
This field is used temporarily during serialisation to store all the annotations that need to be saved. At all other times, this will be null;


annotsByType

transient Map<String,AnnotationSet> annotsByType
Maps annotation types (Strings) to AnnotationSets


annotsByStartNode

transient Map<Integer,Object> annotsByStartNode
Maps node ids (Integers) to Annotations or a Collection of Annotations that start from that node


annotationSetListeners

protected transient Vector<AnnotationSetListener> annotationSetListeners

gateListeners

private transient Vector<GateListener> gateListeners

longestAnnot

protected transient Long longestAnnot
A caching value that greatly improves the performance of get methods that have a defined beginning and end. By tracking the maximum length that an annotation can be, we know the maximum amount of nodes outside of a specified range that must be checked to see if an annotation starting at one of those nodes crosses into the range. This mechanism is not perfect because we do not check if we have to decrease it if an annotation is removed from the set. However, usually annotations are removed because they are about to be replaced with another one that is >= to the length of the one being replaced, so this isn't a big deal. At worst, it means that the get methods simply checks a few more start positions than it needs to.


emptyAnnotationSet

public static final AnnotationSet emptyAnnotationSet

annFactory

protected static AnnotationFactory annFactory
Constructor Detail

AnnotationSetImpl

public AnnotationSetImpl(Document doc)
Construction from Document.


AnnotationSetImpl

public AnnotationSetImpl(Document doc,
                         String name)
Construction from Document and name.


AnnotationSetImpl

public AnnotationSetImpl(AnnotationSet c)
                  throws ClassCastException
Construction from an existing AnnotationSet

Throws:
ClassCastException
Method Detail

iterator

public Iterator<Annotation> iterator()
Get an iterator for this set

Specified by:
iterator in interface SimpleAnnotationSet
Specified by:
iterator in interface Iterable<Annotation>
Specified by:
iterator in interface Collection<Annotation>
Specified by:
iterator in interface Set<Annotation>
Specified by:
iterator in class AbstractCollection<Annotation>

remove

public boolean remove(Object o)
               throws ClassCastException
Remove an element from this set.

Specified by:
remove in interface SimpleAnnotationSet
Specified by:
remove in interface Collection<Annotation>
Specified by:
remove in interface Set<Annotation>
Overrides:
remove in class AbstractCollection<Annotation>
Parameters:
o - the element to remove
Returns:
true if the set was modified by this operation, false otherwise.
Throws:
ClassCastException

removeFromIdIndex

protected boolean removeFromIdIndex(Annotation a)
Remove from the ID index.


removeFromTypeIndex

protected void removeFromTypeIndex(Annotation a)
Remove from the type index.


removeFromOffsetIndex

protected void removeFromOffsetIndex(Annotation a)
Remove from the offset indices.


size

public int size()
The size of this set

Specified by:
size in interface SimpleAnnotationSet
Specified by:
size in interface Collection<Annotation>
Specified by:
size in interface Set<Annotation>
Specified by:
size in class AbstractCollection<Annotation>

get

public Annotation get(Integer id)
Find annotations by id

Specified by:
get in interface SimpleAnnotationSet
Parameters:
id - the ID to search for
Returns:
the annotation from this set with this ID, or null if there is no annotation with this ID in this set.

get

public AnnotationSet get()
Get all annotations.

Specified by:
get in interface SimpleAnnotationSet
Returns:
an ImmutableAnnotationSet, empty or not

get

public AnnotationSet get(String type)
Select annotations by type

Specified by:
get in interface SimpleAnnotationSet
Parameters:
type - the annotation type to search for.
Returns:
an ImmutableAnnotationSet

get

public AnnotationSet get(Set<String> types)
                  throws ClassCastException
Select annotations by a set of types. Expects a Set of String.

Specified by:
get in interface SimpleAnnotationSet
Parameters:
types - the set of annotation types to search for.
Returns:
an ImmutableAnnotationSet
Throws:
ClassCastException

get

public AnnotationSet get(String type,
                         FeatureMap constraints)
Select annotations by type and features This will return an annotation set containing just those annotations of a particular type (i.e. with a particular name) and which have features with specific names and values. (It will also return annotations that have features besides those specified, but it will not return any annotations that do not have all the specified feature-value pairs.) However, if constraints contains a feature whose value is equal to gate.creole.ANNIEConstants.LOOKUP_CLASS_FEATURE_NAME (which is normally "class"), then GATE will attempt to match that feature using an ontology which it will try to retreive from a feature on the both the annotation and in constraints. If these do not return identical ontologies, or if either the annotation or constraints does not contain an ontology, then matching will fail, and the annotation will not be added. In summary, this method will not work normally for features with the name "class".

Specified by:
get in interface AnnotationSet
Parameters:
type - The name of the annotations to return.
constraints - A feature map containing all of the feature value pairs that the annotation must have in order for them to be returned.
Returns:
An annotation set containing only those annotations with the given name and which have the specified set of feature-value pairs.

get

public AnnotationSet get(String type,
                         Set featureNames)
Select annotations by type and feature names

Specified by:
get in interface AnnotationSet
Parameters:
type - the annotation type to return. If null then all annotation types are searched.
featureNames - the feature names which an annotation must have to be matched.
Returns:
An annotation set containing only those annotations with the given type and at least the given features. If no annotations match these constraints, an empty set is returned. The returned set is immutable.

get

public AnnotationSet get(Long offset)
Select annotations by offset. This returns the set of annotations whose start node is the least such that it is less than or equal to offset. If a positional index doesn't exist it is created. If there are no nodes at or beyond the offset param then it will return an empty annotationset.

Specified by:
get in interface AnnotationSet
Parameters:
offset - the offset at which to start the search.
Returns:
a set of annotations, all of which start at the same offset >= offset. The returned set is immutable.

get

public AnnotationSet get(Long startOffset,
                         Long endOffset)
Select annotations by offset. This returns the set of annotations that overlap totaly or partially with the interval defined by the two provided offsets.The result will include all the annotations that either:

getStrict

public AnnotationSet getStrict(Long startOffset,
                               Long endOffset)
Select annotations by offset. This returns the set of annotations that overlap strictly with the interval defined by the two provided offsets.The result will include all the annotations that start at the start offset and end strictly at the end offset


get

public AnnotationSet get(String neededType,
                         Long startOffset,
                         Long endOffset)
Select annotations by offset. This returns the set of annotations of the given type that overlap totaly or partially with the interval defined by the two provided offsets.The result will include all the annotations that either:

getCovering

public AnnotationSet getCovering(String neededType,
                                 Long startOffset,
                                 Long endOffset)
Select annotations of the given type that completely span the range. Formally, for any annotation a, a will be included in the return set if:

get

public AnnotationSet get(String type,
                         FeatureMap constraints,
                         Long offset)
Select annotations by type, features and offset

Specified by:
get in interface AnnotationSet
Parameters:
type - the annotation type to search for
constraints - the set of features an annotation must have to be matched
offset - the offset at which to anchor the search.
Returns:
An annotation set containing those annotations that match the constraints, or an empty set if there are no such annotations. The returned set is immutable.

getContained

public AnnotationSet getContained(Long startOffset,
                                  Long endOffset)
Select annotations contained within an interval, i.e. those annotations whose start position is >= startOffset and < endOffset and whose end position is <= endOffset.

Specified by:
getContained in interface AnnotationSet
Parameters:
startOffset - the start of the interval, inclusive
endOffset - the end of the interval, inclusive
Returns:
the set of annotations from this set contained completely inside the interval, or an empty set if no such annotations exist. The returned set is immutable.

firstNode

public Node firstNode()
Get the node with the smallest offset

Specified by:
firstNode in interface AnnotationSet

lastNode

public Node lastNode()
Get the node with the largest offset

Specified by:
lastNode in interface AnnotationSet

nextNode

public Node nextNode(Node node)
Get the first node that is relevant for this annotation set and which has the offset larger than the one of the node provided.

Specified by:
nextNode in interface AnnotationSet

setAnnotationFactory

public static void setAnnotationFactory(AnnotationFactory newFactory)
Set the annotation factory used to create annotation objects. The default factory is DefaultAnnotationFactory.


add

public Integer add(Node start,
                   Node end,
                   String type,
                   FeatureMap features)
Create and add an annotation with pre-existing nodes, and return its id. Note that only Nodes retrieved from the same annotation set should be used to create a new annotation using this method. Using Nodes from other annotation sets may lead to undefined behaviour. If in any doubt use the Long based add method instead of this one.

Specified by:
add in interface SimpleAnnotationSet
Parameters:
start - the start node for the new annotation
end - the end node for the new annotation
type - the annotation type
features - the features for the new annotation
Returns:
the newly generated annotation ID, which will be distinct from all other annotations in this set.

add

public boolean add(Annotation a)
            throws ClassCastException
Add an existing annotation. Returns true when the set is modified.

Specified by:
add in interface SimpleAnnotationSet
Specified by:
add in interface Collection<Annotation>
Specified by:
add in interface Set<Annotation>
Overrides:
add in class AbstractCollection<Annotation>
Parameters:
a - the annotation to add
Returns:
true if the set was modified by this operation, false otherwise.
Throws:
ClassCastException

addAll

public boolean addAll(Collection<? extends Annotation> c)
Adds multiple annotations to this set in one go. All the objects in the provided collection should be of Annotation type, otherwise a ClassCastException will be thrown. The provided annotations will be used to create new annotations using the appropriate add() methods from this set. The new annotations will have different IDs from the old ones (which is required in order to preserve the uniqueness of IDs inside an annotation set).

Specified by:
addAll in interface Collection<Annotation>
Specified by:
addAll in interface Set<Annotation>
Overrides:
addAll in class AbstractCollection<Annotation>
Parameters:
c - a collection of annotations
Returns:
true if the set has been modified as a result of this call.

addAllKeepIDs

protected boolean addAllKeepIDs(Collection<? extends Annotation> c)
Adds multiple annotations to this set in one go. All the objects in the provided collection should be of Annotation type, otherwise a ClassCastException will be thrown. This method does not create copies of the annotations like addAll() does but simply adds the new annotations to the set. It is intended to be used solely by annotation sets in order to construct the results for various get(...) methods.

Parameters:
c - a collection of annotations
Returns:
true if the set has been modified as a result of this call.

getNodes

private final Node[] getNodes(Long start,
                              Long end)
                       throws InvalidOffsetException
Returns the nodes corresponding to the Longs. The Nodes are created if they don't exist.

Throws:
InvalidOffsetException

add

public Integer add(Long start,
                   Long end,
                   String type,
                   FeatureMap features)
            throws InvalidOffsetException
Create and add an annotation and return its id

Specified by:
add in interface SimpleAnnotationSet
Parameters:
start - the start offset for the new annotation
end - the end offset for the new annotation
type - the annotation type
features - the features for the new annotation
Returns:
the newly generated annotation ID, which will be distinct from all other annotations in this set.
Throws:
InvalidOffsetException - if the start or end offsets are null, or if the start offset is less than 0 or the end offset is greater than the length of the document.

add

public void add(Integer id,
                Long start,
                Long end,
                String type,
                FeatureMap features)
         throws InvalidOffsetException
Create and add an annotation from database read data In this case the id is already known being previously fetched from the database

Specified by:
add in interface AnnotationSet
Parameters:
id - the ID for the new annotation
start - the start offset for the new annotation
end - the end offset for the new annotation
type - the annotation type
features - the features for the new annotation
Throws:
InvalidOffsetException - if the start or end offsets are null, or if the start offset is less than 0 or the end offset is greater than the length of the document.

indexByType

protected void indexByType()
Construct the positional index.


indexByStartOffset

protected void indexByStartOffset()
Construct the positional indices for annotation start


addToTypeIndex

void addToTypeIndex(Annotation a)
Add an annotation to the type index. Does nothing if the index doesn't exist.


addToStartOffsetIndex

void addToStartOffsetIndex(Annotation a)
Add an annotation to the start offset index. Does nothing if the index doesn't exist.


edit

public void edit(Long start,
                 Long end,
                 DocumentContent replacement)
Propagate changes to the document content. Has, unfortunately, to be public, to allow DocumentImpls to get at it. Oh for a "friend" declaration. Doesn't throw InvalidOffsetException as DocumentImpl is the only client, and that checks the offsets before calling this method.


getName

public String getName()
Get the name of this set.

Specified by:
getName in interface SimpleAnnotationSet
Returns:
the name of this annotation set, or null if this set does not have a name (i.e. it is the default annotation set for a document, or it is a subset view of another annotation set).

getDocument

public Document getDocument()
Get the document this set is attached to.

Specified by:
getDocument in interface SimpleAnnotationSet

getAllTypes

public Set<String> getAllTypes()
Get a set of java.lang.String objects representing all the annotation types present in this annotation set.

Specified by:
getAllTypes in interface SimpleAnnotationSet

getAnnotsByStartNode

private final Collection<Annotation> getAnnotsByStartNode(Integer id)
Returns a set of annotations starting at that position This intermediate method is used to simplify the code as the values of the annotsByStartNode hashmap can be Annotations or a Collection of Annotations. Returns null if there are no Annotations at that position


clone

public Object clone()
             throws CloneNotSupportedException
Overrides:
clone in class Object
Returns:
a clone of this set.
Throws:
CloneNotSupportedException

removeAnnotationSetListener

public void removeAnnotationSetListener(AnnotationSetListener l)
Specified by:
removeAnnotationSetListener in interface AnnotationSet

addAnnotationSetListener

public void addAnnotationSetListener(AnnotationSetListener l)
Specified by:
addAnnotationSetListener in interface AnnotationSet

fireAnnotationAdded

protected void fireAnnotationAdded(AnnotationSetEvent e)

fireAnnotationRemoved

protected void fireAnnotationRemoved(AnnotationSetEvent e)

removeGateListener

public void removeGateListener(GateListener l)
Specified by:
removeGateListener in interface AnnotationSet

addGateListener

public void addGateListener(GateListener l)
Specified by:
addGateListener in interface AnnotationSet

fireGateEvent

protected void fireGateEvent(GateEvent e)

writeObject

private void writeObject(ObjectOutputStream out)
                  throws IOException
Throws:
IOException

readObject

private void readObject(ObjectInputStream in)
                 throws IOException,
                        ClassNotFoundException
Throws:
IOException
ClassNotFoundException