Hello all!
I know that UIMA-FIT is capable of initializing enum-typed configuration
parameters from a string equal to the name of one of the values in an enum
class. i.e. this works as documented:
public static enum SomeEnum {
A_VALUE;
}
@ConfigurationParameter( name = "example", mandatory = false,
defaultValue="A_VALUE" )
private SomeEnum enumValue;
However, I recently had to refactor one my enums into a normal class with
static members, and I found that UIMA-FIT is equally capable of
initializing these parameters from a string equal to the symbol of a class
instance set up as a static member i.e. this also works:
public static class SomeClass {
public static final SomeClass A_VALUE = new SomeClass()
}
@ConfigurationParameter( name = "example", mandatory = false,
defaultValue="A_VALUE" )
private SomeClass classValue;
Is this known behaviour? I tried stepping through the conf param
initialization logic, but I got lost in the depths of Spring, and I get the
feeling that this is basically a side-effect of spring's implementation of
enum initialization details, which seems a little unreliable... Then again,
enum types are basically immutable collections of class instances as static
members, no?
Is it reasonable to use this "feature"? if so, should it be mentioned in
the documentation?
I have attached a working example of what I mean if my explanation above
doesn't make sense.
Any comments would be welcome, I'm very curious to know why this works and
whether it is reliable enough to be used in production...
Thanks!
jta
ps: reposting from the abandoned uima-fit mailing list. Sorry for
crossposting!
--
sent from a phone. please excuse terseness and tpyos.
enviado desde un teléfono. por favor disculpe la parquedad y los erroers.
package demo;
import java.io.IOException;
import org.apache.uima.analysis_engine.AnalysisEngineDescription;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.cas.CAS;
import org.apache.uima.collection.CollectionException;
import org.apache.uima.collection.CollectionProcessingEngine;
import org.apache.uima.collection.CollectionReaderDescription;
import org.apache.uima.collection.EntityProcessStatus;
import org.apache.uima.collection.StatusCallbackListener;
import org.apache.uima.collection.metadata.CpeDescriptorException;
import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
import org.apache.uima.fit.component.JCasCollectionReader_ImplBase;
import org.apache.uima.fit.cpe.CpeBuilder;
import org.apache.uima.fit.descriptor.ConfigurationParameter;
import org.apache.uima.fit.factory.AnalysisEngineFactory;
import org.apache.uima.fit.factory.CollectionReaderFactory;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.util.InvalidXMLException;
import org.apache.uima.util.Progress;
import org.xml.sax.SAXException;
public class StaticInit extends JCasAnnotator_ImplBase {
public static final String PARAM_EX1 = "ex1";
@ConfigurationParameter( name = PARAM_EX1, mandatory = false,
defaultValue = "SOME_VALUE"
)
private Example1 ex1;
public static final String PARAM_EX2 = "ex2";
@ConfigurationParameter( name = PARAM_EX1, mandatory = false,
defaultValue = "SOME_VALUE"
)
private Example2 ex2;
@Override
public void process( JCas aJCas ) throws AnalysisEngineProcessException {
System.out.println( ex1.foo );
System.out.println( ex2.foo );
}
public static enum Example1 {
SOME_VALUE( "This is an enum", "" )
;
private final String foo;
private final String bar;
private Example1( String foo, String bar ) {
this.foo = foo;
this.bar = bar;
}
}
public static class Example2 {
public static final Example2 SOME_VALUE = new Example2( "This is a static member", "" );
private final String foo;
private final String bar;
private Example2( String foo, String bar ) {
this.foo = foo;
this.bar = bar;
}
}
public static void main( String[] args ) throws ResourceInitializationException, IOException, SAXException, CpeDescriptorException, InvalidXMLException {
CollectionReaderDescription crd = CollectionReaderFactory.createReaderDescription( SomeReader.class );
AnalysisEngineDescription ae = AnalysisEngineFactory.createEngineDescription(StaticInit.class );
CpeBuilder cpb = new CpeBuilder();
cpb.setReader( crd );
cpb.setAnalysisEngine( ae );
CollectionProcessingEngine cpe = cpb.createCpe( new SomeCallcakListener() );
cpe.process();
}
public static class SomeReader extends JCasCollectionReader_ImplBase {
private boolean next = true;
public SomeReader() {}
@Override public void getNext( JCas jCas ) throws IOException, CollectionException {}
@Override public boolean hasNext() throws IOException, CollectionException {
boolean now = next;
this.next = false;
return now;
}
@Override public Progress[] getProgress() { return null; }
}
public static class SomeCallcakListener implements StatusCallbackListener {
@Override public void entityProcessComplete( CAS aCas, EntityProcessStatus aStatus ) {}
@Override public void initializationComplete() {}
@Override public void batchProcessComplete() {}
@Override public void collectionProcessComplete() {}
@Override public void paused() {}
@Override public void resumed() {}
@Override public void aborted() {}
}
}