001: package org.ofbiz.rules.engine;
002:
003: /**
004: * <p><b>Title:</b> Not
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 Not is a structure that fails if it can prove itself
029: * against a program.
030: * <p>
031: * This simple behavior can have surprising results. For
032: * example:
033: * <blockquote><pre>
034: * goodBachelor(X) :- male (X), not married(X);
035: * badBachelor(X) :- not married(X), male(X);
036: * married(jim);
037: * male(jeremy);
038: * male(jim);
039: * </pre></blockquote>
040: *
041: * Against this program, a query <code>badBachelor(X)</code>
042: * will find no bachelors. This rule negates <code>married(X)
043: * </code> which will bind uninstantiated <code>X</code> to
044: * <code>jim</code> every time. Thus <code>married(X)</code>
045: * will always be true, <code>not married(X)</code> will
046: * always be false, and there are no bad bachelors, which is
047: * untrue. The good rule instantiates <code>X</code> to a good
048: * candidate first, and the negation will prove or disprove
049: * itself for that candidate.
050: * <p>
051: * (A parser of the logic code given above will produce
052: * engine calls similar to the following code.)
053: *
054: * <blockquote><pre>
055: * Program p = new Program();
056: * Variable x = new Variable("X");
057: *
058: * p.addAxiom(new Rule(new Structure[]{
059: * new Structure("goodBachelor", new Term[]{x}),
060: * new Structure("male", new Term[]{x}),
061: * new Not ("married", new Term[]{x})}));
062: *
063: * p.addAxiom(new Rule(new Structure[]{
064: * new Structure("badBachelor", new Term[]{x}),
065: * new Not ("married", new Term[]{x}),
066: * new Structure("male", new Term[]{x})}));
067: * p.addAxiom(new Fact("married", "jim"));
068: * p.addAxiom(new Fact("male", "jeremy"));
069: * p.addAxiom(new Fact("male", "jim"));
070: *
071: * Query qGood = new Query(
072: * p, new Structure("goodBachelor", new Term[]{x}));
073: * while(qGood.canFindNextProof()) {
074: * System.out.println(
075: * "goodBachelor query finds: " + qGood.lookup("X"));
076: * }
077: *
078: * Query qBad = new Query(
079: * p, new Structure("badBachelor", new Term[]{x}));
080: * while(qBad.canFindNextProof()) {
081: * System.out.println(
082: * "badBachelor query finds: " + qBad.lookup("X"));
083: * }
084: *
085: * </pre></blockquote>
086: *
087: * Running this code prints out:
088: * <blockquote><pre>
089: * goodBachelor query finds: jeremy
090: * </pre></blockquote>
091: *
092: * @author Steven J. Metsker
093: * @version 1.0
094: */
095: public class Not extends Structure {
096:
097: /**
098: * Contructs a Not from the specified object.
099: * <p>
100: * Such a Not will search a program for a fact that contains
101: * just the functor. For example, it might make sense to create a
102: * <code>Not("demo")</code> that would look for the fact
103: * <code>demo</code> to see if the program is in demo mode.
104: *
105: * @param functor the functor for this structure
106: */
107: public Not(Object functor) {
108: this (functor, new Term[0]);
109: }
110:
111: /**
112: * Constructs a Not with the specified functor and terms.
113: *
114: * This is the normal way to define a Not. This structure will
115: * be true if there is no possible proof of it in a program.
116: *
117: * @param functor the functor of the structure
118: *
119: * @param terms the terms of the structure, which may be either
120: * variables or other structures
121: */
122: public Not(Object functor, Term[] terms) {
123: super (functor, terms);
124: }
125:
126: /**
127: * Constructs a Not version of the supplied structure.
128: *
129: * @param structure the structure to negate
130: */
131: public Not(Structure s) {
132: this (s.functor, s.terms);
133: }
134:
135: /**
136: * Create a <code>ConsultingNot</code> counterpart that
137: * can prove itself.
138: *
139: * @param AxiomSource where to find axioms to prove
140: * against
141: *
142: * @param Scope the scope to use for variables in the
143: * <code>ConsultingStructure</code>
144: *
145: * @return a <code>ConsultingNot</code> counterpart that
146: * can prove itself
147: */
148: public Term copyForProof(AxiomSource as, Scope scope) {
149: Term[] newTerms = new Term[terms.length];
150:
151: for (int i = 0; i < terms.length; i++) {
152: newTerms[i] = terms[i].copyForProof(as, scope);
153: }
154: return new ConsultingNot(new ConsultingStructure(as, functor,
155: newTerms));
156: }
157:
158: /**
159: * Returns true if the supplied object is an equivalent
160: * not structure.
161: *
162: * @param object the object to compare
163: *
164: * @return true, if the supplied object is a Not, and
165: * the two object's sub-structures are equal
166: */
167: public boolean equals(Object o) {
168: if (!(o instanceof Not))
169: return false;
170: Not n = (Not) o;
171:
172: if (!functorAndArityEquals(n)) {
173: return false;
174: }
175: for (int i = 0; i < terms.length; i++) {
176: if (!(terms[i].equals(n.terms[i]))) {
177: return false;
178: }
179: }
180: return true;
181: }
182:
183: /**
184: * Returns a string description of this Not.
185: *
186: * @return a string description of this Not
187: */
188: public String toString() {
189: return "not " + super.toString();
190: }
191: }
|