#region License Information
/* HeuristicLab
* Copyright (C) 2002-2014 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 HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using System;
using System.Drawing;
namespace HeuristicLab.Optimization.Networks {
[Item("InputPort", "An asynchronous input port of an optimization network node.")]
[StorableClass]
public class InputPort : ValuePort, IInputPort where T : class, IItem {
public static new Image StaticItemImage {
get { return HeuristicLab.Common.Resources.VSImageLibrary.ArrowDown; }
}
[Storable]
private IOutputPort outputPort;
public IOutputPort OutputPort {
get { return outputPort; }
set {
if (outputPort != value) {
DeregisterOutputPortEvents();
outputPort = value;
RegisterOutputPortEvents();
OnOutputPortChanged();
}
}
}
IOutputPort IInputPort.OutputPort {
get { return outputPort; }
set {
var val = value as IOutputPort;
if ((value != null) && (val == null))
throw new InvalidOperationException(
string.Format("Type mismatch. OutputPort is not a \"{0}\".",
typeof(IOutputPort).GetPrettyName())
);
OutputPort = val;
}
}
[StorableConstructor]
protected InputPort(bool deserializing) : base(deserializing) { }
protected InputPort(InputPort original, Cloner cloner) : base(original, cloner) {
outputPort = cloner.Clone(original.outputPort);
RegisterOutputPortEvents();
}
public InputPort() : base("InputPort") { }
public InputPort(string name) : base(name) { }
public InputPort(string name, string description) : base(name, description) { }
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
RegisterOutputPortEvents();
}
public override IDeepCloneable Clone(Cloner cloner) {
return new InputPort(this, cloner);
}
public event EventHandler OutputPortChanged;
protected void OnOutputPortChanged() {
var handler = OutputPortChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
protected void RegisterOutputPortEvents() {
if (outputPort != null) {
outputPort.ValueChanged += OutputPort_ValueChanged;
outputPort.ToStringChanged += OutputPort_ToStringChanged;
}
}
protected void DeregisterOutputPortEvents() {
if (outputPort != null) {
outputPort.ValueChanged -= OutputPort_ValueChanged;
outputPort.ToStringChanged -= OutputPort_ToStringChanged;
}
}
protected void OutputPort_ValueChanged(object sender, EventArgs e) {
Value = (T)OutputPort.Value.Clone();
//DISCUSS: clone value?
//DISCUSS: switch threads to decouple value propagation and to make communication asynchronous -> should be done in Node?
}
protected void OutputPort_ToStringChanged(object sender, EventArgs e) {
OnOutputPortChanged();
}
}
}