Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/InputOutputPort.cs @ 11452

Last change on this file since 11452 was 11452, checked in by swagner, 10 years ago

#2205: Worked on optimization networks

File size: 4.2 KB
Line 
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
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
25using System;
26
27namespace HeuristicLab.Optimization.Networks {
28  [Item("InputOutputPort", "A synchronous input/output port of an optimization network node.")]
29  [StorableClass]
30  public class InputOutputPort<TIn, TOut> : ValuePort<TIn>, IInputOutputPort<TIn, TOut>
31    where TIn : class, IItem
32    where TOut : class, IItem {
33    [Storable]
34    private IOutputInputPort<TIn, TOut> outputInputPort;
35    public IOutputInputPort<TIn, TOut> OutputInputPort {
36      get { return outputInputPort; }
37      set {
38        if (outputInputPort != value) {
39          DeregisterOutputInputPortEvents();
40          outputInputPort = value;
41          RegisterOutputInputPortEvents();
42          OnOutputInputPortChanged();
43        }
44      }
45    }
46    IOutputInputPort IInputOutputPort.OutputInputPort {
47      get { return outputInputPort; }
48      set {
49        var val = value as IOutputInputPort<TIn, TOut>;
50        if ((value != null) && (val == null))
51          throw new InvalidOperationException(
52            string.Format("Type mismatch. OutputInputPort is not a \"{0}\".",
53                          typeof(IOutputInputPort<TIn, TOut>).GetPrettyName())
54          );
55        OutputInputPort = val;
56      }
57    }
58
59    [StorableConstructor]
60    protected InputOutputPort(bool deserializing) : base(deserializing) { }
61    protected InputOutputPort(InputOutputPort<TIn, TOut> original, Cloner cloner) : base(original, cloner) {
62      outputInputPort = cloner.Clone(original.outputInputPort);
63      RegisterOutputInputPortEvents();
64    }
65    public InputOutputPort() : base("InputOutputPort") { }
66    public InputOutputPort(string name) : base(name) { }
67    public InputOutputPort(string name, string description) : base(name, description) { }
68
69    [StorableHook(HookType.AfterDeserialization)]
70    private void AfterDeserialization() {
71      RegisterOutputInputPortEvents();
72    }
73
74    public override IDeepCloneable Clone(Cloner cloner) {
75      return new InputOutputPort<TIn, TOut>(this, cloner);
76    }
77
78    public event EventHandler OutputInputPortChanged;
79    protected void OnOutputInputPortChanged() {
80      var handler = OutputInputPortChanged;
81      if (handler != null) handler(this, EventArgs.Empty);
82    }
83
84    public event EventHandler<OutputInputPortEventArgs<TIn, TOut>> ValueReceived;
85    protected TOut OnValueReceived(TIn value) {
86      var args = new OutputInputPortEventArgs<TIn, TOut>(value);
87      var handler = ValueReceived;
88      if (handler != null) handler(this, args);
89      return args.Result;
90    }
91
92    protected void RegisterOutputInputPortEvents() {
93      if (outputInputPort != null) {
94        outputInputPort.ValueSent += OutputInputPort_ValueSent;
95      }
96    }
97    protected void DeregisterOutputInputPortEvents() {
98      if (outputInputPort != null) {
99        outputInputPort.ValueSent -= OutputInputPort_ValueSent;
100      }
101    }
102    protected void OutputInputPort_ValueSent(object sender, OutputInputPortEventArgs<TIn, TOut> e) {
103      var value = e.Value;
104      if (value != null)
105        value = (TIn)value.Clone();
106      Value = value;
107      e.Result = OnValueReceived(value);
108      //DISCUSS: clone value?
109      //DISCUSS: switch threads to decouple value propagation and resulting action -> should be done in Node?
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.