[5338] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Drawing;
|
---|
| 25 | using System.IO;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 | using HeuristicLab.Persistence.Default.Xml;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Clients.OKB {
|
---|
[5344] | 33 | [Item("OKB Problem", "A base class for problems which are stored in the OKB.")]
|
---|
[5338] | 34 | [StorableClass]
|
---|
[5344] | 35 | public abstract class OKBProblem : Item, IProblem {
|
---|
[5338] | 36 | public virtual Type ProblemType {
|
---|
| 37 | get { return typeof(IProblem); }
|
---|
| 38 | }
|
---|
| 39 | private long problemId;
|
---|
| 40 | public long ProblemId {
|
---|
| 41 | get { return problemId; }
|
---|
| 42 | }
|
---|
| 43 | private IProblem problem;
|
---|
| 44 | public IProblem Problem {
|
---|
| 45 | get { return problem; }
|
---|
| 46 | private set {
|
---|
[5344] | 47 | if (value == null) throw new ArgumentNullException("Problem", "Problem cannot be null.");
|
---|
[5338] | 48 | if (value != problem) {
|
---|
| 49 | CancelEventArgs<string> e = new CancelEventArgs<string>(value.Name);
|
---|
| 50 | OnNameChanging(e);
|
---|
| 51 | if (!e.Cancel) {
|
---|
| 52 | DeregisterProblemEvents();
|
---|
| 53 | problem = value;
|
---|
| 54 | RegisterProblemEvents();
|
---|
| 55 |
|
---|
| 56 | OnToStringChanged();
|
---|
| 57 | OnItemImageChanged();
|
---|
| 58 | OnNameChanged();
|
---|
| 59 | OnDescriptionChanged();
|
---|
| 60 | OnProblemChanged();
|
---|
| 61 | OnSolutionCreatorChanged();
|
---|
| 62 | OnEvaluatorChanged();
|
---|
| 63 | OnOperatorsChanged();
|
---|
| 64 | OnReset();
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[5344] | 70 | public override Image ItemImage {
|
---|
| 71 | get { return Problem.ItemImage; }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | public string Name {
|
---|
| 75 | get { return Problem.Name; }
|
---|
| 76 | set { throw new NotSupportedException("Name cannot be changed."); }
|
---|
| 77 | }
|
---|
| 78 | public string Description {
|
---|
| 79 | get { return Problem.Description; }
|
---|
| 80 | set { throw new NotSupportedException("Description cannot be changed."); }
|
---|
| 81 | }
|
---|
| 82 | public bool CanChangeName {
|
---|
| 83 | get { return false; }
|
---|
| 84 | }
|
---|
| 85 | public bool CanChangeDescription {
|
---|
| 86 | get { return false; }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | public IKeyedItemCollection<string, IParameter> Parameters {
|
---|
| 90 | get { return Problem.Parameters; }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[5338] | 93 | public IParameter SolutionCreatorParameter {
|
---|
[5344] | 94 | get { return Problem.SolutionCreatorParameter; }
|
---|
[5338] | 95 | }
|
---|
| 96 | public ISolutionCreator SolutionCreator {
|
---|
[5344] | 97 | get { return Problem.SolutionCreator; }
|
---|
[5338] | 98 | }
|
---|
| 99 | public IParameter EvaluatorParameter {
|
---|
[5344] | 100 | get { return Problem.EvaluatorParameter; }
|
---|
[5338] | 101 | }
|
---|
| 102 | public IEvaluator Evaluator {
|
---|
[5344] | 103 | get { return Problem.Evaluator; }
|
---|
[5338] | 104 | }
|
---|
| 105 | public IEnumerable<IOperator> Operators {
|
---|
[5344] | 106 | get { return Problem.Operators; }
|
---|
[5338] | 107 | }
|
---|
| 108 |
|
---|
| 109 | #region Persistence Properties
|
---|
| 110 | [Storable(Name = "ProblemId")]
|
---|
| 111 | private long StorableProblemId {
|
---|
| 112 | get { return problemId; }
|
---|
| 113 | set { problemId = value; }
|
---|
| 114 | }
|
---|
| 115 | [Storable(Name = "Problem")]
|
---|
| 116 | private IProblem StorableProblem {
|
---|
[5344] | 117 | get { return problem; }
|
---|
| 118 | set {
|
---|
| 119 | problem = value;
|
---|
| 120 | RegisterProblemEvents();
|
---|
| 121 | }
|
---|
[5338] | 122 | }
|
---|
| 123 | #endregion
|
---|
| 124 |
|
---|
| 125 | [StorableConstructor]
|
---|
| 126 | protected OKBProblem(bool deserializing) : base(deserializing) { }
|
---|
| 127 | protected OKBProblem(OKBProblem original, Cloner cloner)
|
---|
| 128 | : base(original, cloner) {
|
---|
| 129 | problemId = original.problemId;
|
---|
| 130 | problem = cloner.Clone(original.problem);
|
---|
[5344] | 131 | RegisterProblemEvents();
|
---|
[5338] | 132 | }
|
---|
[5344] | 133 | protected OKBProblem(IProblem initialProblem)
|
---|
[5338] | 134 | : base() {
|
---|
[5344] | 135 | if (initialProblem == null) throw new ArgumentNullException("initialProblem", "Initial problem cannot be null.");
|
---|
[5338] | 136 | problemId = -1;
|
---|
[5344] | 137 | problem = initialProblem;
|
---|
| 138 | RegisterProblemEvents();
|
---|
[5338] | 139 | }
|
---|
| 140 |
|
---|
| 141 | public void Load(long problemId) {
|
---|
| 142 | if (this.problemId != problemId) {
|
---|
| 143 | IProblem problem;
|
---|
| 144 | ProblemData problemData = OKBClient.Instance.GetProblemData(problemId);
|
---|
| 145 | using (MemoryStream stream = new MemoryStream(problemData.Data)) {
|
---|
| 146 | problem = XmlParser.Deserialize<IProblem>(stream);
|
---|
| 147 | }
|
---|
| 148 | if (ProblemType.IsAssignableFrom(problem.GetType())) {
|
---|
| 149 | this.problemId = problemId;
|
---|
| 150 | Problem = problem;
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | public void CollectParameterValues(IDictionary<string, IItem> values) {
|
---|
[5344] | 156 | Problem.CollectParameterValues(values);
|
---|
[5338] | 157 | }
|
---|
| 158 |
|
---|
| 159 | #region Events
|
---|
[5344] | 160 | public event EventHandler ProblemChanged;
|
---|
| 161 | protected virtual void OnProblemChanged() {
|
---|
| 162 | EventHandler handler = ProblemChanged;
|
---|
| 163 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 164 | }
|
---|
[5338] | 165 | public event EventHandler<CancelEventArgs<string>> NameChanging;
|
---|
| 166 | protected virtual void OnNameChanging(CancelEventArgs<string> e) {
|
---|
| 167 | var handler = NameChanging;
|
---|
| 168 | if (handler != null) handler(this, e);
|
---|
| 169 | }
|
---|
| 170 | public event EventHandler NameChanged;
|
---|
| 171 | protected virtual void OnNameChanged() {
|
---|
| 172 | var handler = NameChanged;
|
---|
| 173 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 174 | }
|
---|
| 175 | public event EventHandler DescriptionChanged;
|
---|
| 176 | protected virtual void OnDescriptionChanged() {
|
---|
| 177 | var handler = DescriptionChanged;
|
---|
| 178 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 179 | }
|
---|
| 180 | public event EventHandler SolutionCreatorChanged;
|
---|
| 181 | protected virtual void OnSolutionCreatorChanged() {
|
---|
| 182 | var handler = SolutionCreatorChanged;
|
---|
| 183 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 184 | }
|
---|
| 185 | public event EventHandler EvaluatorChanged;
|
---|
| 186 | protected virtual void OnEvaluatorChanged() {
|
---|
| 187 | var handler = EvaluatorChanged;
|
---|
| 188 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 189 | }
|
---|
| 190 | public event EventHandler OperatorsChanged;
|
---|
| 191 | protected virtual void OnOperatorsChanged() {
|
---|
| 192 | var handler = OperatorsChanged;
|
---|
| 193 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 194 | }
|
---|
| 195 | public event EventHandler Reset;
|
---|
| 196 | protected virtual void OnReset() {
|
---|
| 197 | var handler = Reset;
|
---|
| 198 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | protected virtual void RegisterProblemEvents() {
|
---|
| 202 | if (Problem != null) {
|
---|
| 203 | Problem.ToStringChanged += new EventHandler(Problem_ToStringChanged);
|
---|
| 204 | Problem.ItemImageChanged += new EventHandler(Problem_ItemImageChanged);
|
---|
| 205 | Problem.NameChanging += new EventHandler<CancelEventArgs<string>>(Problem_NameChanging);
|
---|
| 206 | Problem.NameChanged += new EventHandler(Problem_NameChanged);
|
---|
| 207 | Problem.DescriptionChanged += new EventHandler(Problem_DescriptionChanged);
|
---|
| 208 | Problem.SolutionCreatorChanged += new EventHandler(Problem_SolutionCreatorChanged);
|
---|
| 209 | Problem.EvaluatorChanged += new EventHandler(Problem_EvaluatorChanged);
|
---|
| 210 | Problem.OperatorsChanged += new EventHandler(Problem_OperatorsChanged);
|
---|
| 211 | Problem.Reset += new EventHandler(Problem_Reset);
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 | protected virtual void DeregisterProblemEvents() {
|
---|
| 215 | if (Problem != null) {
|
---|
| 216 | Problem.ToStringChanged -= new EventHandler(Problem_ToStringChanged);
|
---|
| 217 | Problem.ItemImageChanged -= new EventHandler(Problem_ItemImageChanged);
|
---|
| 218 | Problem.NameChanging -= new EventHandler<CancelEventArgs<string>>(Problem_NameChanging);
|
---|
| 219 | Problem.NameChanged -= new EventHandler(Problem_NameChanged);
|
---|
| 220 | Problem.DescriptionChanged -= new EventHandler(Problem_DescriptionChanged);
|
---|
| 221 | Problem.SolutionCreatorChanged -= new EventHandler(Problem_SolutionCreatorChanged);
|
---|
| 222 | Problem.EvaluatorChanged -= new EventHandler(Problem_EvaluatorChanged);
|
---|
| 223 | Problem.OperatorsChanged -= new EventHandler(Problem_OperatorsChanged);
|
---|
| 224 | Problem.Reset -= new EventHandler(Problem_Reset);
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | private void Problem_ToStringChanged(object sender, EventArgs e) {
|
---|
| 229 | OnToStringChanged();
|
---|
| 230 | }
|
---|
| 231 | private void Problem_ItemImageChanged(object sender, EventArgs e) {
|
---|
| 232 | OnItemImageChanged();
|
---|
| 233 | }
|
---|
| 234 | private void Problem_NameChanging(object sender, CancelEventArgs<string> e) {
|
---|
| 235 | OnNameChanging(e);
|
---|
| 236 | }
|
---|
| 237 | private void Problem_NameChanged(object sender, EventArgs e) {
|
---|
| 238 | OnNameChanged();
|
---|
| 239 | }
|
---|
| 240 | private void Problem_DescriptionChanged(object sender, EventArgs e) {
|
---|
| 241 | OnDescriptionChanged();
|
---|
| 242 | }
|
---|
| 243 | private void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
| 244 | OnSolutionCreatorChanged();
|
---|
| 245 | }
|
---|
| 246 | private void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
| 247 | OnEvaluatorChanged();
|
---|
| 248 | }
|
---|
| 249 | private void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
| 250 | OnOperatorsChanged();
|
---|
| 251 | }
|
---|
| 252 | private void Problem_Reset(object sender, EventArgs e) {
|
---|
| 253 | OnReset();
|
---|
| 254 | }
|
---|
| 255 | #endregion
|
---|
| 256 | }
|
---|
| 257 | }
|
---|