#region License Information
/* HeuristicLab
* Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace HeuristicLab.Optimization.Networks {
[Item("ClientNode", "A node of an optimization network which calls service nodes.")]
[StorableClass]
public class ClientNode : Node, IClientNode {
private object locker = new object();
new public PortCollection Ports {
get { return base.Ports; }
}
[Storable]
private IItemCollection calls;
public IItemCollection Calls {
get { return calls; }
}
[StorableConstructor]
protected ClientNode(bool deserializing) : base(deserializing) { }
protected ClientNode(ClientNode original, Cloner cloner)
: base(original, cloner) {
calls = cloner.Clone(original.calls);
}
public ClientNode()
: base("ClientNode") {
calls = new ItemCollection();
}
public ClientNode(string name)
: base(name) {
calls = new ItemCollection();
}
public ClientNode(string name, string description)
: base(name, description) {
calls = new ItemCollection();
}
public override IDeepCloneable Clone(Cloner cloner) {
return new ClientNode(this, cloner);
}
public virtual async Task CallServicesAsync() {
await CallServicesAsync(CancellationToken.None);
}
public virtual async Task CallServicesAsync(CancellationToken token) {
foreach (var clientPort in Ports.OfType()) {
var parameters = clientPort.PrepareParameters();
// retrieve inputs
foreach (var inputPort in Ports.OfType().
Where(x => parameters.ContainsKey(x.Name) &&
parameters[x.Name].Type == ServiceParameterType.Input &&
parameters[x.Name].DataType.IsAssignableFrom(x.DataType))) {
parameters[inputPort.Name].Value = (IItem)inputPort.Value.Clone();
}
parameters = await clientPort.CallServiceAsync(parameters, token);
lock (locker) {
// set outputs
foreach (var outputPort in Ports.OfType().
Where(x => parameters.ContainsKey(x.Name) &&
parameters[x.Name].Type == ServiceParameterType.Output &&
x.DataType.IsAssignableFrom(parameters[x.Name].DataType))) {
outputPort.Value = (IItem)parameters[outputPort.Name].Value.Clone();
}
Calls.Add(parameters);
}
}
}
}
}