Package org.apache.uima.util
Class CasCopier
- java.lang.Object
-
- org.apache.uima.util.CasCopier
-
public class CasCopier extends java.lang.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 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
Fields Modifier and Type Field Description private java.lang.String
cachedSrcViewName
private CASImpl
cachedTgtView
private java.util.Deque<java.lang.Runnable>
fsToDo
Deferred calls to copy Features of a FSprivate boolean
isChangeViewName
true if the copyCasView api was used, and the target view name corresponding to the source view name is changedprivate boolean
isEqualTypeSystems
private boolean
lenient
private java.util.Map<TOP,TOP>
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 targetprivate static FeatureImpl
MISSING_FEAT
private static TypeImpl
MISSING_TYPE
private CASImpl
originalSrcCas
private CASImpl
originalTgtCas
private Int2ObjListMap<FeatureImpl>
src2TgtFeat
private Int2ObjListMap<TypeImpl>
src2TgtType
private Annotation
srcCasDocumentAnnotation
private CASImpl
srcCasViewImpl
private TypeSystemImpl
srcTsi
private java.lang.String
srcViewName
private CASImpl
tgtCasViewImpl
private TypeSystemImpl
tgtTsi
private java.lang.String
tgtViewName
-
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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.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.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
copyArray(TOP 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(java.lang.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(java.lang.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, java.lang.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
copyFs2(TOP aFS)
Copy one FS from the src CAS to the tgt CAS View context: The caller must set the srcCasViewImpl and the tgtCasViewImplprivate TOP
copyFs2Fs(TOP fs)
private TOP
copyFsInner(TOP srcFs)
Copies a FS from the source CAS to the destination CAS.private CASImpl
getCorrespondingTgtView(java.lang.String viewNameFromSrcSofaId)
private java.lang.String
getDestSofaId(java.lang.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, java.lang.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 Detail
-
MISSING_TYPE
private static final TypeImpl MISSING_TYPE
-
MISSING_FEAT
private static final FeatureImpl MISSING_FEAT
-
originalSrcCas
private final CASImpl originalSrcCas
-
originalTgtCas
private final CASImpl originalTgtCas
-
srcCasViewImpl
private CASImpl srcCasViewImpl
-
tgtCasViewImpl
private CASImpl tgtCasViewImpl
-
srcViewName
private java.lang.String srcViewName
-
tgtViewName
private java.lang.String tgtViewName
-
srcTsi
private final TypeSystemImpl srcTsi
-
tgtTsi
private final TypeSystemImpl tgtTsi
-
src2TgtType
private final Int2ObjListMap<TypeImpl> src2TgtType
-
src2TgtFeat
private final Int2ObjListMap<FeatureImpl> src2TgtFeat
-
isEqualTypeSystems
private final boolean isEqualTypeSystems
-
cachedSrcViewName
private java.lang.String cachedSrcViewName
-
cachedTgtView
private CASImpl cachedTgtView
-
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 Annotation srcCasDocumentAnnotation
-
lenient
private final boolean lenient
-
mFsMap
private final java.util.Map<TOP,TOP> 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
private final java.util.Deque<java.lang.Runnable> fsToDo
Deferred calls to copy Features of a FS
-
-
Constructor Detail
-
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 Detail
-
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 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
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 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
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 constructoraCopySofa
- if true, the sofa data and mimeType will be copied. If false they will not.
-
copyCasView
public void copyCasView(java.lang.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 fromaCopySofa
- if true, the sofa data and mimeType will be copied. If false they will not.
-
copyCasView
public void copyCasView(CAS aSrcCasView, java.lang.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 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
public void copyCasView(java.lang.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 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
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 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
public <T extends FeatureStructure> T copyFs(T 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- 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
private TOP copyFs2(TOP 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 to a copy in the target CAS, or null if the target CAS doesn't have a corresponding type definition
-
copyFsInner
private TOP copyFsInner(TOP srcFs)
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
private CASImpl getCorrespondingTgtView(java.lang.String viewNameFromSrcSofaId)
- Returns:
- the view in the target corresponding by name (after sofa name mapping if any) to the source view
-
getDestSofaId
private java.lang.String getDestSofaId(java.lang.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. 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
private <T extends FeatureStructure> void copyFeatures(T srcFSi, T tgtFSi)
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
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(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. 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.
-
getOrCreateView
private static CASImpl getOrCreateView(CASImpl aCas, java.lang.String aViewName)
Gets the named view; if the view doesn't exist it will be created.
-
isDocumentAnnotation
private <T extends FeatureStructure> boolean isDocumentAnnotation(T aFS)
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
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)
-
getTargetFeature
private FeatureImpl getTargetFeature(FeatureImpl srcFi)
-
getTargetFeature2
private FeatureImpl getTargetFeature2(FeatureImpl srcFi)
-
-