Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.GP.Boolean/3.3/Symbols/VariableFunctionTree.cs @ 3001

Last change on this file since 3001 was 2222, checked in by gkronber, 15 years ago

Merged changes from GP-refactoring branch back into the trunk #713.

File size: 3.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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 HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.GP.Interfaces;
26using System.Xml;
27
28namespace HeuristicLab.GP.Boolean {
29  public class VariableFunctionTree : TerminalTreeNode {
30    public string VariableName { get; set; }
31
32    public VariableFunctionTree(Variable variable)
33      : base(variable) {
34    }
35
36    protected VariableFunctionTree(VariableFunctionTree original)
37      : base(original) {
38      VariableName = original.VariableName;
39    }
40
41    public override bool HasLocalParameters {
42      get {
43        return true;
44      }
45    }
46
47    public override IOperation CreateShakingOperation(IScope scope) {
48      Scope myVariableScope = new Scope();
49      scope.AddSubScope(myVariableScope);
50      myVariableScope.AddVariable(CreateVariableIndexVariable());
51      return new AtomicOperation(Function.Manipulator, myVariableScope);
52    }
53
54    public override IOperation CreateInitOperation(IScope scope) {
55      Scope myVariableScope = new Scope();
56      scope.AddSubScope(myVariableScope);
57      myVariableScope.AddVariable(CreateVariableIndexVariable());
58      return new AtomicOperation(Function.Initializer, myVariableScope);
59    }
60
61    public override object Clone() {
62      return new VariableFunctionTree(this);
63    }
64
65    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<System.Guid, IStorable> persistedObjects) {
66      XmlNode node = document.CreateElement(name);
67      XmlAttribute varAttr = document.CreateAttribute("Variable");
68      varAttr.Value = VariableName;
69      node.Attributes.Append(varAttr);
70      return node;
71    }
72
73    public override void Populate(XmlNode node, IDictionary<System.Guid, IStorable> restoredObjects) {
74      base.Populate(node, restoredObjects);
75      VariableName = node.Attributes["Variable"].Value;
76    }
77
78    private IVariable CreateVariableIndexVariable() {
79      StringData data = new StringData(VariableName);
80      data.Changed += (sender, args) => VariableName = data.Data;
81      var variable = new HeuristicLab.Core.Variable(Variable.VARIABLENAME, data);
82      variable.ValueChanged += (sender, args) => VariableName = ((StringData)variable.Value).Data;
83      return variable;
84    }
85  }
86}
Note: See TracBrowser for help on using the repository browser.