Hi folks

> On 15. Dec 2018, at 13:41, Nicolas Paris <[email protected]> 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 the
     * results to the target CAS. An in-place upgrade can be achieved by using 
the same CAS as
     * 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,
                sourceTypeSystem);

        // Make sure JCas is properly initialized too
        aTargetCas.getJCas();
    }

---

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 with
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.

Cheers,

-- Richard

Reply via email to