Class CasUtil

java.lang.Object
org.apache.uima.fit.util.CasUtil

public final class CasUtil extends Object
Utility methods for convenient access to the CAS.
  • Field Details

    • UIMA_BUILTIN_JCAS_PREFIX

      public static final String UIMA_BUILTIN_JCAS_PREFIX
      Package name of JCas wrapper classes built into UIMA.
      See Also:
  • Method Details

    • iteratorFS

      @Deprecated public static <T extends FeatureStructure> Iterator<T> iteratorFS(CAS cas, Type type)
      Deprecated.
      Use cas.select(type).iterator()
      Get an iterator over the given feature structures type.
      Type Parameters:
      T - the JCas type.
      Parameters:
      cas - a CAS.
      type - a type.
      Returns:
      a return value.
      See Also:
    • iterator

      public static <T extends AnnotationFS> Iterator<T> iterator(CAS cas, Type type)
      Get an iterator over the given annotation type.
      Type Parameters:
      T - the JCas type.
      Parameters:
      cas - a CAS.
      type - a type.
      Returns:
      a return value.
      See Also:
    • getType

      public static Type getType(CAS cas, Class<?> type)
      Get the CAS type for the given JCas wrapper class.
      Parameters:
      cas - the CAS hosting the type system.
      type - the JCas wrapper class.
      Returns:
      the CAS type.
    • getType

      public static Type getType(TypeSystem aTypeSystem, Class<?> type)
      Get the CAS type for the given JCas wrapper class.
      Parameters:
      aTypeSystem - the CAS hosting the type system.
      type - the JCas wrapper class.
      Returns:
      the CAS type.
    • getType

      public static Type getType(CAS aCas, String aTypename)
      Get the CAS type for the given name.
      Parameters:
      aCas - the CAS hosting the type system.
      aTypename - the fully qualified type name.
      Returns:
      the CAS type.
    • getType

      public static Type getType(TypeSystem aTypeSystem, String aTypename)
      Get the CAS type for the given name.
      Parameters:
      aTypeSystem - the type system
      aTypename - the fully qualified type name.
      Returns:
      the CAS type.
    • getAnnotationType

      public static Type getAnnotationType(CAS aCas, Class<?> aJCasClass)
      Get the CAS type for the given JCas wrapper class making sure it is or inherits from Annotation.
      Parameters:
      aCas - the CAS hosting the type system.
      aJCasClass - the JCas wrapper class.
      Returns:
      the CAS type.
    • getAnnotationType

      public static Type getAnnotationType(CAS aCas, String aTypeName)
      Get the CAS type for the given name making sure it is or inherits from Annotation.
      Parameters:
      aCas - the CAS hosting the type system.
      aTypeName - the fully qualified type name.
      Returns:
      the CAS type.
    • selectFS

      public static List<FeatureStructure> selectFS(ArrayFS array, Type type)
      Convenience method to iterator over all feature structures of a given type.
      Parameters:
      array - features structure array.
      type - the type.
      Returns:
      A collection of the selected type.
      See Also:
    • select

      public static List<AnnotationFS> select(ArrayFS array, Type type)
      Convenience method to iterator over all annotations of a given type.
      Parameters:
      array - features structure array.
      type - the type.
      Returns:
      A collection of the selected type.
      See Also:
    • selectAllFS

      public static Collection<FeatureStructure> selectAllFS(CAS aCas)
      Convenience method to iterator over all features structures.
      Parameters:
      aCas - the CAS hosting the type system.
      Returns:
      A collection of the selected type.
      See Also:
    • selectFS

      @Deprecated public static <T extends FeatureStructure> List<T> selectFS(CAS cas, Type type)
      Deprecated.
      Use cas.select(type).asList()
      Convenience method to iterator over all feature structures of a given type.
      Parameters:
      cas - the CAS containing the type system.
      type - the type.
      Returns:
      A collection of the selected type.
      See Also:
    • selectAll

      public static Collection<AnnotationFS> selectAll(CAS aCas)
      Convenience method to iterator over all annotations.
      Parameters:
      aCas - the CAS hosting the type system.
      Returns:
      A collection of the selected type.
      See Also:
    • select

      public static Collection<AnnotationFS> select(CAS cas, Type type)
      Convenience method to iterator over all annotations of a given type.
      Parameters:
      cas - the CAS containing the type system.
      type - the type.
      Returns:
      A collection of the selected type.
      See Also:
    • selectAt

      public static List<AnnotationFS> selectAt(CAS aCas, Type aType, int aBegin, int aEnd)
      Get all annotations of the given type at the specified offsets.
      Parameters:
      aCas - the CAS containing the annotations.
      aType - the type of annotations to fetch.
      aBegin - the begin offset.
      aEnd - the end offset.
      Returns:
      the annotations at the specified offsets.
    • selectSingleAt

      public static AnnotationFS selectSingleAt(CAS aCas, Type aType, int aBegin, int aEnd)
      Get the single instance of the specified type from the CAS at the given offsets.
      Parameters:
      aCas - the CAS containing the annotations.
      aType - the type of annotations to fetch.
      aBegin - the begin offset.
      aEnd - the end offset.
      Returns:
      the single annotation at the specified offsets.
    • selectBetween

      public static List<AnnotationFS> selectBetween(Type type, AnnotationFS ann1, AnnotationFS ann2)
      Get a list of annotations of the given annotation type located between two annotations. Does not use subiterators and does not respect type priorities. Zero-width annotations what lie on the borders are included in the result, e.g. if the boundary annotations are [1..2] and [2..3] then an annotation [2..2] is returned. If there is a non-zero overlap between the boundary annotations, the result is empty. The method properly handles cases where the second boundary annotations occurs before the first boundary annotation by switching their roles.
      Parameters:
      type - a UIMA type.
      ann1 - the first boundary annotation.
      ann2 - the second boundary annotation.
      Returns:
      a return value.
      See Also:
    • selectBetween

      public static List<AnnotationFS> selectBetween(CAS cas, Type type, AnnotationFS ann1, AnnotationFS ann2)
      Get a list of annotations of the given annotation type located between two annotations. Does not use subiterators and does not respect type priorities. Zero-width annotations what lie on the borders are included in the result, e.g. if the boundary annotations are [1..2] and [2..3] then an annotation [2..2] is returned. If there is a non-zero overlap between the boundary annotations, the result is empty. The method properly handles cases where the second boundary annotations occurs before the first boundary annotation by switching their roles.
      Parameters:
      cas - a CAS.
      type - a UIMA type.
      ann1 - the first boundary annotation.
      ann2 - the second boundary annotation.
      Returns:
      a return value.
      See Also:
    • selectCovered

      public static List<AnnotationFS> selectCovered(Type type, AnnotationFS coveringAnnotation)
      Get a list of annotations of the given annotation type constraint by a certain annotation. Iterates over all annotations of the given type to find the covered annotations. Does not use subiterators and does not respect type prioritites. Was adapted from Subiterator. Uses the same approach except that type priorities are ignored.

      The covering annotation is never returned itself, even if it is of the queried-for type or a subtype of that type.

      The method only returns properly covered annotations, that is annotations where the begin/end offsets are equal to the 'covering' annotation or where both the begin/end are included in the span of the 'covering' annotation. Partially overlapping annotations are not returned.

      Parameters:
      type - a UIMA type.
      coveringAnnotation - the covering annotation.
      Returns:
      a return value.
      See Also:
    • selectCovered

      public static List<AnnotationFS> selectCovered(CAS cas, Type type, AnnotationFS coveringAnnotation)
      Get a list of annotations of the given annotation type constraint by a certain annotation. Iterates over all annotations of the given type to find the covered annotations. Does not use subiterators and does not respect type prioritites. Was adapted from Subiterator. Uses the same approach except that type priorities are ignored.

      The covering annotation is never returned itself, even if it is of the queried-for type or a subtype of that type.

      The method only returns properly covered annotations, that is annotations where the begin/end offsets are equal to the 'covering' annotation or where both the begin/end are included in the span of the 'covering' annotation. Partially overlapping annotations are not returned.

      Parameters:
      cas - a CAS.
      type - a UIMA type.
      coveringAnnotation - the covering annotation.
      Returns:
      a return value.
      See Also:
    • selectCovered

      public static List<AnnotationFS> selectCovered(CAS cas, Type type, int begin, int end)
      Get a list of annotations of the given annotation type constraint by a certain annotation. Iterates over all annotations of the given type to find the covered annotations. Does not use subiterators and does not respect type prioritites. Was adapted from Subiterator. Uses the same approach except that type priorities are ignored.

      Note: this is significantly slower than using selectCovered(CAS, Type, AnnotationFS). It is possible to use selectCovered(cas, type, new Annotation(jCas, int, int)), but that will allocate memory in the jCas for the new annotation. If you do that repeatedly many times, memory may fill up.

      The method only returns properly covered annotations, that is annotations where the begin/end offsets are equal to the given begin/end or where both the begin/end are included in the span of the given span. Partially overlapping annotations are not returned.

      Parameters:
      cas - a CAS.
      type - a UIMA type.
      begin - begin offset.
      end - end offset.
      Returns:
      a return value.
      See Also:
    • selectCovering

      public static List<AnnotationFS> selectCovering(Type type, AnnotationFS coveredAnnotation)
      Get a list of annotations of the given annotation type constraint by a certain annotation. Iterates over all annotations to find the covering annotations.

      The method only returns properly covering annotations, that is annotations where the begin/end offsets are equal to the begin/end of the given annotation or where given 'covered' annotation is properly contained within the span of the 'covering' annotation. Partially overlapping annotations are not returned.

      Note: this is REALLY SLOW! You don't want to use this. Instead, consider using indexCovering(CAS, Type, Type) or a ContainmentIndex.

      Parameters:
      type - a UIMA type.
      coveredAnnotation - the covered annotation.
      Returns:
      a return value.
      See Also:
    • selectCovering

      public static List<AnnotationFS> selectCovering(CAS cas, Type type, AnnotationFS coveredAnnotation)
      Get a list of annotations of the given annotation type constraint by a certain annotation. Iterates over all annotations to find the covering annotations.

      The method only returns properly covering annotations, that is annotations where the begin/end offsets are equal to the begin/end of the given annotation or where given 'covered' annotation is properly contained within the span of the 'covering' annotation. Partially overlapping annotations are not returned.

      Note: this is REALLY SLOW! You don't want to use this. Instead, consider using indexCovering(CAS, Type, Type) or a ContainmentIndex.

      Parameters:
      cas - a CAS.
      type - a UIMA type.
      coveredAnnotation - the covered annotation.
      Returns:
      a return value.
      See Also:
    • selectCovering

      public static List<AnnotationFS> selectCovering(CAS cas, Type type, int begin, int end)
      Get a list of annotations of the given annotation type constraint by a certain annotation. Iterates over all annotations to find the covering annotations.

      The method only returns properly covering annotations, that is annotations where the begin/end offsets are equal to the given begin/end to or where given span is properly contained within the span of the 'covering' annotation. Partially overlapping annotations are not returned.

      Note: this is REALLY SLOW! You don't want to use this. Instead, consider using indexCovering(CAS, Type, Type) or a ContainmentIndex.

      Parameters:
      cas - a CAS.
      type - a UIMA type.
      begin - begin offset.
      end - end offset.
      Returns:
      a return value.
      See Also:
    • indexCovering

      public static Map<AnnotationFS,List<AnnotationFS>> indexCovering(CAS cas, Type type, Type coveringType)
      Create an index for quickly lookup up the annotations covering a particular annotation. This is preferable to using selectCovering(CAS, Type, int, int) because the overhead of scanning the CAS occurs only when the index is build. Subsequent lookups to the index are fast.

      The method only returns properly covering annotations, that is annotations where the begin/end offsets are equal to the begin/end of the given annotation or where given 'covered' annotation is properly contained within the span of the 'covering' annotation. Partially overlapping annotations are not returned.

      When querying for the annotations covering a given annotation, the given annotation itself is never returned, even if it is of the queried type.

      Parameters:
      cas - a CAS.
      type - type to create the index for - this is used in lookups.
      coveringType - type of covering annotations.
      Returns:
      the index.
      See Also:
    • indexCovered

      public static Map<AnnotationFS,List<AnnotationFS>> indexCovered(CAS cas, Type type, Type coveredType)
      Create an index for quickly lookup up the annotations covered by a particular annotation. This is preferable to using selectCovered(CAS, Type, int, int) because the overhead of scanning the CAS occurs only when the index is build. Subsequent lookups to the index are fast. The order of entries in the map is not defined. However, lists of covered annotations in the map are guaranteed to be in the same order as in the UIMA default annotation index.

      The method only returns properly covered annotations, that is annotations where the begin/end offsets are equal to the 'covering' annotation or where both the begin/end are included in the span of the 'covering' annotation. Partially overlapping annotations are not returned.

      When querying for the annotations covered by a given annotation, the given annotation itself is never returned, even if it is of the queried type. *

      Parameters:
      cas - a CAS.
      type - type to create the index for - this is used in lookups.
      coveredType - type of covering annotations.
      Returns:
      the index.
      See Also:
    • selectOverlapping

      public static List<AnnotationFS> selectOverlapping(Type aType, AnnotationFS aBoundaryAnnotation)
      Get a list of annotations of the given annotation type overlapping the given annotation. Does not use subiterators and does not respect type prioritites.
      Parameters:
      aType - a UIMA type.
      aBoundaryAnnotation - the covering annotation.
      Returns:
      a list of overlapping annotations.
      See Also:
    • selectOverlapping

      public static List<AnnotationFS> selectOverlapping(CAS aCas, Type aType, AnnotationFS aBoundaryAnnotation)
      Get a list of annotations of the given annotation type overlapping the given annotation. Does not use subiterators and does not respect type prioritites.
      Parameters:
      aCas - a CAS.
      aType - a UIMA type.
      aBoundaryAnnotation - the covering annotation.
      Returns:
      a list of overlapping annotations.
      See Also:
    • selectOverlapping

      public static List<AnnotationFS> selectOverlapping(CAS aCas, Type aType, int aSelBegin, int aSelEnd)
      Get a list of annotations of the given annotation type overlapping the given span. Does not use subiterators and does not respect type prioritites.
      Parameters:
      aCas - a CAS.
      aType - a UIMA type.
      aSelBegin - begin offset.
      aSelEnd - end offset.
      Returns:
      a list of overlapping annotations.
      See Also:
    • selectByIndex

      public static AnnotationFS selectByIndex(CAS cas, Type type, int index)
      This method exists simply as a convenience method for unit testing. It is not very efficient and should not, in general be used outside the context of unit testing.
      Parameters:
      cas - a CAS containing the annotation.
      type - a UIMA type.
      index - this can be either positive (0 corresponds to the first annotation of a type) or negative (-1 corresponds to the last annotation of a type.)
      Returns:
      an annotation of the given type
      See Also:
    • selectSingle

      public static AnnotationFS selectSingle(CAS cas, Type type)
      Get the single instance of the specified type from the CAS.
      Parameters:
      cas - a CAS containing the annotation.
      type - a UIMA type.
      Returns:
      the single instance of the given type. throws IllegalArgumentException if not exactly one instance if the given type is present.
    • selectSingleFS

      public static FeatureStructure selectSingleFS(CAS cas, Type type)
      Get the single instance of the specified type from the CAS.
      Parameters:
      cas - a CAS containing the annotation.
      type - a UIMA type.
      Returns:
      the single instance of the given type. throws IllegalArgumentException if not exactly one instance if the given type is present.
    • selectSingleRelative

      public static AnnotationFS selectSingleRelative(Type type, AnnotationFS annotation, int index)
      Return an annotation preceding or following of a given reference annotation.
      Parameters:
      type - a type.
      annotation - anchor annotation
      index - relative position to access. A negative value selects a preceding annotation while a positive number selects a following annotation.
      Returns:
      the addressed annotation.
      See Also:
    • selectSingleRelative

      public static AnnotationFS selectSingleRelative(CAS cas, Type type, AnnotationFS aAnchor, int aPosition)
      Return an annotation preceding or following of a given reference annotation. If the type parameter corresponds to the type or a subtype of the anchor annotation and the relative position is 0, then the anchor annotation is returned.
      Parameters:
      cas - a CAS containing the annotation.
      type - a type.
      aAnchor - anchor annotation
      aPosition - relative position to access. A negative value selects a preceding annotation while a positive number selects a following annotation.
      Returns:
      the addressed annotation.
      Throws:
      IndexOutOfBoundsException - if the relative position points beyond the type index bounds.
      IllegalArgumentException - if the relative position is 0 and the anchor type does not subsume the selected type.
      See Also:
    • selectPreceding

      public static List<AnnotationFS> selectPreceding(CAS cas, Type type, AnnotationFS anchor, int count)
      Returns the n annotations preceding the given annotation
      Parameters:
      cas - a CAS.
      type - a UIMA type.
      anchor - anchor annotation
      count - number of annotations to collect
      Returns:
      List of aType annotations preceding anchor annotation
      See Also:
    • selectFollowing

      public static List<AnnotationFS> selectFollowing(CAS cas, Type type, AnnotationFS anchor, int count)
      Returns the n annotations following the given annotation
      Parameters:
      cas - a CAS.
      type - a UIMA type.
      anchor - anchor annotation
      count - number of annotations to collect
      Returns:
      List of aType annotations following anchor annotation
      See Also:
    • exists

      public static <T extends TOP> boolean exists(CAS aCas, Type aType)
      Test if a CAS contains an annotation of the given type.
      Type Parameters:
      T - the annotation type.
      Parameters:
      aCas - a CAS.
      aType - a annotation type.
      Returns:
      true if there is at least one annotation of the given type in the CAS.
    • getView

      public static CAS getView(CAS cas, String viewName, CAS fallback)
      Convenience method to get the specified view or a default view if the requested view does not exist. The default can also be null.
      Parameters:
      cas - a CAS
      viewName - the requested view.
      fallback - the default view if the requested view does not exist.
      Returns:
      the requested view or the default if the requested view does not exist.
    • getView

      public static CAS getView(CAS cas, String viewName, boolean create)
      Convenience method to get the specified view or create a new view if the requested view does not exist.
      Parameters:
      cas - a CAS
      viewName - the requested view.
      create - the view is created if it does not exist.
      Returns:
      the requested view
      Throws:
      IllegalArgumentException - if the view does not exist and is not to be created.
    • toText

      public static <T extends AnnotationFS> List<String> toText(Iterable<T> iterable)
      Fetch the text covered by the specified annotations and return it as a list of strings.
      Type Parameters:
      T - UIMA JCas type.
      Parameters:
      iterable - annotation container.
      Returns:
      list of covered strings.
    • toText

      public static <T extends AnnotationFS> List<String> toText(Iterator<T> iterator)
      Fetch the text covered by the specified annotations and return it as a list of strings.
      Type Parameters:
      T - UIMA JCas type.
      Parameters:
      iterator - annotation iterator.
      Returns:
      list of covered strings.
    • isAnnotationType

      public static boolean isAnnotationType(CAS aCas, Type aType)
    • requireAnnotationType

      public static void requireAnnotationType(CAS aCas, Type aType)