001: /*
002: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.mandarax.kernel.meta;
019:
020: import java.util.Collection;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Vector;
024:
025: import org.mandarax.kernel.Clause;
026: import org.mandarax.util.DataSource;
027: import org.mandarax.util.StaticDataSource;
028:
029: /**
030: * Clause set for symmetric predicates such as <code>equals()</code>.
031: * E.g.,in case a equals b, only one of the facts equals(a,b) and equals(b,a) is in the set
032: * because it is known (from the semantic of the method) that <code>a.equals(b)</code>
033: * yields true if and only if <code>b.equals(a)</code> is yields true.
034: * @see org.mandarax.util.AutoFacts
035: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
036: * @version 3.4 <7 March 05>
037: * @since 1.0
038: * @deprecated it is recommended to use the more general class org.mandarax.util.AutoFacts instead
039: */
040: public class JSymmetricClauseSet extends AbstractClauseSet {
041:
042: private DataSource argumentSet = null;
043:
044: /**
045: * Constructor.
046: * @param aPredicate a predicate
047: * @param argumentSets the collections of objects
048: * @throws java.lang.IllegalAccessException Thrown if the method wrapped by the predicate is not public.
049: * @throws java.lang.IllegalArgumentException Thrown if the return type of the method wrapped by the predicate is not boolean or if the number of argument sets does not match.
050: */
051: public JSymmetricClauseSet(JPredicate aPredicate,
052: Collection argumentSet) throws NullPointerException,
053: IllegalAccessException, IllegalArgumentException {
054: super ();
055:
056: initialize(aPredicate, new StaticDataSource(argumentSet));
057: }
058:
059: /**
060: * Constructor.
061: * @param aPredicate a predicate
062: * @param argumentSets the collections of objects
063: * @param isNegated true if the set should be negated, false otherwise
064: * @throws java.lang.IllegalAccessException Thrown if the method wrapped by the predicate is not public.
065: * @throws java.lang.IllegalArgumentException Thrown if the return type of the method wrapped by the predicate is not boolean or if the number of argument sets does not match.
066: */
067: public JSymmetricClauseSet(JPredicate aPredicate,
068: Collection argumentSet, boolean isNegated)
069: throws NullPointerException, IllegalAccessException,
070: IllegalArgumentException {
071: super (isNegated);
072:
073: initialize(aPredicate, new StaticDataSource(argumentSet));
074: }
075:
076: /**
077: * Constructor.
078: * @param aPredicate a predicate
079: * @param argumentSets the data sources (kind of dynamic collections)
080: * @throws java.lang.IllegalAccessException Thrown if the method wrapped by the predicate is not public.
081: * @throws java.lang.IllegalArgumentException Thrown if the return type of the method wrapped by the predicate is not boolean or if the number of argument sets does not match.
082: */
083: public JSymmetricClauseSet(JPredicate aPredicate,
084: DataSource argumentSet) throws NullPointerException,
085: IllegalAccessException, IllegalArgumentException {
086: super ();
087:
088: initialize(aPredicate, argumentSet);
089: }
090:
091: /**
092: * Constructor.
093: * @param aPredicate a predicate
094: * @param argumentSets the data sources (kind of dynamic collections)
095: * @param isNegated true if the set should be negated, false otherwise
096: * @throws java.lang.IllegalAccessException Thrown if the method wrapped by the predicate is not public.
097: * @throws java.lang.IllegalArgumentException Thrown if the return type of the method wrapped by the predicate is not boolean or if the number of argument sets does not match.
098: */
099: public JSymmetricClauseSet(JPredicate aPredicate,
100: DataSource argumentSet, boolean isNegated)
101: throws NullPointerException, IllegalAccessException,
102: IllegalArgumentException {
103: super (isNegated);
104:
105: initialize(aPredicate, argumentSet);
106: }
107:
108: /**
109: * Build the collection of facts.
110: * @return the collection of facts built
111: */
112: protected Collection buildFacts() {
113: Collection collData = argumentSet.getData();
114: List data = null;
115:
116: // cast to or build a list since looping with an index is easier here
117: if (collData instanceof List) {
118: data = (List) collData;
119: } else {
120: Vector v = new Vector(collData.size());
121:
122: for (Iterator it = collData.iterator(); it.hasNext();) {
123: v.add(it.next());
124: }
125:
126: data = v;
127: }
128:
129: int n = data.size();
130: Clause f = null;
131: Vector coll = new Vector(n * (n + 1) / 2);
132: Object[] par = new Object[1];
133:
134: for (int i = 0; i < data.size(); i++) {
135: for (int j = 0; j <= i; j++) {
136: par[0] = data.get(j);
137: f = buildFact(data.get(i), par);
138:
139: if (f != null) {
140: coll.add(f);
141: }
142: }
143: }
144:
145: return coll;
146: }
147:
148: /**
149: * Validate the parameters provided to set up the set.
150: * @param aPredicate the predicate
151: * @param argSet the data source
152: * @throws java.lang.IllegalAccessException Thrown if the method wrapped by the predicate is not public.
153: * @throws java.lang.IllegalArgumentException Thrown if the return type of the method wrapped by the predicate is not boolean or if the number of argument sets does not match.
154: */
155: private void initialize(JPredicate aPredicate, DataSource argSet)
156: throws NullPointerException, IllegalAccessException,
157: IllegalArgumentException {
158: if (argSet == null) {
159: throw new NullPointerException(
160: "The argument sets cannot be null");
161: }
162:
163: initialize(aPredicate);
164:
165: argumentSet = argSet;
166: }
167: }
|