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.Linq;
|
---|
26 | using System.Threading;
|
---|
27 | using System.Threading.Tasks;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Optimization.Networks {
|
---|
30 | [Item("ClientNode", "A node of an optimization network which calls service nodes.")]
|
---|
31 | [StorableClass]
|
---|
32 | public class ClientNode : Node, IClientNode {
|
---|
33 | private object locker = new object();
|
---|
34 |
|
---|
35 | new public PortCollection Ports {
|
---|
36 | get { return base.Ports; }
|
---|
37 | }
|
---|
38 |
|
---|
39 | [Storable]
|
---|
40 | private IItemCollection<ServiceParameterCollection> calls;
|
---|
41 | public IItemCollection<ServiceParameterCollection> Calls {
|
---|
42 | get { return calls; }
|
---|
43 | }
|
---|
44 |
|
---|
45 | [StorableConstructor]
|
---|
46 | protected ClientNode(bool deserializing) : base(deserializing) { }
|
---|
47 | protected ClientNode(ClientNode original, Cloner cloner)
|
---|
48 | : base(original, cloner) {
|
---|
49 | calls = cloner.Clone(original.calls);
|
---|
50 | }
|
---|
51 | public ClientNode()
|
---|
52 | : base("ClientNode") {
|
---|
53 | calls = new ItemCollection<ServiceParameterCollection>();
|
---|
54 | }
|
---|
55 | public ClientNode(string name)
|
---|
56 | : base(name) {
|
---|
57 | calls = new ItemCollection<ServiceParameterCollection>();
|
---|
58 | }
|
---|
59 | public ClientNode(string name, string description)
|
---|
60 | : base(name, description) {
|
---|
61 | calls = new ItemCollection<ServiceParameterCollection>();
|
---|
62 | }
|
---|
63 |
|
---|
64 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
65 | return new ClientNode(this, cloner);
|
---|
66 | }
|
---|
67 |
|
---|
68 | public virtual async Task CallServicesAsync() {
|
---|
69 | await CallServicesAsync(CancellationToken.None);
|
---|
70 | }
|
---|
71 | public virtual async Task CallServicesAsync(CancellationToken token) {
|
---|
72 | foreach (var clientPort in Ports.OfType<IClientPort>()) {
|
---|
73 | var parameters = clientPort.PrepareParameters();
|
---|
74 |
|
---|
75 | // retrieve inputs
|
---|
76 | foreach (var inputPort in Ports.OfType<IInputPort>().
|
---|
77 | Where(x => parameters.ContainsKey(x.Name) &&
|
---|
78 | parameters[x.Name].Type == ServiceParameterType.Input &&
|
---|
79 | parameters[x.Name].DataType.IsAssignableFrom(x.DataType))) {
|
---|
80 | parameters[inputPort.Name].Value = (IItem)inputPort.Value.Clone();
|
---|
81 | }
|
---|
82 |
|
---|
83 | parameters = await clientPort.CallServiceAsync(parameters, token);
|
---|
84 |
|
---|
85 | lock (locker) {
|
---|
86 | // set outputs
|
---|
87 | foreach (var outputPort in Ports.OfType<IOutputPort>().
|
---|
88 | Where(x => parameters.ContainsKey(x.Name) &&
|
---|
89 | parameters[x.Name].Type == ServiceParameterType.Output &&
|
---|
90 | x.DataType.IsAssignableFrom(parameters[x.Name].DataType))) {
|
---|
91 | outputPort.Value = (IItem)parameters[outputPort.Name].Value.Clone();
|
---|
92 | }
|
---|
93 |
|
---|
94 | Calls.Add(parameters);
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|