source: branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/ClientNode.cs @ 11468

Last change on this file since 11468 was 11468, checked in by swagner, 8 years ago

#2205: Worked on optimization networks

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