Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/16/14 03:57:49 (10 years ago)
Author:
swagner
Message:

#2205: Worked on optimization networks

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  
    9393    }
    9494
    95     protected virtual void Execute(ServiceParameterCollection parameters) {
     95    protected virtual void Execute(ServiceParameterCollection parameters, CancellationToken token) {
    9696      if (Template == null) throw new InvalidOperationException("Template is null");
    9797
     
    120120      algorithm.Stopped += Algorithm_Stopped;
    121121      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        }
    136142      }
    137143    }
     
    191197      IServicePort s = port as IServicePort;
    192198      if (s != null) {
    193         s.Called += ServicePort_Called;
     199        s.ProcessParameters += ServicePort_ProcessParameters;
    194200      }
    195201    }
     
    203209      IServicePort s = port as IServicePort;
    204210      if (s != null) {
    205         s.Called -= ServicePort_Called;
     211        s.ProcessParameters -= ServicePort_ProcessParameters;
    206212      }
    207213    }
     
    212218      UpdateParameter((IInputPort)sender);
    213219    }
    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);
    216222    }
    217223    #endregion
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/ClientNode.cs

    r11465 r11468  
    6868    }
    6969
    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();
    7276
    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        }
    7584
    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();
    8294          }
    8395
    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);
    9797        }
    98       });
     98      }
    9999    }
    100100  }
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/ClientPort.cs

    r11454 r11468  
    2626using System.Drawing;
    2727using System.Linq;
     28using System.Threading;
     29using System.Threading.Tasks;
    2830
    2931namespace HeuristicLab.Optimization.Networks {
     
    127129      return parameters;
    128130    }
    129     public ServiceParameterCollection Call(ServiceParameterCollection parameters) {
     131    public ServiceParameterCollection CallService(ServiceParameterCollection parameters) {
    130132      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);
    132146    }
    133147
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/HeuristicLab.Optimization.Networks-3.3.csproj

    r11465 r11468  
    1010    <RootNamespace>HeuristicLab.Optimization.Networks</RootNamespace>
    1111    <AssemblyName>HeuristicLab.Optimization.Networks-3.3</AssemblyName>
    12     <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
     12    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
    1313    <FileAlignment>512</FileAlignment>
    1414    <TargetFrameworkProfile />
     
    2222    <ErrorReport>prompt</ErrorReport>
    2323    <WarningLevel>4</WarningLevel>
     24    <Prefer32Bit>false</Prefer32Bit>
    2425  </PropertyGroup>
    2526  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
     
    3031    <ErrorReport>prompt</ErrorReport>
    3132    <WarningLevel>4</WarningLevel>
     33    <Prefer32Bit>false</Prefer32Bit>
    3234  </PropertyGroup>
    3335  <PropertyGroup>
     
    4547    <ErrorReport>prompt</ErrorReport>
    4648    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
     49    <Prefer32Bit>false</Prefer32Bit>
    4750  </PropertyGroup>
    4851  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
     
    5457    <ErrorReport>prompt</ErrorReport>
    5558    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
     59    <Prefer32Bit>false</Prefer32Bit>
    5660  </PropertyGroup>
    5761  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
     
    6367    <ErrorReport>prompt</ErrorReport>
    6468    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
     69    <Prefer32Bit>false</Prefer32Bit>
    6570  </PropertyGroup>
    6671  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
     
    7277    <ErrorReport>prompt</ErrorReport>
    7378    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
     79    <Prefer32Bit>false</Prefer32Bit>
    7480  </PropertyGroup>
    7581  <ItemGroup>
     
    129135  </ItemGroup>
    130136  <ItemGroup>
     137    <None Include="ClassDiagram.cd" />
    131138    <None Include="HeuristicLab.snk" />
    132139    <None Include="Plugin.cs.frame" />
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/IClientNode.cs

    r11465 r11468  
    2222using HeuristicLab.Core;
    2323using System;
     24using System.Threading;
     25using System.Threading.Tasks;
    2426
    2527namespace HeuristicLab.Optimization.Networks {
     
    2830    IItemCollection<ServiceParameterCollection> Calls { get; }
    2931
    30     void CallAsync();
     32    Task CallServicesAsync();
     33    Task CallServicesAsync(CancellationToken token);
    3134  }
    3235}
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/IClientPort.cs

    r11454 r11468  
    2121
    2222using System;
     23using System.Threading;
     24using System.Threading.Tasks;
    2325
    2426namespace HeuristicLab.Optimization.Networks {
     
    3133    bool IsValidServicePort(IServicePort servicePort);
    3234    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);
    3439  }
    3540}
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/IServicePort.cs

    r11454 r11468  
    2323using HeuristicLab.Core;
    2424using System;
     25using System.Threading;
     26using System.Threading.Tasks;
    2527
    2628namespace HeuristicLab.Optimization.Networks {
    2729  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;
    3035  }
    3136}
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/ServicePort.cs

    r11454 r11468  
    2424using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2525using System;
     26using System.Threading;
     27using System.Threading.Tasks;
    2628
    2729namespace HeuristicLab.Optimization.Networks {
     
    4042    }
    4143
    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);
    4455    }
    4556
    46     public event EventHandler<EventArgs<ServiceParameterCollection>> Called;
    47     protected virtual ServiceParameterCollection OnCalled(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));
    5061      return parameters;
    5162    }
Note: See TracChangeset for help on using the changeset viewer.