001: package org.ofbiz.rules.engine;
002:
003: /**
004: * <p><b>Title:</b> Rule
005: * <p><b>Description:</b> None
006: * <p>Copyright (c) 1999 Steven J. Metsker.
007: * <p>Copyright (c) 2001 The Open For Business Project - www.ofbiz.org
008: *
009: * <p>Permission is hereby granted, free of charge, to any person obtaining a
010: * copy of this software and associated documentation files (the "Software"),
011: * to deal in the Software without restriction, including without limitation
012: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
013: * and/or sell copies of the Software, and to permit persons to whom the
014: * Software is furnished to do so, subject to the following conditions:
015: *
016: * <p>The above copyright notice and this permission notice shall be included
017: * in all copies or substantial portions of the Software.
018: *
019: * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
020: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
021: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
022: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
023: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
024: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
025: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
026: *
027: * <br>
028: * <p>A Rule represents a logic statement that a structure is true
029: * if a following series of other structures are true.
030: * <p>
031: * For example,
032: * <blockquote><pre>
033: * bachelor(X) :- male(X), unmarried(X);
034: * </pre></blockquote>
035: * <p>
036: * is a logical rule.
037: * <p>
038: * A rule can make a provable version of itself, a DynamicRule,
039: * that is a essentially a copy of the structures in the rule,
040: * with a new scope and with a program to consult for other
041: * rules.
042: *
043: * @author Steven J. Metsker
044: * @version 1.0
045: */
046: public class Rule implements Axiom {
047: protected Structure[] structures;
048:
049: /**
050: * Construct rule from the given structures.
051: *
052: * @param Structure[] the structures that make up this rule.
053: *
054: */
055: public Rule(Structure[] structures) {
056: this .structures = structures;
057: }
058:
059: /**
060: * Construct a one-structure rule from the given structure.
061: *
062: * @param Structure the structure that makes up this rule.
063: *
064: */
065: public Rule(Structure s) {
066: this (new Structure[] { s });
067: }
068:
069: /**
070: * Return a provable version of this rule.
071: *
072: * @return a provable version of this rule
073: */
074: public DynamicAxiom dynamicAxiom(AxiomSource as) {
075: return new DynamicRule(as, new Scope(), this );
076: }
077:
078: /**
079: * Returns true if the supplied object is an equivalent
080: * rule.
081: *
082: * @param object the object to compare
083: *
084: * @return true, if the supplied object's structures equal
085: * this rule's structures
086: */
087: public boolean equals(Object o) {
088: if (!(o instanceof Rule))
089: return false;
090: Rule r = (Rule) o;
091:
092: if (!(structures.length == r.structures.length)) {
093: return false;
094: }
095: for (int i = 0; i < structures.length; i++) {
096: if (!(structures[i].equals(r.structures[i]))) {
097: return false;
098: }
099: }
100: return true;
101: }
102:
103: /**
104: * Return the first structure in this rule.
105: *
106: * @return the first structure in this rule
107: */
108: public Structure head() {
109: return structures[0];
110: }
111:
112: /**
113: * Returns a string representation of this rule.
114: *
115: * @return a string representation of this rule.
116: */
117: public String toString() {
118: StringBuffer buf = new StringBuffer();
119:
120: for (int i = 0; i < structures.length; i++) {
121: if (i == 1) {
122: buf.append(" :- ");
123: }
124: if (i > 1) {
125: buf.append(", ");
126: }
127: buf.append(structures[i].toString());
128: }
129: return buf.toString();
130: }
131: }
|