001    package aima.logic.fol.inference.proof;
002    
003    import java.util.List;
004    
005    /**
006     * @author Ciaran O'Reilly
007     * 
008     */
009    public class ProofPrinter {
010    
011            /**
012             * Utility method for outputting proofs in a formatted textual
013             * representation.
014             * 
015             * @param aProof
016             * @return a String representation of the Proof.
017             */
018            public static String printProof(Proof aProof) {
019                    StringBuilder sb = new StringBuilder();
020    
021                    sb.append("Proof, Answer Bindings: ");
022                    sb.append(aProof.getAnswerBindings());
023                    sb.append("\n");
024    
025                    List<ProofStep> steps = aProof.getSteps();
026                    
027                    int maxStepWidth = "Step".length();
028                    int maxProofWidth = "Proof".length();
029                    int maxJustificationWidth = "Justification".length();
030                    
031                    // Calculate the maximum width for each column in the proof
032                    for (ProofStep step : steps) {
033                            String sn = "" + step.getStepNumber();
034                            if (sn.length() > maxStepWidth) {
035                                    maxStepWidth = sn.length();
036                            }
037                            if (step.getProof().length() > maxProofWidth) {
038                                    maxProofWidth = step.getProof().length();
039                            }
040                            if (step.getJustification().length() > maxJustificationWidth) {
041                                    maxJustificationWidth = step.getJustification().length();
042                            }
043                    }
044    
045                    // Give a little extra padding
046                    maxStepWidth += 1;
047                    maxProofWidth += 1;
048                    maxJustificationWidth += 1;
049                    
050                    String f = "|%-" + maxStepWidth + "s| %-" + maxProofWidth + "s|%-"
051                                    + maxJustificationWidth + "s|\n";
052                    
053                    int barWidth = 5 + maxStepWidth + maxProofWidth + maxJustificationWidth;
054                    StringBuilder bar = new StringBuilder();
055                    for (int i = 0; i < barWidth; i++) {
056                            bar.append("-");
057                    }
058                    bar.append("\n");
059    
060                    sb.append(bar);
061                    sb.append(String.format(f, "Step", "Proof", "Justification"));
062                    sb.append(bar);
063                    for (ProofStep step : steps) {
064                            sb.append(String.format(f, "" + step.getStepNumber(), step
065                                            .getProof(),
066                                            step.getJustification()));
067                    }
068                    sb.append(bar);
069    
070                    return sb.toString();
071            }
072    }