001package org.cpsolver.ifs.extension; 002 003import org.cpsolver.ifs.assignment.Assignment; 004import org.cpsolver.ifs.model.Constraint; 005import org.cpsolver.ifs.model.Model; 006import org.cpsolver.ifs.model.ModelListener; 007import org.cpsolver.ifs.model.Value; 008import org.cpsolver.ifs.model.Variable; 009import org.cpsolver.ifs.solver.Solver; 010import org.cpsolver.ifs.util.DataProperties; 011 012/** 013 * Generic extension of IFS solver. <br> 014 * <br> 015 * All extensions should extend this class. <br> 016 * 017 * @version IFS 1.3 (Iterative Forward Search)<br> 018 * Copyright (C) 2006 - 2014 Tomáš Müller<br> 019 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 020 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 021 * <br> 022 * This library is free software; you can redistribute it and/or modify 023 * it under the terms of the GNU Lesser General Public License as 024 * published by the Free Software Foundation; either version 3 of the 025 * License, or (at your option) any later version. <br> 026 * <br> 027 * This library is distributed in the hope that it will be useful, but 028 * WITHOUT ANY WARRANTY; without even the implied warranty of 029 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 030 * Lesser General Public License for more details. <br> 031 * <br> 032 * You should have received a copy of the GNU Lesser General Public 033 * License along with this library; if not see 034 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 035 * @param <V> Variable 036 * @param <T> Value 037 */ 038public class Extension<V extends Variable<V, T>, T extends Value<V, T>> implements ModelListener<V, T> { 039 private Model<V, T> iModel = null; 040 private Solver<V, T> iSolver = null; 041 private DataProperties iProperties = null; 042 043 /** 044 * Constructor 045 * 046 * @param solver 047 * IFS solver 048 * @param properties 049 * input configuration 050 */ 051 public Extension(Solver<V, T> solver, DataProperties properties) { 052 iSolver = solver; 053 iProperties = properties; 054 } 055 056 /** Registration of a model. This is called by the solver before start. 057 * @param model problem model 058 **/ 059 public void register(Model<V, T> model) { 060 iModel = model; 061 iModel.addModelListener(this); 062 } 063 064 /** 065 * Unregistration of a model. This is called by the solver when extension is 066 * removed. 067 * @param model problem model 068 */ 069 public void unregister(Model<V, T> model) { 070 iModel.removeModelListener(this); 071 iModel = null; 072 } 073 074 /** 075 * Returns true if there is a model registered to this extension, i.e., when 076 * extension is registered. 077 * @return true if registered with a solver 078 */ 079 public boolean isRegistered() { 080 return iModel != null; 081 } 082 083 /** Returns the model 084 * @return problem model 085 **/ 086 public Model<V, T> getModel() { 087 return iModel; 088 } 089 090 /** Returns the solver 091 * @return current solver 092 **/ 093 public Solver<V, T> getSolver() { 094 return iSolver; 095 } 096 097 /** Returns input configuration 098 * @return solver configuration 099 **/ 100 public DataProperties getProperties() { 101 return iProperties; 102 } 103 104 /** Called after a value is assigned to a variable */ 105 @Override 106 public void afterAssigned(Assignment<V, T> assignment, long iteration, T value) { 107 } 108 109 /** Called after a value is unassigned from a variable */ 110 @Override 111 public void afterUnassigned(Assignment<V, T> assignment, long iteration, T value) { 112 } 113 114 /** Called before a value is assigned to a variable */ 115 @Override 116 public void beforeAssigned(Assignment<V, T> assignment, long iteration, T value) { 117 } 118 119 /** Called after a value is unassigned from a variable */ 120 @Override 121 public void beforeUnassigned(Assignment<V, T> assignment, long iteration, T value) { 122 } 123 124 /** Called when a constraint is added to the model */ 125 @Override 126 public void constraintAdded(Constraint<V, T> constraint) { 127 } 128 129 /** Called when a constraint is removed from the model */ 130 @Override 131 public void constraintRemoved(Constraint<V, T> constraint) { 132 } 133 134 /** Called when a variable is added to the model */ 135 @Override 136 public void variableAdded(V variable) { 137 } 138 139 /** Called when a variable is removed from the model */ 140 @Override 141 public void variableRemoved(V variable) { 142 } 143 144 /** Initialization -- called before the solver is started */ 145 @Override 146 public boolean init(Solver<V, T> solver) { 147 return true; 148 } 149}