001package org.cpsolver.ifs.util;
002
003import org.apache.log4j.Logger;
004import org.cpsolver.ifs.assignment.Assignment;
005import org.cpsolver.ifs.model.Model;
006import org.cpsolver.ifs.model.Value;
007import org.cpsolver.ifs.model.Variable;
008
009/**
010 * Abstract problem loader class.
011 * 
012 * @version IFS 1.3 (Iterative Forward Search)<br>
013 *          Copyright (C) 2006 - 2016 Tomas Muller<br>
014 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
015 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
016 * <br>
017 *          This library is free software; you can redistribute it and/or modify
018 *          it under the terms of the GNU Lesser General Public License as
019 *          published by the Free Software Foundation; either version 3 of the
020 *          License, or (at your option) any later version. <br>
021 * <br>
022 *          This library is distributed in the hope that it will be useful, but
023 *          WITHOUT ANY WARRANTY; without even the implied warranty of
024 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
025 *          Lesser General Public License for more details. <br>
026 * <br>
027 *          You should have received a copy of the GNU Lesser General Public
028 *          License along with this library; if not see
029 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
030 */
031public abstract class ProblemLoader<V extends Variable<V, T>, T extends Value<V, T>, M extends Model<V,T>> implements Runnable {
032    private M iModel = null;
033    private Assignment<V, T> iAssignment = null;
034    private Callback iCallback = null;
035
036    /**
037     * Constructor
038     * 
039     * @param model
040     *            an empty instance of timetable model
041     * @param assignment current assignment
042     */
043    public ProblemLoader(M model, Assignment<V, T> assignment) {
044        iModel = model;
045        iAssignment = assignment;
046    }
047
048    /**
049     * Returns provided model.
050     * 
051     * @return provided model
052     */
053    public M getModel() {
054        return iModel;
055    }
056    
057    /**
058     * Returns provided assignment
059     * @return provided assignment
060     */
061    public Assignment<V, T> getAssignment() {
062        return iAssignment;
063    }
064
065    /**
066     * Load the model.
067     * @throws Exception thrown when the load fails
068     */
069    public abstract void load() throws Exception;
070
071    /**
072     * Sets callback class
073     * 
074     * @param callback
075     *            method {@link Callback#execute()} is executed when load is
076     *            done
077     */
078    public void setCallback(Callback callback) {
079        iCallback = callback;
080    }
081
082    @Override
083    public void run() {
084        try {
085            load();
086        } catch (Exception e) {
087            Logger.getLogger(this.getClass()).error(e.getMessage(), e);
088        } finally {
089            if (iCallback != null)
090                iCallback.execute();
091        }
092    }
093
094}