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;
|
---|
26 |
|
---|
27 | namespace 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 | }
|
---|
49 |
|
---|
50 | [StorableConstructor]
|
---|
51 | protected InputOutputPort(bool deserializing) : base(deserializing) { }
|
---|
52 | protected InputOutputPort(InputOutputPort<TIn, TOut> original, Cloner cloner) : base(original, cloner) {
|
---|
53 | outputInputPort = cloner.Clone(original.outputInputPort);
|
---|
54 | RegisterOutputInputPortEvents();
|
---|
55 | }
|
---|
56 | public InputOutputPort() : base("InputOutputPort") { }
|
---|
57 | public InputOutputPort(string name) : base(name) { }
|
---|
58 | public InputOutputPort(string name, string description) : base(name, description) { }
|
---|
59 |
|
---|
60 | [StorableHook(HookType.AfterDeserialization)]
|
---|
61 | private void AfterDeserialization() {
|
---|
62 | RegisterOutputInputPortEvents();
|
---|
63 | }
|
---|
64 |
|
---|
65 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
66 | return new InputOutputPort<TIn, TOut>(this, cloner);
|
---|
67 | }
|
---|
68 |
|
---|
69 | public event EventHandler OutputInputPortChanged;
|
---|
70 | protected void OnOutputInputPortChanged() {
|
---|
71 | var handler = OutputInputPortChanged;
|
---|
72 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
73 | }
|
---|
74 |
|
---|
75 | public event EventHandler<OutputInputPortEventArgs<TIn, TOut>> ValueReceived;
|
---|
76 | protected TOut OnValueReceived(TIn value) {
|
---|
77 | var args = new OutputInputPortEventArgs<TIn, TOut>(value);
|
---|
78 | var handler = ValueReceived;
|
---|
79 | if (handler != null) handler(this, args);
|
---|
80 | return args.Result;
|
---|
81 | }
|
---|
82 |
|
---|
83 | protected void RegisterOutputInputPortEvents() {
|
---|
84 | if (outputInputPort != null) {
|
---|
85 | outputInputPort.ValueSent += OutputInputPort_ValueSent;
|
---|
86 | }
|
---|
87 | }
|
---|
88 | protected void DeregisterOutputInputPortEvents() {
|
---|
89 | if (outputInputPort != null) {
|
---|
90 | outputInputPort.ValueSent -= OutputInputPort_ValueSent;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | protected void OutputInputPort_ValueSent(object sender, OutputInputPortEventArgs<TIn, TOut> e) {
|
---|
94 | var value = e.Value;
|
---|
95 | if (value != null)
|
---|
96 | value = (TIn)value.Clone();
|
---|
97 | Value = value;
|
---|
98 | e.Result = OnValueReceived(value);
|
---|
99 | //DISCUSS: clone value?
|
---|
100 | //DISCUSS: switch threads to decouple value propagation and resulting action -> should be done in Node?
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|