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


/**
 * Logical negation, logical &quot;not&quot;.
 *
 * @version $Revision: 1.6 $
 * @author  Michael Meyling
 */
public class Negation extends AbstractFormula implements Argument, Formula {

    /** formula to negate */
    private final Formula formula;


    /**
     * Constructs negation of first argument.
     *
     * @param   arguments    formula to negate
     * @throws  ArgumentException    if there is not exactly one
     *          argument that is an instance of {@link Formula}
     */
    public Negation(final Argument[] arguments) throws ArgumentException {
        super(arguments);
        if (arguments.length != 1) {
            throw new ArgumentException(
                BasicConstants.CODE_CONSTRUCTOR_WRONG_ARGUMENT_NUMBER,
                BasicCreator.getName(this.getClass())
                + BasicConstants.OPERATOR_WITH_ONE_ARGUMENT);
        }
        if (!(arguments[0] instanceof Formula)) {
            throw new ArgumentException(
                BasicConstants.CODE_CONSTRUCTOR_WRONG_ARGUMENT_TYPE,
                BasicCreator.getName(this.getClass())
                + BasicConstants.FIRST_ARGUMENT_TYPE
                + BasicConstants.FORMULA,
                arguments[0]);
        }
        this.formula = (Formula) arguments[0];
    }


    /**
     * Constructs negation of formula.
     *
     * @param   formula     formula to negate
     * @throws  ArgumentException   should never occur
     *
     */
    public Negation(final Formula formula)
            throws ArgumentException {
        this(new Argument[] { formula } );

    }


    /**
     * @inherited
     * @return  <code>1</code>, the number of part formulas
     */
    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() {
        return formula.getFreeSubjectVariables();
    };


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


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


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


    /** @inherited */
    public final String toString() {
        return "-" + this.formula.toString();
    }

}
