Totes les classes i metodes es troben al paquet jess, per tant farem:
import jess.*;
-
Per a executar un fitxer.clp
NullDisplay nd=new NullDisplay();
Rete jess_engine=new Rete(nd);
FileInputStream clp=new FileInputStream("test.clp");
Jesp parser=new Jesp(clp,jess_engine);
do
{
try
{
parser.parse(false);
}
catch
(ReteException re)
{
re.printStackTrace(nd.stderr());
}
}while
(clp.available()>0);
si el fitxer.clp conte les comandes run i reset, durant el primer parse()
s'executaran totes les accions
per carregar algunes funcions de les classes definides als paquets jess.Userpackage
es pot fer carregant els paquets de la seguent manera:
Rete jess_engine=new Rete(nd);
String [] packages = {"jess.StringFunctions",
"jess.PredFunctions",
"jess.MultiFunctions",
"jess.MiscFunctions",
"jess.MathFunctions",
"jess.BagFunctions",
"jess.reflect.ReflectFunctions",
"jess.view.ViewFunctions" };
for (int i=0; i<packages.length;
i++}
{
try
{
jess_engine.addUserpackage((Userpackage) class.forName(packages[i].newInstance());
}
catch
(Throwable t){ }
}
Aquest metode controla que quan un paquet que es crida no esta present,
no produeixi un error
-
si el que es vol es executar comandes individualment:
try
{
jess_engine.executeCommand("(reset)");
jess_engine.executeCommand("(assert
(foo bar foo))");
jess_engine.executeCommand("(run)");
//prints
'42'
System.out.println(jess_engine.executeCommand("(+
37 5)"));
}
catch (ReteException ex)
{
System.err.println("Foo
bar error.");
}
Utilitzant les funcions store i fetch per transferir dades entre jess i
java. Existeixen un conjunt de funcions per enviar objectes java entre
jess i java. Cada objecte de la classe Rete conte una hashtable on tant
jess com java poden accedir per insertar i recuperar dades.
Els metodes disponibles a java son (pertanyen a la classe Rete):
public Value store(String name,
Value val);
inserta el nou value i retorna el Value antic null/nil si no n'hi havia
cap
public Value store(String name,
Object val);
inserta el nou objecte java i retorna l'objecte antic null/nil si no
n'hi havia cap
public Value fetch(String name);
retorna el valor actual amb nom 'name' de la taula o null/nil si no
n'hi ha cap
mentre que els metodes disponibles a jess son:
(store <name> <value>)
inserta el nou Value i retorna el value existent avans de la insercio
(fetch <name>)
retorna el value existent a la taula o null/nil si no n'hi ha cap
Les classes jess.Value i jess.ValueVector. Un Value es un objecte. Cada
dada de jess esta continguda en un Value. Una vegada cridat al constructor
de la dada, el seu tipus i continguts on poden ser modificats. Un Value
suporta una funcion Type() que retorna una d'aquestes constants definides
a la class jess.RU
final public static int NONE
= 0; ; an empty value (not NIL)
final public static int ATOM
= 1; ; a symbol
final public static int STRING
= 2; ; a string
final public static int INTEGER
= 4; ; an integer
final public static int VARIABLE
= 8; ; a variable
final public static int FACT_ID
= 16; ; a fact index
final public static int FLOAT
= 32; ; a double float
final public static int FUNCALL
= 64; ; a function call
final public static int ORDERED_FACT
= 128; ; an ordered fact
final public static int UNORDERED_FACT
= 256; ; a deftemplate fact
final public static int LIST
= 512; ; a multifield
final public static int DESCRIPTOR
= 1024; ; (internal use)
final public static int EXTERNAL_ADDRESS
= 2048; ; a Java object
final public static int INTARRAY
= 4096; ; (internal use)
final public static int MULTIVARIABLE
= 8192; ; a multivariable
final public static int SLOT
= 16384; ; (internal use)
final public static int MULTISLOT
= 32768; ; (internal use)
Els objecte Value es construeixen especifican la variable i el tipus.
Els constructors son:
public Value(Object o, int
type) throws ReteException
public Value(String s, int
type) throws ReteException
public Value(Value v)
public Value(ValueVector f,
int type) throws ReteException
public Value(double d, int
type) throws ReteException
public Value(int value, int
type) throws ReteException
public Value(int[] a, int
type) throws ReteException
Existeixen un conjunt de funcions per obtenir informacio del Value
public Object externalAddressValue()
throws ReteException
public String stringValue()
throws ReteException
public ValueVector factValue()
throws ReteException
public ValueVector funcallValue()
throws ReteException
public ValueVector listValue()
throws ReteException
public double floatValue()
throws ReteException
public double numericValue()
throws ReteException
public int descriptorValue()
throws ReteException
public int factIDValue() throws
ReteException
public int intValue() throws
ReteException
public int[] intArrayValue()
throws ReteException
Un exemple complert
El mateix exemple
utilitzant executeCommand
Una nova versió
més eficient
|