/*
 * 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.kernel.meta;

import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;

import org.mandarax.kernel.Clause;
import org.mandarax.kernel.ClauseSet;
import org.mandarax.util.DataSource;
import org.mandarax.util.StaticDataSource;

/**
 * Clause set use the java object model to build a fact base.
 * A JClauseSet has a method and an array of parameters.
 * A call to <code>clauses()</code> loops over all combinations of
 * elements in the object collections and invokes the method for any combination
 * of elements. The method is supposed to return a boolean. In case this
 * boolean is true, a fact is built with the method (wrapped by an instance of <code>JPredicate</code>)
 * as predicate and the respective set of objects (wrapped by constant terms). <p>
 * After creating the object a couple of checks is performed, e.g. regarding the arity
 * of the array, whether the return type of the method is a boolean and whether the
 * method is public and therefore accessible. <p>
 * If something is wrong, an exception is thrown. However, we do <i>not</i> check
 * whether all objects in the collections can be casted to the types expected by the method
 * at the respective positions. In this case, the exceptions thrown in <code>invoke()</code>
 * are captured, a message is printed out to <code>System.err()</code> and we coninue without
 * adding the fact.
 * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
 * @version 3.3.1 <9 August 2004>
 * @since 1.0
 */
public final class JClauseSet extends org.mandarax.kernel.meta.AbstractClauseSet implements ClauseSet {

    private DataSource[] argumentSets = null;

    /**
     * Constructor.
     * @param aPredicate a predicate
     * @param argumentSets the collections of objects
     * @throws java.lang.IllegalAccessException Thrown if the method wrapped by the predicate is not public.
     * @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.
     */
    public JClauseSet(JPredicate aPredicate, Collection[] argumentSets)
            throws NullPointerException, IllegalAccessException,
                   IllegalArgumentException {
        this (aPredicate, argumentSets, false);
    }

    /**
     * Constructor.
     * @param aPredicate a predicate
     * @param argumentSets the collections of objects
     * @param isNegated true if the set should be negated, false otherwise
     * @throws java.lang.IllegalAccessException Thrown if the method wrapped by the predicate is not public.
     * @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.
     */
    public JClauseSet(
            JPredicate aPredicate, Collection[] argumentSets, boolean isNegated)
                throws NullPointerException, IllegalAccessException,
                       IllegalArgumentException {
        this (aPredicate, buildDataSources (argumentSets), isNegated);
    }

    /**
     * Constructor.
     * @param aPredicate a predicate
     * @param argumentSets the data sources (kind of dynamic collections)
     * @throws java.lang.IllegalAccessException Thrown if the method wrapped by the predicate is not public.
     * @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.
     */
    public JClauseSet(JPredicate aPredicate, DataSource[] argumentSets)
            throws NullPointerException, IllegalAccessException,
                   IllegalArgumentException {
        this (aPredicate, argumentSets, false);
    }

    /**
     * Constructor.
     * @param aPredicate a predicate
     * @param argumentSets the data sources (kind of dynamic collections)
     * @param isNegated true if the set should be negated, false otherwise
     * @throws java.lang.IllegalAccessException Thrown if the method wrapped by the predicate is not public.
     * @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.
     */
    public JClauseSet(
            JPredicate aPredicate, DataSource[] argumentSets, boolean isNegated)
                throws NullPointerException, IllegalAccessException,
                       IllegalArgumentException {
        super (isNegated);

        initialize (aPredicate, argumentSets);
    }

    /**
     * Build an array of data sources from an array of collections.
     * @return an array of datasources
     * @param argumentSets the collections of objects
     */
    private static DataSource[] buildDataSources(Collection[] coll) {
        DataSource[] ds = new DataSource[coll.length];

        for(int i = 0; i < coll.length; i++) {
            ds[i] = new StaticDataSource (coll[i]);
        }

        return ds;
    }

    /**
     * Build the collection of facts.
     * @return java.util.Collection
     */
    protected Collection buildFacts() {
        Collection data = argumentSets[0].getData ();
        Collection coll = new Vector (argumentSets.length * data.size ());

        for(Iterator it = data.iterator (); it.hasNext (); ) {
            Object[] args = new Object[argumentSets.length - 1];

            buildFacts (coll, it.next (), args, 0);
        }

        return coll;
    }

    /**
     * Recursive method to loop over the parameter sets.
     * @param container the container collecting the facts built
     * @param target the target that receives the method
     * @param args the arguments
     * @param pointer recursion counter
     */
    private void buildFacts(Collection container, Object target,
                            Object[] args, int pointer) {
        if(pointer < args.length) {

            // i.e., we have to go to a new inner loop
            Collection data = argumentSets[pointer + 1].getData ();

            for(Iterator it = data.iterator (); it.hasNext (); ) {
                args[pointer] = it.next ();

                buildFacts (container, target, args, pointer + 1);
            }
        } else {

            // i.e., we are in the most inner loopo and can build the facts
            Clause c = buildFact (target, args);

            if(c != null) {
                container.add (c);
            }
        }
    }

    /**
     * Validate the parameters provided to set up the set.
     * @param aPredicate the predicate
     * @param argSets the data sources
     * @throws java.lang.IllegalAccessException Thrown if the Predicate is not public.
     * @throws java.lang.IllegalArgumentException Thrown if the return type of the Predicate is not boolean or if the number of argument sets does not match.
     */
    private void initialize(JPredicate aPredicate, DataSource[] argSets)
            throws NullPointerException, IllegalAccessException,
                   IllegalArgumentException {
        if(argSets == null) {
            throw new NullPointerException (
                "The argument sets cannot be null");
        }

        initialize (aPredicate);

        argumentSets = argSets;
    }
}
