source: trunk/tools/ExternalEvaluation/CSharp/ExternalEvaluation.Service/PollService.cs @ 15014

Last change on this file since 15014 was 15014, checked in by pfleck, 4 years ago

Added code and tools for the ExternalEvaluationProblem. (e.g. Java-side evaluation)

File size: 2.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 System;
23
24namespace HeuristicLab.Problems.ExternalEvaluation.Service {
25  public class PollService : Service {
26    private BlockingQueue<SolutionMessage> messageQueue;
27
28    public PollService(IListenerFactory driverFactory)
29      : base(driverFactory) {
30      messageQueue = new BlockingQueue<SolutionMessage>(int.MaxValue);
31    }
32
33    public override void Stop() {
34      messageQueue.Close();
35      base.Stop();
36    }
37
38    public SolutionMessage GetSolution() {
39      try {
40        return messageQueue.Dequeue();
41      } catch (QueueClosedException) { }
42      return null;
43    }
44
45    public QualityMessage.Builder PrepareQualityMessage(SolutionMessage msg, double quality) {
46      QualityMessage.Builder qualityMessageBuilder = QualityMessage.CreateBuilder();
47      qualityMessageBuilder.SetSolutionId(msg.SolutionId)
48                           .SetQuality(quality);
49      return qualityMessageBuilder;
50    }
51
52    public void SendQualityMessage(SolutionMessage sMsg, QualityMessage qMsg) {
53      Send(qMsg, sMsg);
54    }
55
56    public void SendQuality(SolutionMessage msg, double quality) {
57      QualityMessage.Builder qualityMessageBuilder = QualityMessage.CreateBuilder();
58      qualityMessageBuilder.SetSolutionId(msg.SolutionId)
59                           .SetQuality(quality);
60      Send(qualityMessageBuilder.Build(), msg);
61    }
62
63    public event EventHandler SolutionProduced;
64
65    protected override void OnSolutionProduced(SolutionMessage msg, IChannel channel) {
66      base.OnSolutionProduced(msg, channel);
67      try {
68        messageQueue.Enqueue(msg);
69      } catch (QueueClosedException) {
70        return;
71      }
72      EventHandler handler = SolutionProduced;
73      if (handler != null) handler(this, EventArgs.Empty);
74    }
75  }
76}
Note: See TracBrowser for help on using the repository browser.