[11401] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2014 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 HeuristicLab.Common;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 25 | using System;
|
---|
[11409] | 26 | using System.Drawing;
|
---|
[11401] | 27 |
|
---|
| 28 | namespace HeuristicLab.Optimization.Networks {
|
---|
| 29 | [Item("InputPort", "An asynchronous input port of an optimization network node.")]
|
---|
| 30 | [StorableClass]
|
---|
[11421] | 31 | public class InputPort<T> : ValuePort<T>, IInputPort<T> where T : class, IItem {
|
---|
[11409] | 32 | public static new Image StaticItemImage {
|
---|
| 33 | get { return HeuristicLab.Common.Resources.VSImageLibrary.ArrowDown; }
|
---|
| 34 | }
|
---|
| 35 |
|
---|
[11401] | 36 | [Storable]
|
---|
| 37 | private IOutputPort<T> outputPort;
|
---|
| 38 | public IOutputPort<T> OutputPort {
|
---|
| 39 | get { return outputPort; }
|
---|
| 40 | set {
|
---|
| 41 | if (outputPort != value) {
|
---|
| 42 | DeregisterOutputPortEvents();
|
---|
| 43 | outputPort = value;
|
---|
| 44 | RegisterOutputPortEvents();
|
---|
| 45 | OnOutputPortChanged();
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
| 49 | IOutputPort IInputPort.OutputPort {
|
---|
| 50 | get { return outputPort; }
|
---|
[11412] | 51 | set {
|
---|
| 52 | var val = value as IOutputPort<T>;
|
---|
| 53 | if ((value != null) && (val == null))
|
---|
| 54 | throw new InvalidOperationException(
|
---|
| 55 | string.Format("Type mismatch. OutputPort is not a \"{0}\".",
|
---|
| 56 | typeof(IOutputPort<T>).GetPrettyName())
|
---|
| 57 | );
|
---|
| 58 | OutputPort = val;
|
---|
| 59 | }
|
---|
[11401] | 60 | }
|
---|
| 61 |
|
---|
| 62 | [StorableConstructor]
|
---|
| 63 | protected InputPort(bool deserializing) : base(deserializing) { }
|
---|
| 64 | protected InputPort(InputPort<T> original, Cloner cloner) : base(original, cloner) {
|
---|
| 65 | outputPort = cloner.Clone(original.outputPort);
|
---|
| 66 | RegisterOutputPortEvents();
|
---|
| 67 | }
|
---|
| 68 | public InputPort() : base("InputPort") { }
|
---|
| 69 | public InputPort(string name) : base(name) { }
|
---|
| 70 | public InputPort(string name, string description) : base(name, description) { }
|
---|
| 71 |
|
---|
| 72 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 73 | private void AfterDeserialization() {
|
---|
| 74 | RegisterOutputPortEvents();
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 78 | return new InputPort<T>(this, cloner);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | public event EventHandler OutputPortChanged;
|
---|
| 82 | protected void OnOutputPortChanged() {
|
---|
| 83 | var handler = OutputPortChanged;
|
---|
| 84 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | protected void RegisterOutputPortEvents() {
|
---|
[11412] | 88 | if (outputPort != null) {
|
---|
| 89 | outputPort.ValueChanged += OutputPort_ValueChanged;
|
---|
[11431] | 90 | outputPort.PathChanged += OutputPort_PathChanged;
|
---|
[11412] | 91 | }
|
---|
[11401] | 92 | }
|
---|
| 93 | protected void DeregisterOutputPortEvents() {
|
---|
[11412] | 94 | if (outputPort != null) {
|
---|
| 95 | outputPort.ValueChanged -= OutputPort_ValueChanged;
|
---|
[11431] | 96 | outputPort.PathChanged -= OutputPort_PathChanged;
|
---|
[11412] | 97 | }
|
---|
[11401] | 98 | }
|
---|
| 99 | protected void OutputPort_ValueChanged(object sender, EventArgs e) {
|
---|
[11431] | 100 | var value = OutputPort.Value;
|
---|
| 101 | if (value != null)
|
---|
| 102 | value = (T)value.Clone();
|
---|
| 103 | Value = value;
|
---|
[11406] | 104 | //DISCUSS: clone value?
|
---|
| 105 | //DISCUSS: switch threads to decouple value propagation and to make communication asynchronous -> should be done in Node?
|
---|
[11401] | 106 | }
|
---|
[11431] | 107 | protected void OutputPort_PathChanged(object sender, EventArgs e) {
|
---|
[11423] | 108 | OnOutputPortChanged();
|
---|
| 109 | }
|
---|
[11401] | 110 | }
|
---|
| 111 | }
|
---|