Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/StructureTemplate.cs @ 18067

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

#3136

  • changed the StructureTemplateView -> nodes of type SubFunctionTreeNode are now clickable
    • still need to overhaul the UI elements
  • added a way to parse SubFunctionTreeNode with a unique name in InfixExpressionParser
File size: 2.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using HeuristicLab.Core;
4using HEAL.Attic;
5using HeuristicLab.Common;
6using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
7
8namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
9  [StorableType("E3C038DB-C6AA-457D-9F65-AF16C44CCE22")]
10  [Item("StructureTemplate", "Structure Template")]
11  public class StructureTemplate : Item {
12
13    #region Properties
14    [Storable]
15    private string template = "";
16    public string Template {
17      get => template;
18      set {
19        if(template != value) {
20          template = value;
21          tree = Parser.Parse(template);
22          GetSubFunctions(Tree);
23          OnChanged();
24        }
25      }
26    }
27
28    [Storable]
29    private ISymbolicExpressionTree tree;
30    public ISymbolicExpressionTree Tree => tree;
31
32    [Storable]
33    public IDictionary<SubFunctionTreeNode, SubFunction> SubFunctions { get; private set; } = new Dictionary<SubFunctionTreeNode, SubFunction>();
34
35    protected InfixExpressionParser Parser { get; set; } = new InfixExpressionParser();
36    #endregion
37
38    #region Events
39    public event EventHandler Changed;
40   
41    private void OnChanged() => Changed?.Invoke(this, EventArgs.Empty);
42    #endregion
43
44    #region Constructors
45    public StructureTemplate() { }
46
47    [StorableConstructor]
48    protected StructureTemplate(StorableConstructorFlag _) : base(_) { }
49
50    protected StructureTemplate(StructureTemplate original, Cloner cloner) { }
51    #endregion
52
53    #region Cloning
54    public override IDeepCloneable Clone(Cloner cloner) =>
55      new StructureTemplate(this, cloner);
56    #endregion
57
58    private void GetSubFunctions(ISymbolicExpressionTree tree) {
59      int count = 1;
60      SubFunctions.Clear();
61      foreach (var node in tree.IterateNodesPrefix())
62        if (node is SubFunctionTreeNode subFunctionTreeNode) {
63          var subFunction = new SubFunction() {
64            Name = $"f{count++}({string.Join(",", subFunctionTreeNode.Arguments)})",
65            FunctionArguments = subFunctionTreeNode.Arguments
66          };
67          subFunctionTreeNode.SubFunction = subFunction;
68          SubFunctions.Add(subFunctionTreeNode, subFunction);
69        }
70    }
71  }
72}
Note: See TracBrowser for help on using the repository browser.