Package org.apache.uima.util
Class CasCopier
java.lang.Object
org.apache.uima.util.CasCopier
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 view names for FSs are the same except if:
- The constructor of the CAS Copier instance specifies two different view names, or
- The copyCasView method(s) specify two different view names.
In both these cases, the FSs copied are between different views.
Feature Structures belonging to one view will be belonging to the other view in the copy.
Exception: Feature Structures which are subtypes of AnnotationBase belong to the view
associated with the AnnotationBase's sofa reference.
The source and target CASs must be separate CASs (that is, not two views of the same CAS),
unless the target View name is different from the source view name.
The copied FSs have their associated CAS references set to a corresponding view in the target.
The corresponding view is the view in the target with the same view name as the source view
(perhaps after sofa mapping specified by the target CAS's current component sofa mapping info),
except for the copyCasView calls, where the target view is specified explicitly.
But if the FS being copied is a subtype of AnnotationBase, then the corresponding view for the
copy of the sofa reference (if not null) is used.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate String
private CASImpl
Deferred calls to copy Features of a FSprivate boolean
true if the copyCasView api was used, and the target view name corresponding to the source view name is changedprivate final boolean
private final boolean
key is source FS, value is target FS Target not set for SofaFSs Target not set if lenient specified and src type isn't in targetprivate static final FeatureImpl
private static final TypeImpl
private final CASImpl
private final CASImpl
private final Int2ObjListMap
<FeatureImpl> private final Int2ObjListMap
<TypeImpl> private Annotation
private CASImpl
private final TypeSystemImpl
private String
private CASImpl
private final TypeSystemImpl
private String
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionboolean
alreadyCopied
(int aFS) Note: only for backwards compatibility 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.boolean
alreadyCopied
(TOP 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.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 CASImplprivate TOP
static void
Does a complete deep copy of one CAS into another CAS.static void
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 <T extends FeatureStructure>
voidcopyFeatures
(T srcFSi, T tgtFSi) Copy feature values from one FS to another.<T extends FeatureStructure>
TcopyFs
(T aFS) Copy 1 feature structure from the originalSrcCas to a new Cas.private TOP
Copy one FS from the src CAS to the tgt CAS View context: The caller must set the srcCasViewImpl and the tgtCasViewImplprivate TOP
private TOP
copyFsInner
(TOP srcFs) Copies a FS from the source CAS to the destination CAS.private CASImpl
getCorrespondingTgtView
(String viewNameFromSrcSofaId) private String
getDestSofaId
(String viewNameFromSrcSofaId) 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
(CASImpl aCas, String aViewName) Gets the named view; if the view doesn't exist it will be created.private FeatureImpl
getTargetFeature
(FeatureImpl srcFi) private FeatureImpl
getTargetFeature2
(FeatureImpl srcFi) private TypeImpl
getTargetType
(TypeImpl srcTi) private <T extends FeatureStructure>
booleanisDocumentAnnotation
(T aFS) Determines whether the given FS is the DocumentAnnotation in the srcCasView.
-
Field Details
-
MISSING_TYPE
-
MISSING_FEAT
-
originalSrcCas
-
originalTgtCas
-
srcCasViewImpl
-
tgtCasViewImpl
-
srcViewName
-
tgtViewName
-
srcTsi
-
tgtTsi
-
src2TgtType
-
src2TgtFeat
-
isEqualTypeSystems
private final boolean isEqualTypeSystems -
cachedSrcViewName
-
cachedTgtView
-
isChangeViewName
private boolean isChangeViewNametrue if the copyCasView api was used, and the target view name corresponding to the source view name is changed -
srcCasDocumentAnnotation
-
lenient
private final boolean lenient -
mFsMap
key is source FS, value is target FS Target not set for SofaFSs Target not set if lenient specified and src type isn't in target -
fsToDo
Deferred calls to copy Features of a FS
-
-
Constructor Details
-
CasCopier
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
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
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 fromaDestCas
- the CAS to copy toaCopySofa
- if true, the sofa data and mimeType of each view will be copied. If false they will not.
-
copyCas
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 fromaDestCas
- 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
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 constructoraCopySofa
- if true, the sofa data and mimeType will be copied. If false they will not.
-
copyCasView
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 fromaCopySofa
- if true, the sofa data and mimeType will be copied. If false they will not.
-
copyCasView
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 fromaTgtCasViewName
- The name of the view in the destination CAS to copy intoaCopySofa
- if true, the sofa data and mimeType will be copied. If false they will not.
-
copyCasView
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 fromaTgtCasView
- The view in the destination CAS to copy intoaCopySofa
- if true, the sofa data and mimeType will be copied. If false they will not.
-
copyCasViewDifferentCASs
-
copyCasView
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 constructoraTgtCasView
- the CAS view to copy to. This must be a view of the tgtCas set in the constructoraCopySofa
- 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
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- Type Parameters:
T
- the generic type of the returned Feature Structure- Parameters:
aFS
- the Feature Structure to copy- Returns:
- a deep copy of the Feature Structure - any referred to FSs will also be copied, or null if the target CAS doesn't define a corresponding type
-
copyFs2
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 to a copy in the target CAS, or null if the target CAS doesn't have a corresponding type definition
-
copyFs2Fs
-
copyFsInner
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. The _casView is set to the target cas view, unless the FS being copied is a subtype of Annotation Base. - In that case, it is set to the view associated with the sofa ref.- Parameters:
srcFs
- the FS to copy. Must be contained within the source CAS.- Returns:
- the copy of
aFS
in the target CAS.
-
getCorrespondingTgtView
- Returns:
- the view in the target corresponding by name (after sofa name mapping if any) to the source view
-
getDestSofaId
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:
viewNameFromSrcSofaId
-- Returns:
- id unless the id matches the source view name, and that name is being changed
-
copyFeatures
Copy feature values from one FS to another. For reference-valued features, this does a deep copy.- Parameters:
srcFS
- FeatureStructure to copy fromtgtFS
- FeatureStructure to copy to, which must not be in the index (index corruption checks skipped)
-
alreadyCopied
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
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: only for backwards compatibility 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
- Parameters:
arrayFS
-- Returns:
- a copy of the array
-
getOrCreateView
Gets the named view; if the view doesn't exist it will be created. -
isDocumentAnnotation
Determines whether the given FS is the DocumentAnnotation in the srcCasView. Supports subtypes of DocumentAnnotation. Returns true if the FS is the actual one that is being used by the source CAS as the Document Annotation instance. -
casViewsInSameCas
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)
-
getTargetType
-
getTargetFeature
-
getTargetFeature2
-