You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@uima.apache.org by Fabien POULARD <ui...@fabienpoulard.info> on 2009/11/27 16:47:30 UTC

Get annotation class from Type

Hi,

I was wondering if there was an easy way (I did look but found
nothing) to get the Annotation class corresponding to a particular
Type.

As I did not find any I wrote this hacky method... but I don't like
it, it looks too hacky to work for long !

public static Class<? extends Annotation> typeToAnnotation(Type t) {
  // We need to access the class of the annotation corresponding to the
  // type to use as item
  String typename  = t.getName();
  String annotname = typename.replace("_Type", "");
  Class<? extends Annotation> itemclass = (Class<? extends
Annotation>) Class.forName(annotname);
  return itemclass;
}

What do you think ? Do you have any pointer ? I deeped into the
LowLevelTypeSystem as well as the FSGenerator but found nothing.

--
Fabien Poulard

Re: Get annotation class from Type

Posted by Thilo Goetz <tw...@gmx.de>.
On 12/1/2009 16:37, Fabien POULARD wrote:
> Hi,
>
> On Tue, Dec 1, 2009 at 12:12 PM, Thilo Goetz<tw...@gmx.de>  wrote:
>> I'll debate this just for the sake of other people who
>> may be following this thread.  It seems much simpler
>> and less error prone to use the built-in UIMA CAS APIs
>> for this, as described here:
>> http://incubator.apache.org/uima/downloads/releaseDocs/2.2.2-incubating/docs/html/references/references.html#ugr.ref.cas
>> And it'll be faster, too, without the JCas/Java reflection
>> detour.
>
> May you point to something more specific ? I've looked over the
> documentation and the API but I still did not find anything relevant
> for my problem (other than Java reflection).
>
> --
> Fabien Poulard

Sorry, but I still don't understand what your problem is.
This tells you how to read/write feature values in a
generic way.  If that's not what you need, this solution
is not for you.  If you must know the JCas type to use it
in Java Generics in some way, this does not help.

I was responding directly to Tommaso, since his example
did not seem very compelling to me.

--Thilo

Re: Get annotation class from Type

Posted by Fabien POULARD <gr...@gmail.com>.
Hi,

On Tue, Dec 1, 2009 at 12:12 PM, Thilo Goetz <tw...@gmx.de> wrote:
> I'll debate this just for the sake of other people who
> may be following this thread.  It seems much simpler
> and less error prone to use the built-in UIMA CAS APIs
> for this, as described here:
> http://incubator.apache.org/uima/downloads/releaseDocs/2.2.2-incubating/docs/html/references/references.html#ugr.ref.cas
> And it'll be faster, too, without the JCas/Java reflection
> detour.

May you point to something more specific ? I've looked over the
documentation and the API but I still did not find anything relevant
for my problem (other than Java reflection).

--
Fabien Poulard

Re: Get annotation class from Type

Posted by Thilo Goetz <tw...@gmx.de>.

On 12/1/2009 09:46, Tommaso Teofili wrote:
> 2009/11/29 Steven Bethard<st...@gmail.com>
>
>> On Sat, Nov 28, 2009 at 1:12 AM, Fabien POULARD<gr...@gmail.com>
>> wrote:
>>> On Fri, Nov 27, 2009 at 4:56 PM, Thilo Goetz<tw...@gmx.de>  wrote:
>>>> I guess this will work, as long as there even is a JCas type
>>>> for the type you're looking for.  There may not always be one,
>>>> and we have this whole meta-programming kind of API for this
>>>> kind of situation.  If you tell us what you need the class for,
>>>> we may be able to suggest alternative ways of handling this.
>>>
>>> My need is quite simple, a component I develop work on some types
>>> passed to the component in parameter. I retrieve the type from its
>>> name passed in parameter with a simple :
>>>
>>> Type mMonType  = cas.getTypeSystem().getType(theParameterTypeStr);
>>>
>>> ... and I check it is not null before using it.
>>>
>>> Now I use some generic classes (java generics) for my processing and I
>>> need to "configure" them with the class of the annotation
>>> corresponding to this type. That's where I'm lost...
>>
>> If you know you're loading a Java class for a type, why not have your
>> "theParameterTypeStr" be the fully qualified Java class name instead
>> of the UIMA type? Then you can use the usual Java Class.forName()
>> directly.
>>
>
> Using reflection is a simple quick way I also use sometimes for this
> purpose.

I'll debate this just for the sake of other people who
may be following this thread.  It seems much simpler
and less error prone to use the built-in UIMA CAS APIs
for this, as described here:
http://incubator.apache.org/uima/downloads/releaseDocs/2.2.2-incubating/docs/html/references/references.html#ugr.ref.cas
And it'll be faster, too, without the JCas/Java reflection
detour.

> Just beware, if you want to instantiate your Annotations automatically, that
> JCasGen generated types usually have more than one constructor so you should
> choose the one you deserve (i.e.: the constructor with the JCas parameter,
> that is usually the first one).
>
> ex.:
> Type mMonType  = cas.getTypeSystem().getType(
> theParameterTypeStr);
>
> Class typeClass = Class.forName(mMonType.getName());
> Constructor jcasConstructor = getProperConstructor(typeClass);
> FeatureStructure fs = (FeatureStructure) jcasConstructor.newInstance(aJCas);
> ...
>
> private Constructor getProperConstructor(Class typeClass) {
> ....
> //FIXME : dummy sample
> return typeClass.getConstructors()[1];
> ..
> }
>
> Tommaso
>
>
>>
>> Steve
>> --
>> Where did you get that preposterous hypothesis?
>> Did Steve tell you that?
>>         --- The Hiphopopotamus
>>
>

Re: Get annotation class from Type

Posted by Tommaso Teofili <to...@gmail.com>.
2009/11/29 Steven Bethard <st...@gmail.com>

> On Sat, Nov 28, 2009 at 1:12 AM, Fabien POULARD <gr...@gmail.com>
> wrote:
> > On Fri, Nov 27, 2009 at 4:56 PM, Thilo Goetz <tw...@gmx.de> wrote:
> >> I guess this will work, as long as there even is a JCas type
> >> for the type you're looking for.  There may not always be one,
> >> and we have this whole meta-programming kind of API for this
> >> kind of situation.  If you tell us what you need the class for,
> >> we may be able to suggest alternative ways of handling this.
> >
> > My need is quite simple, a component I develop work on some types
> > passed to the component in parameter. I retrieve the type from its
> > name passed in parameter with a simple :
> >
> > Type mMonType  = cas.getTypeSystem().getType(theParameterTypeStr);
> >
> > ... and I check it is not null before using it.
> >
> > Now I use some generic classes (java generics) for my processing and I
> > need to "configure" them with the class of the annotation
> > corresponding to this type. That's where I'm lost...
>
> If you know you're loading a Java class for a type, why not have your
> "theParameterTypeStr" be the fully qualified Java class name instead
> of the UIMA type? Then you can use the usual Java Class.forName()
> directly.
>

Using reflection is a simple quick way I also use sometimes for this
purpose.
Just beware, if you want to instantiate your Annotations automatically, that
JCasGen generated types usually have more than one constructor so you should
choose the one you deserve (i.e.: the constructor with the JCas parameter,
that is usually the first one).

ex.:
Type mMonType  = cas.getTypeSystem().getType(
theParameterTypeStr);

Class typeClass = Class.forName(mMonType.getName());
Constructor jcasConstructor = getProperConstructor(typeClass);
FeatureStructure fs = (FeatureStructure) jcasConstructor.newInstance(aJCas);
...

private Constructor getProperConstructor(Class typeClass) {
....
//FIXME : dummy sample
return typeClass.getConstructors()[1];
..
}

Tommaso


>
> Steve
> --
> Where did you get that preposterous hypothesis?
> Did Steve tell you that?
>        --- The Hiphopopotamus
>

Re: Get annotation class from Type

Posted by Steven Bethard <st...@gmail.com>.
On Sat, Nov 28, 2009 at 1:12 AM, Fabien POULARD <gr...@gmail.com> wrote:
> On Fri, Nov 27, 2009 at 4:56 PM, Thilo Goetz <tw...@gmx.de> wrote:
>> I guess this will work, as long as there even is a JCas type
>> for the type you're looking for.  There may not always be one,
>> and we have this whole meta-programming kind of API for this
>> kind of situation.  If you tell us what you need the class for,
>> we may be able to suggest alternative ways of handling this.
>
> My need is quite simple, a component I develop work on some types
> passed to the component in parameter. I retrieve the type from its
> name passed in parameter with a simple :
>
> Type mMonType  = cas.getTypeSystem().getType(theParameterTypeStr);
>
> ... and I check it is not null before using it.
>
> Now I use some generic classes (java generics) for my processing and I
> need to "configure" them with the class of the annotation
> corresponding to this type. That's where I'm lost...

If you know you're loading a Java class for a type, why not have your
"theParameterTypeStr" be the fully qualified Java class name instead
of the UIMA type? Then you can use the usual Java Class.forName()
directly.

Steve
-- 
Where did you get that preposterous hypothesis?
Did Steve tell you that?
        --- The Hiphopopotamus

Re: Get annotation class from Type

Posted by Fabien POULARD <gr...@gmail.com>.
Hi,

Thanks for the quick answer.

On Fri, Nov 27, 2009 at 4:56 PM, Thilo Goetz <tw...@gmx.de> wrote:
> I guess this will work, as long as there even is a JCas type
> for the type you're looking for.  There may not always be one,
> and we have this whole meta-programming kind of API for this
> kind of situation.  If you tell us what you need the class for,
> we may be able to suggest alternative ways of handling this.

My need is quite simple, a component I develop work on some types
passed to the component in parameter. I retrieve the type from its
name passed in parameter with a simple :

Type mMonType  = cas.getTypeSystem().getType(theParameterTypeStr);

... and I check it is not null before using it.

Now I use some generic classes (java generics) for my processing and I
need to "configure" them with the class of the annotation
corresponding to this type. That's where I'm lost...

I could use a common ancestor like "tcas.Annotation" but I'd prefer to
use the correct class.

Any hint ?

--
Fabien Poulard

Re: Get annotation class from Type

Posted by Thilo Goetz <tw...@gmx.de>.
Fabien POULARD wrote:
> Hi,
> 
> I was wondering if there was an easy way (I did look but found
> nothing) to get the Annotation class corresponding to a particular
> Type.
> 
> As I did not find any I wrote this hacky method... but I don't like
> it, it looks too hacky to work for long !
> 
> public static Class<? extends Annotation> typeToAnnotation(Type t) {
>   // We need to access the class of the annotation corresponding to the
>   // type to use as item
>   String typename  = t.getName();
>   String annotname = typename.replace("_Type", "");
>   Class<? extends Annotation> itemclass = (Class<? extends
> Annotation>) Class.forName(annotname);
>   return itemclass;
> }
> 
> What do you think ? Do you have any pointer ? I deeped into the
> LowLevelTypeSystem as well as the FSGenerator but found nothing.

I guess this will work, as long as there even is a JCas type
for the type you're looking for.  There may not always be one,
and we have this whole meta-programming kind of API for this
kind of situation.  If you tell us what you need the class for,
we may be able to suggest alternative ways of handling this.

--Thilo

> 
> --
> Fabien Poulard