001: package org.mandarax.util.comparators;
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:
021: import java.io.Serializable;
022: import java.util.Comparator;
023: import java.util.List;
024:
025: import org.mandarax.kernel.ComplexTerm;
026: import org.mandarax.kernel.Prerequisite;
027: import org.mandarax.kernel.Rule;
028: import org.mandarax.kernel.Term;
029: import org.mandarax.kernel.TermContainer;
030: import org.mandarax.kernel.VariableTerm;
031:
032: /**
033: * Abstract class for clause set comparators.
034: * Implements a couple of useful count methods that can be used
035: * in subclasses.
036: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
037: * @version 3.4 <7 March 05>
038: * @since 2.2
039: */
040: public abstract class AbstractClauseSetComparator implements
041: Comparator, Serializable {
042:
043: /**
044: * Constructor.
045: */
046: public AbstractClauseSetComparator() {
047: super ();
048: }
049:
050: /**
051: * Count the number of negated prerequisites of a rule.
052: * @return the number of prerequisites
053: * @param rule a rule
054: */
055: protected int countNegatedPrerequisites(Rule rule) {
056: int count = 0;
057: List body = rule.getBody();
058: for (int i = 0; i < body.size(); i++) {
059: Prerequisite prereq = (Prerequisite) body.get(i);
060: if (prereq.isNegatedAF())
061: count = count + 1;
062: }
063: return count;
064: }
065:
066: /**
067: * Count the number of prerequisites of a rule.
068: * @return the number of prerequisites
069: * @param rule a rule
070: */
071: protected int countPrerequisites(Rule rule) {
072:
073: return rule.getBody().size();
074: }
075:
076: /**
077: * Count the number of variables in a term container
078: * (fact of complex term).
079: * @return the number of variables detected
080: * @param tc the term container
081: */
082: protected int countVariables(TermContainer tc) {
083: int count = 0;
084: Term[] terms = tc.getTerms();
085: for (int i = 0; i < terms.length; i++) {
086: if (terms[i] instanceof VariableTerm)
087: count = count + 1;
088: else if (terms[i] instanceof ComplexTerm)
089: count = count + countVariables((ComplexTerm) terms[i]);
090: }
091: return count;
092: }
093:
094: /**
095: * Count the number of variables in the prerequisites of a rule.
096: * @return the number of prerequisites
097: * @param rule a rule
098: */
099: protected int countVariablesInPrerequisites(Rule rule) {
100: int count = 0;
101: List body = rule.getBody();
102: for (int i = 0; i < body.size(); i++) {
103: Prerequisite prereq = (Prerequisite) body.get(i);
104: count = count + countVariables(prereq);
105: }
106: return count;
107: }
108:
109: /**
110: * Get a descriptive name.
111: * @return a string
112: */
113: public abstract String getName();
114:
115: /**
116: * Convert the object to a string.
117: * @return a string
118: */
119: public String toString() {
120: return getName();
121: }
122:
123: /**
124: * Compares objects. All instances are stateless and considered to be equal if the classes are the same!
125: * @param obj an object
126: * @return a boolean
127: */
128: public boolean equals(Object obj) {
129: return obj != null && obj.getClass() == getClass();
130:
131: }
132:
133: /**
134: * Get the hash code of this object.
135: * @return an integer
136: */
137: public int hashCode() {
138: return getClass().hashCode();
139: }
140: }
|