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