Hi Folks,
Thought I'd let you all in on what I've been doing for 4.1b4, and
share a little goodie I cooked up.
I got inspired so I finally dug in and dealt with the
atoms-vs.-Strings-static-tables-serialization problem. It turns out
that I was able to turn RU.putAtom and RU.getAtom into internal
implementation details of the Value class with absolutely no sacrifice
in speed (perhaps even a small gain, becase I did a lot of cleanup
along the way. The core code is actually somewhat smaller, even with
new features.) This means that Value can deal with the 'problem' in
its readObject() method, which I will add for 4.2 (the first
non-Java-1.1-compatible release.) Voila - Serializable rules, facts,
deffunctions... This will be fun.
The downside to this is that a number of Jess' public APIs have
changed trivially; you will need to change any Userfunction classes
you've written to work with Jess 4.1b4 and beyond. The changes are
simple: the Userfunction.name() method no longer returns 'int' - now
it returns the name of the function as a String. Similarly, constructs
like Defrules, Deffacts, etc, with public name() methods: all those
methods return String. RU.getAtom and RU.putAtom are no longer public:
you can't (and shouldn't!) use these anymore, for
anything. The Value.atomValue() and variableValue() methods are gone:
just use stringValue(). Rete.addUserfunction is now named
Rete.addFunction; similarly, routines like findDeffunction,
findUserfunction, etc. Have collapsed into one findFunction method.
----------------------------------------------------------------------
Topic two
----------------------------------------------------------------------
I've recently been having a conversation offline with a user who was
interested in doing Graphics from Jess - i.e., drawing lines and
boxes. If you're a Java programmer, you know that really, you need to
do this in a Component.paint() method. I realized this morning that a
trivial adapter class (like the ones I did for AWT event handling)
would make this possible. So I give you the new jess.reflect.Canvas
class, along with a .clp file that shows how to use it. Enjoy!
----------------------------------------------------------------------
/* **********************************************************************
* jess.reflect.Canvas - A GUI adapter for Jess. Lets you do graphics
* from Jess code!
*
* Construct one of these with the name of a 'painting' Userfunction as
* the first argument (a deffunction, usually.) The function should expect a
* Component as its first argument, and a java.awt.Graphics object
* as its second argument (both as EXTERNAL_OBJECTs)
*
* (C) 1998 E.J. Friedman-Hill and the Sandia Corporation
* $Id$
********************************************************************** */
package jess.reflect;
import jess.*;
public class Canvas extends java.awt.Canvas
{
private Funcall m_fc;
private Rete m_engine;
public Canvas(String uf, Rete engine) throws ReteException
{
m_engine = engine;
m_fc = new Funcall(uf, engine);
m_fc.add(new Value(this, RU.EXTERNAL_ADDRESS));
m_fc.setLength(3);
}
public void paint(java.awt.Graphics g)
{
try
{
m_fc.set(new Value(g, RU.EXTERNAL_ADDRESS), 2);
Funcall.simpleExecute(m_fc, m_engine.globalContext());
}
catch (ReteException re)
{
m_engine.errStream().println(re);
}
}
}
----------------------------------------------------------------------
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Draw.clp
;; Simple example of using the jess.reflect.Canvas class to draw in a
;; Jess GUI without writing any Java code!
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; painter(java.awt.Component, java.awt.Graphics)
;; Draw a red X on a blue background. The X goes between the
;; component's corners.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(deffunction painter (?canvas ?graph)
(bind ?x (get-member (call ?canvas getSize) width))
(bind ?y (get-member (call ?canvas getSize) height))
(?graph setColor (get-member java.awt.Color blue))
(?graph fillRect 0 0 ?x ?y)
(?graph setColor (get-member java.awt.Color red))
(?graph drawLine 0 0 ?x ?y)
(?graph drawLine ?x 0 0 ?y))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; closer(java.awt.event.WindowEvent)
;; Your generic window-closing event handler
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(deffunction closer (?event)
(if (= (call ?event getID) (get-member ?event WINDOW_CLOSING)) then
(call (get ?event source) dispose)
(call java.lang.System exit 0)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Create some components. Note how we use the special Canvas subclass
;; and tell it which function to call to paint itself. We also install
;; a handler so the window responds to close events.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defglobal ?*f* = (new java.awt.Frame "Drawing Demo"))
(?*f* addWindowListener (new jess.reflect.WindowListener closer
(engine)))
(defglobal ?*c* = (new jess.reflect.Canvas painter (engine)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Put things together and display
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(?*f* add "Center" ?*c*)
(?*c* setSize 100 100)
(?*f* pack)
(?*f* show)
----------------------------------------------------------------------
Enjoy!
---------------------------------------------------------
Ernest Friedman-Hill
Distributed Systems Research Phone: (510) 294-2154
Sandia National Labs FAX: (510) 294-2234
Org. 8920, MS 9214 [EMAIL PROTECTED]
PO Box 969 http://herzberg.ca.sandia.gov
Livermore, CA 94550
---------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the
list. List problems? Notify [EMAIL PROTECTED]
---------------------------------------------------------------------