![]() | ![]() | ![]() | Creating a Unit Instance | ![]() |
Blackboard objects in GBBopen are called unit instances. Each unit
instance is a member of a unit class. The unit class defines the
structure of all instances of the unit class, such as the slots in each unit
instance. At a more precise Common Lisp level, every GBBopen unit instance is
member of a (possibly non-direct) subclass of GBBopen's unit class,
standard-unit-instance
standard-object
standard-unit-class
standard-class
The word “unit” is used in GBBopen because it has a very neutral meaning that is unlikely to be confused with programming terminology, such as “object,” or with application-domain concepts, such as “location” or “goal”. When someone refers to “unit instances,” it is clear that they are talking about GBBopen's blackboard objects. Thus, “unit instance” always refers to an instance of a particular unit class, and “unit class” refers to a class of unit instances.
So let's start using them...
If you ended the Common Lisp session used in the last exercise, begin a new session and evaluate the following forms:
cl-user> (load "<install-dir>/initiate.lisp") ... cl-user> :gbbopen-user ... gbbopen-user>
|
location
unit class
We begin by defining a unit class location
x
and y
.
gbbopen-user> (define-unit-class location () (x y)) #<location> gbbopen-user>
This unit-class definition instructs GBBopen to:
:x
for the x
slot
and :y
for the y
slot. You will use these
initialization arguments in the next step, to specify initial slot
values for an instance.
x-of
y-of
x
and y
slot values.
Next, you can create a unit instance for the location
ui
by entering the
following form:
gbbopen-user> (defvar ui) ui gbbopen-user>
Now, create a unit instance and assign it to the variable ui
by
entering the form:
gbbopen-user> (setf ui (make-instance 'location :x 40 :y 60)) #<location 1> gbbopen-user>
This creates a unit instance of the location
x
and y
slots with the values specified by the
:x
and :y
initialization arguments. The created unit instance
is then assigned to the variable ui
.
Now, display a description of the unit instance by using GBBopen's
gbbopen-user> (describe-instance ui) Location #<location 1> Instance name: 1 Space instances: None Dimensional values: None Non-link slots: x: 40 y: 60 Link slots: None gbbopen-user>
Note that you didn't specify a name for the unit instance when you created it.
In fact, there is often no natural reason to name each unit instance you
create. By default, GBBopen names unit instances by giving it a sequentially
increasing number. (For example, the unit instance you just created is named
1
.) GBBopen requires that each instance of a unit class is uniquely
named within the class and the accessor
gbbopen-user> (instance-name-of ui) 1 gbbopen-user>
You can look up a particular unit instance by its name. For example, to find
the location
1
unit instance by name, enter the form:
gbbopen-user> (find-instance-by-name 1 'location) #<location 1> gbbopen-user>
Of course, we assigned the location
ui
, but it is nice to know that we can always find our unit
instance using its name.
x
slot value
Use the x-of
reader and writer methods GBBopen defined for the
x
slot (in Step 3 above) to get the value of the x
slot of the
unit instance:
gbbopen-user> (x-of ui) 40 gbbopen-user>change the slot value to 50:
gbbopen-user> (setf (x-of ui) 50) 50 gbbopen-user>and then get the (new) value of the
x
slot:
gbbopen-user> (x-of ui) 50 gbbopen-user>
Display the description of the location
x
slot value:
gbbopen-user> (describe-instance ui) Location #<location 1> Instance name: 1 Space instances: None Dimensional values: None Non-link slots: x: 50 y: 60 Link slots: None gbbopen-user>
The GBBopen Project
![]() | ![]() | ![]() | Creating a Unit Instance | ![]() |