You could also try something like the following:
WARNING: not real code...
file: DeviceMonitor.jess
(import com.yourpackage.Device)
(defclass Device com.yourpackage.Device)
(defrule shutdown-out-of-limit-devices
(Device (outOfLimit TRUE) (id ?deviceId) (OBJECT ?obj))
=>
(printout t "Device " ?deviceId " is out of limit")
(printout t "Shutting it down now...")
(?obj shutdown)
)
file: Device.java
import java.beans.*;
public class Device
{
private boolean outOfLimit = false;
private java.beans.PropertyChangeSupport pcs = new
PropertyChangeSupport(this); // or somthing...
public Device( String deviceId )
{
this.deviceId = deviceId;
pollDeviceStatus();
}
public String getId() { return deviceId; }
public void shutdown() { /* whatever... */ }
public void setOutOfLimit( boolean outOfLimit )
{
boolean oldValue = this.outOfLimit;
this.outOfLimit = outOfLimit;
pcs.firePropertyChangeEvent( "outOfLimit", new Boolean(oldValue),
new Boolean(outOfLimit));
}
public boolean isOutOfLimit() { return isOutOfLimit; }
public void pollDeviceStatus()
{
// get actual device status from serial port, USB, net, etc.
boolean newStatus = getDeviceActualStatus(); // TBD
boolean prevStatus = device.isOutOfLimit();
if ( newStatus != prevStatus ) // prevent redundant changes
device.setOutOfLimit( newStatus );
}
}
file: Main.java
import jess.*;
public class Main
{
private HashMap devices = new HashMap();
private void createDeviceProxies()
{
// populate devices hashmap with your devices
// e.g. Device device = new Device("device id 1");
// devices.put("device id 1", device);
}
static public void main( String[] args )
{
createDeviceProxies();
Rete rete = new Rete();
Jesp parser = new Jesp( ... ); // plus, load your rules, etc
// be sure your jess script does defclass for your "Device" class
rete.reset();
for ( Device device : devices )
{
rete.definstance("Device", device, true );
}
while ( true )
{
for ( Device device : devices )
{
device.pollDeviceStatus();
}
// now all devices are updated, reason about what to do
rete.run();
// since we are polling, rest for a while
try { Thread.sleep(1000); } catch Exception /// blah blah blah
}
}
}
Of course, if your devices can supply asynchronous event notifications
you wouldn't have to do the polling as shown above - you'd simply call
rete.runUntilHalt() in your main method and let your Device objects
update themselves via the event handlers.
Hope this helps.
Alan
Donnelly wrote:
Given a list of device names, when a device is out of limits I want to
assert an appropriately named fact
devicename-exceeds-limits.
If I use (assert (sym-cat ?device ?condition)) the fact becomes
sym-cat devicename -exceeds-limits.
How can I create correctly named facts?
Stephen Donnelly
--------------------------------------------------------------------
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]
--------------------------------------------------------------------
--------------------------------------------------------------------
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]
--------------------------------------------------------------------