Creating a Unit InstanceTopDeleting InstancesCreating a Space InstanceGoTo Top

Creating a Space Instance

In GBBopen, space instances serve as containers for unit instances. As we saw in the last exercise, unit instances need not be placed in a space instance. They are perfectly useful on their own. In this exercise we will create a space instance and add and remove unit instances from it. A unit instance can also be contained in multiple space instances at the same time. Although containment “in” a space instance is a more technically correct phrase, historically developers talk of unit instances being “on” a space instance or of adding a unit instance “to” a space instance. Whichever preposition is used, the meaning is the same.

As with unit instances, each space instance is a member of a space class. The space class defines the structure of all instances of the space class, such as any slots associated with each space instance. Unlike unit instances, however, the standard GBBopen space class, standard-space-instance, is often sufficient for most applications. Therefore, space-instance subclasses rarely need to be defined.

Space instances can be organized into hierarchical structures, much like the directories in a hierarchical file system. A useful, but not exact, analogy is to think of unit instances as being similar to files and space instances as similar to directories. Stretching this analogy one step further, adding a unit instance to a space instance is akin to creating a symbolic link to a file in a directory. Removing the unit instance from the space instance is like removing the symbolic link: the unit instance itself, like the file, is not deleted by the removal.


This exercise shows you how to:


Prerequisites

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> (define-unit-class location ()
                  (x y))
  #<location>
  gbbopen-user> (defparameter ui (make-instance 'location :x 40 :y 60))
  ui
  gbbopen-user>

ASDF, clbuild, and Quicklisp users
Remember to (asdf:operate 'asdf:load-op :gbbbopen) or (require :gbbopen) in place of loading <install-dir>/initiate.lisp.

Step 1: Create a space instance

Create a space instance named known-world. To simplify access to the space instance during subsequent activities, first define a global variable called si by entering the following form:

  gbbopen-user> (defvar si)
  si
  gbbopen-user>

Now, create the space instance and assign it to the variable si by entering the form:

  gbbopen-user> (setf si (make-space-instance '(known-world)))
  #<standard-space-instance (known-world)>
  gbbopen-user>

The argument to the make-space-instance is the “path” to be used for the created space instance. A space-instance path is the complete list of space-instance names, starting with the name of the most distant indirect parent, that uniquely identifies a space instance in the blackboard repository. Our known-world space instance does not have a parent, so its path is simply (known-world).

Step 2: Display a description of the blackboard repository

Display a description of the blackboard repository, which now contains the known-world space instance:

  gbbopen-user> (describe-blackboard-repository)
  
  Space Instance                Contents
  --------------                --------
  known-world                   Empty

  Unit Class                    Instances
  ----------                    ---------
  location                              1
  standard-space-instance               1
                                ---------
                                        2 instances
  gbbopen-user>

The description indicates that:

Step 3: Find a space instance by its path

You can look up a particular space instance by its space-instance path:

  gbbopen-user> (find-space-instance-by-path '(known-world))
  #<standard-space-instance (known-world)>
  gbbopen-user>

We assigned the known-world space instance to the global variable si, but it is nice to know that we can always find it again using its path.

Step 4: Add the unit instance to the space instance

Now, add the location unit instance to the space instance. Enter the following form:

  gbbopen-user> (add-instance-to-space-instance ui si)
  #<location 1>
  gbbopen-user>

Step 5: Again, display the blackboard-repository description

Display the description of the blackboard repository again:

  gbbopen-user> (describe-blackboard-repository)
  
  Space Instance                Contents
  --------------                --------
  known-world                   1 instance (1 location)

  Unit Class                    Instances
  ----------                    ---------
  location                              1
  standard-space-instance               1
                                ---------
                                        2 instances
  gbbopen-user>

This time the description indicates that the known-world space instance has one instance of the location unit class stored on it.

Step 6: Display the description of the unit instance

Display the description of the location unit instance once again, observing the change in the space instances from the last exercise:

  gbbopen-user> (describe-instance ui)
  Location #<location 1>
    Instance name: 1
    Space instances: ((known-world))
    Dimensional values: None
    Non-link slots:
      x:  50
      y:  60
    Link slots: None
  gbbopen-user>

Step 7: Find the unit instance on the space instance

Now that the location unit instance is on the known-world space instance, we can find it on the space instance. The form is as follows, where :all is a very basic retrieval pattern specifying that all unit instances on the known-world space instance are to be returned:

  gbbopen-user> (find-instances 'location '(known-world) :all)
  (#<location 1>)
  gbbopen-user>
The list of found unit instances is returned. (In this case, there is only one in the list.)

This is a very simple retrieval; however, GBBopen can perform extremely complex searches as well. We will use more complex retrieval patterns in upcoming exercises.

Step 8: Add another unit instance to the space instance

Create a second instance of the location unit class, with x and y slot values 80 and 90, respectively, and add it to the known-world space instance:

  gbbopen-user> (add-instance-to-space-instance 
                   (make-instance 'location :x 80 :y 90)
                   si)
  #<location 2>
  gbbopen-user>

This time we did not assign the new location unit instance to a global variable but, as before, we can find the unit instance by its name:

  gbbopen-user> (find-instance-by-name 2 'location)
  #<location 2>
  gbbopen-user>

As you would expect, we can retrieve both location unit instances from the known-world space instance using find-instances:

  gbbopen-user> (find-instances 'location '(known-world) :all)
  (#<location 2> #<location 1>)
  gbbopen-user>

Step 9: Again, display the blackboard-repository description

Display the description of the blackboard repository again:

  gbbopen-user> (describe-blackboard-repository)
  
  Space Instance                Contents
  --------------                --------
  known-world                   2 instances (2 location)

  Unit Class                    Instances
  ----------                    ---------
  location                              2
  standard-space-instance               1
                                ---------
                                        3 instances
  gbbopen-user>

This time the description indicates that the known-world space instance has both instances of the location unit class stored on it.

Step 10: Remove a unit instance from the space instance

Now, remove the first location unit instance from the known-world space instance. Enter the following form:

  gbbopen-user> (remove-instance-from-space-instance ui si)
  #<location 1>
  gbbopen-user>
As you would expect, only the second location unit instance remains on the known-world space instance:
  gbbopen-user> (find-instances 'location '(known-world) :all)
  (#<location 2>)
  gbbopen-user>
and describing the location unit instance confirms this:
  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 InstanceTopDeleting InstancesCreating a Space InstanceGoTo Top