// $Id: Quantifier.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.ArgumentException;


/**
 * Basis class for Quantifier.
 *
 * @version $Revision: 1.6 $
 * @author  Michael Meyling
 */
public abstract class Quantifier extends AbstractFormula
        implements Argument, Formula {

    /** subject variable of this quantifier */
    private final SubjectVariableMarker variable;

    /** the formula to qantify */
    private final Formula formula;


    /**
     * Constructs a logical quantifier.
     *
     * @param   arguments     subject variable and formula to operate on
     * @throws  ArgumentException    if there are not exactly two
     *          arguments ore the first argument is not an instance of
     *          {@link SubjectVariableMarker} or the second argument
     *          is not an instance of {@link Formula} or
     *          the subject variable is not free in the formula
     */
     public Quantifier(final Argument[] arguments)
            throws ArgumentException {
        super(arguments);
        if (arguments.length != 2) {
            throw new ArgumentException(
                BasicConstants.CODE_CONSTRUCTOR_WRONG_ARGUMENT_NUMBER,
                BasicCreator.getName(this.getClass())
                + BasicConstants.OPERATOR_WITH_TWO_ARGUMENTS);
        }
        if (!(arguments[0] instanceof SubjectVariableMarker)) {
            throw new ArgumentException(
                BasicConstants.CODE_CONSTRUCTOR_WRONG_ARGUMENT_TYPE,
                BasicCreator.getName(this.getClass())
                + BasicConstants.FIRST_ARGUMENT_TYPE
                + BasicConstants.SUBJECT_VARIABLE,
                arguments[0]);
        }
        if (!(arguments[1] instanceof Formula)) {
            throw new ArgumentException(
                BasicConstants.CODE_CONSTRUCTOR_WRONG_ARGUMENT_TYPE,
                BasicCreator.getName(this.getClass())
                + BasicConstants.SECOND_ARGUMENT_TYPE
                + BasicConstants.FORMULA,
                arguments[1]);
        }
        this.variable = (SubjectVariableMarker) arguments[0];
        this.formula = (Formula) arguments[1];
        if (!formula.getFreeSubjectVariables().contains(variable)
                && !formula.containsPatternVariables()) {
            throw new ArgumentException(
                BasicConstants.CODE_CONSTRUCTOR_ARGUMENT_CHECK_FAILED,
                BasicCreator.getName(this.getClass())
                + BasicConstants.QUALIFIED_MUST_BE_FREE);
        }
    }


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


    /** @inherited */
    public final Formula getPartFormula(final int i) {
        if (i == 0) {
            return formula;
        } else {
            throw new IllegalArgumentException(
                BasicConstants.WRONG_FORMULA_NUMBER
                + "{0}");
        }
    }


    /** @inherited */
    public final SubjectVariables getFreeSubjectVariables() {
        SubjectVariables variables
            = new SubjectVariables(formula.getFreeSubjectVariables());
        if (variable instanceof SubjectVariable) {
            variables.remove(variable);
        }
        return variables;
    }


    /** @inherited */
    public final SubjectVariables getBoundSubjectVariables() {
        SubjectVariables variables
            = new SubjectVariables(formula.getBoundSubjectVariables());
        if (variable instanceof SubjectVariable) {
            variables.add(variable);
        }
        return variables;
    }


    /** @inherited */
    public final SubjectVariables getSubjectVariables() {
        SubjectVariables variables = new SubjectVariables(formula.getSubjectVariables());
        if (variable instanceof SubjectVariable) {
            variables.add(variable);
        }
        return variables;
    }


    /**
     * Get the subject variable of this quantifier.
     * @return  subject variable
     */
    public final SubjectVariableMarker getVariable() {
        return variable;
    }



}
