You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@uima.apache.org by vinod <vk...@gmail.com> on 2012/04/11 05:10:31 UTC
annotation type
Hello,
I have a very basic question on Annotation type and
would appreciate any pointers.
I am using reflection to create an instance of an annotation,
before I add it to the cas, I want to check to
see if there is an instance of that type in the cas. However, the line
'jcas.getJFSIndexRepository().getAnnotationIndex(ann.type).iterator();'
below returns a non-zero set
containing DocumentAnnotation!
<pre>
Class uimaTypeClass = Class.forName(objectName);
Class[] constructorArgs = {JCas.class};
Object[] constructorArgVals = {jcas};
Constructor constructor = uimaTypeClass.getConstructor(constructorArgs);
Annotation ann = (Annotation)constructor.newInstance(constructorArgVals);
Iterator itr = jcas.getJFSIndexRepository().getAnnotationIndex(ann.type).iterator();
if(itr.hasNext()){
ann = (Annotation)itr.next();
}
</pre>
Thanks.
Re: annotation type
Posted by Marshall Schor <ms...@schor.com>.
You've tripped over a bit of Java arcania...
The fix is to change the line:
Iterator itr = jcas.getJFSIndexRepository().getAnnotationIndex(ann.type).iterator();
to
Iterator itr = jcas.getJFSIndexRepository().getAnnotationIndex(ann.getTypeIndexID()).iterator();
The reason is that we make the size of JCas java objects as small as possible.
One way we do that is to take fields which are the same for every instance of a
particular JCas class, and move them into the class, as class-level fields, so
they are not present in each instance.
Java, when you write
Annotator a = ... ; a.type
computes type as a class field reference to the value in the class "Annotator"
which you declared "a" to be. What you wanted was for Java to look at the class
for the instance of a, and use that class's "type" field value, but that's not
how Java is designed.
To get around this, we've put in a method, getTypeIndexID(), which gets the
field value from the class of the actual instance the method is called on.
If there is some documentation page somewhere that led you to use .type instead
of .getTypeIndexID(), please reply so we can improve the documentation.
-Marshall
On 4/10/2012 11:10 PM, vinod wrote:
> Hello,
> I have a very basic question on Annotation type and
> would appreciate any pointers.
>
> I am using reflection to create an instance of an annotation,
> before I add it to the cas, I want to check to
> see if there is an instance of that type in the cas. However, the line
> 'jcas.getJFSIndexRepository().getAnnotationIndex(ann.type).iterator();'
> below returns a non-zero set
> containing DocumentAnnotation!
>
> <pre>
> Class uimaTypeClass = Class.forName(objectName);
> Class[] constructorArgs = {JCas.class};
> Object[] constructorArgVals = {jcas};
>
> Constructor constructor = uimaTypeClass.getConstructor(constructorArgs);
> Annotation ann = (Annotation)constructor.newInstance(constructorArgVals);
>
> Iterator itr = jcas.getJFSIndexRepository().getAnnotationIndex(ann.type).iterator();
>
> if(itr.hasNext()){
> ann = (Annotation)itr.next();
> }
> </pre>
>
> Thanks.
>
>
Re: annotation type
Posted by vinod <vk...@gmail.com>.
Marshall Schor <ms...@...> writes:
>
> In your test case below, can you print out the actual
type of the class held in
> the variable uimaTypeClass?
>
> -Marshall
>
> On 4/10/2012 11:10 PM, vinod wrote:
> > Hello,
> > I have a very basic question on Annotation type and
> > would appreciate any pointers.
> >
> > I am using reflection to create an instance of an annotation,
> > before I add it to the cas, I want to check to
> > see if there is an instance of that type in the cas.
However, the line
> > 'jcas.getJFSIndexRepository().getAnnotationIndex(ann.type).iterator();'
> > below returns a non-zero set
> > containing DocumentAnnotation!
> >
> > <pre>
> > Class uimaTypeClass = Class.forName(objectName);
> > Class[] constructorArgs = {JCas.class};
> > Object[] constructorArgVals = {jcas};
> >
> > Constructor constructor =
uimaTypeClass.getConstructor(constructorArgs);
> > Annotation ann =
(Annotation)constructor.newInstance(constructorArgVals);
> >
> > Iterator itr =
jcas.getJFSIndexRepository().getAnnotationIndex(ann.type).iterator();
> >
> > if(itr.hasNext()){
> > ann = (Annotation)itr.next();
> > }
> > </pre>
> >
> > Thanks.
> >
> >
>
>
Yes, in fact, I do get an instance and a correct one too
using the code in my original post. I am just not
been able to get an iterator using the resulting annotation
(ann in this case).
Thanks.
Re: annotation type
Posted by Marshall Schor <ms...@schor.com>.
In your test case below, can you print out the actual type of the class held in
the variable uimaTypeClass?
-Marshall
On 4/10/2012 11:10 PM, vinod wrote:
> Hello,
> I have a very basic question on Annotation type and
> would appreciate any pointers.
>
> I am using reflection to create an instance of an annotation,
> before I add it to the cas, I want to check to
> see if there is an instance of that type in the cas. However, the line
> 'jcas.getJFSIndexRepository().getAnnotationIndex(ann.type).iterator();'
> below returns a non-zero set
> containing DocumentAnnotation!
>
> <pre>
> Class uimaTypeClass = Class.forName(objectName);
> Class[] constructorArgs = {JCas.class};
> Object[] constructorArgVals = {jcas};
>
> Constructor constructor = uimaTypeClass.getConstructor(constructorArgs);
> Annotation ann = (Annotation)constructor.newInstance(constructorArgVals);
>
> Iterator itr = jcas.getJFSIndexRepository().getAnnotationIndex(ann.type).iterator();
>
> if(itr.hasNext()){
> ann = (Annotation)itr.next();
> }
> </pre>
>
> Thanks.
>
>