12.6 Programs and Queries


 
Building Parsers with Java
By Steven  John  Metsker

Table of Contents
Chapter  12.   Engines

    Content

A program is a collection of axioms, which are facts or rules (which you have yet to meet). A query is a structure that can prove itself against a program. Figure 12.3 shows the Program and Query classes.

Figure 12.3. The Program and Query classes. A program is a collection of axioms and an implementation of the AxiomSource interface. The Query class has a constructor that accepts a source of axioms and a structure to unify with the axioms.

graphics/12fig03.gif

The following Java program creates a logic program, loads it with facts, and uses a query to extract all its results:

 package sjm.examples.engine;  import sjm.engine.*; /**  * Show the construction and use of a simple program.  */ public class ShowProgram { /**  * Return a small database of cities and their altitudes.  */ public static Program altitudes() {     Fact [] facts = new Fact[]{       new Fact("city", "abilene",      new Integer(1718)),       new Fact("city", "addis ababa",  new Integer(8000)),       new Fact("city", "denver",       new Integer(5280)),       new Fact("city", "flagstaff",    new Integer(6970)),       new Fact("city", "jacksonville", new Integer(8)),       new Fact("city", "leadville",    new Integer(10200)),       new Fact("city", "madrid",       new Integer(1305)),       new Fact("city", "richmond",     new Integer(19)),       new Fact("city", "spokane",      new Integer(1909)),       new Fact("city", "wichita",      new Integer(1305))       };     Program p = new Program();     for (int i = 0; i < facts.length; i++) {         p.addAxiom(facts[i]);     }     return p; } /**  * Show the construction and use of a simple program.  */ public static void main(String[] args) {     Program p = altitudes();     Variable name = new Variable("Name");     Variable height = new Variable("Height");     Structure s = new Structure(         "city", new Term[]{name, height});     Query q = new Query(p, s);     while (q.canFindNextProof()) {         System.out.println(             name + " is about " +             height + " feet above sea level.");     } } } 

This program prints the following:

 abilene is about 1718 feet above sea level.  addis ababa is about 8000 feet above sea level. denver is about 5280 feet above sea level. flagstaff is about 6970 feet above sea level. jacksonville is about 8 feet above sea level. leadville is about 10200 feet above sea level. madrid is about 1305 feet above sea level. richmond is about 19 feet above sea level. spokane is about 1909 feet above sea level. wichita is about 1305 feet above sea level. 

The program works by loading a program with facts and then querying the program. ShowProgram.main() constructs a Query object with this code:

 Variable name = new Variable("Name");  Variable height = new Variable("Height"); Structure s = new Structure(     "city", new Term[]{name, height}); Query q = new Query(p, s); 

If the query structure s printed itself, it would appear as

 city(Name, Height) 

ShowProgram.main() creates a query by combining this structure with a logic program to prove against. In a while loop, the program repeatedly asks the Query object q whether it can find another proof:

 while (q.canFindNextProof()) {      System.out.println(         name + " is about " +         height + " feet above sea level."); } 

Each time the Query object q finds a proof, it unifies its structure with one of the program's facts. When this structure unifies, its variables unify. When Variable objects are printed, they show the value with which they have unified.


   
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