#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Optimization;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Persistence.Default.Xml;
namespace HeuristicLab.Clients.OKB.RunCreation {
[Item("OKB Problem", "A base class for problems which are stored in the OKB.")]
[StorableClass]
public abstract class OKBProblem : Item, IHeuristicOptimizationProblem {
public virtual Type ProblemType {
get { return typeof(IHeuristicOptimizationProblem); }
}
private long problemId;
public long ProblemId {
get { return problemId; }
}
private IHeuristicOptimizationProblem problem;
protected IHeuristicOptimizationProblem Problem {
get { return problem; }
private set {
if (value == null) throw new ArgumentNullException("Problem", "Problem cannot be null.");
if (value != problem) {
CancelEventArgs e = new CancelEventArgs(value.Name);
OnNameChanging(e);
if (!e.Cancel) {
DeregisterProblemEvents();
problem = value;
RegisterProblemEvents();
OnToStringChanged();
OnItemImageChanged();
OnNameChanged();
OnDescriptionChanged();
OnProblemChanged();
OnSolutionCreatorChanged();
OnEvaluatorChanged();
OnOperatorsChanged();
OnReset();
}
}
}
}
public override Image ItemImage {
get { return Problem.ItemImage; }
}
public static new Image StaticItemImage {
get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
}
public string Name {
get { return Problem.Name; }
set { throw new NotSupportedException("Name cannot be changed."); }
}
public string Description {
get { return Problem.Description; }
set { throw new NotSupportedException("Description cannot be changed."); }
}
public bool CanChangeName {
get { return false; }
}
public bool CanChangeDescription {
get { return false; }
}
public IKeyedItemCollection Parameters {
get { return Problem.Parameters; }
}
public IParameter SolutionCreatorParameter {
get { return Problem.SolutionCreatorParameter; }
}
public ISolutionCreator SolutionCreator {
get { return Problem.SolutionCreator; }
}
public IParameter EvaluatorParameter {
get { return Problem.EvaluatorParameter; }
}
public IEvaluator Evaluator {
get { return Problem.Evaluator; }
}
public IEnumerable Operators {
get { return Problem.Operators; }
}
#region Persistence Properties
[Storable(Name = "ProblemId")]
private long StorableProblemId {
get { return problemId; }
set { problemId = value; }
}
[Storable(Name = "Problem")]
private IHeuristicOptimizationProblem StorableProblem {
get { return problem; }
set {
problem = value;
RegisterProblemEvents();
}
}
#endregion
[StorableConstructor]
protected OKBProblem(bool deserializing) : base(deserializing) { }
protected OKBProblem(OKBProblem original, Cloner cloner)
: base(original, cloner) {
problemId = original.problemId;
problem = cloner.Clone(original.problem);
RegisterProblemEvents();
}
protected OKBProblem(IHeuristicOptimizationProblem initialProblem)
: base() {
if (initialProblem == null) throw new ArgumentNullException("initialProblem", "Initial problem cannot be null.");
problemId = -1;
problem = initialProblem;
RegisterProblemEvents();
}
public void Load(long problemId) {
if (this.problemId != problemId) {
IHeuristicOptimizationProblem problem;
byte[] problemData = RunCreationClient.GetProblemData(problemId);
using (MemoryStream stream = new MemoryStream(problemData)) {
problem = XmlParser.Deserialize(stream);
}
if (ProblemType.IsAssignableFrom(problem.GetType())) {
this.problemId = problemId;
Problem = problem;
}
}
}
public IProblem CloneProblem() {
return (IProblem)Problem.Clone();
}
public void CollectParameterValues(IDictionary values) {
Problem.CollectParameterValues(values);
}
#region Events
public event EventHandler ProblemChanged;
protected virtual void OnProblemChanged() {
EventHandler handler = ProblemChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler> NameChanging;
protected virtual void OnNameChanging(CancelEventArgs e) {
var handler = NameChanging;
if (handler != null) handler(this, e);
}
public event EventHandler NameChanged;
protected virtual void OnNameChanged() {
var handler = NameChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler DescriptionChanged;
protected virtual void OnDescriptionChanged() {
var handler = DescriptionChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler SolutionCreatorChanged;
protected virtual void OnSolutionCreatorChanged() {
var handler = SolutionCreatorChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler EvaluatorChanged;
protected virtual void OnEvaluatorChanged() {
var handler = EvaluatorChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler OperatorsChanged;
protected virtual void OnOperatorsChanged() {
var handler = OperatorsChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler Reset;
protected virtual void OnReset() {
var handler = Reset;
if (handler != null) handler(this, EventArgs.Empty);
}
protected virtual void RegisterProblemEvents() {
if (Problem != null) {
Problem.ToStringChanged += new EventHandler(Problem_ToStringChanged);
Problem.ItemImageChanged += new EventHandler(Problem_ItemImageChanged);
Problem.NameChanging += new EventHandler>(Problem_NameChanging);
Problem.NameChanged += new EventHandler(Problem_NameChanged);
Problem.DescriptionChanged += new EventHandler(Problem_DescriptionChanged);
Problem.SolutionCreatorChanged += new EventHandler(Problem_SolutionCreatorChanged);
Problem.EvaluatorChanged += new EventHandler(Problem_EvaluatorChanged);
Problem.OperatorsChanged += new EventHandler(Problem_OperatorsChanged);
Problem.Reset += new EventHandler(Problem_Reset);
}
}
protected virtual void DeregisterProblemEvents() {
if (Problem != null) {
Problem.ToStringChanged -= new EventHandler(Problem_ToStringChanged);
Problem.ItemImageChanged -= new EventHandler(Problem_ItemImageChanged);
Problem.NameChanging -= new EventHandler>(Problem_NameChanging);
Problem.NameChanged -= new EventHandler(Problem_NameChanged);
Problem.DescriptionChanged -= new EventHandler(Problem_DescriptionChanged);
Problem.SolutionCreatorChanged -= new EventHandler(Problem_SolutionCreatorChanged);
Problem.EvaluatorChanged -= new EventHandler(Problem_EvaluatorChanged);
Problem.OperatorsChanged -= new EventHandler(Problem_OperatorsChanged);
Problem.Reset -= new EventHandler(Problem_Reset);
}
}
private void Problem_ToStringChanged(object sender, EventArgs e) {
OnToStringChanged();
}
private void Problem_ItemImageChanged(object sender, EventArgs e) {
OnItemImageChanged();
}
private void Problem_NameChanging(object sender, CancelEventArgs e) {
OnNameChanging(e);
}
private void Problem_NameChanged(object sender, EventArgs e) {
OnNameChanged();
}
private void Problem_DescriptionChanged(object sender, EventArgs e) {
OnDescriptionChanged();
}
private void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
OnSolutionCreatorChanged();
}
private void Problem_EvaluatorChanged(object sender, EventArgs e) {
OnEvaluatorChanged();
}
private void Problem_OperatorsChanged(object sender, EventArgs e) {
OnOperatorsChanged();
}
private void Problem_Reset(object sender, EventArgs e) {
OnReset();
}
#endregion
}
}