Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/18/14 03:25:58 (10 years ago)
Author:
swagner
Message:

#2205: Worked on optimization networks

Location:
branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/AlgorithmServiceNode.cs

    r11468 r11481  
    2525using System;
    2626using System.Collections.Generic;
     27using System.Linq;
    2728using System.Threading;
    28 using System.Linq;
    2929
    3030namespace HeuristicLab.Optimization.Networks {
     
    3434    private object locker = new object();
    3535    private Dictionary<IAlgorithm, AutoResetEvent> waitHandles = new Dictionary<IAlgorithm,AutoResetEvent>();
     36    private Dictionary<IAlgorithm, Exception> exceptions = new Dictionary<IAlgorithm, Exception>();
    3637
    3738    new public PortCollection Ports {
     
    118119      algorithm.Runs.Clear();
    119120      algorithm.Prepare();
     121      algorithm.ExceptionOccurred += Algorithm_ExceptionOccurred;
    120122      algorithm.Stopped += Algorithm_Stopped;
    121       algorithm.Start();
    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         }
    142       }
    143     }
    144 
     123
     124      using (token.Register(() => { algorithm.Stop(); })) {
     125        algorithm.Start();
     126        waitHandles[algorithm].WaitOne();
     127      }
     128
     129      lock (locker) {
     130        waitHandles[algorithm].Dispose();
     131        waitHandles.Remove(algorithm);
     132
     133        Exception ex = null;
     134        if (exceptions.TryGetValue(algorithm, out ex)) {
     135          exceptions.Remove(algorithm);
     136          throw ex;
     137        }
     138      }
     139
     140      // retrieve results
     141      var run = algorithm.Runs.First();
     142      foreach (var p in parameters.Where(x => x.Type == ServiceParameterType.Output)) {
     143        IItem result = null;
     144        if (run.Results.TryGetValue(p.Name, out result)) {
     145          p.Value = result;
     146        }
     147      }
     148
     149      lock (locker) {
     150        Runs.Add(run);
     151      }
     152    }
     153
     154    private void Algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
     155      var algorithm = (IAlgorithm)sender;
     156      lock (locker) {
     157        exceptions.Add(algorithm, e.Value);
     158      }
     159      algorithm.Stop();
     160    }
    145161    private void Algorithm_Stopped(object sender, EventArgs e) {
    146162      lock (locker) {
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/ClientNode.cs

    r11468 r11481  
    2323using HeuristicLab.Core;
    2424using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    25 using System;
    26 using System.Collections.Generic;
     25using System.Linq;
    2726using System.Threading;
    28 using System.Linq;
    2927using System.Threading.Tasks;
    3028
     
    6967
    7068    public virtual async Task CallServicesAsync() {
    71       await CallServicesAsync(new CancellationToken());
     69      await CallServicesAsync(CancellationToken.None);
    7270    }
    7371    public virtual async Task CallServicesAsync(CancellationToken token) {
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/ClientPort.cs

    r11468 r11481  
    107107      return true;
    108108    }
     109    public void CloneServicePortParameters() {
     110      Parameters.Clear();
     111      foreach (var p in servicePort.Parameters) {
     112        Parameters.Add((IServiceParameter)p.Clone());
     113      }
     114    }
    109115    public ServiceParameterCollection PrepareParameters() {
    110116      if (!Valid) throw new InvalidOperationException("Port configurations do not match");
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/IClientPort.cs

    r11468 r11481  
    3232
    3333    bool IsValidServicePort(IServicePort servicePort);
     34    void CloneServicePortParameters();
    3435    ServiceParameterCollection PrepareParameters();
    3536    ServiceParameterCollection CallService(ServiceParameterCollection parameters);
  • branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/ServicePort.cs

    r11468 r11481  
    4343
    4444    public ServiceParameterCollection Process(ServiceParameterCollection parameters) {
    45       return Process(parameters, new CancellationToken());
     45      return Process(parameters, CancellationToken.None);
    4646    }
    4747    public ServiceParameterCollection Process(ServiceParameterCollection parameters, CancellationToken token) {
     
    4949    }
    5050    public async Task<ServiceParameterCollection> ProcessAsync(ServiceParameterCollection parameters) {
    51       return await ProcessAsync(parameters, new CancellationToken());
     51      return await ProcessAsync(parameters, CancellationToken.None);
    5252    }
    5353    public async Task<ServiceParameterCollection> ProcessAsync(ServiceParameterCollection parameters, CancellationToken token) {
Note: See TracChangeset for help on using the changeset viewer.