I am using the Lobos library - https://github.com/budu/lobos In it there's a function create, which is called like this - (create db (table :some-name)), where db is earlier defined as - (def db {:classname "org.postgresql.Driver" :subprotocol "postgresql" :user "postgres" :password "" :subname "//localhost:5432/flyway"})
If I have to call the above function from Java, how do I - 1) define the def (do I have to load some variable)? 2) I am loading the reqd. lobos libraries using [ RT.load("lobos/core", true); ] and then getting the reference to the create function like - [ Var foo = RT.var("lobos.core", "create"); ] But how do I pass the required arguments to this function. Do I need to create an array?
you should go through the normal Clojure Vars to access the functions. To actually call the functions you use invoke. Note: table is a macro, so you can't call it directly. You have to use the table* function and do any sugar provided by the table macro yourself. Here is an example how this could look like:
import clojure.lang.RT; import clojure.lang.Var;
public class Foo { // Required core functions. static final Var symbol = RT.var("clojure.core", "symbol"); static final Var require = RT.var("clojrue.core", "require"); static final Var keyword = RT.var("clojure.core", "keyword");
On Tue, Jul 26, 2011 at 12:43 PM, mmwaikar <mmwai...@gmail.com> wrote: > Hi,
> I am using the Lobos library - https://github.com/budu/lobos > In it there's a function create, which is called like this - (create db > (table :some-name)), where db is earlier defined as - (def db > {:classname "org.postgresql.Driver" > :subprotocol "postgresql" > :user "postgres" > :password "" > :subname "//localhost:5432/flyway"})
> If I have to call the above function from Java, how do I - > 1) define the def (do I have to load some variable)? > 2) I am loading the reqd. lobos libraries using [ RT.load("lobos/core", > true); ] and then getting the reference to the create function like - [ Var > foo = RT.var("lobos.core", "create"); ] > But how do I pass the required arguments to this function. Do I need to > create an array?
If i were doing same thing I would put all necessary initalization into a Clojure script and wrapped necessary functions to have less arguments to pass from Java. This way later you'll need to load single script only. Say
(ns (use lobos.core)) (def db {.....}) (defn create2 [table-name] (create db (table table-name)))
This might not be convenient in your case however.
static final Var symbol = RT.var("clojure.core", "symbol"); static final Var require = RT.var("clojrue.core", "require"); static final Var keyword = RT.var("clojure.core", "keyword");
static final Var create = RT.var("lobos.core", "create"); static final Var table = RT.var("lobos.schema", "table*"); static final Var debuglevel = RT.var("lobos.core", "set-debug-level");
and called wrapper.createTable() then I get - (#<core$create_STAR_ lobos.core$create_STAR_@49431028> (quote {:classname "org.postgresql.Driver", :subprotocol "postgresql", :user "postgres", :password "lindb2011", :subname "//localhost:5432/flyway"}))
I really don't understand the output, but it is definitely not what I am expecting.
Please let me know.
Thanks Petr, I have gone through that link, and I'll try this approach if the first one doesn't work.
You should go through require.invoke(). Not RT.load().
> and called wrapper.createTable() then I get - (#<core$create_STAR_ lobos.core$create_STAR_@49431028> (quote {:classname "org.postgresql.Driver", :subprotocol "postgresql", :user "postgres", :password "lindb2011", :subname "//localhost:5432/flyway"}))
> I really don't understand the output, but it is definitely not what I am expecting.
create is obviously also a macro. You'll have to use create*.
This is my last information on how the officially blessed way to interface with Clojure from Java looks like. I'm not sure about RT.map.
If the thing you want to call is a macro, you have to hope that the library other provided the logic as star function (as in Nicolas' case). If there is no function containing the actual logic, you have to re-implement the macro in Java. cf. http://stackoverflow.com/questions/6672934/how-to-call-clojure-macros...
It may also be useful to read up on primitives, since primitive support is often a source of impedance mismatch when software in one language talks to software in another. Would someone mind supplying a link to a description of how Clojure works with Java primitives in the 1.2.1 and 1.3 releases?
On Thu, Jul 28, 2011 at 1:58 PM, Meikel Brandmeyer <m...@kotka.de> wrote: > If the thing you want to call is a macro, you have to hope that the library other provided the logic as star function (as in Nicolas' case). If there is no function containing the actual logic, you have to re-implement the macro in Java. cf. http://stackoverflow.com/questions/6672934/how-to-call-clojure-macros...
There's also the eval(read(whatever)) route, if you need to resort to it.
-- Protege: What is this seething mass of parentheses?! Master: Your father's Lisp REPL. This is the language of a true hacker. Not as clumsy or random as C++; a language for a more civilized age.