001 package net.sf.cpsolver.coursett; 002 003 import org.apache.log4j.Logger; 004 005 006 import net.sf.cpsolver.coursett.model.TimetableModel; 007 import net.sf.cpsolver.ifs.solution.Solution; 008 import net.sf.cpsolver.ifs.solver.Solver; 009 import net.sf.cpsolver.ifs.util.Callback; 010 011 /** 012 * Abstract timetable saver class. 013 * 014 * @version 015 * CourseTT 1.1 (University Course Timetabling)<br> 016 * Copyright (C) 2006 Tomáš Müller<br> 017 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 018 * Lazenska 391, 76314 Zlin, Czech Republic<br> 019 * <br> 020 * This library is free software; you can redistribute it and/or 021 * modify it under the terms of the GNU Lesser General Public 022 * License as published by the Free Software Foundation; either 023 * version 2.1 of the License, or (at your option) any later version. 024 * <br><br> 025 * This library is distributed in the hope that it will be useful, 026 * but WITHOUT ANY WARRANTY; without even the implied warranty of 027 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 028 * Lesser General Public License for more details. 029 * <br><br> 030 * You should have received a copy of the GNU Lesser General Public 031 * License along with this library; if not, write to the Free Software 032 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 033 */ 034 035 public abstract class TimetableSaver implements Runnable { 036 private Solver iSolver = null; 037 private Callback iCallback = null; 038 /** Constructor 039 */ 040 public TimetableSaver(Solver solver) { 041 iSolver = solver; 042 } 043 /** Solver */ 044 public Solver getSolver() { return iSolver; } 045 /** Solution to be saved */ 046 protected Solution getSolution() { return iSolver.currentSolution(); } 047 /** Model of the solution */ 048 protected TimetableModel getModel() { return (TimetableModel)iSolver.currentSolution().getModel(); } 049 /** Save the solution*/ 050 public abstract void save() throws Exception; 051 /** Sets callback class 052 * @param callback method {@link Callback#execute()} is executed when save is done 053 */ 054 public void setCallback(Callback callback) { iCallback = callback; } 055 056 public void run() { 057 try { 058 save(); 059 } catch (Exception e) { 060 Logger.getLogger(this.getClass()).error(e.getMessage(),e); 061 } finally { 062 if (iCallback!=null) 063 iCallback.execute(); 064 } 065 } 066 }