// $Id: ConjunctionRule.java,v 1.10 2004/02/27 16:29:56 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.rule;


import com.meyling.principia.argument.AbstractArgumentList;
import com.meyling.principia.argument.Argument;
import com.meyling.principia.argument.ArgumentException;
import com.meyling.principia.argument.Counter;
import com.meyling.principia.argument.Text;
import com.meyling.principia.logic.basic.Conjunction;
import com.meyling.principia.logic.basic.Formula;
import com.meyling.principia.logic.paragraph.ParagraphCreator;
import com.meyling.principia.logic.paragraph.ProofLineList;
import com.meyling.principia.logic.paragraph.RuleDeclaration;
import com.meyling.principia.module.Module;
import com.meyling.principia.module.Version;


/**
 * Implemention of conjunction rule:
 * <p>
 * <code>   A1    </code><p>
 * <code>   A2    </code><p>
 * <code>---------</code><p>
 * <code> A1 & A2 </code><p>
 *
 * @version $Revision: 1.10 $
 * @author  Michael Meyling
 */
public class ConjunctionRule extends AbstractArgumentList
        implements Argument, Rule {

    /** minimum version required for using this rule */
    private final static Version version;

    /** proof line of <code>A1</code>*/
    private final int n;

    /** proof line of <code>A2</code>*/
    private final int m;


    /**
     * Initialize version information.
     */
    static {
        try {
            version = new Version(
                    new Argument[] {new Text("1.02.01")});
        } catch (ArgumentException e) {
            throw new IllegalArgumentException(e.toString());
        }
    }


    /**
     * Constructs a conjunction rule.
     *
     * @param   arguments    two line numbers to operate on
     *                       both arguments could reference any formula
     *                       but the conjunction of both formulas
     *                       must be a formula too
     * @throws  ArgumentException    if there are not exactly two
     *          arguments that are instances of {@link Counter}
     *          or these arguments are not bigger than zero
     *
     */
    public ConjunctionRule(final Argument[] arguments)
            throws ArgumentException {
        super (arguments);
        if (arguments.length != 2) {
            throw new ArgumentException(
                RuleConstants.CODE_CONSTRUCTOR_WRONG_ARGUMENT_NUMBER,
                RuleCreator.getName(this.getClass())
                + RuleConstants.OPERATOR_WITH_TWO_ARGUMENTS);
        }
        if (!(arguments[0] instanceof Counter)) {
            throw new ArgumentException(
                RuleConstants.CODE_CONSTRUCTOR_WRONG_ARGUMENT_TYPE,
                RuleCreator.getName(this.getClass())
                + RuleConstants.FIRST_ARGUMENT_TYPE
                + RuleConstants.NUMBER,
                arguments[0]);
        }
        this.n = ((Counter) arguments[0]).getNumber() - 1;
        if (n < 0) {
            throw new ArgumentException(
                RuleConstants.CODE_CONSTRUCTOR_WRONG_ARGUMENT_TYPE,
                RuleCreator.getName(this.getClass())
                + RuleConstants.FIRST_ARGUMENT_TYPE
                + RuleConstants.POSITIVE_INTEGER_EXPECTED,
                arguments[0]);
        }
        if (!(arguments[1] instanceof Counter)) {
            throw new ArgumentException(
                RuleConstants.CODE_METHOD_ARGUMENT_CHECK_FAILED,
                RuleCreator.getName(this.getClass())
                + RuleConstants.SECOND_ARGUMENT_TYPE
                + RuleConstants.NUMBER,
                arguments[1]);
        }
        this.m = ((Counter) arguments[1]).getNumber() - 1;
        if (m < 0) {
            throw new ArgumentException(
                RuleConstants.CODE_CONSTRUCTOR_WRONG_ARGUMENT_TYPE,
                RuleCreator.getName(this.getClass())
                + RuleConstants.SECOND_ARGUMENT_TYPE
                + RuleConstants.POSITIVE_INTEGER_EXPECTED,
                arguments[1]);
        }

    }


    /**
     * Check if proof could be extended with formula because of
     * conjunction rule.
     *
     * @param   module   module
     * @param   proofLines  proof lines
     * @param   position number of proof lines that could be used
     *                    in the rule
     * @param   formula  formula to add
     * @throws  ArgumentException    if addition is not possible
     *          because the proof line numbers are not allowed (e.g.
     *          between 1 and the minimum of <code>position</code>
     *          and current maximum proof line number)
     *          or the second proof line is no {@link Implication}
     *          or the first part formula of the second proof line
     *          is not equal to the first proof line
     *          or the second part formula of the second proof line
     *          is not equal to
     *          <code>formula</code>
     *          or a version conflict occurs
     */
    public final void check(final Module module,
            final ProofLineList proofLines,
            final int position,
            final Formula formula) throws ArgumentException {

        if (version.greaterThan(module.getRuleVersion())) {
            throw new ArgumentException(
                RuleConstants.CODE_METHOD_ARGUMENT_CHECK_FAILED,
                RuleCreator.getName(this.getClass())
                    + " " + RuleConstants.RULE_VERSION_CONFLICT
                    + version.getText(),
                this);
        }
        if (n < 0 || n >= proofLines.getArgumentSize() || n >= position) {
            throw new ArgumentException(
                RuleConstants.CODE_METHOD_ARGUMENT_CHECK_FAILED,
                RuleCreator.getName(this.getClass())
                    + RuleConstants.PROOF_LINES_OUT_OF_RANGE,
                this.getArgument(0));
        }
        if (m < 0 || m >= proofLines.getArgumentSize() || m >= position) {
            throw new ArgumentException(
                RuleConstants.CODE_METHOD_ARGUMENT_CHECK_FAILED,
                RuleCreator.getName(this.getClass())
                    + RuleConstants.PROOF_LINES_OUT_OF_RANGE,
                this.getArgument(1));
        }

        final Formula formula1 = (Formula) proofLines.getArgument(n)
            .getArgument(0);
        final Formula formula2 = (Formula) proofLines.getArgument(m)
            .getArgument(0);
        final Conjunction result;
        try {
            result = new Conjunction(
                new Argument[] {formula1, formula2});
        } catch (ArgumentException e) {     // e.g. if variable
            throw new ArgumentException(    // is double bound
                RuleConstants.CODE_METHOD_ARGUMENT_CHECK_FAILED,
                RuleCreator.getName(this.getClass())
                + RuleConstants.CONJUNCTION_RULE_NOT_POSSIBLE,
                this,
                proofLines.getArgument(n).getArgument(0));
        }

        if (!result.equals(formula)) {
            throw new ArgumentException(
                RuleConstants.CODE_METHOD_ARGUMENT_CHECK_FAILED,
                RuleCreator.getName(this.getClass())
                    + RuleConstants.RESULT_NOT_EQUAL_WITH_EXPECTED,
                this);
        }
    }


    /** @inherited */
    public ProofLineList extendWithout(final Module module,
            final ProofLineList proofLines, final int position)
            throws UnsupportedOperationException {
        throw new UnsupportedOperationException (
            "this rule is neccessary: "
            + RuleCreator.getName(this.getClass()));
    }


    /** @inherited */
    public int[] getProofLines() {
        return new int[] {this.n, this.m};
    }


    /** @inherited */
    public Rule changeProofLines(final int[] mapping) {
        try {
            return (Rule) create(new Argument[] {
                new Counter(mapping[n] + 1), new Counter(mapping[m] + 1)});
        } catch (ArgumentException e) {
            throw new IllegalArgumentException(e.toString());
        } catch (IndexOutOfBoundsException e) {
            throw new IllegalArgumentException(e.toString());
        }
    }


    /**
     * Check this rule could be declared.
     *
     * @param   module      the module context
     * @param   declaration the declaration that shall declare this rule
     * @throws  IllegalArgumentException    if an programming error occured
     * @throws  ArgumentException    if declaration failed
     */
    public final static void checkDeclaration(final Module module,
        final RuleDeclaration declaration) throws ArgumentException {
        if (declaration.getArgumentSize() > 2) {
            throw new ArgumentException(
                RuleConstants.CODE_METHOD_ARGUMENT_CHECK_FAILED,
                ParagraphCreator.getName(RuleDeclaration.class)
                    + RuleConstants.OPERATOR_WITH_TWO_ARGUMENTS,
                declaration);
        }
    }


    /** @inherited */
    public final Version getVersion() {
        return ConjunctionRule.version;
    }


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


    /** @inherited */
    public final String toString() {
        return "Conjunction of " + getArgument(0)
        + " "  + getArgument(1);
    }


}
