You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@uima.apache.org by Mansi verma <ve...@gmail.com> on 2013/05/30 09:43:36 UTC

External Resource Creation

Hi

I have to load a set of files as external resources in uima. The annotator
reads a file containing the list of files to load in the engine as shared
resource. Since the number of files may change, I cannot bind them in the
descriptor.

To accomplish this I used the following snippet of code. However it throws
an exception of invalid class cast.

ExternalResourceDescription extDesc = ExternalResourceFactory.
            createExternalResourceDescription(key,MyResource_impl.class,
fileName);
Map<String, Object> initParams =  new HashMap<String, Object>() ;
MyResource file =
(MyResource_impl)UIMAFramework.produceResource(extDep..getResourceSpecifier(),
initParams);

Now the error is as follows :
Caused by: java.lang.ClassCastException:
org.apache.uima.resource.impl.ConfigurableDataResource_impl cannot be cast
to org.my.project.resource.MyResource_impl

I tried an alternate approach too wherein I declared
ExternalResourceDependency but I do not know how to create an external
resource object using this dependency. The code is as follows

ExternalResourceDependency extDep =
UIMAFramework.getResourceSpecifierFactory().createExternalResourceDependency();
                extDep.setDescription("file for "+key);
                extDep.setInterfaceName(MyResource_impl.class.toString());
                extDep.setKey(key+"File");
                extDep.setOptional(false);
                extDep.setSourceUrl(new URL(fileName));

Thanks
Manisha

Re: External Resource Creation

Posted by Richard Eckart de Castilho <ri...@gmail.com>.
Am 30.05.2013 um 12:11 schrieb Mansi verma <ve...@gmail.com>:

> Ok
> 
> I am making a mistake. I dont want to use UimaFit to initialize the
> resource. If I initialize it using uimafit, i will have to run the pipeline
> using uimafit too. Am I right ?

You are not right (at least you shoudn't be). Descriptors built with uimaFIT
should work with any UIMA runtime environment (uimaFIT, CPE, UIMA-AS, …).
There maybe things to observe, like making sure the type systems are properly
communicated to the runtime environment, but in general an XML descriptor
generated with uimaFIT has nothing uimaFIT specific in it. If it turns out
there is a problem loading and using an uimaFIT-generated XML descriptor,
that would be considered a bug.

If your components use the uimaFIT annotations (@ConfigurationParameter,
@ExternalResource), you need to have uimaFIT on the classpath, but you
do not need to actually run your pipeline with uimaFIT. You should be able
to run your pipeline with any UIMA runtime environment (uimaFIT, CPE, UIMA-AS, …).
If it turns out there is a problem, that would be a bug.

> I want to be able to initialize the external Shared Resource my self, as
> the number of such resources are unknown, they are passed at run time. So
> the pipeline has to take in what resources to load from some file and then
> load them. Is that possible.

UIMA does not allow passing pre-instantiated objects into a pipeline. It wants
to be able to instantiate everything itself via its ResourceManager mechanism.
Hence all the fuzz about the (XML) descriptors. It's possible, of course, to
to everything manually and use a custom resource manager implementation. There
is a proof-of-concept for this in the uimaFIT svn, but that wouldn't work with
any arbitrary UIMA runtime environment then.

As far as I can tell, the UIMA resource mechanism is not designed to operate
with unknown numbers of resources. A component in UIMA declares a resource
dependency with a specific key. Only one resource can be bound to that key,
not multiple. So in the concept of UIMA using static descriptors, the number
of external resources used by a component must be known. See also [1].

If you dynamically create your descriptors using plain UIMA or uimaFIT, you
can decide at creation time how many dependencies you create. But there is
no official way you can determine the number/names of available resources
at runtime, even if you dynamically created the bindings. You could pass in the
names of the bound resources via an additional multi-valued parameter or you'd
need to use reflection to inspect private variables of the UIMA framework.
See also [2]

> I see that I can produce a resource using UIMAFramework.produceResource but
> it requires a resourceSpecifier. I do not know how to initialize a resource
> specifier. I was trying to use uimafit to first generate the external
> resource description and then use its resource specifier to call
> produceResource function.
> 
> This approach is cumbersome and isnt working!! So is there a way to
> initialize ResourceSpecifier so that I can use produceResource function
> from uimaFramework.

Maybe some other UIMA developer/user can provide additional insight.

Cheers,

-- Richard

[1] https://issues.apache.org/jira/browse/UIMA-2801
[2] https://issues.apache.org/jira/browse/UIMA-2903


Re: External Resource Creation

Posted by Mansi verma <ve...@gmail.com>.
Ok

I am making a mistake. I dont want to use UimaFit to initialize the
resource. If I initialize it using uimafit, i will have to run the pipeline
using uimafit too. Am I right ?

I want to be able to initialize the external Shared Resource my self, as
the number of such resources are unknown, they are passed at run time. So
the pipeline has to take in what resources to load from some file and then
load them. Is that possible.

I see that I can produce a resource using UIMAFramework.produceResource but
it requires a resourceSpecifier. I do not know how to initialize a resource
specifier. I was trying to use uimafit to first generate the external
resource description and then use its resource specifier to call
produceResource function.

This approach is cumbersome and isnt working!! So is there a way to
initialize ResourceSpecifier so that I can use produceResource function
from uimaFramework.


Sorry for the trouble.

Thanks
Manisha
On Thu, May 30, 2013 at 3:13 PM, Richard Eckart de Castilho <
richard.eckart@gmail.com> wrote:

> Hi,
>
> since you are using uimaFIT, did you look at the documentation here
>
> https://code.google.com/p/uimafit/wiki/ExternalResources
>
> In particular, it makes a difference if the component you want to inject
> the resource into uses the uimaFIT @ExternalResource annotation and
> inherits from the uimaFIT base classes for resource or invokes
> ExternalResourceInitializer itself. If that is the case, the uimaFIT
> factory create* factory methdos already create the
> ExternalResourceDependency for you.
>
> If you are using a non-uimaFIT-aware component, you need to use
> createDependencyAndBind.
>
> That said, you seem to try to instantiate the external resource yourself
> (maybe as a test?), normally the UIMA ResourceManager should do that for
> you when you run a pipeline. At appears to me that ResourceManager_impl is
> not using UIMAFramework.produceResource()  instantiate the resource (cf.
> UIMA 2.4.0 ResourceManager_impl lines 520ff and 290ff). So I suppose there
> is a chance it works if you just don't try to instantiate the resource
> yourself and leave it to UIMA.
>
> Cheers,
>
> -- Richard
>
> Am 30.05.2013 um 09:43 schrieb Mansi verma <ve...@gmail.com>:
>
> > Hi
> >
> > I have to load a set of files as external resources in uima. The
> annotator
> > reads a file containing the list of files to load in the engine as shared
> > resource. Since the number of files may change, I cannot bind them in the
> > descriptor.
> >
> > To accomplish this I used the following snippet of code. However it
> throws
> > an exception of invalid class cast.
> >
> > ExternalResourceDescription extDesc = ExternalResourceFactory.
> >            createExternalResourceDescription(key,MyResource_impl.class,
> > fileName);
> > Map<String, Object> initParams =  new HashMap<String, Object>() ;
> > MyResource file =
> >
> (MyResource_impl)UIMAFramework.produceResource(extDep..getResourceSpecifier(),
> > initParams);
> >
> > Now the error is as follows :
> > Caused by: java.lang.ClassCastException:
> > org.apache.uima.resource.impl.ConfigurableDataResource_impl cannot be
> cast
> > to org.my.project.resource.MyResource_impl
> >
> > I tried an alternate approach too wherein I declared
> > ExternalResourceDependency but I do not know how to create an external
> > resource object using this dependency. The code is as follows
> >
> > ExternalResourceDependency extDep =
> >
> UIMAFramework.getResourceSpecifierFactory().createExternalResourceDependency();
> >                extDep.setDescription("file for "+key);
> >                extDep.setInterfaceName(MyResource_impl.class.toString());
> >                extDep.setKey(key+"File");
> >                extDep.setOptional(false);
> >                extDep.setSourceUrl(new URL(fileName));
> >
> > Thanks
> > Manisha
>
>

Re: External Resource Creation

Posted by Richard Eckart de Castilho <ri...@gmail.com>.
Hi,

since you are using uimaFIT, did you look at the documentation here

https://code.google.com/p/uimafit/wiki/ExternalResources

In particular, it makes a difference if the component you want to inject the resource into uses the uimaFIT @ExternalResource annotation and inherits from the uimaFIT base classes for resource or invokes ExternalResourceInitializer itself. If that is the case, the uimaFIT factory create* factory methdos already create the ExternalResourceDependency for you. 

If you are using a non-uimaFIT-aware component, you need to use createDependencyAndBind.

That said, you seem to try to instantiate the external resource yourself (maybe as a test?), normally the UIMA ResourceManager should do that for you when you run a pipeline. At appears to me that ResourceManager_impl is not using UIMAFramework.produceResource()  instantiate the resource (cf. UIMA 2.4.0 ResourceManager_impl lines 520ff and 290ff). So I suppose there is a chance it works if you just don't try to instantiate the resource yourself and leave it to UIMA.

Cheers,

-- Richard

Am 30.05.2013 um 09:43 schrieb Mansi verma <ve...@gmail.com>:

> Hi
> 
> I have to load a set of files as external resources in uima. The annotator
> reads a file containing the list of files to load in the engine as shared
> resource. Since the number of files may change, I cannot bind them in the
> descriptor.
> 
> To accomplish this I used the following snippet of code. However it throws
> an exception of invalid class cast.
> 
> ExternalResourceDescription extDesc = ExternalResourceFactory.
>            createExternalResourceDescription(key,MyResource_impl.class,
> fileName);
> Map<String, Object> initParams =  new HashMap<String, Object>() ;
> MyResource file =
> (MyResource_impl)UIMAFramework.produceResource(extDep..getResourceSpecifier(),
> initParams);
> 
> Now the error is as follows :
> Caused by: java.lang.ClassCastException:
> org.apache.uima.resource.impl.ConfigurableDataResource_impl cannot be cast
> to org.my.project.resource.MyResource_impl
> 
> I tried an alternate approach too wherein I declared
> ExternalResourceDependency but I do not know how to create an external
> resource object using this dependency. The code is as follows
> 
> ExternalResourceDependency extDep =
> UIMAFramework.getResourceSpecifierFactory().createExternalResourceDependency();
>                extDep.setDescription("file for "+key);
>                extDep.setInterfaceName(MyResource_impl.class.toString());
>                extDep.setKey(key+"File");
>                extDep.setOptional(false);
>                extDep.setSourceUrl(new URL(fileName));
> 
> Thanks
> Manisha