uima-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Richard Eckart de Castilho <...@apache.org>
Subject Re: Is it possible to define dynamically typed annotations?
Date Sat, 15 Dec 2018 22:40:53 GMT
Hi folks

> On 15. Dec 2018, at 13:41, Nicolas Paris <nicolas.paris@riseup.net> wrote:
> On Sat, Dec 15, 2018 at 07:20:33AM -0500, Alain D├ęsilets wrote:
>> Is it possible to create dynamically typed annotations in UIMA? In other
>> words, would it be possible for users of my system to create a new type of
>> annotation without having to recompile the Java code?

My take on the problem is to redefine the CAS in-place. The following
code is used by the WebAnno annotation editor to handle the case where
the user modifies the type system:


     * Load the contents from the source CAS, upgrade it to the target type system and write
     * results to the target CAS. An in-place upgrade can be achieved by using the same CAS
     * source and target.
    private void upgradeCas(CAS aSourceCas, CAS aTargetCas, TypeSystemDescription aTargetTypeSystem)
        throws UIMAException, IOException
        // Save source CAS type system (do this early since we might do an in-place upgrade)
        TypeSystem sourceTypeSystem = aSourceCas.getTypeSystem();

        // Save source CAS contents
        ByteArrayOutputStream serializedCasContents = new ByteArrayOutputStream();
        Serialization.serializeWithCompression(aSourceCas, serializedCasContents, sourceTypeSystem);

        // Re-initialize the target CAS with new type system
        CAS tempCas = JCasFactory.createJCas(aTargetTypeSystem).getCas();
        CASCompleteSerializer serializer = Serialization.serializeCASComplete((CASImpl) tempCas);
        Serialization.deserializeCASComplete(serializer, (CASImpl) aTargetCas);

        // Leniently load the source CAS contents into the target CAS
        CasIOUtils.load(new ByteArrayInputStream(serializedCasContents.toByteArray()), aTargetCas,

        // Make sure JCas is properly initialized too


This procedure takes a bit so it shouldn't be done often and also it discards any non-reachable
feature structures - but it works. It also discards any information that is not compatible
the target type system - within any limits that lenient CAS loading may impose.

Basically you call it with 

          upgradeCas(aCas, aCas, aTargetTypeSystem);

in order to perform an in-place upgrade of a singla CAS to the given type system.


-- Richard
View raw message