I made a little test case. To isolate things to just how array-valued
parameters are programmatically set and accessed, I used a dummy annotator from
the uimaj-examples project called the "RoomNumberAnnotator" - the version I used
has no "initialize" of its own (so it didn't do anything in its initialize
method).
Then I did the following "driver" code, modelled after your code, except I only
did one parameter - the string array valued p1:
public static void main(String[] args) throws
ResourceInitializationException {
AnalysisEngineDescription_impl primitiveDesc = new
AnalysisEngineDescription_impl();
primitiveDesc.setPrimitive(true);
primitiveDesc.getMetaData().setName("test of setting string array
parameters");
primitiveDesc.setAnnotatorImplementationName("org.apache.uima.tutorial.ex1.RoomNumberAnnotator");
ConfigurationParameter p1 = new ConfigurationParameter_impl();
p1.setMultiValued(true); // needed to have this parameter take an array of
values
p1.setName("AttributeList");
p1.setDescription("Attribute List");
p1.setType(ConfigurationParameter.TYPE_STRING); // This specifies the type
of each element of the array
primitiveDesc.getMetaData().getConfigurationParameterDeclarations().setConfigurationParameters(
new ConfigurationParameter[] { p1 });
// This sets the configuration parameter to a value which is an array of
Strings, "v1" and "v2"
primitiveDesc.getMetaData().getConfigurationParameterSettings().setParameterSettings(
new NameValuePair[] {
new NameValuePair_impl("AttributeList", new String[] {"v1", "v2"})}
);
TypeSystemDescription typeSystem = new TypeSystemDescription_impl();
primitiveDesc.getAnalysisEngineMetaData().setTypeSystem(typeSystem);
// For testing - make an UIMA Analysis Engine from this specification
AnalysisEngine ae = UIMAFramework.produceAnalysisEngine(primitiveDesc);
// Get the UIMA Context from where we can try getting the configuration
parameter settings
UimaContext context = ae.getUimaContext();
// Get the configured value
Object v = context.getConfigParameterValue("AttributeList");
System.out.format("Type of object returned is %s, object is %s%n",
v.getClass(), v) ;
// print out the individual strings in the array
String[] av = (String[]) v;
System.out.format("The two values are %s and %s%n", av[0], av[1]);
}
I ran this and it printed out:
Type of object returned is class [Ljava.lang.String;, object is
[Ljava.lang.String;@45a36ab6
The two values are v1 and v2.
No errors were thrown.
Hope that example helps.
-Marshall
On 5/22/2013 11:08 AM, harshal patni wrote:
> Yes I did! When I do that it throws the following error -
> It throws a cast error stating Type String cannot be converted to String
> array.
>
> Harshal
>
>
> On Wed, May 22, 2013 at 8:03 PM, Marshall Schor <[email protected]> wrote:
>
>> did you have the multi-values setting turned on in the parameter
>> specification?
>>
>>
>> On 5/22/2013 10:15 AM, harshal patni wrote:
>>> Hi Marshall,
>>> Thank you for your response. Yes I tried setting and
>> array
>>> as a second argument of NameValuePair_Impl. However it throws an error
>>> since the setType() has only 4 options (String, Boolean, Float,
>> Integer). I
>>> had to set it to string forcibly, coz if I dont UIMA by default sets it
>> to
>>> NULL which again causes an error.
>>>
>>> Harshal
>>>
>>>
>>> On Wed, May 22, 2013 at 7:33 PM, Marshall Schor <[email protected]> wrote:
>>>
>>>> most likely this is a problem in building the parameter setting.
>>>>
>>>> I did one of these using the Eclipse editor for UIMA XML files, and
>> looked
>>>> at
>>>> what it generated as the setting:
>>>>
>>>> <configurationParameterSettings>
>>>> <nameValuePair>
>>>> <name>astringarray</name>
>>>> <value>
>>>> <array>
>>>> <string>x1</string>
>>>> <string>x2</string>
>>>> </array>
>>>> </value>
>>>> </nameValuePair>
>>>> </configurationParameterSettings>
>>>>
>>>> Have you tried creating NameValuePair_impl instances, with arrays as the
>>>> 2nd
>>>> argument (i.e., the "value")?
>>>>
>>>> for instance:
>>>> ... new NameValuePair_impl("AttributeList", new String[] {"value1",
>>>> "value2",
>>>> ...}) ...
>>>>
>>>> -Marshall
>>>>
>>>>
>>>> On 5/21/2013 11:34 PM, harshal patni wrote:
>>>>> Hello Himanshu,
>>>>> I tried setting the setMultivalues(true). The
>>>>> problem is the following -
>>>>> I use setType (String). I need to do this coz else it takes it as null.
>>>> But
>>>>> as soon as we setType(String) it throws a cast error saying string
>> cannot
>>>>> be cast to a String array. If you look at the source code of
>>>> conceptMapper
>>>>> it tried to cast in code.
>>>>>
>>>>> Hope that makes sense..
>>>>> Any idea how this could be resolved?
>>>>> Please also add code snippet to make it easier to understand.
>>>>>
>>>>> Thanks,
>>>>> Harshal
>>>>>
>>>>>
>>>>> On Wed, May 22, 2013 at 1:34 AM, Himanshu Gahlot <
>>>>> [email protected]> wrote:
>>>>>
>>>>>> Use setMultiValued()<
>>>>>>
>> http://uima.apache.org/downloads/releaseDocs/2.3.0-incubating/docs/api/org/apache/uima/resource/metadata/ConfigurationParameter.html#setMultiValued%28boolean%29
>>>>>>> .
>>>>>> So, p1.setMultiValued() should make this config param accept multiple
>>>>>> String values.
>>>>>>
>>>>>> Himanshu
>>>>>>
>>>>>>
>>>>>> On Tue, May 21, 2013 at 1:10 AM, Renaud Richardet
>>>>>> <[email protected]>wrote:
>>>>>>
>>>>>>> Hi Harshal,
>>>>>>> Have you tried UimaFit? I think it would be a better option, if you
>> do
>>>>>> not
>>>>>>> want to use XML descriptors.
>>>>>>> Hope that helps, Renaud
>>>>>>>
>>>>>>>
>>>>>>> On Tue, May 21, 2013 at 8:49 AM, harshal patni <
>>>> [email protected]
>>>>>>>> wrote:
>>>>>>>> Hello Everyone,
>>>>>>>> I am currently writing ConceptMapper in code (not
>>>>>>> using
>>>>>>>> XML files). Basically I am definitely AnalysisEngineDescriptions and
>>>>>>>> TypeSystem Description in java code. I create the following 2
>>>>>> parameters
>>>>>>>> using "ConfigurationParameter" class.
>>>>>>>>
>>>>>>>> 1. AttributeList
>>>>>>>> 2. FeatureList
>>>>>>>>
>>>>>>>> The problem is both these should be arrays. But
>> ConfigurationParameter
>>>>>>> only
>>>>>>>> provide the following types (String, Boolean, Integer, Float).
>> When I
>>>>>>> pass
>>>>>>>> these values as string it fails and throws a
>>>>>> java.lang.ClassCastException
>>>>>>>> error. COz it expects an array vs a String. How should I solve this
>>>>>>> issue?
>>>>>>>> Below is the AnalysisEngineDescription code.
>>>>>>>>
>>>>>>>>
>> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
>>>>>>>> primitiveDesc = new AnalysisEngineDescription_impl();
>>>>>>>>
>>>>>>>> primitiveDesc.setPrimitive(true);
>>>>>>>>
>>>>>>>> primitiveDesc.getMetaData().setName("Concept Mapper Offset
>>>>>> Tokenizer");
>>>>>>>> primitiveDesc.setAnnotatorImplementationName(
>>>>>>>> "org.apache.uima.conceptMapper.ConceptMapper");
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> ConfigurationParameter p1 = new ConfigurationParameter_impl();
>>>>>>>>
>>>>>>>> p1.setName("AttributeList");
>>>>>>>>
>>>>>>>> p1.setDescription("Attribute List");
>>>>>>>>
>>>>>>>> p1.setType(ConfigurationParameter.TYPE_STRING); // Here is the
>>>> problem
>>>>>>>> ConfigurationParameter p2 = new ConfigurationParameter_impl();
>>>>>>>>
>>>>>>>> p2.setName("FeatureList");
>>>>>>>>
>>>>>>>> p2.setDescription("Feature List");
>>>>>>>>
>>>>>>>> p2.setType(ConfigurationParameter.TYPE_STRING);
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>> primitiveDesc.getMetaData().getConfigurationParameterDeclarations().setConfigurationParameters(
>>>>>>>> new ConfigurationParameter[] { p1,p2 });
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>> primitiveDesc.getMetaData().getConfigurationParameterSettings().setParameterSettings(
>>>>>>>> new NameValuePair[] { new NameValuePair_impl("AttributeList",
>>>>>>> "canonical"),
>>>>>>>> new NameValuePair_impl("FeatureList", "DictCanon") });
>>>>>>>>
>>>>>>>> TypeSystemDescription typeSystem = new
>>>>>>> TypeSystemDescription_impl();
>>>>>>>> dicTerm(typeSystem);
>>>>>>>>
>>>>>>>> conceptMapperTokenizer(typeSystem);
>>>>>>>>
>>>>>>>> baseTokenizer(typeSystem);
>>>>>>>>
>>>>>>>>
>>>>>> primitiveDesc.getAnalysisEngineMetaData().setTypeSystem(typeSystem);
>> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
>>>>>>>> Also how should I pass the dictionary file in code?
>>>>>>>>
>>>>>>>>
>>>>>>>> Thanks,
>>>>>>>>
>>>>>>>> Harshal
>>>>>>>>
>>>>>>> --
>>>>>>> Renaud Richardet
>>>>>>> Blue Brain Project PhD candidate
>>>>>>> EPFL Station 15
>>>>>>> CH-1015 Lausanne
>>>>>>> phone: +41-78-675-9501
>>>>>>> http://people.epfl.ch/renaud.richardet
>>>>>>>
>>
>