Class CasCopier

java.lang.Object
org.apache.uima.util.CasCopier

public class CasCopier extends Object
Utility class for doing deep copies of FeatureStructures from one CAS to another. To handle cases where the source CAS has multiple references to the same FS, you can create one instance of CasCopier and use it to copy multiple FeatureStructures. The CasCopier will remember previously copied FeatureStructures, so if you later copy another FS that has a reference to a previously copied FS, it will not duplicate the multiply-referenced FS. This class makes use of CASImpl methods, but is only passed CAS objects, which may be CAS Wrappers. To make this more feasible, the implementors of CAS Wrappers need to implement the method getLowLevelCas() which should return a reference to the underlying CAS which can be successfully cast to a CASImpl. The source and target CASs must be separate CASs (that is, not two views of the same CAS), with one exception: If the CopyCasView API is being used, and the target View name is different from the source view name,
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private class 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final int
     
    private static final int
     
    private static final int
     
    private static final int
     
    private static final int
     
    private static final int
     
    private IntVector
    feature structures whose slots need copying are put on this list, together with their source as pairs of ints.
    private boolean
    true if the copyCasView api was used, and the target view name corresponding to the source view name is changed
    private static final int
     
    private static final int
     
    private final boolean
     
    private final Feature
     
    private final int
     
    private final Int2IntHashMap
    key is source FS, value is target FS Target not set for DocumentAnnotation or SofaFSs Target not set if lenient specified and src type isn't in target
    private final CAS
     
    private final CASImpl
     
    private final CAS
     
    private final CASImpl
     
    private int
     
    private CASImpl
     
    private final int
     
    private final TypeImpl
     
    private final int
     
    private final TypeSystemImpl
     
    private String
     
    private CASImpl
     
    private final TypeSystemImpl
     
    private String
     
    private final CasCopier.TypeInfo[]
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    CasCopier(CAS aSrcCas, CAS aDestCas)
    Creates a new CasCopier that can be used to copy FeatureStructures from one CAS to another.
    CasCopier(CAS aSrcCas, CAS aDestCas, boolean lenient)
    Creates a new CasCopier that can be used to copy FeatureStructures from one CAS to another.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    alreadyCopied(int aFS)
    Note: if lenient is in effect, this method will return false for FSs which are not copied because the target doesn't have that type.
    boolean
    Note: if lenient is in effect, this method will return false for FSs which are not copied because the target doesn't have that type.
    private boolean
    Change from https://issues.apache.org/jira/browse/UIMA-3112 : requires that the CAS returned from the getLowLevelCAS() be castable to CASImpl
    private int
    copyArray(int srcFS)
     
    static void
    copyCas(CAS aSrcCas, CAS aDestCas, boolean aCopySofa)
    Does a complete deep copy of one CAS into another CAS.
    static void
    copyCas(CAS aSrcCas, CAS aDestCas, boolean aCopySofa, boolean lenient)
    Does a complete deep copy of one CAS into another CAS.
    void
    copyCasView(String aSrcCasViewName, boolean aCopySofa)
    Does a deep copy of the contents of one CAS View into another CAS's same-named-view If the destination view already exists in the destination CAS, then it will be the target of the copy.
    void
    copyCasView(String aSrcCasViewName, CAS aTgtCasView, boolean aCopySofa)
    Does a deep copy of the contents of one CAS View into another CAS view, with a possibly different name.
    void
    copyCasView(CAS aSrcCasView, boolean aCopySofa)
    Does a deep copy of the contents of one CAS View into another CAS's same-named-view If the destination view already exists in the destination CAS, then it will be the target of the copy.
    void
    copyCasView(CAS aSrcCasView, String aTgtCasViewName, boolean aCopySofa)
    Does a deep copy of the contents of one CAS View into another CAS view, with a possibly different name.
    void
    copyCasView(CAS aSrcCasView, CAS aTgtCasView, boolean aCopySofa)
    Does a deep copy of the contents of one CAS View into another CAS view, with a possibly different name.
    private void
    copyCasViewDifferentCASs(CAS aSrcCasView, CAS aTgtCasView, boolean aCopySofa)
     
    private void
    copyFeatures(int srcFS, int tgtFS)
    Copy feature values from one FS to another.
    Copy 1 feature structure from the originalSrcCas to a new Cas.
    private int
    copyFs2(int aFS)
    Copy one FS from the src CAS to the tgt CAS View context: The caller must set the srcCasViewImpl and the tgtCasViewImpl
     
    private int
    copyFsInner(int aFS)
    Copies a FS from the source CAS to the destination CAS.
    private String
    There are two cases for getting target sofa name from the source one, depending on whether or not the API which allows specifying a different target view name for the source view name, is in use.
    private static CASImpl
    getOrCreateView(CAS aCas, String aViewName)
    Gets the named view; if the view doesn't exist it will be created.
    getTypeInfo(int srcTypeCode)
     
    private boolean
    Determines whether the given FS is the DocumentAnnotation in the srcCasView.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • FRC_SKIP

      private static final int FRC_SKIP
      See Also:
    • FRC_STRING

      private static final int FRC_STRING
      See Also:
    • FRC_LONG

      private static final int FRC_LONG
      See Also:
    • FRC_DOUBLE

      private static final int FRC_DOUBLE
      See Also:
    • FRC_INT_LIKE

      private static final int FRC_INT_LIKE
      See Also:
    • FRC_REF

      private static final int FRC_REF
      See Also:
    • K_SRC_FEAT_OFFSET

      private static final int K_SRC_FEAT_OFFSET
      See Also:
    • K_TGT_FEAT_CODE

      private static final int K_TGT_FEAT_CODE
      See Also:
    • tInfoArray

      private final CasCopier.TypeInfo[] tInfoArray
    • originalSrcCas

      private final CAS originalSrcCas
    • originalTgtCas

      private final CAS originalTgtCas
    • originalSrcCasImpl

      private final CASImpl originalSrcCasImpl
    • originalTgtCasImpl

      private final CASImpl originalTgtCasImpl
    • srcCasViewImpl

      private CASImpl srcCasViewImpl
    • tgtCasViewImpl

      private CASImpl tgtCasViewImpl
    • srcViewName

      private String srcViewName
    • tgtViewName

      private String tgtViewName
    • srcTsi

      private final TypeSystemImpl srcTsi
    • tgtTsi

      private final TypeSystemImpl tgtTsi
    • srcStringType

      private final TypeImpl srcStringType
    • srcStringTypeCode

      private final int srcStringTypeCode
    • isChangeViewName

      private boolean isChangeViewName
      true if the copyCasView api was used, and the target view name corresponding to the source view name is changed
    • srcCasDocumentAnnotation

      private int srcCasDocumentAnnotation
    • mDestSofaFeature

      private final Feature mDestSofaFeature
    • mDestSofaFeatureCode

      private final int mDestSofaFeatureCode
    • srcSofaTypeCode

      private final int srcSofaTypeCode
    • lenient

      private final boolean lenient
    • mFsMap

      private final Int2IntHashMap mFsMap
      key is source FS, value is target FS Target not set for DocumentAnnotation or SofaFSs Target not set if lenient specified and src type isn't in target
    • fsToDo

      private IntVector fsToDo
      feature structures whose slots need copying are put on this list, together with their source as pairs of ints. First int is the target Cas ref Second int is the source Cas ref List is operated as a stack, from the end, for efficiency
  • Constructor Details

    • CasCopier

      public CasCopier(CAS aSrcCas, CAS aDestCas)
      Creates a new CasCopier that can be used to copy FeatureStructures from one CAS to another. Note that if you are merging data from multiple CASes, you must create a new CasCopier for each source CAS. Note: If the feature structure and/or feature is not defined in the type system of the destination CAS, the copy will fail (in other words, the lenient setting is false, by default).
      Parameters:
      aSrcCas - the CAS to copy from.
      aDestCas - the CAS to copy into.
    • CasCopier

      public CasCopier(CAS aSrcCas, CAS aDestCas, boolean lenient)
      Creates a new CasCopier that can be used to copy FeatureStructures from one CAS to another. Note that if you are merging data from multiple CASes, you must create a new CasCopier for each source CAS. This version of the constructor supports a "lenient copy" option. When set, the CAS copy function will ignore (not attempt to copy) FSs and features not defined in the type system of the destination CAS, rather than throwing an exception.
      Parameters:
      aSrcCas - the CAS to copy from.
      aDestCas - the CAS to copy into.
      lenient - ignore FSs and features not defined in the type system of the destination CAS
  • Method Details

    • copyCas

      public static void copyCas(CAS aSrcCas, CAS aDestCas, boolean aCopySofa)
      Does a complete deep copy of one CAS into another CAS. The contents of each view in the source CAS will be copied to the same-named view in the destination CAS. If the view does not already exist it will be created. All FeatureStructures that are indexed in a view in the source CAS will become indexed in the same-named view in the destination CAS. Note: If the feature structure and/or feature is not defined in the type system of the destination CAS, the copy will fail (in other words, the lenient setting is false, by default).
      Parameters:
      aSrcCas - the CAS to copy from
      aDestCas - the CAS to copy to
      aCopySofa - if true, the sofa data and mimeType of each view will be copied. If false they will not.
    • copyCas

      public static void copyCas(CAS aSrcCas, CAS aDestCas, boolean aCopySofa, boolean lenient)
      Does a complete deep copy of one CAS into another CAS. The contents of each view in the source CAS will be copied to the same-named view in the destination CAS. If the view does not already exist it will be created. All FeatureStructures that are indexed in a view in the source CAS will become indexed in the same-named view in the destination CAS. This version of the method supports a "lenient copy" option. When set, the CAS copy function will ignore (not attempt to copy) FSs and features not defined in the type system of the destination CAS, rather than throwing an exception.
      Parameters:
      aSrcCas - the CAS to copy from
      aDestCas - the CAS to copy to; must be a completely different CAS than the source (that is, not an alternative "view" of the source)
      aCopySofa - if true, the sofa data and mimeType of each view will be copied. If false they will not.
      lenient - ignore FSs and features not defined in the type system of the destination CAS
    • copyCasView

      public void copyCasView(CAS aSrcCasView, boolean aCopySofa)
      Does a deep copy of the contents of one CAS View into another CAS's same-named-view If the destination view already exists in the destination CAS, then it will be the target of the copy. Otherwise, a new view will be created with that name and will become the target of the copy. All FeatureStructures (except for those dropped because the target type system doesn't have the needed type) that are indexed in the source CAS view will become indexed in the target view. Cross-view references may result in creating additional views in the destination CAS; for these views, any Sofa data in the source is *not* copied.
      Parameters:
      aSrcCasView - the CAS to copy from. This must be a view in the src Cas set by the constructor
      aCopySofa - if true, the sofa data and mimeType will be copied. If false they will not.
    • copyCasView

      public void copyCasView(String aSrcCasViewName, boolean aCopySofa)
      Does a deep copy of the contents of one CAS View into another CAS's same-named-view If the destination view already exists in the destination CAS, then it will be the target of the copy. Otherwise, a new view will be created with that name and will become the target of the copy. All FeatureStructures (except for those dropped because the target type system doesn't have the needed type) that are indexed in the source CAS view will become indexed in the target view. Cross-view references may result in creating additional views in the destination CAS; for these views, any Sofa data in the source is *not* copied. Any views created because of cross-view references will have the same view name as in the source.
      Parameters:
      aSrcCasViewName - the name of the view in the source CAS to copy from
      aCopySofa - if true, the sofa data and mimeType will be copied. If false they will not.
    • copyCasView

      public void copyCasView(CAS aSrcCasView, String aTgtCasViewName, boolean aCopySofa)
      Does a deep copy of the contents of one CAS View into another CAS view, with a possibly different name. If the destination view already exists in the destination CAS, then it will be the target of the copy. Otherwise, a new view will be created with that name and will become the target of the copy. All FeatureStructures (except for those dropped because the target type system doesn't have the needed type) that are indexed in the source CAS view will become indexed in the target view. Cross-view references may result in creating additional views in the destination CAS; for these views, any Sofa data in the source is *not* copied. Any views created because of cross-view references will have the same view name as in the source.
      Parameters:
      aSrcCasView - The view in the source to copy from
      aTgtCasViewName - The name of the view in the destination CAS to copy into
      aCopySofa - if true, the sofa data and mimeType will be copied. If false they will not.
    • copyCasView

      public void copyCasView(String aSrcCasViewName, CAS aTgtCasView, boolean aCopySofa)
      Does a deep copy of the contents of one CAS View into another CAS view, with a possibly different name. All FeatureStructures (except for those dropped because the target type system doesn't have the needed type) that are indexed in the source CAS view will become indexed in the target view. Cross-view references may result in creating additional views in the destination CAS; for these views, any Sofa data in the source is *not* copied. Any views created because of cross-view references will have the same view name as in the source.
      Parameters:
      aSrcCasViewName - The name of the view in the Source CAS to copy from
      aTgtCasView - The view in the destination CAS to copy into
      aCopySofa - if true, the sofa data and mimeType will be copied. If false they will not.
    • copyCasViewDifferentCASs

      private void copyCasViewDifferentCASs(CAS aSrcCasView, CAS aTgtCasView, boolean aCopySofa)
    • copyCasView

      public void copyCasView(CAS aSrcCasView, CAS aTgtCasView, boolean aCopySofa)
      Does a deep copy of the contents of one CAS View into another CAS view, with a possibly different name. All FeatureStructures (except for those dropped because the target type system doesn't have the needed type) that are indexed in the source CAS view will become indexed in the target view. Cross-view references may result in creating additional views in the destination CAS; for these views, any Sofa data in the source is *not* copied. Any views created because of cross-view references will have the same view name as in the source. If the source and target views are both views of the same CAS, then Feature Structures in the view are effectively "cloned", with the following change: Subtypes of AnnotationBase in the source whose sofaRef is for the source View are cloned with their sofaRefs changed to the new targetView.
      Parameters:
      aSrcCasView - the CAS view to copy from. This must be a view of the srcCas set in the constructor
      aTgtCasView - the CAS view to copy to. This must be a view of the tgtCas set in the constructor
      aCopySofa - if true, the sofa data and mimeType will be copied. If false they will not. If true and the sofa data is already set in the target, will throw CASRuntimeException
    • copyFs

      public FeatureStructure copyFs(FeatureStructure aFS)
      Copy 1 feature structure from the originalSrcCas to a new Cas. No indexing of the new FS is done. If the FS has been copied previously (using this CasCopier instance) the same identical copy will be returned rather than making another copy. View handling: ignores the view of the targetCas
      Parameters:
      aFS - the Feature Structure to copy
      Returns:
      a deep copy of the Feature Structure - any referred to FSs will also be copied.
    • copyFs2

      private int copyFs2(int aFS)
      Copy one FS from the src CAS to the tgt CAS View context: The caller must set the srcCasViewImpl and the tgtCasViewImpl
      Parameters:
      aFS - a Feature Structure reference in the originalSrcCas
      Returns:
      a Feature Structure reference in the originalTgtCas
    • copyFs2Fs

      private FeatureStructure copyFs2Fs(FeatureStructure fs)
    • copyFsInner

      private int copyFsInner(int aFS)
      Copies a FS from the source CAS to the destination CAS. Also copies any referenced FSs, except that previously copied FS will not be copied again.
      Parameters:
      aFS - the FS to copy. Must be contained within the source CAS.
      Returns:
      the copy of aFS in the target CAS.
    • getDestSofaId

      private String getDestSofaId(String id)
      There are two cases for getting target sofa name from the source one, depending on whether or not the API which allows specifying a different target view name for the source view name, is in use. If so, then whenever the source sofa name is that src view name, replace it in the target with the specified different target view name.
      Parameters:
      id -
      Returns:
      id unless the id matches the source view name, and that name is being changed
    • getTypeInfo

      private CasCopier.TypeInfo getTypeInfo(int srcTypeCode)
    • copyFeatures

      private void copyFeatures(int srcFS, int tgtFS)
      Copy feature values from one FS to another. For reference-valued features, this does a deep copy.
      Parameters:
      srcFS - FeatureStructure to copy from
      tgtFS - FeatureStructure to copy to, which must not be in the index (index corruption checks skipped)
    • alreadyCopied

      public boolean alreadyCopied(FeatureStructure aFS)
      Note: if lenient is in effect, this method will return false for FSs which are not copied because the target doesn't have that type. It also returns false for sofa FSs and the documentAnnotation FS.
      Parameters:
      aFS - a feature structure
      Returns:
      true if the given FS has already been copied using this CasCopier.
    • alreadyCopied

      public boolean alreadyCopied(int aFS)
      Note: if lenient is in effect, this method will return false for FSs which are not copied because the target doesn't have that type. It also returns false for sofa FSs and the documentAnnotation FS.
      Parameters:
      aFS - a feature structure
      Returns:
      true if the given FS has already been copied using this CasCopier.
    • copyArray

      private int copyArray(int srcFS)
      Parameters:
      arrayFS -
      Returns:
      a copy of the array
    • getOrCreateView

      private static CASImpl getOrCreateView(CAS aCas, String aViewName)
      Gets the named view; if the view doesn't exist it will be created.
    • isDocumentAnnotation

      private boolean isDocumentAnnotation(int aFS)
      Determines whether the given FS is the DocumentAnnotation in the srcCasView. This is more than just a type check; we actually check if it is the one "special" DocumentAnnotation that CAS.getDocumentAnnotation() would return.
    • casViewsInSameCas

      private boolean casViewsInSameCas(CAS c1, CAS c2)
      Change from https://issues.apache.org/jira/browse/UIMA-3112 : requires that the CAS returned from the getLowLevelCAS() be castable to CASImpl
      Parameters:
      c1 - -
      c2 - -
      Returns:
      true if both views are in the same CAS (e.g., they have the same base CAS)