1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using HeuristicLab.Core;
|
---|
4 | using HEAL.Attic;
|
---|
5 | using HeuristicLab.Common;
|
---|
6 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
7 |
|
---|
8 | namespace 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 | }
|
---|