OUTSTANDING !!!!!!! It works !!!!!!
Ernest, thank you very much !!!
OK - this time I have included the (new/modified) code below:
(1) Tank.java => NO CHANGE
(2) DerivedTank.java
////////////////////////////////////////////////////////////////////////////
// Extended tank bean for Jess
// Rich Halsey
//
package jess.pumps;
public class DerivedTank extends Tank
{
public DerivedTank(String name)
{
super(name);
}
public void derivedTankMethod() {
System.out.println("DerivedTank::derivedTankMethod()");
}
}
////////////////////////////////////////////////////////////////////////////
(3) Pump.java => NO CHANGE
(4) DerivedPump.java
////////////////////////////////////////////////////////////////////////////
// Extended pump bean for Jess
// Rich Halsey
//
package jess.pumps;
public class DerivedPump extends Pump
{
public DerivedPump(String name, Tank tank)
{
super(name, tank);
}
public void derivedPumpMethod() {
System.out.println("DerivedPump::derivedPumpMethod()");
}
}
////////////////////////////////////////////////////////////////////////////
(5) MainInJava.java
////////////////////////////////////////////////////////////////////////////
package jess.pumps;
import jess.*;
public class MainInJava
{
public static void main(String[] argv) throws JessException
{
Rete rete = new Rete();
// Read in the rules
rete.executeCommand("(batch jess/pumps/pumps.clp)");
//rete.executeCommand("(batch jess/pumps/pumps-fromjava.clp)");
rete.executeCommand("(reset)");
// Create the Beans
//Tank t = new Tank("MAIN");
DerivedTank t = new DerivedTank("MAIN");
//Pump p = new Pump("MAIN", t);
DerivedPump p = new DerivedPump("MAIN", t);
// Tell Jess about them
Funcall f = new Funcall("definstance", rete);
f.add(new Value("pump", RU.ATOM));
f.add(new Value(p));
f.execute(rete.getGlobalContext());
f = new Funcall("definstance", rete);
f.add(new Value("tank", RU.ATOM));
f.add(new Value(t));
f.execute(rete.getGlobalContext());
while (t.isIntact())
rete.executeCommand("(run)");
} // end main
}
////////////////////////////////////////////////////////////////////////////
(6) pump.clp - changes delimited by the following type of line
;; ///////////////////////////////////////////////////////////////////
;; -*- clips -*-
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Free-standing Pumps and Tanks control example. The damped algrithm
;; coded in this file works reasonably well and can protect both Tanks
;; indefinitely.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Register Java classes for matching like deftemplates.
;; Bean-like properties become slots. Classes must support
;; addPropertyChangeListener. First argument is the 'deftemplate name'.
(import jess.pumps.*)
(import javax.swing.*)
(import java.awt.*)
;;(defclass machine jess.pumps.Machine )
(deftemplate machine (slot name) (slot class) (slot OBJECT))
(defclass tank Tank extends machine)
(defclass pump Pump extends machine)
;; ///////////////////////////////////////////////////////////////////
;; Either the above or the below will run successfully - but not BOTH.
;;(defclass tank DerivedTank extends machine)
;;(defclass pump DerivedPump extends machine)
;; ///////////////////////////////////////////////////////////////////
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; This fact will be used to sleep when idle
(deffacts idle-fact
(idle 0)
(adjust-time 0))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; If tank is low, raise appropriate pump's pumping rate
;; Don't raise it too high!
;; Notice how we can call methods of the objects we match, like
Pump.setFlow()
(defrule warn-if-low
(tank (name ?name) (low TRUE) (intact TRUE))
(not (warning low ?name))
=>
(assert (warning low ?name))
(printout t "WARNING: TANK " ?name " IS LOW!" crlf)
)
(defrule raise-rate-if-low
?warning <- (warning low ?name)
(pump (name ?name) (flow ?flow-rate) (OBJECT ?pump))
(test (< ?flow-rate 25))
(idle ?n)
?a <- (adjust-time ?t&:(< ?t (- ?n 20)))
=>
(retract ?warning)
(set ?pump flow (+ ?flow-rate 1))
(assert (adjust-time ?n))
(retract ?a)
(printout t "Raised pumping rate of pump " ?name " to "
(get ?pump flow) crlf))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; If tank is high, lower appropriate pump's pumping rate
;; Don't lower it too much.
(defrule warn-if-high
(tank (name ?name) (high TRUE) (intact TRUE))
(not (warning high ?name))
=>
(assert (warning high ?name))
(printout t "WARNING: TANK " ?name " IS HIGH!" crlf)
)
(defrule lower-rate-if-high
?warning <- (warning high ?name)
(pump (name ?name) (flow ?flow-rate) (OBJECT ?pump))
(test (> ?flow-rate 0))
(idle ?n)
?a <- (adjust-time ?t&:(< ?t (- ?n 20)))
=>
(retract ?warning)
(retract ?a)
(set ?pump flow (- ?flow-rate 1))
(assert (adjust-time ?n))
(printout t "Lowered pumping rate of pump " ?name " to "
(get ?pump flow) crlf))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; If tank is ok, sleep for 100 milliseconds
;; Notice outcall to Java static method!
(defrule notify-if-ok
?warning <- (warning ? ?name)
(tank (name ?name) (high FALSE) (low FALSE))
=>
(retract ?warning)
(printout t "Tank " ?name " is now OK." crlf))
(defrule sleep-if-bored
(declare (salience -100))
?idle <- (idle ?n)
=>
(retract ?idle)
(call Thread sleep 25)
(assert (idle (+ ?n 1))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; If tank is damaged, report it
(defrule report-fire
?t <- (tank (name ?name) (low TRUE) (intact FALSE))
=>
(printout t "*********************************************" crlf)
(printout t "* Tank " ?name " has run dry and caught fire. " crlf)
(printout t "*********************************************" crlf)
(retract ?t)
(halt))
(defrule report-explosion
?t <- (tank (name ?name) (high TRUE) (intact FALSE))
=>
(printout t "*********************************************" crlf)
(printout t "* Tank " ?name " has overfilled and exploded " crlf)
(printout t "*********************************************" crlf)
(retract ?t)
(halt))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Announce new machine. This is just to demonstrate
;; how inheritance works.
(defrule announce-new-machinery
?m <- (machine (name ?n) (class ?c))
(not (saw-machine ?n ?c))
=>
(assert (saw-machine ?n ?c))
(printout t "*** New Machine *** named " ?n " of type " (call ?c toString)
crlf))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Create hardware, register for matching
;; Create two pumps and two tanks
;; Notes:
;; 1) See how we use the 'deftemplate name' from the defclass calls.
;; 2) Objects in definstance must be assignable to the type named in
defclass
;; 3) The semantics here are different that definstances in CLIPS - this
allows
;; us to use pre-existing objects.
(defrule startup
=>
(bind ?frame (new JFrame "Pumps Demo"))
(call (?frame getContentPane) setLayout (new GridLayout 2 3))
;; ///////////////////////////////////////////////////////////////////
;;(definstance tank (bind ?tank (new Tank "MAIN")))
;;(call (?frame getContentPane) add (?tank getComponent))
;;(definstance pump (bind ?pump (new Pump "MAIN" ?tank)))
;;(call (?frame getContentPane) add (?pump getComponent))
;;(call (?frame getContentPane) add (new JLabel "MAIN"))
;;(definstance tank (bind ?tank (new Tank "AUX")))e
;;(call (?frame getContentPane) add (?tank getComponent))
;;(definstance pump (bind ?pump (new Pump "AUX" ?tank)))
(definstance tank (bind ?tank (new DerivedTank "MAIN")))
(call (?frame getContentPane) add (?tank getComponent))
(definstance pump (bind ?pump (new DerivedPump "MAIN" ?tank)))
(call (?frame getContentPane) add (?pump getComponent))
(call (?frame getContentPane) add (new JLabel "MAIN"))
(definstance tank (bind ?tank (new DerivedTank "AUX")))
(call (?frame getContentPane) add (?tank getComponent))
(definstance pump (bind ?pump (new DerivedPump "AUX" ?tank)))
;; ///////////////////////////////////////////////////////////////////
(call (?frame getContentPane) add (?pump getComponent))
(call (?frame getContentPane) add (new JLabel "AUX"))
(?frame pack)
(?frame setVisible TRUE))
(reset)
(run)
--------------------------------------------------------------------
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]
--------------------------------------------------------------------