ic.doc.ipandora.parser.justification.JustificationGenerator.java Source code

Java tutorial

Introduction

Here is the source code for ic.doc.ipandora.parser.justification.JustificationGenerator.java

Source

/*******************************************************************************
 *  Copyright (C) 2014 Simon Apen-Sadler
 *  
 *  This file is part of iPandora.
 *
 *     iPandora 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 3 of the License, or
 *     (at your option) any later version.
 *
 *     iPandora 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.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with iPandora.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/
package ic.doc.ipandora.parser.justification;

import static ic.doc.ipandora.folformula.Conjunction.AND;
import static ic.doc.ipandora.folformula.Disjunction.OR;
import static ic.doc.ipandora.folformula.Exists.EXISTS;
import static ic.doc.ipandora.folformula.False.FALSE;
import static ic.doc.ipandora.folformula.ForAll.FORALL;
import static ic.doc.ipandora.folformula.IfAndOnlyIf.IFF;
import static ic.doc.ipandora.folformula.Implication.IMPLIES;
import static ic.doc.ipandora.folformula.Negation.NOT;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.BailErrorStrategy;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;

import ic.doc.ipandora.justification.AndElimination;
import ic.doc.ipandora.justification.AndIntroduction;
import ic.doc.ipandora.justification.Assumption;
import ic.doc.ipandora.justification.CaseAnalysisJustification;
import ic.doc.ipandora.justification.EqualsSubstitution;
import ic.doc.ipandora.justification.ExcludedMiddle;
import ic.doc.ipandora.justification.ExistsElimination;
import ic.doc.ipandora.justification.ExistsEliminationConst;
import ic.doc.ipandora.justification.ExistsIntroduction;
import ic.doc.ipandora.justification.FalsityElimination;
import ic.doc.ipandora.justification.FalsityIntroduction;
import ic.doc.ipandora.justification.ForAllElimination;
import ic.doc.ipandora.justification.ForAllIntroduction;
import ic.doc.ipandora.justification.ForAllIntroductionConst;
import ic.doc.ipandora.justification.Given;
import ic.doc.ipandora.justification.Guess;
import ic.doc.ipandora.justification.IffElimination;
import ic.doc.ipandora.justification.IffIntroduction;
import ic.doc.ipandora.justification.ImpliesElimination;
import ic.doc.ipandora.justification.ImpliesIntroduction;
import ic.doc.ipandora.justification.Justification;
import ic.doc.ipandora.justification.LineNumbersOnly;
import ic.doc.ipandora.justification.NotElimination;
import ic.doc.ipandora.justification.NotIntroduction;
import ic.doc.ipandora.justification.NotNotElimination;
import ic.doc.ipandora.justification.OrElimination;
import ic.doc.ipandora.justification.OrIntroduction;
import ic.doc.ipandora.justification.Reflexivity;
import ic.doc.ipandora.justification.Repeat;
import ic.doc.ipandora.justification.TruthIntroduction;
import ic.doc.ipandora.proof.MainProof;
import ic.doc.ipandora.proof.SingleLineProof;

public class JustificationGenerator extends JustificationBaseVisitor<Justification> {

    private final MainProof proof;

    private JustificationGenerator(MainProof proof) {
        this.proof = proof;
    }

    public static Justification generateJustification(MainProof proof, String justification) {
        ANTLRInputStream input = new ANTLRInputStream(justification);
        JustificationLexer lexer = new JustificationLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);

        JustificationParser parser = new JustificationParser(tokens);
        parser.setErrorHandler(new BailErrorStrategy());

        ParseTree tree;

        try {
            tree = parser.prog();
        } catch (Exception e) {
            return null;
        }

        return new JustificationGenerator(proof).visit(tree);
    }

    @Override
    public Justification visitGuess(JustificationParser.GuessContext ctx) {
        List<SingleLineProof> steps = new ArrayList<SingleLineProof>();

        for (int i = 1; i <= proof.getNumberOfProofs(); i++) {
            steps.add(proof.getSingleLineProof(i));
        }

        return new Guess(steps, proof);
    }

    @Override
    public Justification visitEqualsSub(JustificationParser.EqualsSubContext ctx) {
        List<SingleLineProof> steps = proof.getSingleLineProofs(ctx.linesWithBrackets().numbers);
        return new EqualsSubstitution(steps);
    }

    @Override
    public Justification visitExcludedMiddle(JustificationParser.ExcludedMiddleContext ctx) {
        return new ExcludedMiddle(Collections.<SingleLineProof>emptyList());
    }

    @Override
    public Justification visitReflexivity(JustificationParser.ReflexivityContext ctx) {
        return new Reflexivity(Collections.<SingleLineProof>emptyList());
    }

    @Override
    public Justification visitJustificationAndLines(JustificationParser.JustificationAndLinesContext ctx) {
        return visit(ctx.justification());
    }

    @Override
    public Justification visitLinesOnly(JustificationParser.LinesOnlyContext ctx) {
        return new LineNumbersOnly(proof.getSingleLineProofs(ctx.listOfLineNumbers().numbers), proof);
    }

    @Override
    public Justification visitForAllIntroConst(JustificationParser.ForAllIntroConstContext ctx) {
        return new ForAllIntroductionConst(Collections.<SingleLineProof>emptyList());
    }

    @Override
    public Justification visitExistsElimConst(JustificationParser.ExistsElimConstContext ctx) {
        List<SingleLineProof> steps = proof.getSingleLineProofs(ctx.linesWithBrackets().numbers);
        return new ExistsEliminationConst(steps);
    }

    @Override
    public Justification visitCaseAnalysis(JustificationParser.CaseAnalysisContext ctx) {
        List<SingleLineProof> steps = proof.getSingleLineProofs(ctx.linesWithBrackets().numbers);
        return new CaseAnalysisJustification(steps);
    }

    @Override
    public Justification visitGiven(JustificationParser.GivenContext ctx) {
        return new Given(Collections.<SingleLineProof>emptyList());
    }

    @Override
    public Justification visitRepeat(JustificationParser.RepeatContext ctx) {
        List<SingleLineProof> steps = proof.getSingleLineProofs(ctx.linesWithBrackets().numbers);
        return new Repeat(steps);
    }

    @Override
    public Justification visitTruthIntroducion(JustificationParser.TruthIntroducionContext ctx) {
        return new TruthIntroduction(Collections.<SingleLineProof>emptyList());
    }

    @Override
    public Justification visitAssumption(JustificationParser.AssumptionContext ctx) {
        return new Assumption(Collections.<SingleLineProof>emptyList());
    }

    @Override
    public Justification visitNotNotElimination(JustificationParser.NotNotEliminationContext ctx) {
        List<String> lineNumbers = ctx.linesWithBrackets().numbers;
        if (lineNumbers == null) {
            return null;
        }
        List<SingleLineProof> steps = proof.getSingleLineProofs(lineNumbers);

        return new NotNotElimination(steps);
    }

    @Override
    public Justification visitOperatorIntroduction(JustificationParser.OperatorIntroductionContext ctx) {
        List<String> lineNumbers = ctx.linesWithBrackets().numbers;

        if (lineNumbers == null) {
            return null;
        }

        List<SingleLineProof> steps = proof.getSingleLineProofs(lineNumbers);

        switch (ctx.operator().getText()) {
        case IMPLIES:
        case "IMPLIES":
            return new ImpliesIntroduction(steps);
        case AND:
        case "AND":
            return new AndIntroduction(steps);
        case OR:
        case "OR":
            return new OrIntroduction(steps);
        case NOT:
        case "NOT":
            return new NotIntroduction(steps);
        case IFF:
        case "IFF":
            return new IffIntroduction(steps);
        case FORALL:
            return new ForAllIntroduction(steps);
        case EXISTS:
            return new ExistsIntroduction(steps);
        case FALSE:
        case "FALSE":
            return new FalsityIntroduction(steps);

        }
        return null;
    }

    @Override
    public Justification visitOperatorElimination(JustificationParser.OperatorEliminationContext ctx) {
        List<String> lineNumbers = ctx.linesWithBrackets().numbers;
        if (lineNumbers == null) {
            return null;
        }

        List<SingleLineProof> steps = proof.getSingleLineProofs(lineNumbers);

        switch (ctx.operator().getText()) {
        case IMPLIES:
        case "IMPLIES":
            return new ImpliesElimination(steps);
        case AND:
        case "AND":
            return new AndElimination(steps);
        case OR:
        case "OR":
            return new OrElimination(steps);
        case NOT:
        case "NOT":
            return new NotElimination(steps);
        case IFF:
        case "IFF":
            return new IffElimination(steps);
        case FORALL:
            return new ForAllElimination(steps);
        case EXISTS:
            return new ExistsElimination(steps);
        case FALSE:
        case "FALSE":
            return new FalsityElimination(steps);

        }
        return null;
    }
}