#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 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.ComponentModel;
using System.Linq;
using HeuristicLab.Collections;
using HeuristicLab.Common;
using HeuristicLab.Core;
namespace HeuristicLab.Clients.Hive {
public partial class HiveExperiment : IDeepCloneable, IContent, IProgressReporter {
private bool isPrivileged;
public bool IsPrivileged {
get { return isPrivileged; }
set { isPrivileged = value; }
}
private ExecutionState executionState;
public ExecutionState ExecutionState {
get { return executionState; }
internal set {
if (executionState != value) {
executionState = value;
OnExecutionStateChanged();
}
}
}
private TimeSpan executionTime;
public TimeSpan ExecutionTime {
get { return executionTime; }
internal set {
if (executionTime != value) {
executionTime = value;
OnExecutionTimeChanged();
}
}
}
private ItemCollection hiveJobs;
public ItemCollection HiveJobs {
get { return hiveJobs; }
set {
if (hiveJobs != value) {
if (hiveJobs != null) DeregisterHiveJobsEvents();
hiveJobs = value;
if (hiveJobs != null) RegisterHiveJobsEvents();
OnHiveJobsChanged();
}
}
}
private bool isProgressing;
public bool IsProgressing {
get { return isProgressing; }
set {
if (isProgressing != value) {
isProgressing = value;
OnIsProgressingChanged();
}
}
}
private IProgress progress;
public IProgress Progress {
get { return progress; }
set { this.progress = value; }
}
public StateLogListList StateLogList {
get { return new StateLogListList(this.GetAllHiveJobs().Select(x => x.StateLog)); }
}
#region Constructors and Cloning
public HiveExperiment() {
this.ResourceNames = "HEAL";
this.HiveJobs = new ItemCollection();
this.DateCreated = DateTime.Now;
}
protected HiveExperiment(HiveExperiment original, Cloner cloner) {
cloner.RegisterClonedObject(original, this);
this.OwnerUserId = original.OwnerUserId;
this.DateCreated = original.DateCreated;
this.ResourceNames = original.ResourceNames;
this.LastAccessed = original.LastAccessed;
this.Name = original.Name;
this.Description = original.Description;
this.Id = original.Id;
this.HiveJobs = cloner.Clone(original.HiveJobs);
this.ExecutionTime = original.ExecutionTime;
this.IsPrivileged = original.IsPrivileged;
}
public override IDeepCloneable Clone(Cloner cloner) {
return new HiveExperiment(this, cloner);
}
#endregion
#region Events
public event EventHandler ExecutionTimeChanged;
protected virtual void OnExecutionTimeChanged() {
var handler = ExecutionTimeChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler ExecutionStateChanged;
protected virtual void OnExecutionStateChanged() {
var handler = ExecutionStateChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler HiveJobsChanged;
protected virtual void OnHiveJobsChanged() {
var handler = HiveJobsChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler IsProgressingChanged;
protected virtual void OnIsProgressingChanged() {
var handler = IsProgressingChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler Loaded;
public virtual void OnLoaded() {
var handler = Loaded;
if (handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler> HiveJobsAdded;
private void OnHiveJobsAdded(CollectionItemsChangedEventArgs e) {
var handler = HiveJobsAdded;
if (handler != null) handler(this, e);
}
public event EventHandler> HiveJobsRemoved;
private void OnHiveJobsRemoved(CollectionItemsChangedEventArgs e) {
var handler = HiveJobsRemoved;
if (handler != null) handler(this, e);
}
public event EventHandler> HiveJobsReset;
private void OnHiveJobsReset(CollectionItemsChangedEventArgs e) {
var handler = HiveJobsReset;
if (handler != null) handler(this, e);
}
public event EventHandler StateLogListChanged;
private void OnStateLogListChanged() {
var handler = StateLogListChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
#endregion
#region HiveJobs Events
private void RegisterHiveJobsEvents() {
this.hiveJobs.ItemsAdded += new CollectionItemsChangedEventHandler(hiveJobs_ItemsAdded);
this.hiveJobs.ItemsRemoved += new CollectionItemsChangedEventHandler(hiveJobs_ItemsRemoved);
this.hiveJobs.CollectionReset += new CollectionItemsChangedEventHandler(hiveJobs_CollectionReset);
}
private void DeregisterHiveJobsEvents() {
this.hiveJobs.ItemsAdded -= new CollectionItemsChangedEventHandler(hiveJobs_ItemsAdded);
this.hiveJobs.ItemsRemoved -= new CollectionItemsChangedEventHandler(hiveJobs_ItemsRemoved);
this.hiveJobs.CollectionReset -= new CollectionItemsChangedEventHandler(hiveJobs_CollectionReset);
}
private void hiveJobs_CollectionReset(object sender, CollectionItemsChangedEventArgs e) {
foreach (var item in e.Items) {
item.StateLogChanged -= new EventHandler(item_StateLogChanged);
}
OnHiveJobsReset(e);
}
private void hiveJobs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs e) {
foreach (var item in e.Items) {
item.StateLogChanged -= new EventHandler(item_StateLogChanged);
}
OnHiveJobsRemoved(e);
}
private void hiveJobs_ItemsAdded(object sender, CollectionItemsChangedEventArgs e) {
foreach (var item in e.Items) {
item.StateLogChanged += new EventHandler(item_StateLogChanged);
}
OnHiveJobsAdded(e);
}
private void item_StateLogChanged(object sender, EventArgs e) {
OnStateLogListChanged();
}
#endregion
protected override void OnPropertyChanged(PropertyChangedEventArgs e) {
base.OnPropertyChanged(e);
if (e.PropertyName == "Name") {
OnToStringChanged();
}
}
protected override void RaisePropertyChanged(string propertyName) {
if (!(propertyName == "ExecutionTime")
&& !(propertyName == "JobCount")
&& !(propertyName == "CalculatingCount")
&& !(propertyName == "FinishedCount")) {
base.RaisePropertyChanged(propertyName);
}
}
public bool IsFinished() {
return HiveJobs != null
&& HiveJobs.All(x => x.Job.DateFinished.HasValue && x.Job.DateCreated.HasValue);
}
public IEnumerable GetAllHiveJobs() {
if (hiveJobs == null) return Enumerable.Empty();
var jobs = new List();
foreach (HiveJob job in HiveJobs) {
jobs.AddRange(job.GetAllHiveJobs());
}
return jobs;
}
public override string ToString() {
return Name;
}
}
}