Search in sources :

Example 1 with CASCompleteSerializer

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);
    }
}
Also used : CASCompleteSerializer(org.apache.uima.cas.impl.CASCompleteSerializer) FileOutputStream(java.io.FileOutputStream) ObjectOutputStream(java.io.ObjectOutputStream)

Example 2 with CASCompleteSerializer

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;
}
Also used : CASCompleteSerializer(org.apache.uima.cas.impl.CASCompleteSerializer) JCas(org.apache.uima.jcas.JCas) Token(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token) IOException(java.io.IOException) CASException(org.apache.uima.cas.CASException) UIMAException(org.apache.uima.UIMAException) Sentence(de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence)

Example 3 with CASCompleteSerializer

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());
    }
}
Also used : Project(de.tudarmstadt.ukp.clarin.webanno.model.Project) TypeSystem(org.apache.uima.cas.TypeSystem) TypeSystemDescription(org.apache.uima.resource.metadata.TypeSystemDescription) CAS(org.apache.uima.cas.CAS) ByteArrayInputStream(java.io.ByteArrayInputStream) CASCompleteSerializer(org.apache.uima.cas.impl.CASCompleteSerializer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MDC(org.slf4j.MDC)

Example 4 with CASCompleteSerializer

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);
    }
}
Also used : CASCompleteSerializer(org.apache.uima.cas.impl.CASCompleteSerializer) CASException(org.apache.uima.cas.CASException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ObjectInputStream(java.io.ObjectInputStream)

Example 5 with CASCompleteSerializer

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);
    }
}
Also used : CASCompleteSerializer(org.apache.uima.cas.impl.CASCompleteSerializer) FileOutputStream(java.io.FileOutputStream) ObjectOutputStream(java.io.ObjectOutputStream)

Aggregations

CASCompleteSerializer (org.apache.uima.cas.impl.CASCompleteSerializer)6 IOException (java.io.IOException)3 CASException (org.apache.uima.cas.CASException)3 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 Project (de.tudarmstadt.ukp.clarin.webanno.model.Project)1 Sentence (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Sentence)1 Token (de.tudarmstadt.ukp.dkpro.core.api.segmentation.type.Token)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 UIMAException (org.apache.uima.UIMAException)1 CAS (org.apache.uima.cas.CAS)1 TypeSystem (org.apache.uima.cas.TypeSystem)1 JCas (org.apache.uima.jcas.JCas)1 TypeSystemDescription (org.apache.uima.resource.metadata.TypeSystemDescription)1 MDC (org.slf4j.MDC)1