Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2924_DotNetCoreMigration/HeuristicLab.DynamicAssemblyTestApp/AppTest.cs @ 16998

Last change on this file since 16998 was 16998, checked in by dpiringe, 5 years ago

#2924:

  • in project HeuristicLab.CommandLineInterface changed output for options -> hidden options are not shown in the help box anymore
  • in project HeuristicLab.DynamicAssemblyTestApp:
    • added ApplicationAttributes
    • added full cancel/pause/resume support for class AppTest to test the same behaviour between main and child process
  • in project HeuristicLab:
    • changed auto generated Dockerfile -> only copies necessary projects -> speeds up the build process
    • in file HeuristicLab-3.3.csproj:
      • changed DockerDefaultTargetOS to Linux
      • removed reference HeuristicLab.DefinitionLanguage
      • added icon and manifest
    • deleted folder Properties with file launchSettings.json
    • changed the direct access to ApplicationTypes for OptimizeCommand and InspectCommand to new method IApplicationManager.GetInstances<T>(params object[] args)
  • added build script for docker image dockerImageBuild.ps1
File size: 2.9 KB
RevLine 
[16985]1using System;
2using System.IO;
[16998]3using System.Threading;
[16985]4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Optimization;
7using HeuristicLab.PluginInfrastructure;
8
9namespace 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}
Note: See TracBrowser for help on using the repository browser.