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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using System.Diagnostics;
|
---|
26 | using System.IO;
|
---|
27 | using Google.ProtocolBuffers;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Problems.ExternalEvaluation {
|
---|
32 | [Item("ExternalEvaluationProcessDriver", "A driver for external evaluation problems that launches the external application in a new process.")]
|
---|
33 | [StorableClass]
|
---|
34 | public class LocalProcessDriver : ExternalEvaluationDriver {
|
---|
35 | private Process process;
|
---|
36 | [Storable]
|
---|
37 | private string executable;
|
---|
38 | [Storable]
|
---|
39 | private string arguments;
|
---|
40 | private ExternalEvaluationStreamDriver driver;
|
---|
41 |
|
---|
42 | public LocalProcessDriver(string executable, string arguments)
|
---|
43 | : base() {
|
---|
44 | this.executable = executable;
|
---|
45 | this.arguments = arguments;
|
---|
46 | }
|
---|
47 |
|
---|
48 | #region IExternalDriver Members
|
---|
49 |
|
---|
50 | public override void Start() {
|
---|
51 | base.Start();
|
---|
52 | process = new Process();
|
---|
53 | process.StartInfo = new ProcessStartInfo(executable, arguments);
|
---|
54 | process.StartInfo.UseShellExecute = false;
|
---|
55 | process.StartInfo.RedirectStandardInput = true;
|
---|
56 | process.StartInfo.RedirectStandardOutput = true;
|
---|
57 | process.Start();
|
---|
58 | Stream processStdOut = process.StandardOutput.BaseStream;
|
---|
59 | Stream processStdIn = process.StandardInput.BaseStream;
|
---|
60 | driver = new ExternalEvaluationStreamDriver(processStdOut, processStdIn);
|
---|
61 | driver.Start();
|
---|
62 | }
|
---|
63 |
|
---|
64 | public override QualityMessage Evaluate(SolutionMessage solution) {
|
---|
65 | return driver.Evaluate(solution);
|
---|
66 | }
|
---|
67 |
|
---|
68 | public override void EvaluateAsync(SolutionMessage solution, Action<QualityMessage> callback) {
|
---|
69 | driver.EvaluateAsync(solution, callback);
|
---|
70 | }
|
---|
71 |
|
---|
72 | public override void Stop() {
|
---|
73 | base.Stop();
|
---|
74 | if (!process.HasExited) {
|
---|
75 | driver.Stop();
|
---|
76 | if (!process.HasExited) {
|
---|
77 | process.CloseMainWindow();
|
---|
78 | process.WaitForExit(1000);
|
---|
79 | process.Close();
|
---|
80 | }
|
---|
81 | }
|
---|
82 | process = null;
|
---|
83 | }
|
---|
84 |
|
---|
85 | #endregion
|
---|
86 | }
|
---|
87 | }
|
---|