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.util;
019:
020: import java.util.Iterator;
021: import java.util.Vector;
022:
023: import org.mandarax.kernel.Clause;
024: import org.mandarax.kernel.Fact;
025: import org.mandarax.kernel.LogicFactory;
026: import org.mandarax.kernel.Replacement;
027: import org.mandarax.kernel.VariableTerm;
028:
029: /**
030: * Clause iterator for auto facts.
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.2
034: */
035: class AutoFactIterator extends AbstractAutoFactIterator {
036:
037: private Iterator multiIterator = null;
038: private Fact fact = null;
039: private Fact nextFact = null;
040: private VariableTerm[] variables = null;
041: private Vector replacements = null;
042:
043: /**
044: * Constructor.
045: * @param f a fact
046: * @param mci an iterator
047: * @param vars the variables
048: * @param af the client object
049: */
050: public AutoFactIterator(Fact f, Iterator mci, VariableTerm[] vars,
051: AutoFacts af) {
052: super (af);
053:
054: fact = f;
055: multiIterator = mci;
056: variables = vars;
057:
058: initialize();
059: }
060:
061: /**
062: * Indicates whether there are more clauses.
063: * @return boolean
064: */
065: public boolean hasMoreClauses() {
066: searchNextFact();
067:
068: return (nextFact != null);
069: }
070:
071: /**
072: * Initialize the object.
073: */
074: private void initialize() {
075: replacements = new Vector(variables.length);
076:
077: Replacement r;
078:
079: for (int i = 0; i < variables.length; i++) {
080: r = new Replacement();
081: r.original = variables[i];
082:
083: replacements.add(r);
084: }
085: ;
086: }
087:
088: /**
089: * Get the next clause.
090: * @return the next clause
091: */
092: public Clause nextClause() {
093: return nextFact;
094: }
095:
096: /**
097: * Search the next fact. If there is one,
098: * put it into the variable nextFact, otherwise
099: * reset this variable to null.
100: */
101: private void searchNextFact() {
102: nextFact = null;
103:
104: if (multiIterator == null) {
105:
106: // this is the case if there are no variables
107: } else {
108: while (multiIterator.hasNext() && (nextFact == null)) {
109: Object[] values = (Object[]) multiIterator.next();
110:
111: for (int i = 0; i < variables.length; i++) {
112: ((Replacement) replacements.get(i)).replacement = LogicFactory
113: .getDefaultFactory().createConstantTerm(
114: values[i]);
115: }
116:
117: Fact f = fact.applyToFact(replacements);
118:
119: if (perform(f, null)) {
120: nextFact = f;
121:
122: nextFact.setContainer(autofacts);
123: }
124: }
125: }
126: }
127: }
|