JESS: [EXTERNAL] Issue with static methods (or generics?)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

JESS: [EXTERNAL] Issue with static methods (or generics?)

Aurelien Mazurie
Dear Jess users,
I am having trouble instantiating a Java class from within a rule's RHS, and cannot figure out how to solve this problem.

The code in my RHS creates an instance of a class from the JGraphT library. As seen in  https://github.com/jgrapht/jgrapht/blob/master/jgrapht-demo/src/main/java/org/jgrapht/demo/HelloJGraphT.java (lines 91-92), the creation of a DefaultDirectedGraph instance require another class as argument; here, DefaultEdge.

I couldn't find an example in the Jess documentation about how to do that. For example, I tried the following:

(bind ?edge-class org.jgrapht.graph.DefaultEdge)
(bind ?graph (new org.jgrapht.graph.DefaultDirectedGraph ?edge-class))

I also tried variations such as

(bind ?edge-class org.jgrapht.graph.DefaultEdge)
(bind ?graph (new org.jgrapht.graph.DefaultDirectedGraph (?edge-class getClass))

or

(bind ?edge-class (new org.jgrapht.graph.DefaultEdge))
(bind ?graph (new org.jgrapht.graph.DefaultDirectedGraph ?edge-class))

Any idea about what the syntax should be to properly instantiate the DefaultDirectedGraph class? For information (but not certain how relevant it is here), I succeeded in the past in using java.util.regex.Pattern and its static method 'compile' by typing

(bind ?pattern-class java.util.regex.Pattern)
(bind ?matches ((?pattern-class compile "...") matcher ?string))

Best,
Aurélien
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

RE: JESS: [EXTERNAL] Issue with static methods (or generics?)

Friedman-Hill, Ernest

Java syntax like “DefaultEdge.class” is (more or less) just syntactic sugar for ‘java.lang.Class.forName(“org.jgrapht.graph.DefaultEdge”);’ which in Jess will come out like “(Class.forName org.jgrapht.graph.DefaultEdge)”  (taking advantage of the default static imports from the java.lang package.) So you want to do something like

 

    (bind ?edge-class (Class.forName org.jgrapht.graph.DefaultEdge))

    (bind ?graph (new org.jgrapht.graph.DefaultDirectedGraph ?edge-class))

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Aurélien Mazurie
Sent: Wednesday, August 07, 2013 7:11 PM
To: jess-users
Subject: JESS: [EXTERNAL] Issue with static methods (or generics?)

 

Dear Jess users,

I am having trouble instantiating a Java class from within a rule's RHS, and cannot figure out how to solve this problem.

 

The code in my RHS creates an instance of a class from the JGraphT library. As seen in  https://github.com/jgrapht/jgrapht/blob/master/jgrapht-demo/src/main/java/org/jgrapht/demo/HelloJGraphT.java (lines 91-92), the creation of a DefaultDirectedGraph instance require another class as argument; here, DefaultEdge.

 

I couldn't find an example in the Jess documentation about how to do that. For example, I tried the following:

 

            (bind ?edge-class org.jgrapht.graph.DefaultEdge)

            (bind ?graph (new org.jgrapht.graph.DefaultDirectedGraph ?edge-class))

 

I also tried variations such as

 

            (bind ?edge-class org.jgrapht.graph.DefaultEdge)

            (bind ?graph (new org.jgrapht.graph.DefaultDirectedGraph (?edge-class getClass))

 

or

 

            (bind ?edge-class (new org.jgrapht.graph.DefaultEdge))

            (bind ?graph (new org.jgrapht.graph.DefaultDirectedGraph ?edge-class))

 

Any idea about what the syntax should be to properly instantiate the DefaultDirectedGraph class? For information (but not certain how relevant it is here), I succeeded in the past in using java.util.regex.Pattern and its static method 'compile' by typing

 

            (bind ?pattern-class java.util.regex.Pattern)

            (bind ?matches ((?pattern-class compile "...") matcher ?string))

 

Best,

Aurélien

Loading...