You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@uima.apache.org by Richard Eckart de Castilho <re...@apache.org> on 2015/08/01 16:14:05 UTC

Re: Question on JCasUtil's selectCovered(...)

Hi Martin,

assuming this constellation

- Proposition extends Annotation
- Conclusion extends Proposition
- Premise extends Proposition

then the following lines should all be valid:

- selectCovered(jcas, Annotation.class, 0, 100);
- selectCovered(jcas, Proposition.class, 0, 100);
- selectCovered(jcas, Conclusion.class, 0, 100);
- selectCovered(jcas, Premise.class, 0, 100);

more generically, this should also be valid:

<T extends Proposition> void doSomething(JCas jcas, Class<T> aClazz) {
  selectCovered(jcas, aClazz, 0, 100);
}

If you can show a minimal example that doesn't work, I may be able to say more.
To me, so far it looks like a problem with the generics in your code, not with
uimaFIT.

Cheers,

-- Richard

On 31.07.2015, at 22:00, Martin Wunderlich <ma...@gmx.net> wrote:

> Hi all, 
> 
> I am currently developing some feature extractors in the DKPro framework and I have come across a problem with the Class type in the following method in org.apache.uima.fit.util.JCasUtil: 
> 
>  public static <T extends Annotation> List<T> selectCovered(JCas jCas, final Class<T> type,
>          int begin, int end) {
>    return cast(CasUtil.selectCovered(jCas.getCas(), getType(jCas, type), begin, end));
>  }
> 
> In my type system I have a base type „Proposition" that extends „Annotation" and two more types which are derived from the base type: „Conclusion“ and „Premise". If I use the base type for the generic type parameter T in this method above, it works fine. If I use one of the sub-types of the base type, I get the following error: 
> 
> 	- Bound mismatch: The generic method selectCovered(JCas, Class<T>, int, int) of type JCasUtil is not applicable for the arguments (JCas, Class<Conclusion>, int, int). The 
> 	 inferred type Conclusion is not a valid substitute for the bounded parameter <T extends Annotation>
> 
> Background is that I would like to use one abstract feature extractor for common code which is parameterized with <T extends Proposition>  and the two concrete sub-class FE’s, which are parameterized with the two sub-types. 
> 
> Maybe I have some misunderstanding regarding the use of Generics here. Could it be that in the definition of selectCovered() the clause <T extends Annotation> allows only direct sub-types of Annotation? Interestingly enough, if I don’t parameterize the sub-class FEs, I don’t get any errors. 
> Thanks a lot. 
> 
> Cheers, 
> 
> Martin


Re: Question on JCasUtil's selectCovered(...)

Posted by Martin Wunderlich <ma...@gmx.net>.
Thanks a lot, Richard. It is working fine now after making the changes suggested by you. Looks like I need to read up on Generics a bit...

Cheers, 

Martin
 

> Am 02.08.2015 um 00:09 schrieb Richard Eckart de Castilho <re...@apache.org>:
> 
> 
> On 01.08.2015, at 23:06, Martin Wunderlich <ma...@gmx.net> wrote:
> 
>> Yes, I think you might be right, Richard, and that the problem is with the use of Generics. 
>> 
>> This would be the concrete FE class: 
>> 
>> public class StartingPositionOfPremiseUFE<Premise> extends StartingPositionOfPropositionUFE {
>> 
>>   public static String FN_STARTINGPOSITIONOFPROPOSITION = "StartingPositionOfPremise";
>> 
>> 	@Override
>> 	List<Premise> getPropositions(JCas jcas, int start, int end) {
>> 		return JCasUtil.selectCovered(jcas, Premise.class, start, end);
>> 	}
>> }
> 
> I don't see why you would define <Premise> here on the subclass. You would either define a type variable on the subclass or bind a type variable on the superclass, so:
> 
> public class StartingPositionOfPremiseUFE extends StartingPositionOfPropositionUFE<Premise>
> 
>> The call to selectCovered(…) gives the described error. The method getPropositions(…) is defined as abstract in the super-class, which looks like this: 
>> 
>> 
>> abstract public class StartingPositionOfPropositionUFE<T extends Proposition> extends FeatureExtractorResource_ImplBase implements ClassificationUnitFeatureExtractor{
>> 	...
>>   public List<Feature> extract(JCas jcas, TextClassificationUnit classificationUnit) {
>>   	List<? extends Proposition> props = (List<? extends Proposition>) getPropositions(jcas, start, end);
> 
> Instead of <? extends Proposition>, I think you should be using <T>.
> 
>>       if( props != null && props.size() > 0) {
>>       	Proposition firstProposition = props.get(0);
>>       	startingPos = firstProposition.getBegin();
>>       }
>> 
>> 	List<Feature> featList = new ArrayList<Feature>();
>>       featList.add(new Feature(FN_STARTINGPOSITIONOFPROPOSITION, startingPos));
>> 
>>       return featList;
>>   }
>> 
>>   abstract List<?> getPropositions(JCas jcas, int start, int end);
> 
> Again, instead of <?> I think you should be using <T>.
> 
>> }
>> 
>> I’ve removed the irrelevant bits to make it more concise. 
> 
> I still don't see why you would get that error though. 
> 
> Are you sure that "Premise.class" resolves to the right class here and not to another class which accidentally has the same name?
> 
> -- Richard


Re: Question on JCasUtil's selectCovered(...)

Posted by Richard Eckart de Castilho <re...@apache.org>.
On 01.08.2015, at 23:06, Martin Wunderlich <ma...@gmx.net> wrote:

> Yes, I think you might be right, Richard, and that the problem is with the use of Generics. 
> 
> This would be the concrete FE class: 
> 
> public class StartingPositionOfPremiseUFE<Premise> extends StartingPositionOfPropositionUFE {
> 
>    public static String FN_STARTINGPOSITIONOFPROPOSITION = "StartingPositionOfPremise";
> 
> 	@Override
> 	List<Premise> getPropositions(JCas jcas, int start, int end) {
> 		return JCasUtil.selectCovered(jcas, Premise.class, start, end);
> 	}
> }

I don't see why you would define <Premise> here on the subclass. You would either define a type variable on the subclass or bind a type variable on the superclass, so:

public class StartingPositionOfPremiseUFE extends StartingPositionOfPropositionUFE<Premise>

> The call to selectCovered(…) gives the described error. The method getPropositions(…) is defined as abstract in the super-class, which looks like this: 
> 
> 
> abstract public class StartingPositionOfPropositionUFE<T extends Proposition> extends FeatureExtractorResource_ImplBase implements ClassificationUnitFeatureExtractor{
> 	...
>    public List<Feature> extract(JCas jcas, TextClassificationUnit classificationUnit) {
>    	List<? extends Proposition> props = (List<? extends Proposition>) getPropositions(jcas, start, end);

Instead of <? extends Proposition>, I think you should be using <T>.

>        if( props != null && props.size() > 0) {
>        	Proposition firstProposition = props.get(0);
>        	startingPos = firstProposition.getBegin();
>        }
> 
> 	List<Feature> featList = new ArrayList<Feature>();
>        featList.add(new Feature(FN_STARTINGPOSITIONOFPROPOSITION, startingPos));
> 
>        return featList;
>    }
> 
>    abstract List<?> getPropositions(JCas jcas, int start, int end);

Again, instead of <?> I think you should be using <T>.

> }
> 
> I’ve removed the irrelevant bits to make it more concise. 

I still don't see why you would get that error though. 

Are you sure that "Premise.class" resolves to the right class here and not to another class which accidentally has the same name?

-- Richard

Re: Question on JCasUtil's selectCovered(...)

Posted by Martin Wunderlich <ma...@gmx.net>.
Yes, I think you might be right, Richard, and that the problem is with the use of Generics. 

This would be the concrete FE class: 

public class StartingPositionOfPremiseUFE<Premise> extends StartingPositionOfPropositionUFE {
    
    public static String FN_STARTINGPOSITIONOFPROPOSITION = "StartingPositionOfPremise";

	@Override
	List<Premise> getPropositions(JCas jcas, int start, int end) {
		return JCasUtil.selectCovered(jcas, Premise.class, start, end);
	}
}

The call to selectCovered(…) gives the described error. The method getPropositions(…) is defined as abstract in the super-class, which looks like this: 


abstract public class StartingPositionOfPropositionUFE<T extends Proposition> extends FeatureExtractorResource_ImplBase implements ClassificationUnitFeatureExtractor{
	...
    public List<Feature> extract(JCas jcas, TextClassificationUnit classificationUnit) {
    	List<? extends Proposition> props = (List<? extends Proposition>) getPropositions(jcas, start, end);
        if( props != null && props.size() > 0) {
        	Proposition firstProposition = props.get(0);
        	startingPos = firstProposition.getBegin();
        }
        
	List<Feature> featList = new ArrayList<Feature>();
        featList.add(new Feature(FN_STARTINGPOSITIONOFPROPOSITION, startingPos));
        
        return featList;
    }

    abstract List<?> getPropositions(JCas jcas, int start, int end);
}

I’ve removed the irrelevant bits to make it more concise. 

Cheers, 

Martin
 

> Am 01.08.2015 um 16:14 schrieb Richard Eckart de Castilho <re...@apache.org>:
> 
> Hi Martin,
> 
> assuming this constellation
> 
> - Proposition extends Annotation
> - Conclusion extends Proposition
> - Premise extends Proposition
> 
> then the following lines should all be valid:
> 
> - selectCovered(jcas, Annotation.class, 0, 100);
> - selectCovered(jcas, Proposition.class, 0, 100);
> - selectCovered(jcas, Conclusion.class, 0, 100);
> - selectCovered(jcas, Premise.class, 0, 100);
> 
> more generically, this should also be valid:
> 
> <T extends Proposition> void doSomething(JCas jcas, Class<T> aClazz) {
>  selectCovered(jcas, aClazz, 0, 100);
> }
> 
> If you can show a minimal example that doesn't work, I may be able to say more.
> To me, so far it looks like a problem with the generics in your code, not with
> uimaFIT.
> 
> Cheers,
> 
> -- Richard
> 
> On 31.07.2015, at 22:00, Martin Wunderlich <ma...@gmx.net> wrote:
> 
>> Hi all, 
>> 
>> I am currently developing some feature extractors in the DKPro framework and I have come across a problem with the Class type in the following method in org.apache.uima.fit.util.JCasUtil: 
>> 
>> public static <T extends Annotation> List<T> selectCovered(JCas jCas, final Class<T> type,
>>         int begin, int end) {
>>   return cast(CasUtil.selectCovered(jCas.getCas(), getType(jCas, type), begin, end));
>> }
>> 
>> In my type system I have a base type „Proposition" that extends „Annotation" and two more types which are derived from the base type: „Conclusion“ and „Premise". If I use the base type for the generic type parameter T in this method above, it works fine. If I use one of the sub-types of the base type, I get the following error: 
>> 
>> 	- Bound mismatch: The generic method selectCovered(JCas, Class<T>, int, int) of type JCasUtil is not applicable for the arguments (JCas, Class<Conclusion>, int, int). The 
>> 	 inferred type Conclusion is not a valid substitute for the bounded parameter <T extends Annotation>
>> 
>> Background is that I would like to use one abstract feature extractor for common code which is parameterized with <T extends Proposition>  and the two concrete sub-class FE’s, which are parameterized with the two sub-types. 
>> 
>> Maybe I have some misunderstanding regarding the use of Generics here. Could it be that in the definition of selectCovered() the clause <T extends Annotation> allows only direct sub-types of Annotation? Interestingly enough, if I don’t parameterize the sub-class FEs, I don’t get any errors. 
>> Thanks a lot. 
>> 
>> Cheers, 
>> 
>> Martin
>