12.3 Building Blocks


 
Building Parsers with Java
By Steven  John  Metsker

Table of Contents
Chapter  12.   Engines

    Content

All the code for the engine described in this chapter lies within the package sjm.engine . In this package, there is no Engine class; instead, there are a few collaborating classes that together produce an engine. The essential classes that make up the engine are Structure , Variable , Rule , and Program . The classes Structure and Variable are the fundamental building blocks of the engine. Classes Program and Rule are aggregations; a Program object is an aggregation of Rule objects, and a Rule object is an aggregation of Structure objects. You'll visit all these classes, starting with the fundamentals shown in Figure 12.1.

Figure 12.1. Terms, structures, and variables . The classes Structure and Variable implement the Term interface. A structure has a functor, which can be any object, and has zero or more terms, which may be other structures or variables.

graphics/12fig01.gif

12.3.1 Structures

The Structure class provides the basic repository, or data structure, in a logic engine. A structure is a functor associated with a group of terms. A functor can be any object, and a term is either a structure or a variable. A structure is an aggregation of other structures, much as an object in an object-oriented system can be an aggregation of other objects. In the same way that "everything is an object" in an object-oriented language, everything is a structure in a logic engine.

The simplest structure has only a functor and no terms and is called an atom. For example, the following code fragment creates an atom:

 Structure denver = new Structure("denver"); 

The functor of a structure must be an object, such as an instance of String or Integer . Here's an example:

 Structure altitude = new Structure(new Integer(5280)); 

This assignment statement creates a structure having no terms and having a functor that is the Integer object 5280 . Here is a complete program that combines the denver and 5280 atoms :

 package sjm.examples.engine;  import sjm.engine.*; /**  * This class shows a simple structure.  */ public class ShowStructure { public static void main(String args[]) {     Structure denver = new Structure("denver");     Structure altitude = new Structure(new Integer(5280));     Structure city = new Structure(         "city", new Structure[]{denver, altitude});     System.out.println(city); } } 

Compiling and running this program prints the following:

 city(denver, 5280) 

The city structure represents an assertion about reality, and that qualifies it as a fact in logic terminology. In logic, a fact is a structure that contains no variables. Thus, in the preceding program, the objects denver , altitude , and city are all facts.

The code in this chapter follows a convention of writing facts with initial lowercase letters and variables with initial uppercase letters . This convention aids languages, such as Logikus and Prolog, that choose not to allow the declaration of types.

12.3.2 Variables

A variable is an object that has a name and can bind (or instantiate ) to structures or other variables. Here is a program that creates a structure with two variables:

 package sjm.examples.engine;  import sjm.engine.*; /**  * This class shows some variables.  */ public class ShowVariable { public static void main(String args[]) {     Variable name = new Variable("Name");     Variable alt = new Variable("Altitude");     Structure vCity = new Structure(         "city",         new Term[]{name, alt});     System.out.println(vCity); } } 

This class prints the following:

 city(Name, Altitude) 

This code creates the vCity structure using an array of Term rather than an array of Variable . This code could have used an array of Variable with equivalent results. Note that this code follows the convention of using uppercase strings to name variables.


   
Top


Building Parsers with Java
Building Parsers With Javaв„ў
ISBN: 0201719622
EAN: 2147483647
Year: 2000
Pages: 169

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net