RE: JESS: Listening to Jess Events

2004-06-01 Thread Alan Moore
Title: RE: JESS: Listening to Jess Events





 3. Instantiate a Rete object with engine = new 
 Rete(Someclass.class) to register the context with engine.


[alan]
This isn't required to sink jess events. This constructor is for applets or EJB-style containers (I think.)


 3. 
 Register that class to receive events via 
 engine.addJessListener().


[alan]
This is required.


 4. Call engine.setEventMask() for 
 each event that I want to receive.


[alan]
I assume you are combining the event mask(s) so that you only call setEventMask() once.


 
 I tried all this and didn't have any luck, so I'm obviously 
 missing something.
 
 Now, for simplicity sake, let's say that Someclass contains a 
 method called
 process() where:
 
 public void process() {
 engine.executeCommand((batch main.clp));
 engine.executeCommand((batch resolve.clp));
 engine.executeCommand((batch schedule.clp));
 engine.reset();
 engine.runUntilHalt();
 }


[alan]
When do you call Rete#addJessListener()? You proly already know this but it needs to be called before Rete#runUntilHalt();

 
 Now, I have a slew of questions:
 
 1. While runUntilHalt() is executing, does the listening 
 class need to be listening on another thread to receive 
 events while the engine is running?


[alan]
No, your listener method will be called back via the thread initiating the Rete activity. In your case, it will most likely be the thread that calls Rete#runUntilHalt().

 2. Does it matter at all 
 that process() is running Jess script via
 engine.executeCommand() instead of executing API code 
 directly?


[alan]
Nope, shouldn't matter.


 3. Is this an acceptable way of programming in 
 Jess? Should all the script be in one file?


[alan]
It's *all* good.


 4. My main 
 reason for wanting the events produced by Jess was to log 
 them for debugging. (Jess is being used in a webapp here, and 
 I am using Apache Jakarta Log4J as the logger.)


[alan]
I use it for exactly this purpose as well.


 My initial 
 thought was to put logging statements in the implementation 
 of eventHappened() like this:
 
 public void eventHappened(JessEvent je) throws JessException {
 switch (je.getType()) {
 case JessEvent.ACTIVATION:
 Activation activation = (Activation) je.getObject();
 String ruleName = 
 activation.getRule().getDisplayName();
 logger.info(Activation:  + ruleName
 +  was activated or deactivated.);
 break;
 // ... more code
 }
 
 What's the best way of logging Jess output and events when 
 Jess is embedded in a web application? (I don't mean 
 generating JSP views for input/output - that I've got fine.)


[alan]
I use a jess listener to log structured information, such as rule firing, etc. However, you will find life much easier if you add an output router and send the output to your log file also.

Your problem above could be due to an error in your (batch) statements (FileNotFoundException?) or a runtime error in the LHS of one of your rules. Check your webapp/container logs for errors. Also, put a try/catch around the body of process() to catch any errors being thrown by jess.

Good luck!


alan



 
 Thanks!
 -JM
 
 
 
 Jason Morris
 Morris Technical Solutions [EMAIL PROTECTED]
 www.morristechnicalsolutions.com
 fax/phone: 503.692.1088
 
 
 To unsubscribe, send the words 'unsubscribe jess-users 
 [EMAIL PROTECTED]' in the BODY of a message to 
 [EMAIL PROTECTED], NOT to the list (use your own address!) 
 List problems? Notify [EMAIL PROTECTED]
 
 





RE: JESS: Listening to Jess Events

2004-06-01 Thread Alan Moore
Title: RE: JESS: Listening to Jess Events





First, I'm sorry for the HTML email - I can't seem to get this *broken* mail client to do the right thing.


Secondly, in the following:


Your problem above could be due to an error in your (batch) statements (FileNotFoundException?) or a runtime error in the LHS of one of your rules.

I meant RHS of one of your rules. Of course, it is possible that there is an error on the LHS as well but that is more rare.

alan



-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]] On Behalf Of Alan Moore
Sent: Tuesday, June 01, 2004 12:21 PM
To: '[EMAIL PROTECTED]'
Subject: RE: JESS: Listening to Jess Events



 3. Instantiate a Rete object with engine = new 
 Rete(Someclass.class) to register the context with engine. 
[alan] 
This isn't required to sink jess events. This constructor is for applets or EJB-style containers (I think.) 
 3. 
 Register that class to receive events via 
 engine.addJessListener(). 
[alan] 
This is required. 
 4. Call engine.setEventMask() for 
 each event that I want to receive. 
[alan] 
I assume you are combining the event mask(s) so that you only call setEventMask() once. 
 
 I tried all this and didn't have any luck, so I'm obviously 
 missing something. 
 
 Now, for simplicity sake, let's say that Someclass contains a 
 method called 
 process() where: 
 
 public void process() { 
 engine.executeCommand((batch main.clp)); 
 engine.executeCommand((batch resolve.clp)); 
 engine.executeCommand((batch schedule.clp)); 
 engine.reset(); 
 engine.runUntilHalt(); 
 } 
[alan] 
When do you call Rete#addJessListener()? You proly already know this but it needs to be called before Rete#runUntilHalt();

 
 Now, I have a slew of questions: 
 
 1. While runUntilHalt() is executing, does the listening 
 class need to be listening on another thread to receive 
 events while the engine is running? 
[alan] 
No, your listener method will be called back via the thread initiating the Rete activity. In your case, it will most likely be the thread that calls Rete#runUntilHalt().

 2. Does it matter at all 
 that process() is running Jess script via 
 engine.executeCommand() instead of executing API code 
 directly? 
[alan] 
Nope, shouldn't matter. 
 3. Is this an acceptable way of programming in 
 Jess? Should all the script be in one file? 
[alan] 
It's *all* good. 
 4. My main 
 reason for wanting the events produced by Jess was to log 
 them for debugging. (Jess is being used in a webapp here, and 
 I am using Apache Jakarta Log4J as the logger.) 
[alan] 
I use it for exactly this purpose as well. 
 My initial 
 thought was to put logging statements in the implementation 
 of eventHappened() like this: 
 
 public void eventHappened(JessEvent je) throws JessException { 
 switch (je.getType()) { 
 case JessEvent.ACTIVATION: 
 Activation activation = (Activation) je.getObject(); 
 String ruleName = 
 activation.getRule().getDisplayName(); 
 logger.info(Activation:  + ruleName 
 +  was activated or deactivated.); 
 break; 
 // ... more code 
 } 
 
 What's the best way of logging Jess output and events when 
 Jess is embedded in a web application? (I don't mean 
 generating JSP views for input/output - that I've got fine.) 
[alan] 
I use a jess listener to log structured information, such as rule firing, etc. However, you will find life much easier if you add an output router and send the output to your log file also.

Your problem above could be due to an error in your (batch) statements (FileNotFoundException?) or a runtime error in the LHS of one of your rules. Check your webapp/container logs for errors. Also, put a try/catch around the body of process() to catch any errors being thrown by jess.

Good luck! 
alan 



 
 Thanks! 
 -JM 
 
  
 
 Jason Morris 
 Morris Technical Solutions [EMAIL PROTECTED] 
 www.morristechnicalsolutions.com 
 fax/phone: 503.692.1088 
 
  
 To unsubscribe, send the words 'unsubscribe jess-users 
 [EMAIL PROTECTED]' in the BODY of a message to 
 [EMAIL PROTECTED], NOT to the list (use your own address!) 
 List problems? Notify [EMAIL PROTECTED] 
  
 





RE: JESS: Listening to Jess Events

2004-06-01 Thread Jason Morris
Thanks Alan,

Yes, in the interest of trimming the example, I may have trimmed too much
detail.  Let me clarify:

public class Someclass implements JessListener {
  public static Logger logger = Logger.getLogger(Someclass.class);
  private Rete engine;
  private final String cmd1 = (batch main.clp);
  private final String cmd2 = (batch resolver.clp);
  private final String cmd3 = (batch scheduler.clp);


  public Someclass(){
this.engine = new Rete();
  }

  public void process() {
engine.addJessListener(this); // Register this Someclass instance as a
JessListener
engine.setEventMask(JessEvent.ACTIVATION); // for example.  I assume I
can include others with a bitwise OR
engine.executeCommand(cmd1);  // run batch 1
engine.executeCommand(cmd2);  // run batch 2
engine.executeCommand(cmd3);  // run batch 3
engine.reset(); // mainly to load deffacts
engine.runUntilHalt();  // keep churning until a result is produced
  }

  public void eventHappened(JessEvent je) throws JessException {
switch (je.getType()) {
  case JessEvent.ACTIVATION:
Activation activation = (Activation) je.getObject();
String ruleName = activation.getRule().getDisplayName();
  logger.info(Activation:  + ruleName
 +  was activated or deactivated.);
   break;
  // ... more code
  }
}// end class

Now, I'd expect some code like:

Someclass objSomeclass = new Someclass();
objSomeclass.process();

to run the Jess batches and log the various events.

You also mentioned routing the Jess output to the log files, which is also
something that I'd like to do.

I'm only familiar with passing Strings to Log4J loggers, but I see that you
can pass other objects as long as you provide a ObjectRenderer
implemetation.  So assuming that Jess's addOutputRouter() accepts any Writer
object, do you have a Renderer already coded that maps Jess STDOUT to Log4J?

Thanks again, Alan!
-JM


Jason Morris
Morris Technical Solutions
[EMAIL PROTECTED]
www.morristechnicalsolutions.com
fax/phone: 503.692.1088


To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the list
(use your own address!) List problems? Notify [EMAIL PROTECTED]