Building Parsers with Java

sjm.engine
Class Scope

java.lang.Object
  |
  +--sjm.engine.Scope

public class Scope
extends java.lang.Object
implements PubliclyCloneable

A scope is a repository for variables. A dynamic rule has a scope, which means that variables with the same name are the same variable. Consider a rule that describes big cities:

     bigCity(Name) :- city(Name, Pop), >(Pop, 1000000);
 
This example follows the Prolog convention of using capitalized words for variables. The variables Name and Pop have scope throughout the rule.

The bigCity rule proves itself by finding a city and checking its population. When the city structure binds with a city fact in a program, its variables take on the values of the fact. For example, city might bind with the fact city(bigappolis, 8733352) . Then Name and Pop will bind to the values "bigappolis" and 8733352.

When the comparison proves itself, it will compare Pop to 1000000. This is the same variable as Pop in the city structure of the rule. With this successful comparison, the rule completes a successful proof, with Name bound to "bigappolis".


Constructor Summary
Scope()
          Create an empty scope.
Scope(Term[] terms)
          Create a scope that uses the variables in the supplied terms.
 
Method Summary
 void clear()
          Remove all variables from this scope.
 java.lang.Object clone()
          Return a copy of this object.
 boolean isDefined(java.lang.String name)
          Returns true if a variable of the given name appears in this scope.
 Variable lookup(java.lang.String name)
          Returns a variable of the given name from this scope.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Scope

public Scope()
Create an empty scope.

Scope

public Scope(Term[] terms)
Create a scope that uses the variables in the supplied terms.
Parameters:
Term[] - the terms to seed this scope with
Method Detail

clear

public void clear()
Remove all variables from this scope.

clone

public java.lang.Object clone()
Return a copy of this object.
Specified by:
clone in interface PubliclyCloneable
Overrides:
clone in class java.lang.Object
Returns:
a copy of this object

isDefined

public boolean isDefined(java.lang.String name)
Returns true if a variable of the given name appears in this scope.
Parameters:
String - the variable name
Returns:
true, if a variable of the given name appears in this scope.

lookup

public Variable lookup(java.lang.String name)
Returns a variable of the given name from this scope. If the so-named variable is not already in this scope, the scope will create it and add the variable to itself.
Parameters:
String - the variable name
Returns:
a variable of the given name from this scope

by Steve Metsker