0

I have to send some values from the Java class which calls the Jess script for processing in the Jess script.

This is my code till now:

int val1=0;
int val2=1;
Rete engine = new Rete();
Value val = engine.batch("abc.clp");
System.out.println("\n"+val);
engine.watchAll();

How do I pass the values val1 and val2? I found this example but it did not help much.

Community
  • 1
  • 1
user4655509
  • 81
  • 1
  • 8
  • Please add the relevant parts of security.clp, so we can see how you intend to access these two int values. (And: you can edit your question - deleting and creating a new one is irritating for followers of a tag.) – laune Jun 17 '15 at 16:15
  • I'm sorry for the irritation caused. And I don't know how I can access those variables in Jess. How would it be done normally?? What would the best way be? – user4655509 Jun 17 '15 at 17:19
  • The best way depends on what you need to do with them. If you know that one value is 0 and the other one is 1 you might as well put this into the Jess script and forget about passing values from Java to Jess. – laune Jun 17 '15 at 18:01
  • No I'll be taking these values from the user. So I surely don't know about the values. – user4655509 Jun 17 '15 at 18:18
  • If we can, eventually, get around to what must be done with these values in the clp script? – laune Jun 17 '15 at 18:20
  • So the answer the "few questions" is two int values? And they need to be processed by rules? Can you show at least one rule doing that? – laune Jun 17 '15 at 18:32

1 Answers1

1

A set of values from a dialog is best put into a POJO which is inserted into working memory and accessed via a deftemplate declared as being derived from a Java class. Using this in rules is straightforward. The POJO may also hold a field for storing results derived by rule firing.

The script should set up the Jess definitions but it should not call run. This is best done from Java and most certainly after the insert of the fact.

Below is a minimum example showing all of the aforesaid.

import jess.*;
public class Main {
    public static void main( String[] args ) throws Exception {
        Rete rete = new Rete();
        Value val = rete.batch("security.clp");
        Data data = new Data();
        data.setA( 42 );
        data.setB( 24 );
        rete.add( data );
        rete.run();
        System.out.println( "result = " + data.getRes() );
    }
}

The POJO class:

public class Data {
    private int a;
    private int b;
    private String res;
    public void setA( int v ){ a = v; }
    public void setB( int v ){ b = v; }
    public void setRes( String v ){ res = v; }
    public int getA(){ return a; }
    public int getB(){ return b; }
    public String getRes(){ return res; }
}

The clp file, (modified to demonstrate how to access slot values, and added no-loop):

(clear)
(deftemplate Data (declare (from-class Data)))
(defrule matchab
   (declare (no-loop TRUE))
   ?data <- (Data {a > b} (b ?b))
=>
   (printout t (fact-slot-value ?data a) " and " ?b crlf)  
   (modify ?data (res agtb))
)
laune
  • 31,114
  • 3
  • 29
  • 42
  • I'm not able to access the variables a and b in jess! – user4655509 Jun 19 '15 at 03:50
  • Where did you deviate from my working example? - a and b are not variables, they are fields in a Java object accessed via slots in a shadow template for a Jess fact. – laune Jun 19 '15 at 04:28
  • I did exactly as you have mentioned. I just tried printing the values for fields in the Java object. Something like this. (printout t " a and b: " Data {a} crlf) – user4655509 Jun 19 '15 at 05:26
  • You'll have to study Jess some more. This isn't the way to access a fact field. I'm updating the answer. – laune Jun 19 '15 at 05:40