001: package org.ofbiz.rules.engine;
002:
003: import java.util.*;
004: import org.ofbiz.rules.utensil.*;
005:
006: /**
007: * <p><b>Title:</b> Scope
008: * <p><b>Description:</b> None
009: * <p>Copyright (c) 1999 Steven J. Metsker.
010: * <p>Copyright (c) 2001 The Open For Business Project - www.ofbiz.org
011: *
012: * <p>Permission is hereby granted, free of charge, to any person obtaining a
013: * copy of this software and associated documentation files (the "Software"),
014: * to deal in the Software without restriction, including without limitation
015: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
016: * and/or sell copies of the Software, and to permit persons to whom the
017: * Software is furnished to do so, subject to the following conditions:
018: *
019: * <p>The above copyright notice and this permission notice shall be included
020: * in all copies or substantial portions of the Software.
021: *
022: * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
023: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
024: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
025: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
026: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
027: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
028: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
029: *
030: * <br>
031: * <p>A scope is a repository for variables. A dynamic rule has
032: * a scope, which means that variables with the same name
033: * are the same variable. Consider a rule that describes
034: * big cities:
035: *
036: * <blockquote><pre>
037: * bigCity(Name) :- city(Name, Pop), >(Pop, 1000000);
038: * </pre></blockquote>
039: *
040: * This example follows the Prolog convention of using
041: * capitalized words for variables. The variables <code>Name
042: * </code> and <code>Pop</code> have scope throughout the rule.
043: * <p>
044: * The <code>bigCity</code> rule proves itself by finding
045: * a city and checking its population. When the <code>city
046: * </code> structure binds with a city fact in a program, its
047: * variables take on the values of the fact. For example,
048: * <code>city</code> might bind with the fact <code>
049: * city(bigappolis, 8733352) </code>. Then <code>Name</code>
050: * and <code>Pop</code> will bind to the values "bigappolis"
051: * and 8733352.
052: * <p>
053: * When the comparison proves itself, it will compare <code>
054: * Pop</code> to 1000000. This is the same variable as <code>
055: * Pop</code> in the <code>city</code> structure of the rule.
056: * With this successful comparison, the rule completes a
057: * successful proof, with <code>Name</code> bound to
058: * "bigappolis".
059: *
060: * @author Steven J. Metsker
061: * @version 1.0
062: */
063: public class Scope implements PubliclyCloneable {
064: Hashtable dictionary = new Hashtable();
065:
066: /**
067: * Create an empty scope.
068: */
069: public Scope() {
070: }
071:
072: /**
073: * Create a scope that uses the variables in the supplied
074: * terms.
075: *
076: * @param Term[] the terms to seed this scope with
077: */
078: public Scope(Term terms[]) {
079: for (int i = 0; i < terms.length; i++) {
080: Unification u = terms[i].variables();
081: Enumeration e = u.elements();
082:
083: while (e.hasMoreElements()) {
084: Variable v = (Variable) e.nextElement();
085:
086: dictionary.put(v.name, v);
087: }
088: }
089: }
090:
091: /**
092: * Remove all variables from this scope.
093: */
094: public void clear() {
095: dictionary.clear();
096: }
097:
098: /**
099: * Return a copy of this object.
100: *
101: * @return a copy of this object
102: */
103: public Object clone() {
104: try {
105: Scope clone = (Scope) super .clone();
106:
107: clone.dictionary = (Hashtable) dictionary.clone();
108: return clone;
109: } catch (CloneNotSupportedException e) {
110: // this shouldn't happen, since we are Cloneable
111: throw new InternalError();
112: }
113: }
114:
115: /**
116: * Returns true if a variable of the given name appears
117: * in this scope.
118: *
119: * @param String the variable name
120: *
121: * @return true, if a variable of the given name appears
122: * in this scope.
123: */
124: public boolean isDefined(String name) {
125: return dictionary.containsKey(name);
126: }
127:
128: /**
129: * Returns a variable of the given name from this scope.
130: *
131: * If the so-named variable is not already in this scope,
132: * the scope will create it and add the variable to itself.
133: *
134: * @param String the variable name
135: *
136: * @return a variable of the given name from this scope
137: */
138: public Variable lookup(String name) {
139: Variable v = (Variable) dictionary.get(name);
140:
141: if (v == null) {
142: v = new Variable(name);
143: dictionary.put(v.name, v);
144: }
145: return v;
146: }
147: }
|