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 * @author Tomáš Müller 041 * @version IFS 1.3 (Iterative Forward Search)<br> 042 * Copyright (C) 2006 - 2014 Tomáš Müller<br> 043 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 044 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 045 * <br> 046 * This library is free software; you can redistribute it and/or modify 047 * it under the terms of the GNU Lesser General Public License as 048 * published by the Free Software Foundation; either version 3 of the 049 * License, or (at your option) any later version. <br> 050 * <br> 051 * This library is distributed in the hope that it will be useful, but 052 * WITHOUT ANY WARRANTY; without even the implied warranty of 053 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 054 * Lesser General Public License for more details. <br> 055 * <br> 056 * You should have received a copy of the GNU Lesser General Public 057 * License along with this library; if not see 058 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 059 */ 060public class ProgressWriter implements ProgressListener { 061 private PrintStream iTextOut = null; 062 private static int TEXT_LENGTH = 28; 063 private static int DOTS_LENGTH = 48; 064 private int iPrintedDots = -1; 065 066 public ProgressWriter(PrintStream out) { 067 iTextOut = out; 068 } 069 070 @Override 071 public void statusChanged(String status) { 072 // iTextOut.println("Status: "+status); 073 } 074 075 @Override 076 public void phaseChanged(String phase) { 077 if (iPrintedDots > 0) { 078 while (iPrintedDots < DOTS_LENGTH) { 079 iTextOut.print("."); 080 iPrintedDots++; 081 } 082 } 083 iTextOut.println(); 084 iTextOut.print(expand(phase, TEXT_LENGTH, ' ', false) + ": "); 085 iPrintedDots = 0; 086 iTextOut.flush(); 087 } 088 089 @Override 090 public void progressChanged(long currentProgress, long maxProgress) { 091 int dotsToPrint = (maxProgress == 0 ? 0 : (int) ((DOTS_LENGTH * currentProgress) / maxProgress)); 092 while (iPrintedDots < dotsToPrint) { 093 iTextOut.print("."); 094 iPrintedDots++; 095 } 096 iTextOut.flush(); 097 } 098 099 @Override 100 public void progressSaved() { 101 } 102 103 @Override 104 public void progressRestored() { 105 } 106 107 @Override 108 public void progressMessagePrinted(Progress.Message msg) { 109 } 110 111 private static String expand(String source, int length, char ch, boolean beg) { 112 StringBuffer sb = new StringBuffer(source == null ? "" : source.length() > length ? (beg ? source 113 .substring(source.length() - length) : source.substring(0, length)) : source); 114 while (sb.length() < length) { 115 if (beg) 116 sb.insert(0, ch); 117 else 118 sb.append(ch); 119 } 120 return sb.toString(); 121 } 122}