001/* Copyright (C) 2013 TU Dortmund
002 * This file is part of LearnLib, http://www.learnlib.de/.
003 * 
004 * LearnLib is free software; you can redistribute it and/or
005 * modify it under the terms of the GNU Lesser General Public
006 * License version 3.0 as published by the Free Software Foundation.
007 * 
008 * LearnLib is distributed in the hope that it will be useful,
009 * but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
011 * Lesser General Public License for more details.
012 * 
013 * You should have received a copy of the GNU Lesser General Public
014 * License along with LearnLib; if not, see
015 * <http://www.gnu.de/documents/lgpl.en.html>.
016 */
017package de.learnlib.algorithms.baselinelstar;
018
019import net.automatalib.words.Word;
020
021import java.util.Collection;
022
023/**
024 * With this class the contents of an {@link ObservationTable} can be
025 * printed in a human-readable format.
026 */
027public class ObservationTablePrinter {
028
029        public static <I> String getPrintableStringRepresentation(ObservationTable<I> observationTable) {
030                StringBuilder sb = new StringBuilder();
031
032                int firstColumnWidth = getFirstColumnWidth(observationTable);
033                int maxSuffixLength = getMaxWordLength(observationTable.getSuffixes()) + 3;
034
035                Word<I> emptyWord = Word.epsilon();
036                sb.append(paddedString(emptyWord.toString(), firstColumnWidth));
037                sb.append(" | ");
038                for (Word<I> suffix : observationTable.getSuffixes()) {
039                        sb.append(paddedString(suffix.toString(), maxSuffixLength));
040                }
041
042                sb.append("\n\n");
043
044                for (Word<I> state : observationTable.getStates()) {
045                        sb.append(paddedString(state.toString(), firstColumnWidth)).append(" | ");
046                        sb.append(stringPresentationOfRow(observationTable.getRowForPrefix(state), maxSuffixLength));
047                        sb.append("\n");
048                }
049                sb.append("\n");
050
051                for (Word<I> candidate : observationTable.getCandidates()) {
052                        sb.append(paddedString(candidate.toString(), firstColumnWidth)).append(" | ");
053                        sb.append(stringPresentationOfRow(observationTable.getRowForPrefix(candidate), maxSuffixLength));
054                        sb.append("\n");
055                }
056
057                return sb.toString();
058        }
059
060        private static <S> int getFirstColumnWidth(ObservationTable<S> observationTable) {
061                int maxStateLength = getMaxWordLength(observationTable.getStates());
062                int maxCandidateLength = getMaxWordLength(observationTable.getCandidates());
063                return Math.max(maxStateLength, maxCandidateLength);
064        }
065
066        private static <S> int getMaxWordLength(Collection<Word<S>> words) {
067                int length = 0;
068
069                for (Word<S> word : words) {
070                        int wordLength = word.toString().length();
071                        if (wordLength > length) {
072                                length = wordLength;
073                        }
074                }
075
076                return length;
077        }
078
079        private static String stringPresentationOfRow(ObservationTableRow row, int length) {
080                StringBuilder sb = new StringBuilder();
081                for (Boolean value : row.getValues()) {
082                        if (value) {
083                                sb.append(paddedString("1", length));
084                        }
085                        else {
086                                sb.append(paddedString("0", length));
087                        }
088                }
089                return sb.toString();
090        }
091
092        private static String paddedString(String string, int length) {
093                StringBuilder sb = new StringBuilder(length);
094                sb.append(string);
095                for (int i = string.length(); i < length; i++) {
096                        sb.append(" ");
097                }
098                return sb.toString();
099        }
100
101}