[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Initializing objects with lisp
From: |
Alex Lancaster |
Subject: |
Re: Initializing objects with lisp |
Date: |
11 Jun 1999 16:51:47 -0600 |
>>>>> "GR" == glen e p ropella <address@hidden> writes:
GR> Hidee Ho, For those of you wondering how to use Lisp to initialize
GR> your objects, I managed to initialize a Swarm just fine using the
GR> following:
[...]
You can also use the high-level `Archiver' interface to create objects
without having an intermediate createBegin/createEnd. In fact the
intention of the design of the Lisp archiving feature is to use this
interface, and instantiate objects directly, rather than manually
creating an instance, and then `filling it in' using a direct `fopen'
call.
The idea is that the user `names' the objects you want to create
and/or load from a parameter file with a key that is a string, and
also encodes both the name of the class and the values of the instance
variables. The next release of both `mousetrap' and `heatbugs'
actually has this implemented by default, but you can experiment with
the following code, which will work with Swarm 1.4.1.
[Note: The ObjectLoader/ObjectSaver features, whilst still supported,
are now officially deprecated and may go away in the future, and
should *not* be used].
So, the Lisp input file `batch.scm' for `mousetrap' looks like:
(list
(cons 'batchSwarm
(make-instance 'MousetrapBatchSwarm
#:loggingFrequency 1))
(cons 'modelSwarm
(make-instance 'MousetrapModelSwarm
#:gridSize 40
#:triggerLikelihood 1.0
#:numberOutputTriggers 4
#:maxTriggerDistance 4
#:maxTriggerTime 16
#:trapDensity 1.0)))
To instantiate a named object (say `batchSwarm'), you do the
following, in main.m:
// create an instance of the Archiver to retrieve the file
// set the path to `batch.scm'
id archiver = [[[[Archiver createBegin: globalZone]
setHDF5Flag: NO]
setPath: "batch.scm"]
createEnd];
// retrieve the object from the archiver, if it can't be found
// just create the default object; note that the call to the
// archiver will actually *instantiate* the object if the
// parameters are found in the Lisp file
if ((theTopLevelSwarm =
[archiver getObject: "batchSwarm"]) == nil)
raiseEvent(InvalidOperation, "Can't find Archiver or `batchSwarm' key");
[...]
// the instance will respond to the normal buildObjects call, etc.
[theTopLevelSwarm buildObjects];
Advantages of using the Lisp archiver over the Object{Loader,Saver}
method are:
* it supports a fuller-set of instance variable types (i.e. more than
int, char * and the few others that ObjectLoader currently does)
* handles the creation and saving of objects-within-objects (known as
`deep' serialization)
* uses a standard Lisp-style syntax, based on the Scheme programming
language (rather than the ad-hoc @begin/@end syntax) and is thus
extensible
In 2.0, this creation will be even more straightforward, with
convenience methods for creating the Archiver instances and a default
singleton `lispAppArchiver' (like the `probeLibrary' singleton), which
will expect a Lisp input file with name <appname>.scm. So the
heatbugs example becomes even simpler:
Here's heatbugs.scm:
(list
(cons 'batchSwarm
(make-instance 'HeatbugBatchSwarm
#:loggingFrequency 1
#:experimentDuration 200))
(cons 'modelSwarm
(make-instance 'HeatbugModelSwarm
#:numBugs 200
#:minIdealTemp 10000
#:maxIdealTemp 20000
#:minOutputHeat 10000
#:maxOutputHeat 20000
#:randomMoveProbability 0.0)))
The corresponding `batchSwarm' instantiation is:
// No graphics - make a batchmode swarm (using the key
// `batchSwarm' from the default lispAppArchiver) and run it.
if ((theTopLevelSwarm = [lispAppArchiver getWithZone: globalZone
object: "batchSwarm"]) == nil)
raiseEvent(InvalidOperation,
"Can't find the parameters to create batchSwarm");
[theTopLevelSwarm buildObjects];
The `lispAppArchiver' will automatically search for the `heatbugs.scm'
file, parse it, and wait for requests to instantiate objects in the
specified Zone (just like you would normally pass a Zone in a
create:/createBegin: call).
[For HDF5-ers, there is a corresponding hdf5AppArchiver global
instance as well.]
--
Alex Lancaster | e-mail: address@hidden
Swarm Program | web: http://www.santafe.edu/~alex
Santa Fe Institute | tel: +1-(505) 984-8800 (ext 242)
-------------------------------------------------------------------
==================================
Swarm-Support is for discussion of the technical details of the day
to day usage of Swarm. For list administration needs (esp.
[un]subscribing), please send a message to <address@hidden>
with "help" in the body of the message.
- Re: Initializing objects with lisp,
Alex Lancaster <=