use of org.apache.uima.cas.impl.CASCompleteSerializer in project webanno by webanno.
the class CasPersistenceUtils method writeSerializedCas.
public static void writeSerializedCas(JCas aJCas, File aFile) throws IOException {
FileUtils.forceMkdir(aFile.getParentFile());
try (ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(aFile))) {
CASCompleteSerializer serializer = serializeCASComplete(aJCas.getCasImpl());
os.writeObject(serializer);
}
}
use of org.apache.uima.cas.impl.CASCompleteSerializer in project webanno by webanno.
the class BratAnnotatorUtility method clearJcasAnnotations.
public static JCas clearJcasAnnotations(JCas aJCas, SourceDocument aSourceDocument, User aUser, DocumentService repository) throws IOException {
JCas target;
try {
target = JCasFactory.createJCas();
} catch (UIMAException e) {
throw new IOException(e);
}
// Copy the CAS - basically we do this just to keep the full type system information
CASCompleteSerializer serializer = serializeCASComplete(aJCas.getCasImpl());
deserializeCASComplete(serializer, (CASImpl) target.getCas());
// Re-init JCas
try {
target.getCas().getJCas();
} catch (CASException e) {
throw new IOException(e);
}
// Remove all annotations from the target CAS but we keep the type system!
target.reset();
// Copy over essential information
DocumentMetaData.copy(aJCas, target);
// DKPro Core Issue 435
target.setDocumentLanguage(aJCas.getDocumentLanguage());
target.setDocumentText(aJCas.getDocumentText());
// Transfer token boundaries
for (Token t : select(aJCas, Token.class)) {
new Token(target, t.getBegin(), t.getEnd()).addToIndexes();
}
// Transfer sentence boundaries
for (Sentence s : select(aJCas, Sentence.class)) {
new Sentence(target, s.getBegin(), s.getEnd()).addToIndexes();
}
repository.writeAnnotationCas(target, aSourceDocument, aUser, false);
return target;
}
use of org.apache.uima.cas.impl.CASCompleteSerializer in project webanno by webanno.
the class AnnotationSchemaServiceImpl method upgradeCas.
@Override
public void upgradeCas(CAS aCas, SourceDocument aSourceDocument, String aUser) throws UIMAException, IOException {
TypeSystemDescription builtInTypes = TypeSystemDescriptionFactory.createTypeSystemDescription();
TypeSystemDescription projectTypes = getProjectTypes(aSourceDocument.getProject());
TypeSystemDescription allTypes = CasCreationUtils.mergeTypeSystems(asList(projectTypes, builtInTypes));
// Prepare template for new CAS
CAS newCas = JCasFactory.createJCas(allTypes).getCas();
CASCompleteSerializer serializer = Serialization.serializeCASComplete((CASImpl) newCas);
// Save old type system
TypeSystem oldTypeSystem = aCas.getTypeSystem();
// Save old CAS contents
ByteArrayOutputStream os2 = new ByteArrayOutputStream();
Serialization.serializeWithCompression(aCas, os2, oldTypeSystem);
// Prepare CAS with new type system
Serialization.deserializeCASComplete(serializer, (CASImpl) aCas);
// Restore CAS data to new type system
Serialization.deserializeCAS(aCas, new ByteArrayInputStream(os2.toByteArray()), oldTypeSystem, null);
// Make sure JCas is properly initialized too
aCas.getJCas();
try (MDC.MDCCloseable closable = MDC.putCloseable(Logging.KEY_PROJECT_ID, String.valueOf(aSourceDocument.getProject().getId()))) {
Project project = aSourceDocument.getProject();
log.info("Upgraded CAS of user [{}] for " + "document [{}]({}) in project [{}]({})", aUser, aSourceDocument.getName(), aSourceDocument.getId(), project.getName(), project.getId());
}
}
use of org.apache.uima.cas.impl.CASCompleteSerializer in project webanno by webanno.
the class CasPersistenceUtils method readSerializedCas.
public static void readSerializedCas(CAS aCas, File aFile) throws IOException {
try (ObjectInputStream is = new ObjectInputStream(new FileInputStream(aFile))) {
CASCompleteSerializer serializer = (CASCompleteSerializer) is.readObject();
deserializeCASComplete(serializer, (CASImpl) aCas);
// Initialize the JCas sub-system which is the most often used API in DKPro Core
// components
aCas.getJCas();
} catch (CASException | ClassNotFoundException e) {
throw new IOException(e);
}
}
use of org.apache.uima.cas.impl.CASCompleteSerializer in project webanno by webanno.
the class CasPersistenceUtils method writeSerializedCas.
public static void writeSerializedCas(CAS aCas, File aFile) throws IOException {
FileUtils.forceMkdir(aFile.getParentFile());
try (ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(aFile))) {
CASCompleteSerializer serializer = serializeCASComplete((CASImpl) aCas);
os.writeObject(serializer);
}
}
Aggregations