Interface CAS

  • All Superinterfaces:
    AbstractCas
    All Known Implementing Classes:
    CASImpl

    public interface CAS
    extends AbstractCas
    Object-oriented CAS (Common Analysis System) API.

    A CAS object provides the starting point for working with the CAS. It provides access to the type system, to indexes, iterators and filters (constraints). It also lets you create new annotations and other data structures. You can create a CAS object using static methods on the class CasCreationUtils.

    The CAS object is also the container that manages multiple Subjects of Analysis or Sofas. A Sofa represents some form of an unstructured artifact that is processed in a UIMA pipeline. The Java string called the "DocumentText" used in a UIMA text processing pipeline is an example of a Sofa. A Sofa can be analyzed independently using the standard UIMA programming model or analyzed together with other Sofas utilizing the Sofa programming model extensions.

    A Sofa is implemented as a built-in CAS type uima.cas.Sofa. Use CAS.createSofa() to instantiate a Sofa feature structure. The SofaFS class provides methods to set and get the features of a SofaFS. Although Sofas are implemented as standard feature structures, generic CAS APIs must never be used to create Sofas or set their features.

    Use getView(String) or getView(SofaFS) to obtain a view of a particular Sofa in the CAS. This view will provide access to the Sofa data (for example the document text) as well as the index repository, which contains metadata (annotations and other feature structures) about that Sofa.

    Use getTypeSystem() to access the type system. With a TypeSystem object, you can access the Type and Feature objects for the CAS built-in types. Note that this interface also provides constants for the names of the built-in types and features.

    • Method Detail

      • getCurrentView

        CAS getCurrentView()
        Get the Cas view that the current component should use. This should only be used by single-view components.
        Returns:
        the Cas view specified for the current component by Sofa mapping. Defaults to _InitialView if there is no Sofa mapping.
      • getSofa

        @Deprecated
        SofaFS getSofa​(SofaID sofaID)
        Deprecated.
        As of v2.0, use {#getView(String)}. From the view you can access the Sofa data, or call getSofa() if you truly need to access the SofaFS object.
        Get sofaFS for given Subject of Analysis ID.
        Parameters:
        sofaID - -
        Returns:
        The sofaFS.
      • getSofa

        SofaFS getSofa()
        Get the Sofa feature structure associated with this CAS view.
        Returns:
        The SofaFS associated with this CAS view.
      • createView

        CAS createView​(String localViewName)
        Create a view and its underlying Sofa (subject of analysis). The view provides access to the Sofa data and the index repository that contains metadata (annotations and other feature structures) pertaining to that Sofa.

        This method creates the underlying Sofa feature structure, but does not set the Sofa data. Setting ths Sofa data must be done by calling setSofaDataArray(FeatureStructure, String), setSofaDataString(String, String) or setSofaDataURI(String, String) on the CAS view returned by this method.

        Parameters:
        localViewName - the local name, before any sofa name mapping is done, for this view (note: this is the same as the associated Sofa name).
        Returns:
        The view corresponding to this local name.
        Throws:
        CASRuntimeException - if a View with this name already exists in this CAS
      • getJCas

        JCas getJCas​(SofaFS aSofa)
              throws CASException
        Create a JCas view for a Sofa. Note: as of UIMA v2.0, can be replaced with getView(sofaFS).getJCas().
        Parameters:
        aSofa - a Sofa feature structure in this CAS.
        Returns:
        The JCas view for the given Sofa.
        Throws:
        CASException - -
      • getJCas

        @Deprecated
        JCas getJCas​(SofaID aSofaID)
              throws CASException
        Deprecated.
        As of v2.0, use getView(String) followed by getJCas().
        Create a JCas view for a Sofa. Note: this is provided for convenience. It is equivalent to getView(aSofaID).getJCas().
        Parameters:
        aSofaID - the ID of a Sofa defined in this CAS
        Returns:
        The view for the Sofa with ID aSofaID.
        Throws:
        CASException - if no Sofa with the given ID exists in this CAS
      • getView

        CAS getView​(String localViewName)
        Get the view for a Sofa (subject of analysis). The view provides access to the Sofa data and the index repository that contains metadata (annotations and other feature structures) pertaining to that Sofa.
        Parameters:
        localViewName - the local name, before any sofa name mapping is done, for this view (note: this is the same as the associated Sofa name).
        Returns:
        The view corresponding to this local name.
        Throws:
        CASRuntimeException - if no View with this name exists in this CAS
      • getView

        CAS getView​(SofaFS aSofa)
        Get the view for a Sofa (subject of analysis). The view provides access to the Sofa data and the index repository that contains metadata (annotations and other feature structures) pertaining to that Sofa.
        Parameters:
        aSofa - a Sofa feature structure in the CAS
        Returns:
        The view for the given Sofa
      • getLowLevelCAS

        LowLevelCAS getLowLevelCAS()
        Get an instance of the low-level CAS. Low-level and regular CAS can be used in parallel, all data is always contained in both.

        Note: This is for internal use.

        Returns:
        A low-level CAS.
        See Also:
        LowLevelCAS
      • getAnnotationType

        Type getAnnotationType()
        Get the type object for the annotation type.
        Returns:
        The annotation type.
      • getBeginFeature

        Feature getBeginFeature()
        Get the feature object for the annotation begin feature.
        Returns:
        The annotation begin feature.
      • getEndFeature

        Feature getEndFeature()
        Get the feature object for the annotation end feature.
        Returns:
        The annotation end feature.
      • getAnnotationIndex

        <T extends AnnotationFSAnnotationIndex<T> getAnnotationIndex()
        Get the standard annotation index.
        Type Parameters:
        T - either Annotation (if JCas is in use) or AnnotationImpl
        Returns:
        The standard annotation index.
      • getAnnotationIndex

        <T extends AnnotationFSAnnotationIndex<T> getAnnotationIndex​(Type type)
                                                                throws CASRuntimeException
        Get the standard annotation index restricted to a specific annotation type.
        Type Parameters:
        T - the topmost Java class corresponding to the type
        Parameters:
        type - The annotation type the index is restricted to.
        Returns:
        The standard annotation index, restricted to type.
        Throws:
        CASRuntimeException - When type is not an annotation type.
      • createAnnotation

        <T extends AnnotationFSAnnotationFS createAnnotation​(Type type,
                                                               int begin,
                                                               int end)
        Create a new annotation. Note that you still need to insert the annotation into the index repository yourself.
        Type Parameters:
        T - the Java class corresponding to the type
        Parameters:
        type - The type of the annotation.
        begin - The start of the annotation.
        end - The end of the annotation.
        Returns:
        A new annotation object.
      • getDocumentAnnotation

        <T extends AnnotationFS> T getDocumentAnnotation()
        Get the Document Annotation. The Document Annotation has a string-valued feature called "language" where the document language is specified.
        Type Parameters:
        T - the Java class for the document annotation. Could be the JCas cover class or FeatureStructure
        Returns:
        The document annotation, or null if there is none. The return value is the JCas cover class or the plain Java cover class for FeatureStructures if JCas is not in use.
      • setCurrentComponentInfo

        void setCurrentComponentInfo​(ComponentInfo info)
        Informs the CAS of relevant information about the component that is currently processing it. This is called by the framework automatically; users do not need to call it.
        Parameters:
        info - information about the component that is currently processing this CAS.
      • getTypeSystem

        TypeSystem getTypeSystem()
                          throws CASRuntimeException
        Return the type system of this CAS instance.
        Returns:
        The type system, or null if none is available.
        Throws:
        CASRuntimeException - If the type system has not been committed.
      • createSofa

        @Deprecated
        SofaFS createSofa​(SofaID sofaID,
                          String mimeType)
        Deprecated.
        As of v2.0, use createView(String) instead.
        Create a Subject of Analysis. The new sofaFS is automatically added to the SofaIndex.
        Parameters:
        sofaID - -
        mimeType - -
        Returns:
        The sofaFS.
      • getSofaIterator

        FSIterator<SofaFS> getSofaIterator()
        Get iterator for all SofaFS in the CAS.
        Returns:
        an iterator over SofaFS.
      • createFilteredIterator

        <T extends FeatureStructureFSIterator<T> createFilteredIterator​(FSIterator<T> it,
                                                                          FSMatchConstraint cons)
        Create an iterator over structures satisfying a given constraint. Constraints are described in the javadocs for ConstraintFactory and related classes.
        Type Parameters:
        T - - the type of the Feature Structure
        Parameters:
        it - The input iterator.
        cons - The constraint specifying what structures should be returned.
        Returns:
        An iterator over FSs.
      • getConstraintFactory

        ConstraintFactory getConstraintFactory()
        Get a constraint factory. A constraint factory is a simple way of creating FSMatchConstraints.
        Returns:
        A constraint factory to create new FS constraints.
      • createFeaturePath

        FeaturePath createFeaturePath()
        Create a feature path. This is mainly useful for creating FSMatchConstraints.
        Returns:
        A new, empty feature path.
      • getIndexRepository

        FSIndexRepository getIndexRepository()
        Get the index repository.
        Returns:
        The index repository, or null if none is available.
      • fs2listIterator

        <T extends FeatureStructureListIterator<T> fs2listIterator​(FSIterator<T> it)
        Wrap a standard Java ListIterator around an FSListIterator. Use if you feel more comfortable with java style iterators.
        Type Parameters:
        T - The type of FeatureStructure
        Parameters:
        it - The FSListIterator to be wrapped.
        Returns:
        An equivalent ListIterator.
      • reset

        void reset()
            throws CASAdminException
        Reset the CAS, emptying it of all content. Feature structures and iterators will no longer be valid. Note: this method may only be called from an application. Calling it from an annotator will trigger a runtime exception.
        Throws:
        CASRuntimeException - When called out of sequence.
        CASAdminException
        See Also:
        CASMgr
      • getViewName

        String getViewName()
        Get the view name. The view name is the same as the name of the view's Sofa, retrieved by getSofa().getSofaID(), except for the initial View before its Sofa has been created.
        Returns:
        The name of the view
      • size

        int size()
        Estimate the memory consumption of this CAS instance (in bytes).
        Returns:
        The estimated memory used by this CAS instance.
      • createFeatureValuePath

        FeatureValuePath createFeatureValuePath​(String featureValuePath)
                                         throws CASRuntimeException
        Create a feature-value path from a string.
        Parameters:
        featureValuePath - String representation of the feature-value path.
        Returns:
        Feature-value path object.
        Throws:
        CASRuntimeException - If the input string is not well-formed.
      • setDocumentText

        void setDocumentText​(String text)
                      throws CASRuntimeException
        Set the document text. Once set, Sofa data is immutable, and cannot be set again until the CAS has been reset.
        Parameters:
        text - The text to be analyzed.
        Throws:
        CASRuntimeException - If the Sofa data has already been set.
      • setSofaDataString

        void setSofaDataString​(String text,
                               String mimetype)
                        throws CASRuntimeException
        Set the document text. Once set, Sofa data is immutable, and cannot be set again until the CAS has been reset.
        Parameters:
        text - The text to be analyzed.
        mimetype - The mime type of the data
        Throws:
        CASRuntimeException - If the Sofa data has already been set.
      • getDocumentText

        String getDocumentText()
        Get the document text.
        Returns:
        The text being analyzed, or null if not set.
      • getSofaDataString

        String getSofaDataString()
        Get the Sofa Data String (a.k.a. the document text).
        Returns:
        The Sofa data string, or null if not set.
      • setDocumentLanguage

        void setDocumentLanguage​(String languageCode)
                          throws CASRuntimeException
        Sets the language for this document. This value sets the language feature of the special instance of DocumentAnnotation associated with this CAS.
        Parameters:
        languageCode - -
        Throws:
        CASRuntimeException - passthru
      • getDocumentLanguage

        String getDocumentLanguage()
        Gets the language code for this document from the language feature of the special instance of the DocumentationAnnotation associated with this CAS.
        Returns:
        language identifier
      • setSofaDataArray

        void setSofaDataArray​(FeatureStructure array,
                              String mime)
                       throws CASRuntimeException
        Set the Sofa data as an ArrayFS. Once set, Sofa data is immutable, and cannot be set again until the CAS has been reset.
        Parameters:
        array - The ArrayFS to be analyzed.
        mime - The mime type of the data
        Throws:
        CASRuntimeException - If the Sofa data has already been set.
      • getSofaDataArray

        FeatureStructure getSofaDataArray()
        Get the Sofa data array.
        Returns:
        The Sofa Data being analyzed, or null if not set.
      • setSofaDataURI

        void setSofaDataURI​(String uri,
                            String mime)
                     throws CASRuntimeException
        Set the Sofa data as a URI. Once set, Sofa data is immutable, and cannot be set again until the CAS has been reset.
        Parameters:
        uri - The URI of the data to be analyzed.
        mime - The mime type of the data
        Throws:
        CASRuntimeException - If the Sofa data has already been set.
      • getSofaDataURI

        String getSofaDataURI()
        Get the Sofa data array.
        Returns:
        The Sofa URI being analyzed, or null if not set.
      • getSofaDataStream

        InputStream getSofaDataStream()
        Get the Sofa data as a byte stream.
        Returns:
        A stream handle to the Sofa Data, or null if not set.
      • getSofaMimeType

        String getSofaMimeType()
        Get the mime type of the Sofa data being analyzed.
        Returns:
        the mime type of the Sofa
      • addFsToIndexes

        void addFsToIndexes​(FeatureStructure fs)
        Add a feature structure to all appropriate indexes in the repository associated with this CAS View. If no indexes exist for the type of FS that you are adding, then a bag (unsorted) index will be automatically created.

        Important: after you have called addFsToIndexes(...) on a FS, do not change the values of any features used for indexing. If you do, the index will become corrupted and may be unusable. If you need to change an index feature value, first call removeFsFromIndexes(...) on the FS, change the feature values, then call addFsToIndexes(...) again.

        Parameters:
        fs - The Feature Structure to be added.
        Throws:
        NullPointerException - If the fs parameter is null.
      • removeFsFromIndexes

        void removeFsFromIndexes​(FeatureStructure fs)
        Remove a feature structure from all indexes in the repository associated with this CAS View. The remove operation removes the exact fs from the indexes, unlike operations such as moveTo which use the fs argument as a template. It is not an error if the FS is not present in the indexes.
        Parameters:
        fs - The Feature Structure to be removed.
        Throws:
        NullPointerException - If the fs parameter is null.
      • getViewIterator

        Iterator<CAS> getViewIterator()
        Get iterator over all views in this CAS. Each view provides access to Sofa data and the index repository that contains metadata (annotations and other feature structures) pertaining to that Sofa.
        Returns:
        an iterator which returns all views. Each object returned by the iterator is of type CAS.
      • getViewIterator

        Iterator<CAS> getViewIterator​(String localViewNamePrefix)
        Get iterator over all views with the given name prefix. Each view provides access to Sofa data and the index repository that contains metadata (annotations and other feature structures) pertaining to that Sofa.

        When passed the prefix namePrefix, the iterator will return all views who name is either exactly equal to namePrefix or is of the form namePrefix.suffix, where suffix can be any String.

        Parameters:
        localViewNamePrefix - the local name prefix, before any sofa name mapping is done, for this view (note: this is the same as the associated Sofa name prefix).
        Returns:
        an iterator which returns all views with the given name prefix. Each object returned by the iterator is of type CAS.
      • createMarker

        Marker createMarker()
        Sets a mark and returns the marker object set with the current mark which can be used to query when certain FSs were created. This can then be used to identify FSs as added before or after the mark was set and to identify FSs modified after the mark is set. Note: this method may only be called from an application. Calling it from an annotator will trigger a runtime exception.
        Returns:
        a marker object.
      • protectIndexes

        AutoCloseable protectIndexes()
        Call this method to set up a region, ended by a AutoCloseable.close() call on the returned object, You can use this or the protectIndexes(Runnable) method to protected the indexes.

        This approach allows arbitrary code between the protectIndexes and the associated close method.

        The close method is best done in a finally block, or using the try-with-resources statement in Java 8.

        Returns:
        an object used to record things that need adding back
      • protectIndexes

        void protectIndexes​(Runnable runnable)
        Runs the code in the runnable inside a protection block, where any modifications to features done while in this block will be done in a way to protect any indexes which otherwise might become corrupted by the update action; the protection is achieved by temporarily removing the FS (if it is in the indexes), before the update happens. At the end of the block, affected indexes have any removed-under-the-covers FSs added back.
        Parameters:
        runnable - code to execute while protecting the indexes.