#region License Information /* HeuristicLab * Copyright (C) 2002-2010 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.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 { [Item("OKB Experiment", "...")] [Creatable("OKB")] [StorableClass] public sealed class OKBExperiment : NamedItem, IOptimizer, IStorableContent { public string Filename { get; set; } public override Image ItemImage { get { if (Algorithm != null) return Algorithm.ItemImage; else return HeuristicLab.Common.Resources.VS2008ImageLibrary.Event; } } [Storable] private long AlgorithmId { get; set; } [Storable] private IAlgorithm algorithm; private IAlgorithm Algorithm { get { return algorithm; } set { DeregisterAlgorithmEvents(); algorithm = value; RegisterAlgorithmEvents(); algorithm.Problem = Problem; OnChanged(); } } private long ProblemId { get; set; } private IProblem problem; public IProblem Problem { get { return problem; } private set { problem = value; if (Algorithm != null) Algorithm.Problem = problem; OnChanged(); } } public IKeyedItemCollection Parameters { get { if (Algorithm != null) return Algorithm.Parameters; else return null; } } public ResultCollection Results { get { if (Algorithm != null) return Algorithm.Results; else return null; } } public RunCollection Runs { get { if (Algorithm != null) return Algorithm.Runs; else return null; } } public ExecutionState ExecutionState { get { if (Algorithm != null) return Algorithm.ExecutionState; else return ExecutionState.Stopped; } } public TimeSpan ExecutionTime { get { if (Algorithm != null) return Algorithm.ExecutionTime; else return TimeSpan.Zero; } } public OKBExperiment() : base() { name = ItemName; description = ItemDescription; } [StorableConstructor] private OKBExperiment(bool deserializing) : base(deserializing) { } public void LoadAlgorithm(long id) { AlgorithmId = id; AlgorithmData algorithmData = OKBClient.Instance.GetAlgorithmData(id); using (MemoryStream stream = new MemoryStream(algorithmData.Data)) { Algorithm = XmlParser.Deserialize(stream); } } public void LoadProblem(long id) { ProblemId = id; ProblemData problemData = OKBClient.Instance.GetProblemData(id); using (MemoryStream stream = new MemoryStream(problemData.Data)) { Problem = XmlParser.Deserialize(stream); } } public void Pause() { Algorithm.Pause(); } public void Prepare(bool clearRuns) { if (Algorithm != null) Algorithm.Prepare(clearRuns); } public void Prepare() { Algorithm.Prepare(); } public void Start() { Algorithm.Start(); } public void Stop() { Algorithm.Stop(); } #region Events public event EventHandler Changed; private void OnChanged() { EventHandler handler = Changed; if (handler != null) handler(this, EventArgs.Empty); } public event EventHandler> ExceptionOccurred; private void OnExceptionOccurred(Exception exception) { EventHandler> handler = ExceptionOccurred; if (handler != null) handler(this, new EventArgs(exception)); } public event EventHandler ExecutionStateChanged; private void OnExecutionStateChanged() { EventHandler handler = ExecutionStateChanged; if (handler != null) handler(this, EventArgs.Empty); } public event EventHandler ExecutionTimeChanged; private void OnExecutionTimeChanged() { EventHandler handler = ExecutionTimeChanged; if (handler != null) handler(this, EventArgs.Empty); } public event EventHandler Prepared; private void OnPrepared() { EventHandler handler = Prepared; if (handler != null) handler(this, EventArgs.Empty); } public event EventHandler Started; private void OnStarted() { EventHandler handler = Started; if (handler != null) handler(this, EventArgs.Empty); } public event EventHandler Paused; private void OnPaused() { EventHandler handler = Paused; if (handler != null) handler(this, EventArgs.Empty); } public event EventHandler Stopped; private void OnStopped() { EventHandler handler = Stopped; if (handler != null) handler(this, EventArgs.Empty); } private void RegisterAlgorithmEvents() { if (Algorithm != null) { Algorithm.ExceptionOccurred += new EventHandler>(Algorithm_ExceptionOccurred); Algorithm.ExecutionStateChanged += new EventHandler(Algorithm_ExecutionStateChanged); Algorithm.ExecutionTimeChanged += new EventHandler(Algorithm_ExecutionTimeChanged); Algorithm.ItemImageChanged += new EventHandler(Algorithm_ItemImageChanged); Algorithm.Paused += new EventHandler(Algorithm_Paused); Algorithm.Prepared += new EventHandler(Algorithm_Prepared); Algorithm.Started += new EventHandler(Algorithm_Started); Algorithm.Stopped += new EventHandler(Algorithm_Stopped); } } private void DeregisterAlgorithmEvents() { if (Algorithm != null) { Algorithm.ExceptionOccurred -= new EventHandler>(Algorithm_ExceptionOccurred); Algorithm.ExecutionStateChanged -= new EventHandler(Algorithm_ExecutionStateChanged); Algorithm.ExecutionTimeChanged -= new EventHandler(Algorithm_ExecutionTimeChanged); Algorithm.ItemImageChanged -= new EventHandler(Algorithm_ItemImageChanged); Algorithm.Paused -= new EventHandler(Algorithm_Paused); Algorithm.Prepared -= new EventHandler(Algorithm_Prepared); Algorithm.Started -= new EventHandler(Algorithm_Started); Algorithm.Stopped -= new EventHandler(Algorithm_Stopped); } } private void Algorithm_ExceptionOccurred(object sender, EventArgs e) { OnExceptionOccurred(e.Value); } private void Algorithm_ExecutionStateChanged(object sender, EventArgs e) { OnExecutionStateChanged(); } private void Algorithm_ExecutionTimeChanged(object sender, EventArgs e) { OnExecutionTimeChanged(); } private void Algorithm_ItemImageChanged(object sender, EventArgs e) { OnItemImageChanged(); } private void Algorithm_Paused(object sender, EventArgs e) { OnPaused(); } private void Algorithm_Prepared(object sender, EventArgs e) { OnPrepared(); } private void Algorithm_Started(object sender, EventArgs e) { OnStarted(); } private void Algorithm_Stopped(object sender, EventArgs e) { OnStopped(); } #endregion } }