Free cookie consent management tool by TermsFeed Policy Generator

source: branches/BubbleChart/HeuristicLab.Optimization.BubbleChart/3.3/RecursiveDataItem.cs @ 12417

Last change on this file since 12417 was 12379, checked in by pfleck, 10 years ago

#2379

  • Added first draft of the RecursiveDataItem structure.
  • Added a view for the data structure as well was a list of the data structure.
File size: 4.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections;
24using System.Collections.Generic;
25using System.Linq;
26using HeuristicLab.Collections;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Optimization.BubbleChart {
33  [Item("RecursiveDataItem", "")]
34  [StorableClass]
35  public class RecursiveDataItem : NamedItem, IEnumerable<RecursiveDataItem>, IEnumerable<KeyValuePair<string, IItem>> {
36    private readonly ObservableDictionary<string, IItem> data;
37    private readonly ObservableList<RecursiveDataItem> children;
38
39    public ObservableDictionary<string, IItem> Data { get { return data; } }
40    public ObservableList<RecursiveDataItem> Children { get { return children; } }
41
42    [StorableConstructor]
43    private RecursiveDataItem(bool deserializing) : base(deserializing) { }
44    private RecursiveDataItem(RecursiveDataItem original, Cloner cloner)
45      : base(original, cloner) {
46      data = new ObservableDictionary<string, IItem>(original.Data.ToDictionary(x => x.Key, x => cloner.Clone(x.Value)));
47      children = new ObservableList<RecursiveDataItem>(original.Children.Select(x => cloner.Clone(x)));
48    }
49    public RecursiveDataItem()
50      : base() {
51      data = new ObservableDictionary<string, IItem>();
52      children = new ObservableList<RecursiveDataItem>();
53    }
54    public RecursiveDataItem(string name)
55      : base(name) {
56      data = new ObservableDictionary<string, IItem>();
57      children = new ObservableList<RecursiveDataItem>();
58    }
59
60    public override IDeepCloneable Clone(Cloner cloner) {
61      return new RecursiveDataItem(this, cloner);
62    }
63
64    IEnumerator<KeyValuePair<string, IItem>> IEnumerable<KeyValuePair<String, IItem>>.GetEnumerator() {
65      return data.GetEnumerator();
66    }
67    IEnumerator<RecursiveDataItem> IEnumerable<RecursiveDataItem>.GetEnumerator() {
68      return children.GetEnumerator();
69    }
70    IEnumerator IEnumerable.GetEnumerator() {
71      throw new NotImplementedException();
72    }
73
74    public void Add(string key, IItem item) {
75      data.Add(key, item);
76    }
77    public void Add(RecursiveDataItem child) {
78      children.Add(child);
79    }
80
81  }
82
83  static class test {
84    static test() {
85      var run = new RecursiveDataItem("Run") {
86          {"CurrentBestQuality", new DoubleValue(100)},
87          {"Selector", new StringValue("TournamentSelector")},
88          new RecursiveDataItem("Layers") {
89            new RecursiveDataItem("Layer0") {
90               {"CurrentBestQuality", new DoubleValue(10)}
91            },
92            new RecursiveDataItem("Layer1") {
93               {"CurrentBestQuality", new DoubleValue(50)}
94            },
95            new RecursiveDataItem("Layer2") {
96               {"CurrentBestQuality", new DoubleValue(100)}
97            }
98          }
99      };
100      //var run = new RecursiveDataItem(
101      //  name: "Run",
102      //  data: new Dictionary<string, IItem> {
103      //    {"CurrentBestQuality", new DoubleValue(100)},
104      //    {"Selector", new StringValue("TournamentSelector")}, {
105      //      "Layers", new RecursiveDataItem("Layers") {
106      //        new RecursiveDataItem(
107      //          name: "Layer0",
108      //          data: new Dictionary<string, IItem> {
109      //            {"CurrentBestQuality", new DoubleValue(10)}
110      //          }
111      //        ),
112      //        new RecursiveDataItem(
113      //          name: "Layer1",
114      //          data: new Dictionary<string, IItem> {
115      //            {"CurrentBestQuality", new DoubleValue(50)}
116      //          }
117      //        ),
118      //        new RecursiveDataItem(
119      //          name: "Layer2",
120      //          data: new Dictionary<string, IItem> {
121      //            {"CurrentBestQuality", new DoubleValue(100)}
122      //          }
123      //        )
124      //      }
125      //    }
126      //  }
127      //);
128
129    }
130  }
131}
Note: See TracBrowser for help on using the repository browser.