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