Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/27/10 20:27:41 (14 years ago)
Author:
abeham
Message:

#866

  • Worked on view for ExternalEvaluationProcessDriver

#1019

  • Added missing events in SingleObjectiveTestFunctionProblem
Location:
trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/Drivers/ExternalEvaluationDriver.cs

    r3864 r3865  
    3131    public bool IsInitialized { get; protected set; }
    3232
     33    protected ExternalEvaluationDriver()
     34      : base() {
     35      name = ItemName;
     36      description = ItemDescription;
     37    }
     38
    3339    #region IExternalEvaluationDriver Members
    3440
  • trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/Drivers/ExternalEvaluationProcessDriver.cs

    r3864 r3865  
    2323using System.Diagnostics;
    2424using System.IO;
     25using HeuristicLab.Common;
    2526using HeuristicLab.Core;
    2627using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    3031  [StorableClass]
    3132  public class ExternalEvaluationProcessDriver : ExternalEvaluationDriver {
     33    public override bool CanChangeName { get { return false; } }
     34    public override bool CanChangeDescription { get { return false; } }
     35
    3236    private Process process;
    3337    [Storable]
    3438    private string executable;
     39    public string Executable {
     40      get { return executable; }
     41      set {
     42        if (IsInitialized) throw new InvalidOperationException("ExternalEvaluationProcessDriver cannot change the executable path as it has already been started.");
     43        string oldExecutable = executable;
     44        executable = value;
     45        if (!oldExecutable.Equals(executable)) OnExecutableChanged();
     46      }
     47    }
    3548    [Storable]
    3649    private string arguments;
     50    public string Arguments {
     51      get { return arguments; }
     52      set {
     53        if (IsInitialized) throw new InvalidOperationException("ExternalEvaluationProcessDriver cannot change the arguments as it has already been started.");
     54        string oldArguments = arguments;
     55        arguments = value;
     56        if (!oldArguments.Equals(arguments)) OnArgumentsChanged();
     57      }
     58    }
    3759    private ExternalEvaluationStreamDriver driver;
    3860
     
    4466    }
    4567
     68    public override IDeepCloneable Clone(Cloner cloner) {
     69      ExternalEvaluationProcessDriver clone = (ExternalEvaluationProcessDriver)base.Clone(cloner);
     70      clone.executable = executable;
     71      clone.arguments = arguments;
     72      return clone;
     73    }
     74
    4675    #region IExternalDriver Members
    4776
    4877    public override void Start() {
    49       base.Start();
    50       process = new Process();
    51       process.StartInfo = new ProcessStartInfo(executable, arguments);
    52       process.StartInfo.UseShellExecute = false;
    53       process.StartInfo.RedirectStandardInput = true;
    54       process.StartInfo.RedirectStandardOutput = true;
    55       process.Start();
    56       Stream processStdOut = process.StandardOutput.BaseStream;
    57       Stream processStdIn = process.StandardInput.BaseStream;
    58       driver = new ExternalEvaluationStreamDriver(processStdOut, processStdIn);
    59       driver.Start();
     78      if (!String.IsNullOrEmpty(executable.Trim())) {
     79        base.Start();
     80        process = new Process();
     81        process.StartInfo = new ProcessStartInfo(executable, arguments);
     82        process.StartInfo.UseShellExecute = false;
     83        process.StartInfo.RedirectStandardInput = true;
     84        process.StartInfo.RedirectStandardOutput = true;
     85        process.EnableRaisingEvents = true; // required to be notified of exit
     86        process.Start();
     87        Stream processStdOut = process.StandardOutput.BaseStream;
     88        Stream processStdIn = process.StandardInput.BaseStream;
     89        OnProcessStarted();
     90        process.Exited += new EventHandler(process_Exited);
     91        driver = new ExternalEvaluationStreamDriver(processStdOut, processStdIn);
     92        driver.Start();
     93      } else throw new InvalidOperationException("Cannot start ExternalEvaluationProcessDriver because executable is not defined.");
    6094    }
    6195
     
    70104    public override void Stop() {
    71105      base.Stop();
    72       if (!process.HasExited) {
    73         driver.Stop();
     106      if (process != null) {
    74107        if (!process.HasExited) {
    75           process.CloseMainWindow();
    76           process.WaitForExit(1000);
    77           process.Close();
     108          driver.Stop();
     109          if (!process.HasExited) {
     110            process.CloseMainWindow();
     111            process.WaitForExit(1000);
     112            process.Close();
     113            // for some reasons the event process_Exited does not fire
     114            OnProcessExited();
     115          }
    78116        }
     117        process = null;
    79118      }
    80       process = null;
    81119    }
    82120
    83121    #endregion
     122
     123    #region Event handlers (process)
     124    private void process_Exited(object sender, EventArgs e) {
     125      if (IsInitialized) {
     126        if (driver.IsInitialized) driver.Stop();
     127        IsInitialized = false;
     128        process = null;
     129      }
     130      OnProcessExited();
     131    }
     132    #endregion
     133
     134    #region Events
     135    public event EventHandler ExecutableChanged;
     136    protected void OnExecutableChanged() {
     137      EventHandler handler = ExecutableChanged;
     138      if (handler != null) handler(this, EventArgs.Empty);
     139    }
     140
     141    public event EventHandler ArgumentsChanged;
     142    protected void OnArgumentsChanged() {
     143      EventHandler handler = ArgumentsChanged;
     144      if (handler != null) handler(this, EventArgs.Empty);
     145    }
     146
     147    public event EventHandler ProcessStarted;
     148    private void OnProcessStarted() {
     149      EventHandler handler = ProcessStarted;
     150      if (handler != null) handler(this, EventArgs.Empty);
     151    }
     152
     153    public event EventHandler ProcessExited;
     154    private void OnProcessExited() {
     155      EventHandler handler = ProcessExited;
     156      if (handler != null) handler(this, EventArgs.Empty);
     157    }
     158    #endregion
    84159  }
    85160}
  • trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/Drivers/ExternalEvaluationStreamDriver.cs

    r3862 r3865  
    3030  [StorableClass]
    3131  public class ExternalEvaluationStreamDriver : ExternalEvaluationDriver {
     32    public override bool CanChangeName { get { return false; } }
     33    public override bool CanChangeDescription { get { return false; } }
     34
    3235    private CodedInputStream inputStream;
    3336    private Stream input;
  • trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/Properties/AssemblyInfo.frame

    r3862 r3865  
    77// associated with an assembly.
    88[assembly: AssemblyTitle("HeuristicLab.Problems.ExternalEvaluation")]
    9 [assembly: AssemblyDescription("Plugins for Problems that are evaluated outside of HeuristicLab.")]
     9[assembly: AssemblyDescription("Plugin for Problems that are evaluated outside of HeuristicLab.")]
    1010[assembly: AssemblyConfiguration("")]
    1111[assembly: AssemblyCompany("")]
Note: See TracChangeset for help on using the changeset viewer.