// $Id: BasicPatternVariables.java,v 1.8 2004/02/27 16:30:00 m31 Exp $
//
// This file is part of the program suite "Principia Mathematica II"
// which is a working prototype for the main project:
// "Hilbert II" - http://www.qedeq.org
//
// Copyright 2001-2004  Michael Meyling <mime@qedeq.org>.
//
// "Principia Mathematica II" is free software; you can redistribute
// it and/or modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program 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 General Public License for more details.

package com.meyling.principia.logic.basic;


import java.util.Iterator;
import java.util.Map;

import com.meyling.principia.argument.Argument;
import com.meyling.principia.argument.ArgumentException;
import com.meyling.principia.argument.Counter;
import com.meyling.principia.argument.PatternVariable;
import com.meyling.principia.argument.PatternVariables;
import com.meyling.principia.argument.VariableList;


/**
 * Set of pattern variables.
 *
 * @version $Revision: 1.8 $
 * @author  Michael Meyling
 */
public class BasicPatternVariables extends PatternVariables{


    /**
     * Construct an empty set of pattern variables.
     */
    public BasicPatternVariables() {
        super();
    }


    /**
     * Construct an set of pattern variables by an PatternVariables object.
     *
     * @param var   PatternVariables to copy from
     */
    public BasicPatternVariables(final PatternVariables var) {
        super(var);
    }


    /**
     * Fill contents with subject variables, starting with
     * given number + 1.
     *
     * @param   greater   the created subject variables will
     *                    start with <code>greater + 1</code>
     * @throws  IllegalArgumentException    if there is already
     *           a value assigned in any pattern variable
     *           or if creation of Counter failed
     */
    public final void fillWithSubjectVariables(final int greater)
            throws IllegalArgumentException {
        try {
            int i = 1;
            for (Iterator iterator = getMap().entrySet().iterator();
                    iterator.hasNext(); ) {
                final Map.Entry entry = (Map.Entry) iterator.next();
                ((PatternVariable) entry.getValue()).setContent(
                    new SubjectVariable(new Argument[] {
                        new Counter(greater + i++)}));
            }
        } catch (ArgumentException e) {     // TODO: really convert???
            throw new IllegalArgumentException(e.getMessage());
        }
    }


    /**
     * Get highest number of each entry.
     *
     * @return  maxiumum of number value for all entries,
     *          if getSize() == 0 it returns -1
     */
    public final int getHighestNumber() {
        int maximum = -1;   // minimum number value is 0
        for (Iterator iterator = getMap().entrySet().iterator();
                iterator.hasNext(); ) {
            Map.Entry entry = (Map.Entry) iterator.next();
            final int number = ((Counter) ((Argument) entry.getValue())
                .getArgument(0)).getNumber();
            if (maximum < number) {
                maximum = number;
            }
        }
        return maximum;
    }


    /**
     * Get VariableList of all variables, the list is orderd by type and
     * number of the pattern variables.
     *
     * @throws  IllegalArgumentException if there is a programming error.
     */
    public final VariableList getVariables()
            throws IllegalArgumentException {
        try {
            return new VariableList(
                (Argument[]) getMap().values().toArray(new Argument[0]));
        } catch (ArrayStoreException e) {     // TODO: really convert???
            throw new IllegalArgumentException(e.getMessage());
        }
    }



}
