001: package org.ofbiz.rules.engine;
002:
003: /**
004: * <p><b>Title:</b> Anonymous Variable
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>An anonymous variable unifies successfully with any other
029: * term, without binding to the term.
030: * <p>
031: * Anonymous variables are useful for screening out unwanted
032: * terms. For example, if a program describes a marriage in
033: * terms of an id, the husband, wife, and the beginning and
034: * end dates of the marriage, its facts might look something
035: * like:
036: * <blockquote><pre>
037: * marriage(001, balthasar, grimelda, 14560512, 14880711);
038: * // ...
039: * marriage(257, kevin, karla, 19790623, present);
040: * </pre></blockquote>
041: * A rule that extracts just the husband from
042: * <code>marriage</code> facts is:
043: * <blockquote><pre>
044: * husband(Id, Hub) :- marriage(Id, Hub, _, _, _);
045: * </pre></blockquote>
046: * The underscores in this rule represent anonymous variables.
047: * When the rule executes, it will unify its
048: * <code>marriage</code> structure with <code>marriage</code>
049: * facts, without regard to the last three terms of those
050: * facts.
051: * <p>
052: * Without anonymous variables, the <code>husband</code> rule
053: * would need three unused variables. Note that the following
054: * approach would fail:
055: * <blockquote><pre>
056: * husband(Id, Hub) :-
057: * marriage(Id, Hub, Anon, Anon, Anon); // wrong
058: * </pre></blockquote>
059: * This approach, while tempting, will not work because the
060: * variable <code>Anon</code> will bind to the structures it
061: * encounters. Issued against the example program,
062: * <code>Anon</code> will first bind to <code>grimelda</code>.
063: * Next, <code>Anon</code> will attempt to bind to
064: * <code>14560512</code>. This will fail, since
065: * <code>Anon</code> will already be bound to
066: * <code>grimelda</code>.
067: * <p>
068: * The essential behavior anonymous variables provide is that
069: * they unify successfully without binding.
070: *
071: * @author Steven J. Metsker
072: * @version 1.0
073: */
074:
075: public class Anonymous extends Variable {
076:
077: /**
078: * Constructs an anonymous variable.
079: */
080: public Anonymous() {
081: super ("_");
082: }
083:
084: /**
085: * Returns this anonymous variable, which does not unify
086: * with anything and thus does not need to copy itself.
087: *
088: * @return this anonymous variable
089: *
090: * @param AxiomSource ignored
091: *
092: * @param Scope ignored
093: */
094: public Term copyForProof(AxiomSource ignored, Scope ignored2) {
095: return this ;
096: }
097:
098: /**
099: * Return the value of this anonymous variable to use in
100: * functions; this is meaningless in logic programming,
101: * but the method returns the name of this variable.
102: *
103: * @return the name of the anonymous variable
104: */
105: public Object eval() {
106: return name;
107: }
108:
109: /**
110: * Returns an empty unification.
111: * <p>
112: * The <code>unify</code> methods indicate failure by
113: * returning <code>null</code>. Anonymous variables succeed
114: * without binding, so they always return empty unifications.
115: *
116: * @param Structure ignored
117: *
118: * @return A successful, but empty, unification
119: */
120: public Unification unify(Structure ignored) {
121: return Unification.empty;
122: }
123:
124: /**
125: * Returns an empty unification.
126: *
127: * @param Term ignored
128: *
129: * @return an empty unification
130: */
131: public Unification unify(Term ignored) {
132: return Unification.empty;
133: }
134:
135: /**
136: * Returns an empty unification.
137: *
138: * @param Variable ignored
139: *
140: * @return an empty unification
141: */
142: public Unification unify(Variable ignored) {
143: return Unification.empty;
144: }
145:
146: /**
147: * Returns an empty unification.
148: *
149: * @return an empty unification
150: */
151: public Unification variables() {
152: return Unification.empty;
153: }
154: }
|