001: package org.ofbiz.rules.logikus;
002:
003: import org.ofbiz.rules.engine.*;
004:
005: /**
006: * <p><b>Title:</b> Anonymous
007: * <p><b>Description:</b> None
008: * <p>Copyright (c) 1999 Steven J. Metsker.
009: * <p>Copyright (c) 2001 The Open For Business Project - www.ofbiz.org
010: *
011: * <p>Permission is hereby granted, free of charge, to any person obtaining a
012: * copy of this software and associated documentation files (the "Software"),
013: * to deal in the Software without restriction, including without limitation
014: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
015: * and/or sell copies of the Software, and to permit persons to whom the
016: * Software is furnished to do so, subject to the following conditions:
017: *
018: * <p>The above copyright notice and this permission notice shall be included
019: * in all copies or substantial portions of the Software.
020: *
021: * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
022: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
023: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
024: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
025: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
026: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
027: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
028: *
029: * <br>
030: * <p>An anonymous variable unifies successfully with any other
031: * term, without binding to the term.
032: * <p>
033: * Anonymous variables are useful for screening out unwanted
034: * terms. For example, if a program describes a marriage in
035: * terms of an id, the husband, wife, and the beginning and
036: * end dates of the marriage, its facts might look something
037: * like:
038: * <blockquote><pre>
039: * marriage(001, balthasar, grimelda, 14560512, 14880711);
040: * // ...
041: * marriage(257, kevin, karla, 19790623, present);
042: * </pre></blockquote>
043: * A rule that extracts just the husband from
044: * <code>marriage</code> facts is:
045: * <blockquote><pre>
046: * husband(Id, Hub) :- marriage(Id, Hub, _, _, _);
047: * </pre></blockquote>
048: * The underscores in this rule represent anonymous variables.
049: * When the rule executes, it will unify its
050: * <code>marriage</code> structure with <code>marriage</code>
051: * facts, without regard to the last three terms of those
052: * facts.
053: * <p>
054: * Without anonymous variables, the <code>husband</code> rule
055: * would need three unused variables. Note that the following
056: * approach would fail:
057: * <blockquote><pre>
058: * husband(Id, Hub) :-
059: * marriage(Id, Hub, Anon, Anon, Anon); // wrong
060: * </pre></blockquote>
061: * This approach, while tempting, will not work because the
062: * variable <code>Anon</code> will bind to the structures it
063: * encounters. Issued against the example program,
064: * <code>Anon</code> will first bind to <code>grimelda</code>.
065: * Next, <code>Anon</code> will attempt to bind to
066: * <code>14560512</code>. This will fail, since
067: * <code>Anon</code> will already be bound to
068: * <code>grimelda</code>.
069: * <p>
070: * The essential behavior anonymous variables provide is that
071: * they unify successfully without binding.
072: *
073: * @author Steven J. Metsker
074: * @version 1.0
075: */
076: public class Anonymous extends Variable {
077:
078: /**
079: * Constructs an anonymous variable.
080: */
081: public Anonymous() {
082: super ("_");
083: }
084:
085: /**
086: * Returns this anonymous variable, which does not unify
087: * with anything and thus does not need to copy itself.
088: *
089: * @return this anonymous variable
090: *
091: * @param AxiomSource ignored
092: *
093: * @param Scope ignored
094: */
095: public Term copyForProof(AxiomSource ignored, Scope ignored2) {
096:
097: return this ;
098: }
099:
100: /**
101: * Return the value of this anonymous variable to use in
102: * functions; this is meaningless in logic programming,
103: * but the method returns the name of this variable.
104: *
105: * @return the name of the anonymous variable
106: */
107: public Object eval() {
108: return name;
109: }
110:
111: /**
112: * Returns an empty unification.
113: * <p>
114: * The <code>unify</code> methods indicate failure by
115: * returning <code>null</code>. Anonymous variables succeed
116: * without binding, so they always return empty unifications.
117: *
118: * @param Structure ignored
119: *
120: * @return A successful, but empty, unification
121: */
122: public Unification unify(Structure ignored) {
123: return Unification.empty;
124: }
125:
126: /**
127: * Returns an empty unification.
128: *
129: * @param Term ignored
130: *
131: * @return an empty unification
132: */
133: public Unification unify(Term ignored) {
134: return Unification.empty;
135: }
136:
137: /**
138: * Returns an empty unification.
139: *
140: * @param Variable ignored
141: *
142: * @return an empty unification
143: */
144: public Unification unify(Variable ignored) {
145: return Unification.empty;
146: }
147:
148: /**
149: * Returns an empty unification.
150: *
151: * @return an empty unification
152: */
153: public Unification variables() {
154: return Unification.empty;
155: }
156: }
|