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.Core.Networks;
|
---|
25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 | using System;
|
---|
27 | using System.Linq;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Operators {
|
---|
30 | [Item("HookOperator", "An operator which interacts with an optimization network port.")]
|
---|
31 | [StorableClass]
|
---|
32 | public class HookOperator : Operator, IHookOperator {
|
---|
33 | public new ParameterCollection Parameters {
|
---|
34 | get { return base.Parameters; }
|
---|
35 | }
|
---|
36 |
|
---|
37 | [Storable]
|
---|
38 | protected IMessagePort port;
|
---|
39 | public IMessagePort Port {
|
---|
40 | get { return port; }
|
---|
41 | set {
|
---|
42 | if (port != value) {
|
---|
43 | port = value;
|
---|
44 | OnPortChanged();
|
---|
45 | }
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | [StorableConstructor]
|
---|
50 | protected HookOperator(bool deserializing) : base(deserializing) { }
|
---|
51 | protected HookOperator(HookOperator original, Cloner cloner)
|
---|
52 | : base(original, cloner) {
|
---|
53 | port = cloner.Clone(original.port);
|
---|
54 | }
|
---|
55 | public HookOperator() : base() { }
|
---|
56 |
|
---|
57 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
58 | return new HookOperator(this, cloner);
|
---|
59 | }
|
---|
60 |
|
---|
61 | public event EventHandler PortChanged;
|
---|
62 | protected virtual void OnPortChanged() {
|
---|
63 | var handler = PortChanged;
|
---|
64 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
65 | }
|
---|
66 |
|
---|
67 | public override IOperation Apply() {
|
---|
68 | if (Port != null) {
|
---|
69 | var msg = Port.PrepareMessage();
|
---|
70 | foreach (var output in Port.Parameters.Where(p => p.Type.HasFlag(PortParameterType.Output))) {
|
---|
71 | IParameter param;
|
---|
72 | if (Parameters.TryGetValue(output.Name, out param)) {
|
---|
73 | msg[output.Name] = param.ActualValue;
|
---|
74 | } else if (output.DefaultValue == null) {
|
---|
75 | throw new InvalidOperationException(string.Format("Cannot determine value for output parameter \"{0}\".", output.Name));
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | Port.SendMessage(msg, CancellationToken);
|
---|
80 |
|
---|
81 | foreach (var input in Port.Parameters.Where(p => p.Type.HasFlag(PortParameterType.Input))) {
|
---|
82 | IParameter param;
|
---|
83 | if (Parameters.TryGetValue(input.Name, out param)) {
|
---|
84 | param.ActualValue = msg[input.Name];
|
---|
85 | } else {
|
---|
86 | throw new InvalidOperationException(string.Format("Cannot find operator parameter for input parameter \"{0}\".", input.Name));
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 | return null;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|