org.annotation.wordfreak
Class AnnotationTree

java.lang.Object
  extended byorg.annotation.wordfreak.AnnotationTree

public class AnnotationTree
extends java.lang.Object

A data structure for storing extent-based annotations in a sorted tree structure and meta-information relating them. The sorting is based on the annoation's span. In cases where this does not indicate a unique location within the tree methods are available to specify which parent or sibling should be used or to prefer placing the annotation deeper or shallower in the tree.


Field Summary
static java.lang.String ANNOTATION_SOURCE
           
static java.lang.String DEFAULT
           
static java.lang.String DEFAULT_VIEW
           
protected  AnnotationFile file
          The i/o file for this Tree.
protected  java.util.List heads
          List of chain head annotations.
static java.lang.String ID
           
protected  int maxId
          Largest id being used in this tree.
protected  boolean messaging
          Specifies whether views should be notified of changes to this tree.
protected  java.util.Map nextMap
          Mapping between a head annotation and it's chained annotation.
protected  java.util.Map prevMap
          Mapping between a chained annotation and it's head annotation.
protected  java.util.HashMap relations_per_anchor
          Mapping between anchor annotation and their relation.
protected  HashList relations_per_annotation
          Mapping between annotation and every relation which references that annotation.
protected  HashList relations_per_type
          Mapping between a relation type and all the realtions of that type.
protected  Annotation root
          The root of this Tree.
protected  AnnotationSource source
          The data source that this Tree annotates.
protected  java.util.Set usedIds
          Set of id keys currently being used in this tree.
protected  java.util.Map views
          A list of various masked views of this Tree.
static java.lang.String XML_TYPE
           
 
Constructor Summary
AnnotationTree(AnnotationFile f, AnnotationSource s)
           
 
Method Summary
 boolean add(Annotation annotation)
           
 boolean add(Annotation root, Annotation annotation)
          Adds a new annotation to root and updates dependent annotations adjusting it if necessary to maintain tree integrity.
 boolean add(Annotation root, Annotation annotation, Annotation above)
           
 boolean add(Annotation root, Annotation annotation, Annotation above, boolean strictContains)
           
 boolean add(Annotation root, Annotation annotation, Annotation above, boolean preferShallow, int start, int end)
          Adds the specified annotation below the specified root and above the specified above annotation prefering shallow insertion when the specified boolean is true.
 boolean addAbove(Annotation annotation, Annotation above)
           
 AnnotationView addAnnotationView(java.lang.Object key)
           
 AnnotationView addAnnotationView(java.lang.Object key, AnnotationFilter filter)
           
 AnnotationView addAnnotationView(java.lang.Object key, AnnotationFilter aFilter, AnnotationFilter bFilter)
           
 AnnotationView addAnnotationView(java.lang.Object key, AnnotationFilter aFilter, AnnotationFilter bFilter, SortedAnnotations s)
           
 void addAnnotationView(java.lang.Object key, AnnotationView view)
           
 void addAnnotator(java.lang.String annotator)
           
 void addFeature(java.lang.String feature)
           
 int adjacent(Annotation a, Annotation b)
           
 void chainAnnotations(Annotation a, Annotation b)
          Creates a chain between the two specified annotations.
 void clear()
           
 boolean contains(Annotation a)
           
 Relation createRelation(java.lang.String type, java.lang.Object key, java.lang.String[] frame, Annotation[] annotations)
          Returns a new releation with the specified type, anchor key, frame, and annotations.
 boolean deepAdd(Annotation annotation)
           
 boolean deepAdd(Annotation root, Annotation annotation)
           
 void deepInsert(Annotation a)
           
 Annotation deepInsert(java.lang.String type, int s, int e)
           
 void fireTreeNodesChanged(Annotation node, Annotation[] children)
           
 void fireTreeNodesInserted(Annotation node, Annotation[] children)
           
 void fireTreeNodesMoved(Annotation[] children, Annotation oldParent, Annotation newParent)
           
 void fireTreeNodesRemoved(Annotation node, Annotation[] children)
           
 void fireTreeStructureChanged(Annotation node)
           
 AnnotationFile getAnnotationFile()
           
 AnnotationSource getAnnotationSource()
           
 AnnotationView getAnnotationView(java.lang.Object key)
           
 Annotation[] getChain(Annotation a)
          Returns the chains associated with the specified annotation.
 Annotation[] getChains()
          Returns the head annotations associated with all chains in this tree.
 java.lang.Object getChild(Annotation a, int i)
          Returns the child at the specified index of the specified annotation.
 java.lang.Object getChild(java.lang.Object node, int i)
           
 int getChildCount(Annotation a)
          Returns the number of children that the specified annotation has.
 int getChildCount(java.lang.Object node)
           
 Annotation getContainingLeaf(int offset)
          Returns the leaf which contains the offset.
 Annotation getContainingNode(Span span)
          Returns the deepest annotation which contains the specified span.
 byte[] getData(int offset, int length)
           
 int getIndexOfChild(java.lang.Object node, java.lang.Object child)
           
 java.util.List getLeavesInRange(int s, int e)
           
 java.lang.Object getParent(Annotation a)
           
 Relation getRelation(Annotation a)
           
 Relation getRelation(java.lang.String type, Annotation a)
          Returns a relation which is of the specified type and has as its anchor the specified annotation.
 Relation[] getRelations()
          Returns all the relations in this tree.
 Relation[] getRelations(Annotation a)
          Returns all the relations which relate the specified annotation.
 Relation[] getRelations(java.lang.String type)
          Returns all the relations of a given type in this tree.
 Relation[] getRelations(java.lang.String type, Annotation a)
          Returns all relation which are of the specified type and which contain the specified annotation.
 Annotation getRoot()
           
 AnnotationSource getSource()
           
 void insert(Annotation a)
           
 void insert(Annotation parent, Annotation annotation)
          Inserts the specified annotation below the specified parent.
 void insert(Annotation root, Annotation annotation, Annotation above)
           
 Annotation insert(int s, int e)
           
 Annotation insert(int s, int e, Annotation parent)
           
 Annotation insert(java.lang.String type, Annotation s, Annotation e)
          Inserts and annotation which has s as its first child and e as its last child.
 Annotation insert(java.lang.String type, int s, int e)
          Inserts an annotation with specified type and span and returns that new annotation.
 Annotation insert(java.lang.String type, int s, int e, Annotation parent)
           
 Annotation insertParent(Annotation child)
          Creates a parent above an Annotation.
 Annotation insertParent(java.lang.String type, Annotation child)
          Creates a parent above an Annotation.
 void insertSubTree(Annotation a, Annotation p)
           
 Annotation insertZeroLengthAnnotationAfter(Annotation after)
           
 Annotation insertZeroLengthAnnotationAfter(java.lang.String type, Annotation after)
           
 Annotation insertZeroLengthAnnotationBefore(Annotation before)
           
 Annotation insertZeroLengthAnnotationBefore(java.lang.String type, Annotation before)
           
 boolean isAnchor(Annotation chainedAnnotation)
          Returns true of the specified annotation is the head of a chain.
 boolean isLeaf(java.lang.Object node)
           
 boolean isTerminalSpan(int start, int end)
          Returns true if the span given by the specified start and end values can be inserted into this tree as a leaf.
 boolean isTerminalSpan(Span span)
          Returns true if the specified span can be inserted into this tree as a leaf.
 boolean isValidSpan(int start, int end)
          Returns true if the span given by the specified start and end values can be inserted into this tree without crossing another annotation.
 boolean isValidSpan(Span span)
          Returns true if the specified span can be inserted into this tree without crossing another annotation.
 java.util.Iterator iterator()
           
 void move(Annotation node, Annotation dest)
          moves an Annotation from one place to another.
 void put(int roleIndex, Annotation ann, Relation relation)
          Puts the specified annotation at the specified role index in the specified relation.
 void put(java.lang.String role, Annotation ann, Relation relation)
          Puts the specified annotation the specified relation with the specified role name.
 void remove(Annotation annotation)
           
 void remove(Annotation annotation, boolean shrink)
           
 void remove(Annotation annotation, Relation relation)
          Removes the specified annotation from the specified relation or if the specified annotation is the anchor of the relation, removes the relation from this tree.
 void remove(Relation relation)
          Removes the specified relation from this tree.
 void remove(java.lang.String type, Annotation a, Relation relation)
           
 void removeAnnotationView(java.lang.Object key)
           
 Annotation removeFromChain(Annotation a)
          Removes the specified annotation from a chain and returns another annotation from the modified chain if such an annotation exists.
 javax.swing.tree.TreeNode removeSubTree(Annotation annotation)
           
 void setAnnotationSource(AnnotationSource s)
           
 void setRoot(Annotation r)
           
protected  void setSourceFileName(java.lang.String sfn)
           
 boolean shallowAdd(Annotation annotation)
           
 boolean shallowAdd(Annotation root, Annotation annotation)
           
 Annotation terminalInsert(java.lang.String type, int start, int end)
          Inserts a newly created annotation with the specified type and span in this tree and returns the newly created annotation.
 void update(Annotation a)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ID

public static final java.lang.String ID
See Also:
Constant Field Values

ANNOTATION_SOURCE

public static final java.lang.String ANNOTATION_SOURCE
See Also:
Constant Field Values

DEFAULT_VIEW

public static final java.lang.String DEFAULT_VIEW
See Also:
Constant Field Values

XML_TYPE

public static final java.lang.String XML_TYPE
See Also:
Constant Field Values

DEFAULT

public static final java.lang.String DEFAULT
See Also:
Constant Field Values

file

protected AnnotationFile file
The i/o file for this Tree.


source

protected AnnotationSource source
The data source that this Tree annotates.


root

protected Annotation root
The root of this Tree.


views

protected java.util.Map views
A list of various masked views of this Tree. This list is used for updating the views when changes to the tree are made.


usedIds

protected java.util.Set usedIds
Set of id keys currently being used in this tree.


maxId

protected int maxId
Largest id being used in this tree.


heads

protected java.util.List heads
List of chain head annotations.


nextMap

protected java.util.Map nextMap
Mapping between a head annotation and it's chained annotation.


prevMap

protected java.util.Map prevMap
Mapping between a chained annotation and it's head annotation.


relations_per_anchor

protected java.util.HashMap relations_per_anchor
Mapping between anchor annotation and their relation.


relations_per_annotation

protected HashList relations_per_annotation
Mapping between annotation and every relation which references that annotation.


relations_per_type

protected HashList relations_per_type
Mapping between a relation type and all the realtions of that type.


messaging

protected boolean messaging
Specifies whether views should be notified of changes to this tree. This is used to prevent views from updating when large changes are being made until after the changes are complete.

Constructor Detail

AnnotationTree

public AnnotationTree(AnnotationFile f,
                      AnnotationSource s)
Method Detail

setSourceFileName

protected void setSourceFileName(java.lang.String sfn)

setAnnotationSource

public void setAnnotationSource(AnnotationSource s)

getAnnotationSource

public AnnotationSource getAnnotationSource()

getAnnotationView

public AnnotationView getAnnotationView(java.lang.Object key)

addAnnotationView

public AnnotationView addAnnotationView(java.lang.Object key)

addAnnotationView

public AnnotationView addAnnotationView(java.lang.Object key,
                                        AnnotationFilter filter)

addAnnotationView

public AnnotationView addAnnotationView(java.lang.Object key,
                                        AnnotationFilter aFilter,
                                        AnnotationFilter bFilter)

addAnnotationView

public AnnotationView addAnnotationView(java.lang.Object key,
                                        AnnotationFilter aFilter,
                                        AnnotationFilter bFilter,
                                        SortedAnnotations s)

addAnnotationView

public void addAnnotationView(java.lang.Object key,
                              AnnotationView view)

removeAnnotationView

public void removeAnnotationView(java.lang.Object key)

getContainingLeaf

public Annotation getContainingLeaf(int offset)
Returns the leaf which contains the offset.

Parameters:
offset - The offset.
Returns:
A leaf annotation which contains the offset or null if no such annotation exists.

getContainingNode

public Annotation getContainingNode(Span span)
Returns the deepest annotation which contains the specified span.

Parameters:
span - The span which the returned node must contain.
Returns:
The deepest annotation which contains the span or null if no such annotation exists.

add

public boolean add(Annotation annotation)

add

public boolean add(Annotation root,
                   Annotation annotation)
Adds a new annotation to root and updates dependent annotations adjusting it if necessary to maintain tree integrity.

Parameters:
root - parent annotation into which new annotaiton will be inserted.
annotation - new annotation to be added.
Returns:
always returns true.

addAbove

public boolean addAbove(Annotation annotation,
                        Annotation above)

deepAdd

public boolean deepAdd(Annotation annotation)

deepAdd

public boolean deepAdd(Annotation root,
                       Annotation annotation)

shallowAdd

public boolean shallowAdd(Annotation annotation)

shallowAdd

public boolean shallowAdd(Annotation root,
                          Annotation annotation)

add

public boolean add(Annotation root,
                   Annotation annotation,
                   Annotation above)

add

public boolean add(Annotation root,
                   Annotation annotation,
                   Annotation above,
                   boolean strictContains)

add

public boolean add(Annotation root,
                   Annotation annotation,
                   Annotation above,
                   boolean preferShallow,
                   int start,
                   int end)
Adds the specified annotation below the specified root and above the specified above annotation prefering shallow insertion when the specified boolean is true.

Parameters:
root - the annotation below which the annotation is added.
annotation - The annotation to be added.
above - An annotation above which the specified annotation should be inserted.
preferShallow - specifies whether the addition should prefer shallower placement in the tree.
start -
end -
Returns:
true if this annotation was successfully inserted, false otherwise.

insert

public void insert(Annotation parent,
                   Annotation annotation)
Inserts the specified annotation below the specified parent.

Parameters:
parent - The parent annotation of the annotation to be inserted.
annotation - The annotation to insert into this tree.

insert

public void insert(Annotation root,
                   Annotation annotation,
                   Annotation above)

insert

public void insert(Annotation a)

deepInsert

public void deepInsert(Annotation a)

insert

public Annotation insert(int s,
                         int e)

insert

public Annotation insert(int s,
                         int e,
                         Annotation parent)

deepInsert

public Annotation deepInsert(java.lang.String type,
                             int s,
                             int e)

terminalInsert

public Annotation terminalInsert(java.lang.String type,
                                 int start,
                                 int end)
Inserts a newly created annotation with the specified type and span in this tree and returns the newly created annotation.

Parameters:
type - The type of annotation to create.
start - The start of the span of the new annotation.
end - The end of the span of the new annotation.
Returns:
the newly ceated annotation or null if the annotation would intersect with an exiting annotation.

isValidSpan

public boolean isValidSpan(int start,
                           int end)
Returns true if the span given by the specified start and end values can be inserted into this tree without crossing another annotation.

Parameters:
start - The start value of the span.
end - The end value of the span.
Returns:
true if the span given by the specified start and end values does not intersect any existing nodes, false otherwise or if the span is outside of this tree.

isValidSpan

public boolean isValidSpan(Span span)
Returns true if the specified span can be inserted into this tree without crossing another annotation.

Parameters:
span - The span to be tested.
Returns:
true if the specified span does not intersect any existing nodes, false otherwise or if the span is outside of this tree.

isTerminalSpan

public boolean isTerminalSpan(int start,
                              int end)
Returns true if the span given by the specified start and end values can be inserted into this tree as a leaf.

Parameters:
start - The start value of the span.
end - The end value of the span.
Returns:
true if the span given by the specified start and end values does not intersect any existing nodes, false otherwise.

isTerminalSpan

public boolean isTerminalSpan(Span span)
Returns true if the specified span can be inserted into this tree as a leaf.

Parameters:
span - The span to be tested.
Returns:
true if the specified span does not intersect any existing nodes, false otherwise.

insert

public Annotation insert(java.lang.String type,
                         int s,
                         int e,
                         Annotation parent)

insert

public Annotation insert(java.lang.String type,
                         int s,
                         int e)
Inserts an annotation with specified type and span and returns that new annotation.

Parameters:
type - the type.
s - the start of the span.
e - then end of the span.
Returns:
the newly inserted annotation.

insert

public Annotation insert(java.lang.String type,
                         Annotation s,
                         Annotation e)
Inserts and annotation which has s as its first child and e as its last child.


insertSubTree

public void insertSubTree(Annotation a,
                          Annotation p)

insertZeroLengthAnnotationBefore

public Annotation insertZeroLengthAnnotationBefore(Annotation before)

insertZeroLengthAnnotationBefore

public Annotation insertZeroLengthAnnotationBefore(java.lang.String type,
                                                   Annotation before)

insertZeroLengthAnnotationAfter

public Annotation insertZeroLengthAnnotationAfter(Annotation after)

insertZeroLengthAnnotationAfter

public Annotation insertZeroLengthAnnotationAfter(java.lang.String type,
                                                  Annotation after)

insertParent

public Annotation insertParent(Annotation child)
Creates a parent above an Annotation.

Parameters:
child - the annotation to create a parent above.
Returns:
the newly created parent.

insertParent

public Annotation insertParent(java.lang.String type,
                               Annotation child)
Creates a parent above an Annotation.

Parameters:
child - the annotation to create a parent above.
type - the type of annotation to create.
Returns:
the newly created parent.

move

public void move(Annotation node,
                 Annotation dest)
moves an Annotation from one place to another.

Parameters:
node - the annotation to move.
dest - the annotation to move it into.

remove

public void remove(Annotation annotation)

remove

public void remove(Annotation annotation,
                   boolean shrink)

removeSubTree

public javax.swing.tree.TreeNode removeSubTree(Annotation annotation)

update

public void update(Annotation a)

clear

public void clear()

contains

public boolean contains(Annotation a)

iterator

public java.util.Iterator iterator()

setRoot

public void setRoot(Annotation r)

getRoot

public Annotation getRoot()

getAnnotationFile

public AnnotationFile getAnnotationFile()

getLeavesInRange

public java.util.List getLeavesInRange(int s,
                                       int e)

getData

public byte[] getData(int offset,
                      int length)

getSource

public AnnotationSource getSource()

addAnnotator

public void addAnnotator(java.lang.String annotator)

addFeature

public void addFeature(java.lang.String feature)

fireTreeNodesInserted

public void fireTreeNodesInserted(Annotation node,
                                  Annotation[] children)

fireTreeNodesMoved

public void fireTreeNodesMoved(Annotation[] children,
                               Annotation oldParent,
                               Annotation newParent)

fireTreeNodesRemoved

public void fireTreeNodesRemoved(Annotation node,
                                 Annotation[] children)

fireTreeNodesChanged

public void fireTreeNodesChanged(Annotation node,
                                 Annotation[] children)

fireTreeStructureChanged

public void fireTreeStructureChanged(Annotation node)

chainAnnotations

public void chainAnnotations(Annotation a,
                             Annotation b)
Creates a chain between the two specified annotations.

Parameters:
a - The first annotation in the chain.
b - The second annotation in the chain.

getChains

public Annotation[] getChains()
Returns the head annotations associated with all chains in this tree.

Returns:
An array of head annotations associated with all chains in this tree.

getChain

public Annotation[] getChain(Annotation a)
Returns the chains associated with the specified annotation. Annotations can only be in one chain.

Parameters:
a - An annotation in a chain.
Returns:
The chains associated with the specified annotation. The returned array can be empty.

removeFromChain

public Annotation removeFromChain(Annotation a)
Removes the specified annotation from a chain and returns another annotation from the modified chain if such an annotation exists.

Parameters:
a - The annotation to be removed.
Returns:
A annotation from the modified chain or null if no such an annotation exists.

isAnchor

public boolean isAnchor(Annotation chainedAnnotation)
Returns true of the specified annotation is the head of a chain.

Parameters:
chainedAnnotation - Tha annotation to check.
Returns:
True of the specified annotation is the head of a chain, false otherwise.

createRelation

public Relation createRelation(java.lang.String type,
                               java.lang.Object key,
                               java.lang.String[] frame,
                               Annotation[] annotations)
Returns a new releation with the specified type, anchor key, frame, and annotations.

Parameters:
type - The type of the relations.
key - The anchor annotation of this relation.
frame - The names of the frame elements of the relation.
annotations - The annotation which occupy the frame elements.
Returns:
A new releation with the specified type, anchor key, frame, and annotations.

put

public void put(int roleIndex,
                Annotation ann,
                Relation relation)
Puts the specified annotation at the specified role index in the specified relation.

Parameters:
roleIndex - The position in the frame of the specified relation when the specified annotaiton will be placed.
ann - The annotation to be placed at the specified role Index in the specified relation.
relation - The annotation to be modified.

put

public void put(java.lang.String role,
                Annotation ann,
                Relation relation)
Puts the specified annotation the specified relation with the specified role name. Role names can be duplicated and this will always add anothor element to the relations frame.

Parameters:
role - The name of the role slot in the relation when the annotation will be placed.
ann - The annotation to put in the relation.
relation - The relation to be modified.

remove

public void remove(Relation relation)
Removes the specified relation from this tree.

Parameters:
relation - The relation to be removed.

remove

public void remove(Annotation annotation,
                   Relation relation)
Removes the specified annotation from the specified relation or if the specified annotation is the anchor of the relation, removes the relation from this tree.

Parameters:
annotation - The annotaiton to remove.
relation - The relation from which the spevified annotation is removed.

remove

public void remove(java.lang.String type,
                   Annotation a,
                   Relation relation)

getRelations

public Relation[] getRelations()
Returns all the relations in this tree.

Returns:
an array of all the relations this tree.

getRelations

public Relation[] getRelations(java.lang.String type)
Returns all the relations of a given type in this tree.

Parameters:
type - the type of Relation to return
Returns:
an array of all the relations of type type in this tree.

getRelations

public Relation[] getRelations(Annotation a)
Returns all the relations which relate the specified annotation.

Parameters:
a - the Annotation to find Relations for

getRelations

public Relation[] getRelations(java.lang.String type,
                               Annotation a)
Returns all relation which are of the specified type and which contain the specified annotation.

Parameters:
type - The type of relations to be returned.
a - The annotation which the returned relations contain.
Returns:
An array of all relation which are of the specified type and which contain the specified relation.

getRelation

public Relation getRelation(Annotation a)

getRelation

public Relation getRelation(java.lang.String type,
                            Annotation a)
Returns a relation which is of the specified type and has as its anchor the specified annotation.

Parameters:
type - The type of relation to be returned.
a - The anchor of the relations to be returned.
Returns:
A relation which is of the specified type and has as its anchor the specified annotation or null if not such relation exists.

adjacent

public int adjacent(Annotation a,
                    Annotation b)

getChild

public java.lang.Object getChild(java.lang.Object node,
                                 int i)

getChildCount

public int getChildCount(java.lang.Object node)

getIndexOfChild

public int getIndexOfChild(java.lang.Object node,
                           java.lang.Object child)

isLeaf

public boolean isLeaf(java.lang.Object node)

getParent

public java.lang.Object getParent(Annotation a)

getChild

public java.lang.Object getChild(Annotation a,
                                 int i)
Returns the child at the specified index of the specified annotation.

Parameters:
a - The annotation whose child is to be returned.
i - The index of the child to be returned.
Returns:
The child at the specified index of the specified annotation.

getChildCount

public int getChildCount(Annotation a)
Returns the number of children that the specified annotation has.

Parameters:
a - The annotation whose children count is being requested.
Returns:
the number of children that the specified annotation has.


Copyright © 2004 Thomas Morton and Jeremy LaCivita. All Rights Reserved.