- Timestamp:
- 10/16/14 03:57:49 (10 years ago)
- Location:
- branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3
- Files:
-
- 1 added
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/AlgorithmServiceNode.cs
r11465 r11468 93 93 } 94 94 95 protected virtual void Execute(ServiceParameterCollection parameters ) {95 protected virtual void Execute(ServiceParameterCollection parameters, CancellationToken token) { 96 96 if (Template == null) throw new InvalidOperationException("Template is null"); 97 97 … … 120 120 algorithm.Stopped += Algorithm_Stopped; 121 121 algorithm.Start(); 122 waitHandles[algorithm].WaitOne(); 123 124 // retrieve results 125 foreach (var p in parameters.Where(x => x.Type == ServiceParameterType.Output)) { 126 IResult result = null; 127 if (algorithm.Results.TryGetValue(p.Name, out result)) { 128 p.Value = result.Value; 129 } 130 } 131 132 lock (locker) { 133 waitHandles[algorithm].Dispose(); 134 waitHandles.Remove(algorithm); 135 Runs.Add(algorithm.Runs.ToArray()[0]); 122 if (WaitHandle.WaitAny(new WaitHandle[] { waitHandles[algorithm], token.WaitHandle }) == 1) { 123 // retrieve results 124 foreach (var p in parameters.Where(x => x.Type == ServiceParameterType.Output)) { 125 IResult result = null; 126 if (algorithm.Results.TryGetValue(p.Name, out result)) { 127 p.Value = result.Value; 128 } 129 } 130 131 lock (locker) { 132 waitHandles[algorithm].Dispose(); 133 waitHandles.Remove(algorithm); 134 Runs.Add(algorithm.Runs.ToArray()[0]); 135 } 136 } else { // cancellation 137 algorithm.Stop(); 138 lock (locker) { 139 waitHandles[algorithm].Dispose(); 140 waitHandles.Remove(algorithm); 141 } 136 142 } 137 143 } … … 191 197 IServicePort s = port as IServicePort; 192 198 if (s != null) { 193 s. Called += ServicePort_Called;199 s.ProcessParameters += ServicePort_ProcessParameters; 194 200 } 195 201 } … … 203 209 IServicePort s = port as IServicePort; 204 210 if (s != null) { 205 s. Called -= ServicePort_Called;211 s.ProcessParameters -= ServicePort_ProcessParameters; 206 212 } 207 213 } … … 212 218 UpdateParameter((IInputPort)sender); 213 219 } 214 private void ServicePort_ Called(object sender, EventArgs<ServiceParameterCollection> e) {215 Execute(e.Value );220 private void ServicePort_ProcessParameters(object sender, EventArgs<ServiceParameterCollection, CancellationToken> e) { 221 Execute(e.Value, e.Value2); 216 222 } 217 223 #endregion -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/ClientNode.cs
r11465 r11468 68 68 } 69 69 70 public virtual void CallAsync() { 71 Task.Factory.StartNew(() => { 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(); 72 76 73 foreach (var clientPort in Ports.OfType<IClientPort>()) { 74 var parameters = clientPort.PrepareParameters(); 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 } 75 84 76 // retrieve inputs 77 foreach (var inputPort in Ports.OfType<IInputPort>(). 78 Where(x => parameters.ContainsKey(x.Name) && 79 parameters[x.Name].Type == ServiceParameterType.Input && 80 parameters[x.Name].DataType.IsAssignableFrom(x.DataType))) { 81 parameters[inputPort.Name].Value = (IItem)inputPort.Value.Clone(); 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(); 82 94 } 83 95 84 parameters = clientPort.Call(parameters); 85 86 lock (locker) { 87 // set outputs 88 foreach (var outputPort in Ports.OfType<IOutputPort>(). 89 Where(x => parameters.ContainsKey(x.Name) && 90 parameters[x.Name].Type == ServiceParameterType.Output && 91 x.DataType.IsAssignableFrom(parameters[x.Name].DataType))) { 92 outputPort.Value = (IItem)parameters[outputPort.Name].Value.Clone(); 93 } 94 95 Calls.Add(parameters); 96 } 96 Calls.Add(parameters); 97 97 } 98 } );98 } 99 99 } 100 100 } -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/ClientPort.cs
r11454 r11468 26 26 using System.Drawing; 27 27 using System.Linq; 28 using System.Threading; 29 using System.Threading.Tasks; 28 30 29 31 namespace HeuristicLab.Optimization.Networks { … … 127 129 return parameters; 128 130 } 129 public ServiceParameterCollection Call (ServiceParameterCollection parameters) {131 public ServiceParameterCollection CallService(ServiceParameterCollection parameters) { 130 132 if (!Valid) throw new InvalidOperationException("Port configurations do not match"); 131 return ServicePort.Call(parameters); 133 return ServicePort.Process(parameters); 134 } 135 public ServiceParameterCollection CallService(ServiceParameterCollection parameters, CancellationToken token) { 136 if (!Valid) throw new InvalidOperationException("Port configurations do not match"); 137 return ServicePort.Process(parameters, token); 138 } 139 public async Task<ServiceParameterCollection> CallServiceAsync(ServiceParameterCollection parameters) { 140 if (!Valid) throw new InvalidOperationException("Port configurations do not match"); 141 return await ServicePort.ProcessAsync(parameters); 142 } 143 public async Task<ServiceParameterCollection> CallServiceAsync(ServiceParameterCollection parameters, CancellationToken token) { 144 if (!Valid) throw new InvalidOperationException("Port configurations do not match"); 145 return await ServicePort.ProcessAsync(parameters, token); 132 146 } 133 147 -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/HeuristicLab.Optimization.Networks-3.3.csproj
r11465 r11468 10 10 <RootNamespace>HeuristicLab.Optimization.Networks</RootNamespace> 11 11 <AssemblyName>HeuristicLab.Optimization.Networks-3.3</AssemblyName> 12 <TargetFrameworkVersion>v4. 0</TargetFrameworkVersion>12 <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> 13 13 <FileAlignment>512</FileAlignment> 14 14 <TargetFrameworkProfile /> … … 22 22 <ErrorReport>prompt</ErrorReport> 23 23 <WarningLevel>4</WarningLevel> 24 <Prefer32Bit>false</Prefer32Bit> 24 25 </PropertyGroup> 25 26 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> … … 30 31 <ErrorReport>prompt</ErrorReport> 31 32 <WarningLevel>4</WarningLevel> 33 <Prefer32Bit>false</Prefer32Bit> 32 34 </PropertyGroup> 33 35 <PropertyGroup> … … 45 47 <ErrorReport>prompt</ErrorReport> 46 48 <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet> 49 <Prefer32Bit>false</Prefer32Bit> 47 50 </PropertyGroup> 48 51 <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'"> … … 54 57 <ErrorReport>prompt</ErrorReport> 55 58 <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet> 59 <Prefer32Bit>false</Prefer32Bit> 56 60 </PropertyGroup> 57 61 <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'"> … … 63 67 <ErrorReport>prompt</ErrorReport> 64 68 <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet> 69 <Prefer32Bit>false</Prefer32Bit> 65 70 </PropertyGroup> 66 71 <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'"> … … 72 77 <ErrorReport>prompt</ErrorReport> 73 78 <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet> 79 <Prefer32Bit>false</Prefer32Bit> 74 80 </PropertyGroup> 75 81 <ItemGroup> … … 129 135 </ItemGroup> 130 136 <ItemGroup> 137 <None Include="ClassDiagram.cd" /> 131 138 <None Include="HeuristicLab.snk" /> 132 139 <None Include="Plugin.cs.frame" /> -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/IClientNode.cs
r11465 r11468 22 22 using HeuristicLab.Core; 23 23 using System; 24 using System.Threading; 25 using System.Threading.Tasks; 24 26 25 27 namespace HeuristicLab.Optimization.Networks { … … 28 30 IItemCollection<ServiceParameterCollection> Calls { get; } 29 31 30 void CallAsync(); 32 Task CallServicesAsync(); 33 Task CallServicesAsync(CancellationToken token); 31 34 } 32 35 } -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/IClientPort.cs
r11454 r11468 21 21 22 22 using System; 23 using System.Threading; 24 using System.Threading.Tasks; 23 25 24 26 namespace HeuristicLab.Optimization.Networks { … … 31 33 bool IsValidServicePort(IServicePort servicePort); 32 34 ServiceParameterCollection PrepareParameters(); 33 ServiceParameterCollection Call(ServiceParameterCollection parameters); 35 ServiceParameterCollection CallService(ServiceParameterCollection parameters); 36 ServiceParameterCollection CallService(ServiceParameterCollection parameters, CancellationToken token); 37 Task<ServiceParameterCollection> CallServiceAsync(ServiceParameterCollection parameters); 38 Task<ServiceParameterCollection> CallServiceAsync(ServiceParameterCollection parameters, CancellationToken token); 34 39 } 35 40 } -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/IServicePort.cs
r11454 r11468 23 23 using HeuristicLab.Core; 24 24 using System; 25 using System.Threading; 26 using System.Threading.Tasks; 25 27 26 28 namespace HeuristicLab.Optimization.Networks { 27 29 public interface IServicePort : IClientServicePort { 28 ServiceParameterCollection Call(ServiceParameterCollection parameters); 29 event EventHandler<EventArgs<ServiceParameterCollection>> Called; 30 ServiceParameterCollection Process(ServiceParameterCollection parameters); 31 ServiceParameterCollection Process(ServiceParameterCollection parameters, CancellationToken token); 32 Task<ServiceParameterCollection> ProcessAsync(ServiceParameterCollection parameters); 33 Task<ServiceParameterCollection> ProcessAsync(ServiceParameterCollection parameters, CancellationToken token); 34 event EventHandler<EventArgs<ServiceParameterCollection, CancellationToken>> ProcessParameters; 30 35 } 31 36 } -
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/ServicePort.cs
r11454 r11468 24 24 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 25 25 using System; 26 using System.Threading; 27 using System.Threading.Tasks; 26 28 27 29 namespace HeuristicLab.Optimization.Networks { … … 40 42 } 41 43 42 public ServiceParameterCollection Call(ServiceParameterCollection parameters) { 43 return OnCalled(parameters); 44 public ServiceParameterCollection Process(ServiceParameterCollection parameters) { 45 return Process(parameters, new CancellationToken()); 46 } 47 public ServiceParameterCollection Process(ServiceParameterCollection parameters, CancellationToken token) { 48 return OnProcessParameters(parameters, token); 49 } 50 public async Task<ServiceParameterCollection> ProcessAsync(ServiceParameterCollection parameters) { 51 return await ProcessAsync(parameters, new CancellationToken()); 52 } 53 public async Task<ServiceParameterCollection> ProcessAsync(ServiceParameterCollection parameters, CancellationToken token) { 54 return await Task.Run(() => { return Process(parameters, token); }, token); 44 55 } 45 56 46 public event EventHandler<EventArgs<ServiceParameterCollection >> Called;47 protected virtual ServiceParameterCollection On Called(ServiceParameterCollection parameters) {48 var handler = Called;49 if (handler != null) handler(this, new EventArgs<ServiceParameterCollection >(parameters));57 public event EventHandler<EventArgs<ServiceParameterCollection, CancellationToken>> ProcessParameters; 58 protected virtual ServiceParameterCollection OnProcessParameters(ServiceParameterCollection parameters, CancellationToken token) { 59 var handler = ProcessParameters; 60 if (handler != null) handler(this, new EventArgs<ServiceParameterCollection, CancellationToken>(parameters, token)); 50 61 return parameters; 51 62 }
Note: See TracChangeset
for help on using the changeset viewer.