// $Id: BasicFormulaPatternVariable.java,v 1.6 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 com.meyling.principia.argument.Argument;
import com.meyling.principia.argument.ArgumentConstants;
import com.meyling.principia.argument.ArgumentException;
import com.meyling.principia.argument.Counter;
import com.meyling.principia.argument.Enumerator;
import com.meyling.principia.argument.PatternVariables;


/**
 * Pattern variable, for matching purposes.
 *
 * @version $Revision: 1.6 $
 * @author  Michael Meyling
 */
public class BasicFormulaPatternVariable extends BasicPatternVariable
        implements Formula {

    /** number of pattern variable */
    private final Counter number;

    /** assignment of pattern variable */
    private Argument argument = null;


    /**
     * Constructs a pattern variable.
     *
     * @param   arguments  first argument is number of this pattern
     *          variable
     * @throws  ArgumentException    if there is not exactly one
     *          argument that is an instance of {@link Counter}
     */
    public BasicFormulaPatternVariable(final Argument[] arguments)
            throws ArgumentException {
        super(arguments);
        if (arguments.length != 1) {
            if (arguments.length == 0) {
                throw new ArgumentException(
                    ArgumentConstants.CODE_CONSTRUCTOR_WRONG_ARGUMENT_NUMBER,
                    ArgumentConstants.PATTERN_VARIABLE
                    + ArgumentConstants.OPERATOR_WITH_ONE_ARGUMENT);
            } else {
                throw new ArgumentException(
                    ArgumentConstants.CODE_CONSTRUCTOR_WRONG_ARGUMENT_NUMBER,
                    ArgumentConstants.PATTERN_VARIABLE
                    + ArgumentConstants.OPERATOR_WITH_ONE_ARGUMENT,
                    arguments[arguments.length - 1],
                    false);
            }
        }
        if (!(arguments[0] instanceof Counter)) {
            throw new ArgumentException(
                ArgumentConstants.CODE_CONSTRUCTOR_WRONG_ARGUMENT_TYPE,
                ArgumentConstants.PATTERN_VARIABLE
                + ArgumentConstants.FIRST_ARGUMENT_TYPE
                + ArgumentConstants.COUNTER,
                arguments[0]);
        }
        this.number = (Counter) arguments[0];


    }


    /**
     * @inherited
     * @return always 0
     */
    public final int getPartFormulaSize() {
        return 0;
    }


    /** @inherited */
    public final Formula getPartFormula(int i)
            throws IllegalArgumentException {
        throw new IllegalArgumentException(
            BasicConstants.NO_FORMULA_NUMBER);
    }


    /** @inherited */
    public final SubjectVariables getFreeSubjectVariables() {
        return new SubjectVariables();
    };


    /** @inherited */
    public final SubjectVariables getBoundSubjectVariables() {
        return new SubjectVariables();
    }


    /** @inherited */
    public final SubjectVariables getSubjectVariables() {
        return new SubjectVariables();
    }


    /** @inherited */
    public final PatternVariables getPatternVariables() {
        BasicPatternVariables variables = new BasicPatternVariables();
        variables.add(this);
        return variables;
    }


    /**
     * Is the argument matching the pattern variable?
     * Here always true is returned.
     *
     * @param   argument    argument to match pattern variable
     * @return  <code>true</code>
     */
    public boolean isMatching(final Argument argument) {
        if (argument == null) {
            return false;
        }
        return argument instanceof Formula;
    }


    /** @inherited */
    public final Formula replaceBoundSubjectVariable(
            final Enumerator counter,
            final int occurance,
            final SubjectVariable search,
            final SubjectVariable replacement) {
        // nothing to do
        return this;
    }


    /** @inherited */
    public final Formula replaceBoundSubjectVariables(
            final Enumerator counter) {
        // nothing to do
        return this;
    }


    /** @inherited */
    public final Argument create(final Argument[] arguments)
            throws ArgumentException {
        return new BasicFormulaPatternVariable(arguments);
    }

    /** @inherited */
    public String toString() {
        return "@bf" + number.toString();
    }


}
