[16985] | 1 | using System;
|
---|
| 2 | using System.IO;
|
---|
[16998] | 3 | using System.Threading;
|
---|
[16985] | 4 | using HeuristicLab.Common;
|
---|
| 5 | using HeuristicLab.Core;
|
---|
| 6 | using HeuristicLab.Optimization;
|
---|
| 7 | using HeuristicLab.PluginInfrastructure;
|
---|
| 8 |
|
---|
| 9 | namespace HeuristicLab.DynamicAssemblyTestApp {
|
---|
| 10 | [Serializable]
|
---|
[16998] | 11 | [Application("CLIOptimize", "")]
|
---|
[16985] | 12 | public class AppTest : ApplicationBase {
|
---|
| 13 |
|
---|
[16998] | 14 | #region Vars
|
---|
| 15 | [NonSerialized]
|
---|
| 16 | private IExecutable executable;
|
---|
| 17 | [NonSerialized]
|
---|
| 18 | private static AutoResetEvent autoResetEvent = new AutoResetEvent(false);
|
---|
| 19 | [NonSerialized]
|
---|
| 20 | private static object locker = new object();
|
---|
| 21 | #endregion
|
---|
| 22 |
|
---|
| 23 | #region Properties
|
---|
[16985] | 24 | public UniPath InputFilePath { get; set; } = null;
|
---|
| 25 | public UniPath OutputPath { get; set; } = null;
|
---|
[16998] | 26 | #endregion
|
---|
| 27 |
|
---|
| 28 | #region Constructors
|
---|
| 29 | public AppTest() { }
|
---|
| 30 | public AppTest(UniPath inputFilePath, UniPath outputPath) {
|
---|
| 31 | InputFilePath = inputFilePath;
|
---|
| 32 | OutputPath = outputPath;
|
---|
| 33 | }
|
---|
| 34 | #endregion
|
---|
| 35 |
|
---|
[16985] | 36 | public override void Run(ICommandLineArgument[] args) {
|
---|
[16998] | 37 | Init();
|
---|
| 38 | lock (locker) {
|
---|
| 39 | executable.Stopped += Executable_Stopped;
|
---|
| 40 | executable.StartAsync();
|
---|
| 41 | }
|
---|
| 42 | autoResetEvent.WaitOne();
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | public override void OnCancel() {
|
---|
| 46 | lock (locker) {
|
---|
| 47 | base.OnCancel();
|
---|
| 48 | if (executable != null)
|
---|
| 49 | executable.Stop();
|
---|
| 50 | }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | public override void OnPause() {
|
---|
| 54 | base.OnPause();
|
---|
| 55 | lock (locker) {
|
---|
| 56 | if (executable != null) executable.Pause();
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | public override void OnResume() {
|
---|
| 61 | base.OnResume();
|
---|
| 62 | lock (locker) {
|
---|
| 63 | if (executable != null)
|
---|
| 64 | executable.StartAsync();
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | #region Helper
|
---|
| 69 | private void Init() {
|
---|
| 70 | lock (locker) {
|
---|
| 71 | ContentManager.Initialize(new PersistenceContentManager());
|
---|
| 72 | IStorableContent content = ContentManager.Load(InputFilePath.ToString());
|
---|
| 73 | executable = content as IExecutable;
|
---|
| 74 | if (executable == null)
|
---|
| 75 | throw new NotSupportedException("The given file does not contain any algorithm to start.");
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | private void Executable_Stopped(object sender, EventArgs e) {
|
---|
| 80 | lock (locker) {
|
---|
| 81 | IOptimizer optimizer = executable as IOptimizer;
|
---|
| 82 | if (optimizer != null) PrintResults(optimizer);
|
---|
| 83 | ContentManager.Save((IStorableContent)executable, OutputPath.ToString() + Path.DirectorySeparatorChar + "result.hl", true);
|
---|
| 84 | }
|
---|
| 85 | autoResetEvent.Set();
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | private void PrintResults(IOptimizer optimizer) {
|
---|
| 89 | lock (locker) {
|
---|
| 90 | Console.WriteLine("\nRESULT(S):");
|
---|
| 91 | int i = 1;
|
---|
| 92 | foreach (var run in optimizer.Runs) {
|
---|
| 93 | Console.WriteLine($"{"-------------------------------- RUN",35} {$"{i++:D3}" + " --------------------------------",-35}");
|
---|
| 94 | foreach (var res in run.Results) {
|
---|
| 95 | Console.WriteLine($"{res.Key,35} : {res.Value,-35}");
|
---|
[16985] | 96 | }
|
---|
| 97 | }
|
---|
[16998] | 98 | }
|
---|
[16985] | 99 | }
|
---|
[16998] | 100 | #endregion
|
---|
[16985] | 101 | }
|
---|
| 102 | }
|
---|