    001package org.cpsolver.ifs.util;
    002
    003import java.io.PrintStream;
    004
    005/**
    006 * Prints current progres to {@link PrintStream}. <br>
    007 * <br>
    008 * Example usage:
    009 * <pre>
    010 * <code>
    011 * Progress.getInstance().addProgressListener(new ProgressWriter(System.out));<br>
    012 * </code>
    013 * </pre>
    014 * <br>
    015 * Example output:
    016 * <pre>
    017 * <code>
    018 * Reading course.pl ...       :
    019 * Reading altcourse.pl ...    :
    020 * Reading room.pl ...         :
    021 * Creating rooms ...          : ................................................
    022 * Creating variables ...      : ................................................
    023 * Reading students.pl ...     :
    024 * Reading jenr.pl ...         :
    025 * Creating jenrl constraints .: ................................................
    026 * Reading add.pl ...          :
    027 * Creating group constraints .: ................................................
    028 * Creating initial assignment : ................................................
    029 * Creating dept. spread constr: ................................................
    030 * Input data loaded           : ................................................
    031 * Initializing solver         :
    032 * Searching for initial soluti: ................................................
    033 * Improving found solution ...: ................................................
    034 * Improving found solution ...: ................................................
    035 * Improving found solution ...: ...................................
    036 * </code>
    037 * </pre>
    038 *
    039 *
    040 * @version IFS 1.3 (Iterative Forward Search)<br>
    041 *          Copyright (C) 2006 - 2014 Tomáš Müller<br>
    042 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    043 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    044 * <br>
    045 *          This library is free software; you can redistribute it and/or modify
    046 *          it under the terms of the GNU Lesser General Public License as
    047 *          published by the Free Software Foundation; either version 3 of the
    048 *          License, or (at your option) any later version. <br>
    049 * <br>
    050 *          This library is distributed in the hope that it will be useful, but
    051 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    052 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    053 *          Lesser General Public License for more details. <br>
    054 * <br>
    055 *          You should have received a copy of the GNU Lesser General Public
    056 *          License along with this library; if not see
    057 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    058 */
    059public class ProgressWriter implements ProgressListener {
    060    private PrintStream iTextOut = null;
    061    private static int TEXT_LENGTH = 28;
    062    private static int DOTS_LENGTH = 48;
    063    private int iPrintedDots = -1;
    064
    065    public ProgressWriter(PrintStream out) {
    066        iTextOut = out;
    067    }
    068
    069    @Override
    070    public void statusChanged(String status) {
    071        // iTextOut.println("Status: "+status);
    072    }
    073
    074    @Override
    075    public void phaseChanged(String phase) {
    076        if (iPrintedDots > 0) {
    077            while (iPrintedDots < DOTS_LENGTH) {
    078                iTextOut.print(".");
    079                iPrintedDots++;
    080            }
    081        }
    082        iTextOut.println();
    083        iTextOut.print(expand(phase, TEXT_LENGTH, ' ', false) + ": ");
    084        iPrintedDots = 0;
    085        iTextOut.flush();
    086    }
    087
    088    @Override
    089    public void progressChanged(long currentProgress, long maxProgress) {
    090        int dotsToPrint = (maxProgress == 0 ? 0 : (int) ((DOTS_LENGTH * currentProgress) / maxProgress));
    091        while (iPrintedDots < dotsToPrint) {
    092            iTextOut.print(".");
    093            iPrintedDots++;
    094        }
    095        iTextOut.flush();
    096    }
    097
    098    @Override
    099    public void progressSaved() {
    100    }
    101
    102    @Override
    103    public void progressRestored() {
    104    }
    105
    106    @Override
    107    public void progressMessagePrinted(Progress.Message msg) {
    108    }
    109
    110    private static String expand(String source, int length, char ch, boolean beg) {
    111        StringBuffer sb = new StringBuffer(source == null ? "" : source.length() > length ? (beg ? source
    112                .substring(source.length() - length) : source.substring(0, length)) : source);
    113        while (sb.length() < length) {
    114            if (beg)
    115                sb.insert(0, ch);
    116            else
    117                sb.append(ch);
    118        }
    119        return sb.toString();
    120    }
    121}