001: package org.mandarax.reference;
002:
003: /*
004: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: import org.mandarax.kernel.AbstractPropertiesSupport;
021: import org.mandarax.kernel.Clause;
022: import org.mandarax.kernel.DerivationEventListener;
023: import org.mandarax.kernel.Fact;
024: import org.mandarax.kernel.Query;
025:
026: /**
027: * Reference implementation of the Query interface.
028: * <p><strong>Warning:</strong>This class should only be instanciated through a logic factory object.
029: * This class is public and has public constructors only to support tools based on the bean component model
030: * such as xml serialization.
031: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
032: * @version 3.4 <7 March 05>
033: * @since 1.9
034: */
035: public class QueryImpl extends AbstractPropertiesSupport implements
036: Query {
037:
038: private Fact[] facts = null;
039: private DerivationEventListener[] derivationEventListeners = null;
040: private String name = null;
041:
042: /**
043: * Constructor.
044: */
045: public QueryImpl() {
046: super ();
047: }
048:
049: /**
050: * Constructor.
051: */
052: QueryImpl(Fact[] facts, String name) {
053: super ();
054: this .facts = facts;
055: this .name = name;
056: }
057:
058: /**
059: * Check whether objects are equal.
060: * @param obj the object to compare this object with
061: * @return true if the objects are equal, false otherwise
062: */
063: public boolean equals(Object obj) {
064: if ((obj != null) && (obj instanceof QueryImpl)) {
065: QueryImpl aQuery = (QueryImpl) obj;
066: boolean result = (name == null ? aQuery.getName() == null
067: : true);
068: result = result && name.equals(aQuery.getName());
069: Fact[] ff = aQuery.getFacts();
070: result = result && (facts == null ? ff == null : true);
071: result = result && (facts.length == ff.length);
072: for (int i = 0; i < facts.length; i++) {
073: result = result && (facts[i].equals(ff[i]));
074: }
075: return result;
076: }
077: return false;
078: }
079:
080: /**
081: * Get the hashcode of the object.
082: * @return an integer
083: */
084: public int hashCode() {
085: int i1 = name == null ? 0 : name.hashCode();
086: if (facts == null || facts.length == 0)
087: return i1;
088: else
089: return i1 ^ facts[0].hashCode();
090: }
091:
092: /**
093: * Get the facts.
094: * @see org.mandarax.kernel.Fact
095: * @return an array of facts
096: */
097: public Fact[] getFacts() {
098: return facts;
099: }
100:
101: /**
102: * Set the facts.
103: * @see org.mandarax.kernel.Fact
104: * @param facts an array of facts
105: */
106: public void setFacts(Fact[] facts) {
107: this .facts = facts;
108: }
109:
110: /**
111: * Get the common name of this query, i.e., something like "Find the oncle of a person".
112: * @return a string
113: */
114: public String getName() {
115: return name;
116: }
117:
118: /**
119: * Set the common name of this query.
120: * @param a name
121: */
122: public void setName(String queryName) {
123: name = queryName;
124: }
125:
126: /**
127: * Get the registered derivation event listeners.
128: * @return an array of listeners
129: */
130: public DerivationEventListener[] getDerivationEventListeners() {
131: return derivationEventListeners;
132: }
133:
134: /**
135: * Set the derivation event listeners.
136: * @param listeners listeners
137: */
138: public void setDerivationEventListeners(
139: DerivationEventListener[] listeners) {
140: derivationEventListeners = listeners;
141: }
142:
143: /**
144: * Indicates whether the clause is ground (= does not have variables).
145: * @return a boolean
146: */
147: public boolean isGround() {
148: if (this .facts != null) {
149: for (int i = 0; i < this .facts.length; i++) {
150: if (!facts[i].isGround())
151: return false;
152: }
153: }
154: return true;
155: }
156:
157: }
|