Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.JsonInterface.OptimizerIntegration/Views/ExportJsonDialog.cs @ 18046

Last change on this file since 18046 was 18046, checked in by dpiringe, 3 years ago

#3026

  • added a first version of a new feature called ResultCollectionPostProcessors, which should transform/process the results of an optimizer run
    • created a new interface IResultCollectionPostProcessor
    • created a new class SymRegPythonPostProcessor, which formats all ISymbolicRegressionSolution with the usage of the SymbolicDataAnalysisExpressionPythonFormatter
    • changed the generation and instantiation of templates to handle this new feature
    • the template files contains a new area PostProcessors
    • added functionality in Runner to use these new type of result processing
    • added a new tab in ExportJsonDialog to configure PostProcessors
File size: 6.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Drawing;
5using System.IO;
6using System.Linq;
7using System.Windows.Forms;
8using HeuristicLab.Common;
9using HeuristicLab.Core;
10using HeuristicLab.Optimization;
11using HeuristicLab.PluginInfrastructure;
12
13namespace HeuristicLab.JsonInterface.OptimizerIntegration {
14  public partial class ExportJsonDialog : Form {
15
16    #region Private Properties
17    private static FolderBrowserDialog FolderBrowserDialog { get; set; }
18    private IDictionary<TreeNode, UserControl> Node2Control { get; set; } = new Dictionary<TreeNode, UserControl>();
19    private IDictionary<TreeNode, IJsonItemVM> Node2VM { get; set; } = new Dictionary<TreeNode, IJsonItemVM>();
20    private IDictionary<Type, Type> JI2VM { get; set; }
21    private IJsonItem Root { get; set; }
22    private IOptimizer Optimizer { get; set; }
23    private IList<IJsonItemVM> VMs { get; set; }
24    private ICheckedItemList<IResultCollectionPostProcessor> PostProcessors { get; set; }
25    #endregion
26
27    private IContent content;
28    public IContent Content {
29      get => content;
30      set {
31        content = value;
32        //CheckedItemListView
33        #region Clear
34        VMs = new List<IJsonItemVM>();
35        treeView.Nodes.Clear();
36        treeViewResults.Nodes.Clear();
37        #endregion
38        Optimizer = content as IOptimizer;
39        if(Optimizer != null) {
40          Optimizer = (IOptimizer)Optimizer.Clone(); // clone the optimizer
41          Root = JsonItemConverter.Extract(Optimizer);
42          TreeNode parent = new TreeNode(Root.Name);
43          treeView.AfterCheck += TreeView_AfterCheck;
44          BuildTreeNode(parent, Root);
45          treeView.Nodes.Add(parent);
46          treeView.ExpandAll();
47          panelParameterDetails.Controls.Clear();
48          panelResultDetails.Controls.Clear();
49        }
50      }
51    }
52
53    private void InitCache() {
54      JI2VM = new Dictionary<Type, Type>();
55      foreach (var vmType in ApplicationManager.Manager.GetTypes(typeof(IJsonItemVM))) {
56        IJsonItemVM vm = (IJsonItemVM)Activator.CreateInstance(vmType);
57        JI2VM.Add(vm.TargetedJsonItemType, vmType);
58      }
59    }
60
61    public ExportJsonDialog() {
62      InitializeComponent();
63      this.PostProcessors = this.postProcessorListView.Content;
64      this.Icon = HeuristicLab.Common.Resources.HeuristicLab.Icon;
65      InitCache();
66    }
67
68    private void exportButton_Click(object sender, EventArgs e) {
69      if (FolderBrowserDialog == null) {
70        FolderBrowserDialog = new FolderBrowserDialog();
71        FolderBrowserDialog.Description = "Select .json-Template Directory";
72      }
73
74      if (FolderBrowserDialog.ShowDialog() == DialogResult.OK) {
75        try {
76          //foreach(var x in PostProcessors.CheckedItems)
77           
78          JsonTemplateGenerator.GenerateTemplate(
79            Path.Combine(FolderBrowserDialog.SelectedPath, textBoxTemplateName.Text),
80            Optimizer, Root, PostProcessors.CheckedItems.Select(x => x.Value));
81          Close();
82        } catch (Exception ex) {
83          ErrorHandling.ShowErrorDialog(this, ex);
84        }
85      }
86    }
87
88    private void BuildTreeNode(TreeNode node, IJsonItem item) {
89      RegisterItem(node, item, treeView);
90      if (item.Children != null) {
91        foreach (var c in item.Children) {
92          if (IsDrawableItem(c)) {
93            TreeNode childNode = new TreeNode(c.Name);
94            if (c is IResultJsonItem) {
95              treeViewResults.Nodes.Add(childNode);
96              IJsonItemVM vm = RegisterItem(childNode, c, treeViewResults);
97              if (vm != null) vm.Selected = true;             
98            } else {
99              node.Nodes.Add(childNode);
100              BuildTreeNode(childNode, c);
101            }
102          }
103        }
104      }
105    }
106
107    private IJsonItemVM RegisterItem(TreeNode node, IJsonItem item, TreeView tv) {
108      if (JI2VM.TryGetValue(item.GetType(), out Type vmType)) {
109        IJsonItemVM vm = (IJsonItemVM)Activator.CreateInstance(vmType);
110
111        vm.Item = item;
112        vm.TreeNode = node;
113        vm.TreeView = tv;
114        vm.Selected = false;
115
116        VMs.Add(vm);
117        Node2VM.Add(node, vm);
118        UserControl control = JsonItemBaseControl.Create(vm, vm.Control);
119        Node2Control.Add(node, control);
120        return vm;
121      } else {
122        node.ForeColor = Color.LightGray;
123        node.NodeFont = new Font(SystemFonts.DialogFont, FontStyle.Italic);
124      }
125      return null;
126    }
127
128    private bool IsDrawableItem(IJsonItem item) {
129      bool b = false;
130      if (item.Children != null) {
131        foreach (var c in item.Children) {
132          b = b || IsDrawableItem(c);
133        }
134      }
135     
136      return b || !(item is EmptyJsonItem) || !(item is UnsupportedJsonItem);
137    }
138   
139    private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
140      if(Node2Control.TryGetValue(treeView.SelectedNode, out UserControl control)) {
141        SetControlOnPanel(control, panelParameterDetails);
142      }
143    }
144
145    private void treeViewResults_AfterSelect(object sender, TreeViewEventArgs e) {
146      if (Node2Control.TryGetValue(treeViewResults.SelectedNode, out UserControl control)) {
147        SetControlOnPanel(control, panelResultDetails);
148      }
149    }
150
151    private void textBoxTemplateName_Validating(object sender, CancelEventArgs e) {
152      if (string.IsNullOrWhiteSpace(textBoxTemplateName.Text)) {
153        errorProvider.SetError(textBoxTemplateName, "Template name must not be empty.");
154        e.Cancel = true;
155      } else {
156        errorProvider.SetError(textBoxTemplateName, null);
157      }
158    }
159
160    private void SetControlOnPanel(UserControl control, Panel panel) {
161      panel.Controls.Clear();
162      if (control != null) {
163        panel.Controls.Add(control);
164        control.Width = panel.Width;
165        control.Height = panel.Height;
166        control.Dock = DockStyle.Fill;
167        control.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
168      }
169      panel.Refresh();
170    }
171
172    private void TreeView_AfterCheck(object sender, TreeViewEventArgs e) {
173      if (e.Action != TreeViewAction.Unknown) {
174        if (Node2VM.TryGetValue(e.Node, out IJsonItemVM vm)) {
175          vm.Selected = e.Node.Checked;
176        }
177      }
178    }
179
180    private void treeViewResults_AfterCheck(object sender, TreeViewEventArgs e) {
181      if (e.Action != TreeViewAction.Unknown) {
182        if (Node2VM.TryGetValue(e.Node, out IJsonItemVM vm)) {
183          vm.Selected = e.Node.Checked;
184        }
185      }
186    }
187  }
188}
Note: See TracBrowser for help on using the repository browser.