/*
 * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.mandarax.util;

import java.util.Iterator;

import org.mandarax.kernel.Clause;
import org.mandarax.kernel.ClauseSet;
import org.mandarax.kernel.ClauseSetException;
import ws.prova.parser.ParsingException;

/**
 * Iterator for a collection containing clause sets and clauses.
 * New version - no explicit merged collection is needed. This hould result
 * in performance improvements for bot the knowledge base and the inference engine.
 * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
 * @version 3.3.1 <9 August 2004>
 * @since 1.0
 * Prova re-integration modifications
 * @author <A HREF="mailto:a.kozlenkov@city.ac.uk">Alex Kozlenkov</A>
 * @version 3.3.1 <9 August 2004>
 */
public final class MultipleClauseSetIterator extends AbstractClauseIterator {

    private Clause nextClause = null;
    private boolean useParameters = false;
    // [Prova] Used in inference engine
    private ClauseIterator nextClauseIterator = null;
    private Iterator iterator = null;
    private Clause par1 = null;
    private Object par2 = null;
    // [Prova] Used in inference engine
    private java.util.Collection coll;

    /**
     * Constructor.
     * @param coll a collection of clause sets
     */
    public MultipleClauseSetIterator(java.util.Collection coll) {
        super();

        this.coll = coll;
        iterator = coll.iterator();
        useParameters = false;
    }

    /**
     * Constructor.
     * @param coll a collection of clause sets
     * @param query org.mandarax.kernel.Clause
     * @param additionalParameter java.lang.Object
     */
    public MultipleClauseSetIterator(
        java.util.Collection coll,
        Clause query,
        Object additionalParameter) {
        super();

        this.coll = coll;
        iterator = coll.iterator();
        useParameters = true;
        par1 = query;
        par2 = additionalParameter;
    }

    /**
     * Indicates whether there are more clauses.
     * For performance, clauses get special treatment
     * @return true if there are more clauses, false otherwise
     * @throws ClauseSetException
     */
    public boolean hasMoreClauses() throws ClauseSetException, ParsingException {

        // initialize - first step (or after there was a clause)
        if (nextClauseIterator == null) {

            // set the first clause set clause set
            if (iterator.hasNext()) {
                ClauseSet cs = (ClauseSet) iterator.next();

                if ((cs instanceof Clause) && ((Clause) cs).isAtomic()) {
                     nextClause = (Clause) cs;
                    nextClauseIterator = null;

                    return true;
                }

                nextClauseIterator = useParameters ? cs.clauses(par1, par2) : cs.clauses();

                return hasMoreClauses();
            }
            else {
                return false;
            }
        }

        // regular step
        if (nextClauseIterator.hasMoreClauses()) {
            nextClause = nextClauseIterator.nextClause();

            return true;
        }
        else {
            if (iterator.hasNext()) {
                ClauseSet cs = (ClauseSet) iterator.next();

                if (cs instanceof Clause) {
                    nextClause = (Clause) cs;
                    nextClauseIterator = null;

                    return true;
                }

                nextClauseIterator = useParameters ? cs.clauses(par1, par2) : cs.clauses();

                return hasMoreClauses();
            }
            else {
                return false;
            }
        }
    }

    /**
     * [Prova] Return the collection behind the Clause
     * @return
     */
    public java.util.Collection collection() {
        return coll;
    }

    /**
     * Get the next clause.
     * @return the next clause
     */
    public org.mandarax.kernel.Clause nextClause() {
        return nextClause;
    }

    /**
     * [Prova] Direct access to the iterator
     * @return
     */
    public ClauseIterator getNextClauseIterator() {
        return nextClauseIterator;
    }
}
