Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/GroupSymbol.cs @ 6377

Last change on this file since 6377 was 6377, checked in by mkommend, 13 years ago

#1479: Merged trunk changes into branch.

File size: 2.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Collections;
25using HeuristicLab.Common;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
29  public sealed class GroupSymbol : Symbol {
30    private ObservableSet<ISymbol> symbols;
31    public IObservableSet<ISymbol> SymbolsCollection {
32      get { return symbols; }
33    }
34    [Storable]
35    public IEnumerable<ISymbol> Symbols {
36      get { return symbols; }
37    }
38
39    public override bool Enabled {
40      get { return base.Enabled; }
41      set {
42        if (value != Enabled) {
43          base.Enabled = value;
44          foreach (ISymbol symbol in symbols)
45            symbol.Enabled = value;
46        }
47      }
48    }
49
50    [StorableConstructor]
51    private GroupSymbol(bool deserializing) : base(deserializing) { }
52    private GroupSymbol(GroupSymbol original, Cloner cloner)
53      : base(original, cloner) {
54      symbols = new ObservableSet<ISymbol>(original.Symbols.Select(s => cloner.Clone(s)));
55    }
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new GroupSymbol(this, cloner);
58    }
59
60    public GroupSymbol() : this("Group Symbol", Enumerable.Empty<ISymbol>()) { }
61    public GroupSymbol(string name, IEnumerable<ISymbol> symbols)
62      : base(name, "A symbol which groups other symbols") {
63      this.symbols = new ObservableSet<ISymbol>(symbols);
64      InitialFrequency = 0.0;
65    }
66
67    public IEnumerable<ISymbol> Flatten() {
68      return symbols.Union(symbols.OfType<GroupSymbol>().SelectMany(g => g.Flatten()));
69    }
70  }
71}
Note: See TracBrowser for help on using the repository browser.