Re: [Py4j-users] How do I cast Java objects between types on the Python side?
Status: Beta
Brought to you by:
barthe
|
From: Chris A. <chr...@gm...> - 2013-09-07 05:53:46
|
Hi, all.
Issue's resolved. I had to create the array of HGHandles that the
constructor was expecting by hand, so
Java:
x=new HGValueLink("asdf", h1, h2)
became Python:
a=new_array(HGHandle, 2);
a[0]=h1;
a[1]=h2;
x=gateway.jvm.org.hypergraphdb.hGValueLink("asdf",a)
Thanks for your hard work.
-Chris
On Sat, Sep 7, 2013 at 12:16 AM, Chris Akins <chr...@gm...> wrote:
> Hi all.
>
> I'm trying to implement this code in Python: (from
> http://www.hypergraphdb.org/learn?page=IntroStoreData&project=hypergraphdbat the bottom, modified so I don't have to implement a class on the Java
> side)
>
> bookHandle = graph.add("asdf")
> priceHandle = graph.add(9.95)
> link = new gateway.jvm.org.hypergraphdb.HGValueLink("price", bookHandle,
> priceHandle)
>
>
>
> The problem is that in the Python code, the call to graph.add() from the Python side is yielding an object of type
> WeakHandle that implements HGLiveHandle, and HGValueLink only accepts HGHandle[] in its arguments. The end result is
>
>
> that the java side throws an exception saying that a HGValueLink constructor doesn't exist that takes
> [String, handle.WeakHandle, handle.WeakHandle].
>
> (Relevant javadocs at http://www.kobrix.com/javadocs/hgcore/org/hypergraphdb/HGValueLink.html,
>
> http://www.kobrix.com/javadocs/hgcore/org/hypergraphdb/HyperGraph.html,
> http://www.kobrix.com/javadocs/hgcore/org/hypergraphdb/handle/HGLiveHandle.html )
>
> In the linked example the two handles are declared as HGHandle when they're instantiated, so there's no problem when the method call does typechecking.
>
> It seems to me, then, that the easiest way to rectify this is to use some facility of the Gateway to re-cast the object returned from graph.add() as HGHandle, and then just pass that the the HGValueLink constructor as it wants.
>
> Can I do that from the gateway as it exists, or do I have to add a method to my server code to cast it? Am I missing an easier and more obvious solution in my Java inexperience?
>
> Thanks,
> Chris
>
>
|