Class JCasUtil

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

public final class JCasUtil extends Object
Utility methods for convenient access to the JCas.
  • Method Details

    • subiterate

      public static <T extends Annotation> Iterable<T> subiterate(JCas jCas, Class<T> type, AnnotationFS container, boolean ambiguous, boolean strict)
      Convenience method to iterator over all annotations of a given type occurring within the scope of a provided annotation (sub-iteration).
      Type Parameters:
      T - the iteration type.
      Parameters:
      jCas - a JCas.
      type - the type.
      container - the containing annotation.
      ambiguous - If set to false, resulting iterator will be unambiguous.
      strict - Controls if annotations that overlap to the right are considered in or out.
      Returns:
      A sub-iterator iterable.
      See Also:
    • iterator

      @Deprecated public static <T extends TOP> Iterator<T> iterator(JCas jCas, Class<T> type)
      Deprecated.
      Use jcas.select(type).iterator()
      Get an iterator over the given feature structure type.
      Type Parameters:
      T - the JCas type.
      Parameters:
      jCas - a JCas.
      type - a type.
      Returns:
      a return value.
      See Also:
    • iterator

      public static <T extends AnnotationFS> Iterator<T> iterator(AnnotationFS container, Class<T> type, boolean ambiguous, boolean strict)
      Convenience method to get a sub-iterator for the specified type.
      Type Parameters:
      T - the iteration type.
      Parameters:
      container - the containing annotation.
      type - the type.
      ambiguous - If set to false, resulting iterator will be unambiguous.
      strict - Controls if annotations that overlap to the right are considered in or out.
      Returns:
      A sub-iterator.
      See Also:
    • getType

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

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

      public static <T extends TOP> List<T> select(FSArray array, Class<T> type)
      Convenience method select all feature structure from the given type from an array.
      Type Parameters:
      T - the JCas type.
      Parameters:
      array - a feature structure array.
      type - the type.
      Returns:
      A collection of the selected type.
      See Also:
    • select

      public static <T extends TOP> List<T> select(FSList list, Class<T> type)
      Convenience method select all feature structure from the given type from a list.
      Type Parameters:
      T - the JCas type.
      Parameters:
      list - a feature structure list.
      type - the type.
      Returns:
      A collection of the selected type.
      See Also:
    • select

      public static <T extends TOP> Collection<T> select(JCas jCas, Class<T> type)
      Convenience method to iterator over all features structures of a given type.
      Type Parameters:
      T - the iteration type.
      Parameters:
      jCas - the JCas containing the type system.
      type - the type.
      Returns:
      A collection of the selected type.
      See Also:
    • selectAt

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

      public static <T extends Annotation> T selectSingleAt(JCas jCas, Class<T> type, int aBegin, int aEnd)
      Get a single annotations of the given type at the specified offsets.
      Type Parameters:
      T - the type of annotations to fetch.
      Parameters:
      jCas - the CAS containing the annotations.
      type - the type of annotations to fetch.
      aBegin - the begin offset.
      aEnd - the end offset.
      Returns:
      the annotation at the specified offsets.
    • selectAll

      public static Collection<TOP> selectAll(JCas jCas)
      Convenience method to iterator over all features structures.
      Parameters:
      jCas - the JCas containing the type system. the type.
      Returns:
      A collection of the selected type.
      See Also:
    • selectBetween

      public static <T extends Annotation> List<T> selectBetween(Class<T> 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.
      Type Parameters:
      T - the JCas type.
      Parameters:
      type - a UIMA type.
      ann1 - the first boundary annotation.
      ann2 - the second boundary annotation.
      Returns:
      a return value.
      See Also:
    • selectBetween

      public static <T extends Annotation> List<T> selectBetween(JCas jCas, Class<T> 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.
      Type Parameters:
      T - the JCas type.
      Parameters:
      jCas - a JCas containing the annotation.
      type - a UIMA type.
      ann1 - the first boundary annotation.
      ann2 - the second boundary annotation.
      Returns:
      a return value.
      See Also:
    • selectCovered

      public static <T extends AnnotationFS> List<T> selectCovered(Class<T> type, AnnotationFS coveringAnnotation)
      Get a list of annotations of the given annotation type constrained by a 'covering' annotation. Iterates over all annotations of the given type to find the covered annotations. Does not use subiterators.

      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.

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

      public static <T extends Annotation> List<T> selectCovered(JCas jCas, Class<T> type, AnnotationFS coveringAnnotation)
      Get a list of annotations of the given annotation type constrained by a 'covering' annotation. Iterates over all annotations of the given type to find the covered annotations. Does not use subiterators.

      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.

      Type Parameters:
      T - the JCas type.
      Parameters:
      jCas - a JCas containing the annotation.
      type - a UIMA type.
      coveringAnnotation - the covering annotation.
      Returns:
      a return value.
      See Also:
    • selectCovered

      public static <T extends Annotation> List<T> selectCovered(JCas jCas, Class<T> type, int begin, int end)
      Get a list of annotations of the given annotation type constrained by a 'covering' annotation. Iterates over all annotations of the given type to find the covered annotations. Does not use subiterators.

      Note: this is significantly slower than using selectCovered(JCas, Class, AnnotationFS). It is possible to use selectCovered(jCas, cls, 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.

      Type Parameters:
      T - the JCas type.
      Parameters:
      jCas - a JCas containing the annotation.
      type - a UIMA type.
      begin - begin offset.
      end - end offset.
      Returns:
      a return value.
      See Also:
    • selectCovering

      public static <T extends Annotation> List<T> selectCovering(Class<T> 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(JCas, Class, Class) or a ContainmentIndex.

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

      public static <T extends Annotation> List<T> selectCovering(JCas jCas, Class<T> 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(JCas, Class, Class) or a ContainmentIndex.

      Type Parameters:
      T - the JCas type.
      Parameters:
      jCas - a CAS.
      type - a UIMA type.
      coveredAnnotation - the covered annotation.
      Returns:
      a return value.
      See Also:
    • selectCovering

      public static <T extends Annotation> List<T> selectCovering(JCas jCas, Class<T> 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(JCas, Class, Class) or a ContainmentIndex.

      Type Parameters:
      T - the JCas type.
      Parameters:
      jCas - a CAS.
      type - a UIMA type.
      begin - begin offset.
      end - end offset.
      Returns:
      a return value.
      See Also:
    • indexCovering

      public static <T extends Annotation, S extends Annotation> Map<T,List<S>> indexCovering(JCas jCas, Class<? extends T> type, Class<? extends S> coveringType)
      Create an index for quickly lookup up the annotations covering a particular annotation. This is preferable to using selectCovering(JCas, Class, 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.

      Type Parameters:
      T - the covered JCAs type.
      S - the covering JCas type.
      Parameters:
      jCas - a JCas.
      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 <T extends Annotation, S extends Annotation> Map<T,List<S>> indexCovered(JCas jCas, Class<? extends T> type, Class<? extends S> coveredType)
      Create an index for quickly lookup up the annotations covered by a particular annotation. This is preferable to using selectCovered(JCas, Class, 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.

      Type Parameters:
      T - the covering JCas type.
      S - the covered JCas type.
      Parameters:
      jCas - a JCas.
      type - type to create the index for - this is used in lookups.
      coveredType - type of covered annotations.
      Returns:
      the index.
      See Also:
    • selectOverlapping

      public static <T extends Annotation> List<T> selectOverlapping(Class<T> 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 <T extends Annotation> List<T> selectOverlapping(JCas aJCas, Class<T> 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:
      aJCas - a CAS.
      aType - a UIMA type.
      aBoundaryAnnotation - the covering annotation.
      Returns:
      a list of overlapping annotations.
      See Also:
    • selectOverlapping

      public static <T extends Annotation> List<T> selectOverlapping(JCas aJCas, Class<T> aType, int aBegin, int aEnd)
      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:
      aJCas - a CAS.
      aType - a UIMA type.
      aBegin - begin offset.
      aEnd - end offset.
      Returns:
      a list of overlapping annotations.
      See Also:
    • contains

      public static boolean contains(JCas jCas, AnnotationFS coveringAnnotation, Class<? extends Annotation> type)
      Check if the given annotation contains any annotation of the given type.
      Parameters:
      jCas - a JCas containing the annotation.
      coveringAnnotation - the covering annotation.
      type - a UIMA type.
      Returns:
      if an annotation of the given type is present.
      See Also:
    • selectByIndex

      public static <T extends Annotation> T selectByIndex(JCas jCas, Class<T> cls, 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.

      It is intentional that this method only allows annotation types. The CAS indexing mechanisms are not well defined for non-annotation types. There are no reliably, built-in indexes for non-annotation types.

      Type Parameters:
      T - JCas wrapper type.
      Parameters:
      jCas - a JCas containing the annotation.
      cls - 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 <T extends TOP> T selectSingle(JCas jCas, Class<T> type)
      Get the single instance of the specified type from the JCas.
      Type Parameters:
      T - JCas wrapper type.
      Parameters:
      jCas - a JCas 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 <T extends Annotation> T selectSingleRelative(Class<T> aType, AnnotationFS annotation, int index)
      Return an annotation preceding or following of a given reference annotation.
      Type Parameters:
      T - the JCas type.
      Parameters:
      aType - 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.
      Throws:
      IndexOutOfBoundsException - if the relative index points beyond the type index bounds.
      See Also:
    • selectSingleRelative

      public static <T extends Annotation> T selectSingleRelative(JCas aJCas, Class<T> aType, 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.
      Type Parameters:
      T - the JCas type.
      Parameters:
      aJCas - a JCas.
      aType - type of the annotation to be returned.
      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 <T extends Annotation> List<T> selectPreceding(Class<T> aType, AnnotationFS annotation, int count)
      Returns the n annotations preceding the given annotation
      Type Parameters:
      T - the JCas type.
      Parameters:
      aType - a type.
      annotation - anchor annotation
      count - number of annotations to collect
      Returns:
      List of aType annotations preceding anchor annotation
      See Also:
    • selectPreceding

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

      public static <T extends Annotation> List<T> selectFollowing(Class<T> aType, AnnotationFS annotation, int count)
      Returns the n annotations following the given annotation
      Type Parameters:
      T - the JCas type.
      Parameters:
      aType - a type.
      annotation - anchor annotation
      count - number of annotations to collect
      Returns:
      List of aType annotations following anchor annotation
      See Also:
    • selectFollowing

      public static <T extends Annotation> List<T> selectFollowing(JCas aJCas, Class<T> aType, AnnotationFS annotation, int count)
      Returns the n annotations following the given annotation
      Type Parameters:
      T - the JCas type.
      Parameters:
      aJCas - a JCas.
      aType - a type.
      annotation - 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(JCas aJCas, Class<T> aType)
      Test if a JCas contains an annotation of the given type.
      Type Parameters:
      T - the annotation type.
      Parameters:
      aJCas - a JCas.
      aType - a annotation class.
      Returns:
      true if there is at least one annotation of the given type in the JCas.
    • getView

      public static JCas getView(JCas jcas, String viewName, JCas 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:
      jcas - a JCas
      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.
      Throws:
      IllegalStateException - if the JCas wrapper cannot be obtained.
    • getView

      public static JCas getView(JCas jcas, String viewName, boolean create)
      Convenience method to get the specified view or create a new view if the requested view does not exist.
      Parameters:
      jcas - a JCas
      viewName - the requested view.
      create - the view is created if it does not exist.
      Returns:
      the requested view
      Throws:
      IllegalStateException - if the JCas wrapper cannot be obtained.
    • 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.