using System; using System.IO; using System.Threading; using HEAL.Attic; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Optimization; using HeuristicLab.PluginInfrastructure; namespace HeuristicLab.DynamicAssemblyTestApp { [StorableType("33B32897-258B-4E29-8E2E-0A9A8AE67B1D")] [Application("CLIOptimize", "")] public class ConsoleOptimizer : ApplicationBase { #region Vars private IExecutable executable; private readonly ManualResetEvent stoppedSignal = new ManualResetEvent(false); private readonly ManualResetEvent startedSignal = new ManualResetEvent(false); private bool isFinished = false; private static object locker = new object(); #endregion #region Properties [Storable] public UniPath InputFilePath { get; set; } = null; [Storable] public UniPath OutputPath { get; set; } = null; #endregion #region Constructors public ConsoleOptimizer() { } public ConsoleOptimizer(UniPath inputFilePath, UniPath outputPath) { InputFilePath = inputFilePath; OutputPath = outputPath; } #endregion public override void Run(ICommandLineArgument[] args) { lock (locker) { Init(); executable.Started += Executable_Started; executable.Stopped += Executable_Stopped; startedSignal.Reset(); executable.StartAsync(); startedSignal.WaitOne(); } stoppedSignal.WaitOne(); startedSignal.Close(); stoppedSignal.Close(); } private void Executable_Started(object sender, EventArgs e) { startedSignal.Set(); } public override void OnCancel() { base.OnCancel(); startedSignal.WaitOne(); lock (locker) { if (isFinished) throw new InvalidOperationException("Executable has already finished!"); executable.Stop(); } } public override void OnPause() { base.OnPause(); startedSignal.WaitOne(); lock (locker) { if (isFinished) throw new InvalidOperationException("Executable has already finished!"); executable.Pause(); } } public override void OnResume() { base.OnResume(); startedSignal.WaitOne(); lock (locker) { if (isFinished) throw new InvalidOperationException("Executable has already finished!"); startedSignal.Reset(); executable.StartAsync(); startedSignal.WaitOne(); } } #region Helper private void Init() { lock (locker) { ContentManager.Initialize(new PersistenceContentManager()); IStorableContent content = ContentManager.Load(InputFilePath.ToString()); executable = content as IExecutable; if (executable == null) throw new NotSupportedException("The given file does not contain any algorithm to start."); } } private void Executable_Stopped(object sender, EventArgs e) { lock (locker) { isFinished = true; IOptimizer optimizer = executable as IOptimizer; if (optimizer != null) PrintResults(optimizer); ContentManager.Save((IStorableContent)executable, OutputPath.ToString() + Path.DirectorySeparatorChar + "result.hl", true); } stoppedSignal.Set(); } private void PrintResults(IOptimizer optimizer) { lock (locker) { Console.WriteLine("\nRESULT(S):"); int i = 1; foreach (var run in optimizer.Runs) { Console.WriteLine($"{"-------------------------------- RUN",35} {$"{i++:D3}" + " --------------------------------",-35}"); foreach (var res in run.Results) { Console.WriteLine($"{res.Key,35} : {res.Value,-35}"); } } } } #endregion } }