Knowledge Base Operations (original) (raw)
Next: Constraint-Checking Up: The Generic Frame Protocol Previous: Facet Functions
Our model of a knowledge base is as follows. A KB is a collection of frames. These frames may or may not reference frames in other KBs.
Multiple KBs may be open for access, or active, simultaneously. These different KBs may be serviced by different FRSs. For example, a single application might access both a LOOM and a THEO KB simultaneously. Each KB is described by a KB-descriptor object that records information about that KB, such as its name, its package, and the location of the KB on secondary storage (usually specified by a filename). Most of the functions in this library take a KB-descriptor (abbreviated kb) as an optional argument. The default value of the argument is the current KB, which GFP tracks. Each KB-descriptor is a CLOS object whose class determines the FRS that services the KB.
Our model of KB access is meant to allow KBs to reside on a variety of types of secondary storage including both traditional flat files, and database systems on remote servers. Further, we do not assume that the KB is read into memory in its entirety when the KB is opened for access. A GFP knowledge base can be in one of two states. In the_live_ state, a valid descriptor exists for the KB, but none of the frames within the KB are accessible. The only operations that can be performed on the KB are to open it (thereby changing its state to_open_), and to delete it from secondary storage. In the open state, all frames within the KB are accessible to GFP operations such asget-slot-values. Other operations that can be performed on an open KB are closing it (which changes its state back to live) and saving it to secondary storage (which maintains the state as open). By default, closing a KB first saves the KB, and also flushes all frames within the KB from memory, but arguments to close-kb can override these defaults.
The following example creates a KB descriptor for a KB calledpeople that is an instance of the class theo-kb (and therefore will be operated on by the THEO FRS). It then opens a new KB using that descriptor. The descriptor must be created separately from the KB so that the appropriate FRS-specific method can be used to create the KB itself.
(setq pkb (gfp:create-kb 'people
:prototype (gfp:get-prototype 'theo-kb) )
(gfp:open-kb :kb pkb :filename "~/people.theo" :status :new)
create-kb name &key (prototype (class-prototype (class-of (current-kb)))) initargs
Creates a new KB descriptor (see Section 3.4) for an empty knowledge base of the same class as the value of prototype; that CLOS class identifies the underlying FRS that will be used to manipulate the KB. Example classes include theo-kb and loom-kb. Note that the name of this operation is potentially confusing because it creates a new in memory descriptor of a KB; it does not create a knowledge base on external storage. This operation must be called as the first step of accessing any knowledge base, whether that knowledge base already exists, or not.
initargs is a list of initializations in the normal init-plist format. These arguments are used in the initialization of the knowledge base. The implementation of create-kb is not specified and the user cannot, therefore rely on any underlying CLOS protocol, such as initialize-instance, make-instance, or shared-initialize being invoked.
Implementation note: It is the responsibility of each implementation to maintain its own list of KBs. Any KB created with create-kb must be findable by the user using either find-kb , or find-kb-of-type
delete-kb &key kb
This function erases the KB associated with kb from secondary storage. The persistent version of KB is destroyed. This operation may only be applied to KBs in the live state.
open-kb &key filename status kb force-load-p error-p
Opens a the KB stored in file filename, thereby putting that KB in the open state. Status indicates whether an existing KB should be opened (default value is :old) or a new KB should be created (value :new). Once open, the existing frames in a KB can be accessed, and new frames can be created in a KB. Ifforce-load-p is t (default is nil) then the KB must be immediately loaded into virtual memory in its entirety; otherwise no commitment is made as to whether this function actually loads the KB.
This function takes an argument called kb as returned bycreate-kb . That is, create-kb must always be called to create the KB descriptor, before open-kb is called to actually open the KB associated with that descriptor. The type of the KB argument tells open-kb what underlying FRS to employ to actually access the KB on secondary storage.
Error-p affects the behavior of the function when no file called filename exists; in this case the function returns nil when error-p is nil\ rather than signaling an error.
close-kb &key kb save-updates-p flush-memory-p
This operation changes the state of kb from open to live, thereby making its frames inaccessible to other GFP operations. Thesave-updates-p argument (t by default) determines whether the close operation saves to secondary storage all updates made to the KB since the last open-kb or save-kb operation was applied to the KB. The flush-memory-p argument (t by default) determines whether the close operation erases all frames associated with kb from virtual memory.
save-kb &key kb
This function saves an open KB to permanent storage in the file from which it was opened. No commitment is made as to whether the KB is resaved in its entirety, or if updates to the KB only are saved.
revert-kb &key kb error-p
This operation reverts kb to its previously saved state. All changes made to kb since the last save are undone.Kb remains open. The default method for this operation simply closes and then reopenskb.Error-p affects the behavior of the function when kb is not an open KB; the function returns nil when error-p is nil rather than signaling an error.
kb-p kb
Returns true iff kb is a valid KB name or a KB-descriptor.
find-kb name-or-kb &key error-p
If name-or-kb is the name or the descriptor of a KB that is currently known to the system, irrespective of the type of the KB,find-kb returns the KB-descriptor associated with that KB. Error-p affects the behavior of the function when name-or-kb is not a known KB; the function returns nil when error-p is nil rather than signaling an error.
find-kb-of-type name-or-kb &key (prototype (class-prototype (class-of (current-kb)))) error-p
If name-or-kb is the name or the descriptor of a KB of a type consistent with (subtypep) the specified type that is currently known to the system, find-kb-of-type returns the KB-descriptor associated with that KB. Error-p affects the behavior of the function when name-or-kb is not a known KB; the function returns nil when error-p is nil rather than signaling an error.
current-kb
Returns the descriptor associated with the current KB.
with-current-kb kb &body body
This macro evaluates the forms in body in a context where the current KB for the GFP protocol is kb. Outside the lexical scope of the macro, the macro does not alter the current KB.
with-kb-io-syntax (var &key kb (purpose :file)) &body body Macro
Binds the I/O environment in which body is executed to be appropriate to the KB and the given purpose. The purposeargument can be one of :file, or :user-interface. This allows KBs to print data to files in a manner that will be readable and hence will probably be package-qualified and slashified, and yet will also allow printing to user interfaces in a prettier manner. Note: this functionality is independent of the pretty-names of frames being displayed by a user interface tool. The bindings established by with-kb-io-syntax will apply, for example, to slot and facet values whether they are frames or not. Back end implementor's note: this macro is implemented in the back-end protocol byimplement-with-kb-io-syntax-internal.
goto-kb kb
Makes kb the current KB.
rename-kb kb new-name
get-all-kbs
Returns the list of all of the known KBs, irrespective of the KB's implementation type.
get-kbs &key (prototype (class-prototype (class-of (current-kb))))
Returns the list of all of the known KBs whose type matches the type ofprototype.
get-kb-frames &key kb kb-local-only-p
Returns the list of frames in the KB, including both classes and instances. If kb-local-only-p is true, the list returned will contain only the classes defined in kb itself; classes that were inherited from other (included) KBs will not be included.
mapcar-kb-frames function &key kb kb-local-only-p
Maps function over every frame in the specified KB. Ifkb-local-only-p is true, the mapping will be performed over only the classes defined in kb itself; classes that were inherited from other (included) KBs will be ignored.
mapc-kb-frames function &key kb kb-local-only-p
Maps function over every frame in the specified KB. Ifkb-local-only-p is true, the mapping will be performed over only the classes defined in kb itself; classes that were inherited from other (included) KBs will be ignored.
do-kb-frames (var &key kb kb-local-only-p) &body Macro
Evaluates body repeatedly, with the binding of variablevar iterating over every frame in the specified KB. Ifkb-local-only-p is true, the iteration will be performed over only the classes defined in kb itself; classes that were inherited from other (included) KBs will be ignored.
get-kb-root-classes &key type kb kb-local-only-p
Every frame KB has one or more class frames at the top (root) of the taxonomic hierarchy. A class C is a root of a KB in K if there exists no frame D such that D is a super of C and D is in the KB K. This function identifies those classes.
Some FRSs allow user-defined classes to be roots of a KB, whereas other FRSs always import certain system-defined classes (which are typically named something like thing) into each KB and force all user classes to be subs of thing. These system-defined classes may normally be invisible to the user in some back ends. Thetype argument controls which root classes are returned as follows:
- type = :all returns all of the true root classes of the KB, regardless of whether they are system defined or user defined.
- type = :user returns the user-defined root classes of the KB, namely all classes C within the KB such that_C_ was defined by a user application as opposed to being a built-in part of every KB, and such that there exists no class D that is both user defined and a super or all-super of C. That is, there may exist system-defined supers of C.
- type = :system returns the subset of the true root classes of the KB that are system defined. This may be none for some FRSs.
The default type is :all.
If local-only-p is true, the list returned will return only root the classes defined in kb itself; classes that were inherited from other (included) KBs will not be included. This means that a class that has superclasses in some KB included by kb, but has no superclasses defined in kb will be returned as a root class iflocal-only-p is true.
get-kb-instances &key kb kb-local-only-p
Returns the list of instance frames in kb. Ifkb-local-only-p is true, the list returned will contain only the instances defined in kb itself; instances that were inherited from other (included) KBs will not be included.
mapcar-kb-instances function &key kb kb-local-only-p
Map function over all instances in the current KB. Ifkb-local-only-p is true, the mapping will be performed over only the instances defined in kb itself; instances that were inherited from other (included) KBs will be ignored.
mapc-kb-instances function &key kb kb-local-only-p
Map function over all instances in the current KB. Ifkb-local-only-p is true, the mapping will be performed over only the instances defined in kb itself; instances that were inherited from other (included) KBs will be ignored.
do-kb-instances (var &key kb kb-local-only-p) &body body Macro
Repeatedly executes body with var bound to a different instance frame in kb on each iteration. If kb-local-only-p is true, the iteration will be performed over only the instances defined in kb itself; instances that were inherited from other (included) KBs will be ignored.
get-kb-slots &key kb kb-local-only-p
Returns the list of slots that are defined across all of the frames in kb. If kb-local-only-p is true, the list returned will contain only the slots defined in kb itself; slots that were inherited from other (included) KBs will not be included.
mapcar-kb-slots function &key kb kb-local-only-p
Map function over all slots in KB. Ifkb-local-only-p is true, the mapping will be performed over only the slots defined in kb itself; slots that were inherited from other (included) KBs will be ignored.
mapc-kb-slots function &key kb kb-local-only-p
Map function over all slots in KB. Ifkb-local-only-p is true, the mapping will be performed over only the slots defined in kb itself; slots that were inherited from other (included) KBs will be ignored.
do-kb-slots (var &key kb kb-local-only-p) &body body Macro
Repeatedly executes body with var bound to a different slot in kb on each iteration. If kb-local-only-p is true, the iteration will be performed over only the slots defined in kb itself; classes that were inherited from other (included) KBs will be ignored.
get-kb-facets &key kb kb-local-only-p
Returns the list of facets that are defined across all of the frames in kb. If kb-local-only-p is true, the list returned will contain only the facets defined in kb itself; facets that were inherited from other (included) KBs will not be included.
mapcar-kb-facets function &key kb kb-local-only-p
Map function over all facets in KB. Ifkb-local-only-p is true, the mapping will be performed over only the facets defined in kb itself; facets that were inherited from other (included) KBs will be ignored.
mapc-kb-facets function &key kb kb-local-only-p
Map function over all facets in KB. Ifkb-local-only-p is true, the mapping will be performed over only the facets defined in kb itself; facets that were inherited from other (included) KBs will be ignored.
do-kb-facets (var &key kb kb-local-only-p) &body body Macro
Repeatedly executes body with var bound to a different facet in kb on each iteration. If kb-local-only-p is true, the iteration will be performed over only the facets defined in kb itself; facets that were inherited from other (included) KBs will be ignored.
Next: Constraint-Checking Up: The Generic Frame Protocol Previous: Facet Functions
- Peter Karp