>
> > On a related note, what I am trying to do is set up some sort of include
> > guard.  When a file is loaded I want to do something like:
> >
> > If this file has never been loaded before,
> >     do this stuff.
> > Otherwise
> >     Do nothing.
> >
> Well, you could introduce a level of indirection by using "build":
>
>  (try
>      (get-var *foo*)
>   catch
>      (build "(defglobal ?*foo* = moo)"))
>
> (build) sends its areguments through the parser, so it can handle
> constructs.

Thanks for the timely reply.  I ended up defining my own usefunction,
require.  I attach it for those who have similar problems.

Additionally, it requires the jess.Batch class to be declared public.  You
could work around this with executeCommand() calls, but my gut tells me that
what I did was a touch faster, and my system is fairly large (with lots of
file i/o), so I went this route.

Eric Eslinger

/*********************************************************************************************
 *
 * Require.java
 * 
 * An extention to JESS, the jess function (require) will take a string as argument, 
and if
 * that string corresponds to a file that has already been loaded through require, it 
will 
 * do nothing, otherwise it will use the batch facility to load the file.
 * 
 * Requires the Batch class defined in MiscFunctions.java of the jess distribution to 
be
 * public. (which it is currently not)
 *
 * Part of the sci-wise multi-agent inquiry support environment
 *
 * Written by Eric Eslinger
 * Copyright � 1998-1999 University of California
 * All Rights Reserved.
 * 
 *
 * History
 *      30 OCT 98 EME Created today
 
********************************************************************************************/
 
 

package edu.berkeley.paisle.jess_extensions;
import jess.*;
import java.util.Hashtable;

public class Require implements Userfunction {
        public String name() {
                return "require";
        }
        
        public Value call(ValueVector vv, Context context) throws ReteException {
                if ((vv.size() != 2) || ((vv.get(1).type() != RU.STRING) && 
(vv.get(1).type()!=RU.ATOM))) {
                        throw new ReteException("require", "usage: require 
<filename>","");
                }
                String filename = vv.get(1).stringValue();  //get the filename
                synchronized (context.engine()) {
                        Value rlist = context.engine().fetch("require-list");
                        if (rlist == null) {
                                rlist = new Value(new Hashtable(),RU.EXTERNAL_ADDRESS);
                                context.engine().store("require-list",rlist);
                        }
                        Hashtable rtable = (Hashtable) rlist.externalAddressValue();
                        if (rtable.get(filename) == null) {
                                rtable.put(filename,"loaded");
                                new Batch().call(vv,context);
                        }                                
                }       
                return Funcall.TRUE();
        }
        
}

Reply via email to