You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@uima.apache.org by Florian Leitner <fl...@cnio.es> on 2012/08/06 11:51:24 UTC

generics and type safety in UIMA AEs

Can someone help me out on two issues I have (ab-)using generics and thus having type safety issues when implementing UIMA AEs? I am having a hard time figuring out how to implement an AE without having to resort to "black box" casts. It seems I am not fully seeing how generics should be applied via the UIMA API.

1. The first issue is the (superfluous) need to cast an annotation type from the annotation index. Usually, if I get an iterator, the UIMA tutorials instruct us to write:

FSIterator<Annotation> annIt = jcas.getAnnotationIndex(myType).iterator();
 
This is works for non-generic pre-Java 5 code, but because I loose the type information I supplied to the method fetching the index, I end up with something like this:

MyAnnotationType ann = (MyAnnotationType) annIt.next();

This is not type safe because of relying on an (both unsafe and unnecessary!) cast. Can somebody please point out how to create type-safe annotation iterators? How can I instantiate an iterator other than the (non-generic) FSIterator<Annotation> in UIMA? Shouldn't there be something more Java-5-like in the API? E.g.:

FSIterator<MyAnnotationType> = jcas.getAnnotationTypeIndex(MyAnnotationType.class).iterator();

2. The second type safety issue I have is with external resources. Lets assume I've got an interface defining a StringMapResource<T>, i.e., much like in the UIMA tutorials, essentially a Map<String, T> using String keys and some generic values. Usually, in the initialize(UimaContext ctx) method, I'd write:

StringMapResource<MyType> smr = (StringMapResource<MyType>)  ctx.getResourceObject(myResourceKey);

Obviously, this is not an effective way to implement generics, and the compiler complains about this unchecked "back box" casting I am doing when fetching the resource. Is there a type-safe way to initialize resources I am not aware of? Currently, because of this ugly cast, I even am forced to decorate my initialize(UimaContext) methods with hideous @SuppressWarnings("unchecked") annotations and I'd really like to get rid of these "warts". I'd assume there should be some method to instantiate a type-safe, generic resource factory in the UIMA API, right?

Thanks for any help!
Florian

-- 
Florian Leitner, PhD <fl...@gmail.com>

Structural Biology and BioComputing Programme
Spanish National Cancer Research Centre (CNIO)

Address: C/ Melchor Fernandez Almagro 3; E-28029 Madrid
Phone: +34 91 732 8000
Fax: +34 91 224 6980
Internet: http://www.cnio.es


RE: generics and type safety in UIMA AEs

Posted by Torsten Zesch <ze...@ukp.informatik.tu-darmstadt.de>.
Hi Florian,

If you use uimaFIT
http://code.google.com/p/uimafit/
you can use something more type-safe like

for (Token token : JCasUtil.select(jCas, Token.class)){
}

-Torsten

> -----Original Message-----
> From: Florian Leitner [mailto:fleitner.cnio@gmail.com] On Behalf Of Florian
> Leitner
> Sent: Monday, August 06, 2012 11:51 AM
> To: user@uima.apache.org
> Subject: generics and type safety in UIMA AEs
> 
> Can someone help me out on two issues I have (ab-)using generics and thus
> having type safety issues when implementing UIMA AEs? I am having a hard
> time figuring out how to implement an AE without having to resort to "black
> box" casts. It seems I am not fully seeing how generics should be applied via
> the UIMA API.
> 
> 1. The first issue is the (superfluous) need to cast an annotation type from
> the annotation index. Usually, if I get an iterator, the UIMA tutorials instruct
> us to write:
> 
> FSIterator<Annotation> annIt = jcas.getAnnotationIndex(myType).iterator();
> 
> This is works for non-generic pre-Java 5 code, but because I loose the type
> information I supplied to the method fetching the index, I end up with
> something like this:
> 
> MyAnnotationType ann = (MyAnnotationType) annIt.next();
> 
> This is not type safe because of relying on an (both unsafe and unnecessary!)
> cast. Can somebody please point out how to create type-safe annotation
> iterators? How can I instantiate an iterator other than the (non-generic)
> FSIterator<Annotation> in UIMA? Shouldn't there be something more Java-
> 5-like in the API? E.g.:
> 
> FSIterator<MyAnnotationType> =
> jcas.getAnnotationTypeIndex(MyAnnotationType.class).iterator();
> 
> 2. The second type safety issue I have is with external resources. Lets
> assume I've got an interface defining a StringMapResource<T>, i.e., much
> like in the UIMA tutorials, essentially a Map<String, T> using String keys and
> some generic values. Usually, in the initialize(UimaContext ctx) method, I'd
> write:
> 
> StringMapResource<MyType> smr = (StringMapResource<MyType>)
> ctx.getResourceObject(myResourceKey);
> 
> Obviously, this is not an effective way to implement generics, and the
> compiler complains about this unchecked "back box" casting I am doing when
> fetching the resource. Is there a type-safe way to initialize resources I am not
> aware of? Currently, because of this ugly cast, I even am forced to decorate
> my initialize(UimaContext) methods with hideous
> @SuppressWarnings("unchecked") annotations and I'd really like to get rid of
> these "warts". I'd assume there should be some method to instantiate a
> type-safe, generic resource factory in the UIMA API, right?
> 
> Thanks for any help!
> Florian
> 
> --
> Florian Leitner, PhD <fl...@gmail.com>
> 
> Structural Biology and BioComputing Programme Spanish National Cancer
> Research Centre (CNIO)
> 
> Address: C/ Melchor Fernandez Almagro 3; E-28029 Madrid
> Phone: +34 91 732 8000
> Fax: +34 91 224 6980
> Internet: http://www.cnio.es


Re: generics and type safety in UIMA AEs

Posted by Richard Eckart de Castilho <ec...@ukp.informatik.tu-darmstadt.de>.
Am 06.08.2012 um 14:21 schrieb Florian Leitner:

> However, as a matter of fact, I am not sure I understand why uimaFIT isn't part of UIMA. I'd think this stuff is so good it should be included into the Apache code-base.

That has been proposed over on the developers mailing list and is being worked on.

-- Richard

-- 
------------------------------------------------------------------- 
Richard Eckart de Castilho
Technical Lead
Ubiquitous Knowledge Processing Lab (UKP-TUD) 
FB 20 Computer Science Department      
Technische Universität Darmstadt 
Hochschulstr. 10, D-64289 Darmstadt, Germany 
phone [+49] (0)6151 16-7477, fax -5455, room S2/02/B117
eckart@ukp.informatik.tu-darmstadt.de 
www.ukp.tu-darmstadt.de 
Web Research at TU Darmstadt (WeRC) www.werc.tu-darmstadt.de
------------------------------------------------------------------- 







Re: generics and type safety in UIMA AEs

Posted by Florian Leitner <fl...@cnio.es>.
On 6 Aug 2012, at 12:59, Richard Eckart de Castilho wrote:

> Hello Florian,
> 
> with vanilla UIMA, there is no other way than casting. You might want to take a look at uimaFIT though.
> 
>> FSIterator<Annotation> annIt = jcas.getAnnotationIndex(myType).iterator();
> 
> List<Token> tokens = JCasUtil.select(jcas, Token.class)
> 
>> 2. The second type safety issue I have is with external resources. Lets assume I've got an interface defining a StringMapResource<T>, i.e., much like in the UIMA tutorials, essentially a Map<String, T> using String keys and some generic values. Usually, in the initialize(UimaContext ctx) method, I'd write:
>> 
>> StringMapResource<MyType> smr = (StringMapResource<MyType>)  ctx.getResourceObject(myResourceKey);
> 
> This is really a bit difficult to do statically type safe, but uimaFIT allows you to at least avoid the compiler warnings. You'd just set up a class variable of the type you want and annotate it with @ExternalResource:
> 
> @ExternalResource(key="MY_RESOURCE_KEY")
> private StringMapResource<MyType> smr;
> 
> Mind that for this annotation (or the similarliy useful @ConfigurationParameter annotation) to work either your component must inherit from one of the uimaFIT base classes (e.g. org.uimafit.component.JCasAnnotator_ImplBase) or you have to call ConfigurationParameterInitializer and ExternalResourceInitializer manually in initialize().
> 
> Cheers,
> 
> -- Richard
> -- 

Hello Richard,

Thank you very much for your responses!

In general, I am already using uimaFIT to write my tests, because UIMA itself seems virtually "untestable", but I am (so far) reluctant to included it in my main code, too, in the hopes of minimizing my dependencies,  maintenance overhead, and maximizing compatibility (Maybe I should really over think my decision - I am astonished how fast uimaFIT 1.4 was out, following the UIMA 2.4.0 release.)

However, as a matter of fact, I am not sure I understand why uimaFIT isn't part of UIMA. I'd think this stuff is so good it should be included into the Apache code-base.

Regards,
Florian

Re: generics and type safety in UIMA AEs

Posted by Richard Eckart de Castilho <ec...@ukp.informatik.tu-darmstadt.de>.
Hello Florian,

with vanilla UIMA, there is no other way than casting. You might want to take a look at uimaFIT though.

> FSIterator<Annotation> annIt = jcas.getAnnotationIndex(myType).iterator();

List<Token> tokens = JCasUtil.select(jcas, Token.class)

> 2. The second type safety issue I have is with external resources. Lets assume I've got an interface defining a StringMapResource<T>, i.e., much like in the UIMA tutorials, essentially a Map<String, T> using String keys and some generic values. Usually, in the initialize(UimaContext ctx) method, I'd write:
> 
> StringMapResource<MyType> smr = (StringMapResource<MyType>)  ctx.getResourceObject(myResourceKey);

This is really a bit difficult to do statically type safe, but uimaFIT allows you to at least avoid the compiler warnings. You'd just set up a class variable of the type you want and annotate it with @ExternalResource:

@ExternalResource(key="MY_RESOURCE_KEY")
private StringMapResource<MyType> smr;

Mind that for this annotation (or the similarliy useful @ConfigurationParameter annotation) to work either your component must inherit from one of the uimaFIT base classes (e.g. org.uimafit.component.JCasAnnotator_ImplBase) or you have to call ConfigurationParameterInitializer and ExternalResourceInitializer manually in initialize().

Cheers,

-- Richard
-- 
------------------------------------------------------------------- 
Richard Eckart de Castilho
Technical Lead
Ubiquitous Knowledge Processing Lab (UKP-TUD) 
FB 20 Computer Science Department      
Technische Universität Darmstadt 
Hochschulstr. 10, D-64289 Darmstadt, Germany 
phone [+49] (0)6151 16-7477, fax -5455, room S2/02/B117
eckart@ukp.informatik.tu-darmstadt.de 
www.ukp.tu-darmstadt.de 
Web Research at TU Darmstadt (WeRC) www.werc.tu-darmstadt.de
-------------------------------------------------------------------