I found the Solution. !!!! :-)
I hope it help other people with the same issues, this worked out for
me. I read clojure code on github and java code as well after some
time struggling with the objects and functions I found this solution:
package com.github.diegopacheco.sandbox.java.clojure;
import clojure.lang.RT;
import clojure.lang.Var;
/**
*
* @author Diego Pacheco
*
*/
public class JavaCallsClojureInterpreted {
private static final Var seq = RT.var("clojure.core",
"seq");
private static final Var type = RT.var("clojure.core",
"type");
private static final Var keyword = RT.var("clojure.core",
"keyword");
private static final Var hashMap = RT.var("clojure.core", "hash-
map");
private static final Var structMap = RT.var("clojure.core",
"struct-map");
public static void loadClojureScript(String script){
try {
RT.loadResourceScript(script);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Object buildKeyword(String k){
try {
return keyword.invoke(k);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Object buildSeq(Object o){
try {
return seq.invoke(o);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Object buildUserTypeDef(String ns,String structDefName)
{
try {
Var userType = RT.var(ns, structDefName);
return userType.get();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Var buildUserFunction(String ns,String func){
try {
return RT.var(ns, func);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Object infoType(Object o){
try {
return type.invoke(o);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Object buildHashMap(Object... mapArgs){
try {
Object userSeq = buildSeq(mapArgs);
return hashMap.applyTo((clojure.lang.ISeq)userSeq);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String args[]) throws Throwable{
try{
loadClojureScript("company.clj");
Object employeeStructDef =
buildUserTypeDef("user","employee");
System.out.println(infoType(employeeStructDef));
Object oEmployee =
structMap.invoke(employeeStructDef,buildKeyword("name"),"Diego",buildKeyword("age"),
26,
buildKeyword("role"),"Coach",buildKeyword("salary"),1000);
System.out.println(oEmployee);
Var hire = buildUserFunction("user", "hire");
Object r = hire.invoke(oEmployee);
System.out.println(r);
Var printee = buildUserFunction("user", "print-employee");
Object r2 = printee.invoke(oEmployee);
System.out.println(r2);
Var print = buildUserFunction("user", "print-employees");
Object result = print.invoke();
System.out.println(result);
}catch(Exception e){
e.printStackTrace();
}
}
}
Cheers,
@diego_pacheco
On Oct 17, 2:23 am, Diego Pacheco <[email protected]> wrote:
> You Right! BUT still does not work.
>
> I tried:
>
> Var type = RT.var("clojure.core","type");
> Object oType = type.invoke("employee");
>
> Var struct = RT.var("clojure.core", "struct-map");
> Object oStruct =
> struct.invoke(oType,"Diego",26,"Coach",1000);
>
> Var hire = RT.var("user", "hire");
> Object r = hire.invoke(oStruct);
> System.out.println(r);
>
> ......
>
> I got the exception:
>
> java.lang.ClassCastException: java.lang.Class cannot be cast to
> clojure.lang.PersistentStructMap$Def
> at clojure.core$struct_map.doInvoke(core.clj:3128)
> at clojure.lang.RestFn.invoke(RestFn.java:486)
> at clojure.lang.Var.invoke(Var.java:381)
> at
> com.github.diegopacheco.sandbox.java.clojure.JavaCallsClojureInterpreted.ma
> in(JavaCallsClojureInterpreted.java:
> 42)
>
> .............
>
> When I do:
>
> Var struct = RT.var("clojure.core", "create-
> struct");
> Object oStruct =
> struct.invoke("employee","Diego",26,"Coach",
> 1000);
>
> Var hire = RT.var("user", "hire");
> Object r = hire.invoke(oStruct);
> System.out.println(r);
>
> Var printee = RT.var("user", "print-employee");
> Object r2 = printee.invoke(oStruct);
> System.out.println(r2);
>
> Var print = RT.var("user", "print-employees");
> Object result = print.invoke();
> System.out.println(result);
>
> ............
>
> I GOT:
>
> [#<Def clojure.lang.PersistentStructMap$Def@6b6ac8>]
> "Name: - Age: - Role: - Salary:"
> null
> "Name: - Age: - Role: - Salary:"
> clojure.lang.LazySeq@1f
>
> ...........
>
> When I DO:
>
> Var struct = RT.var("clojure.core", "create-struct");
> Object oStruct =
> struct.invoke("employee","Diego",26,"Coach",
> 1000);
>
> Var employee = RT.var("clojure.core", "struct");
> Object oEmployee = employee.invoke(oStruct);
>
> Var hire = RT.var("user", "hire");
> Object r = hire.invoke(oEmployee);
> System.out.println(r);
>
> Var printee = RT.var("user", "print-employee");
> Object r2 = printee.invoke(oEmployee);
> System.out.println(r2);
>
> Var print = RT.var("user", "print-employees");
> Object result = print.invoke();
> System.out.println(result);
>
> .......
>
> I Got:
>
> [{"employee" nil, "Diego" nil, 26 nil, "Coach" nil, 1000 nil}]
> "Name: - Age: - Role: - Salary:"
> null
> "Name: - Age: - Role: - Salary:"
> clojure.lang.LazySeq@1f
>
> I'm loosing my hope, does anybody used this feature before ? Any idea
> what I'm doing wrong ?
>
> Cheers,
> @diego_pacheco
>
> On Oct 14, 3:58 am, Alexander Taggart <[email protected]> wrote:
>
>
>
>
>
>
>
> > I suspect this...
>
> > Var struct = RT.var("clojure.core", "struct","employee :diego 10 :coach
> > 1000");
>
> > isn't doing what you think it will. That will set the root binding of
> > clojure.core/struct to the string you gave it. you probably want to get the
> > var for the struct function, and then invoke it with the arguments to create
> > an employee struct instance.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en