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

Last change on this file since 18068 was 18068, checked in by dpiringe, 9 months ago

#3136

  • modified the StructureTemplateView to enable colorful tree nodes of type SubFunctionTreeNode
  • refactored SubFunctionTreeNode, SubFunction and StructureTemplate
File size: 2.9 KB
Line 
1using System;
2using System.Linq;
3using System.Collections.Generic;
4using HeuristicLab.Core;
5using HEAL.Attic;
6using HeuristicLab.Common;
7using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
8
9namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
10  [StorableType("E3C038DB-C6AA-457D-9F65-AF16C44CCE22")]
11  [Item("StructureTemplate", "Structure Template")]
12  public class StructureTemplate : Item {
13
14    #region Properties
15    [Storable]
16    private string template = "";
17    public string Template {
18      get => template;
19      set {
20        template = value;
21        tree = Parser.Parse(template);
22        GetSubFunctions(Tree);
23        OnChanged();
24      }
25    }
26
27    [Storable]
28    private ISymbolicExpressionTree tree;
29    public ISymbolicExpressionTree Tree => tree;
30
31    [Storable]
32    public IReadOnlyDictionary<string, SubFunction> SubFunctions { get; private set; } = new Dictionary<string, SubFunction>();
33
34    protected InfixExpressionParser Parser { get; set; } = new InfixExpressionParser();
35
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      var subFunctions = new Dictionary<string, SubFunction>();
60      foreach (var node in tree.IterateNodesPrefix())
61        if (node is SubFunctionTreeNode subFunctionTreeNode) {
62          if (!subFunctionTreeNode.Arguments.Any())
63            throw new ArgumentException($"The sub-function '{subFunctionTreeNode}' requires inputs (e.g. {subFunctionTreeNode.Name}(var1, var2)).");
64
65          if (subFunctions.TryGetValue(subFunctionTreeNode.Name, out SubFunction v)) {
66            if(!v.Arguments.SequenceEqual(subFunctionTreeNode.Arguments))
67              throw new ArgumentException(
68                $"The sub-function '{v.Name}' has (at least two) different signatures " +
69                $"({v.Name}({string.Join(",", v.Arguments)}) <> {subFunctionTreeNode.Name}({string.Join(",", subFunctionTreeNode.Arguments)})).");
70          } else {
71            var subFunction = new SubFunction() {
72              Name = subFunctionTreeNode.Name,
73              Arguments = subFunctionTreeNode.Arguments
74            };
75            subFunction.Changed += OnSubFunctionChanged;
76            subFunctions.Add(subFunction.Name, subFunction);
77          }
78        }
79      SubFunctions = subFunctions;
80    }
81
82    private void OnSubFunctionChanged(object sender, EventArgs e) => OnChanged();
83  }
84}
Note: See TracBrowser for help on using the repository browser.