Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FunctionsAndStructIdRefactoring/HeuristicLab.Functions/FunctionTree.cs @ 142

Last change on this file since 142 was 142, checked in by gkronber, 16 years ago

Created a branch for refactoring functions and structId. Largest change: splitting IFunction/FunctionBase into two classes, one for the function and one for the treenode+local variables. _work in progress_

File size: 3.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Core;
6using HeuristicLab.DataAnalysis;
7using System.Xml;
8
9namespace HeuristicLab.Functions {
10  public class FunctionTree : ItemBase, IFunctionTree {
11
12    private List<IFunctionTree> subTrees;
13    private List<IVariable> localVariables;
14    private IFunction function;
15
16    public FunctionTree() : base() {
17      subTrees = new List<IFunctionTree>();
18      localVariables = new List<IVariable>();
19    }
20
21    public FunctionTree(IFunction function) : this() {
22      this.function = function;
23      // create and store clones of all local variables of the function
24      foreach(VariableInfo info in function.VariableInfos) {
25        if(info.Local) {
26          AddVariable((IVariable)function.GetVariable(info.FormalName).Clone());
27        }
28      }
29    }
30
31    #region IFunctionTree Members
32
33    public IList<IFunctionTree> SubTrees {
34      get { return subTrees.AsReadOnly(); }
35    }
36
37    public ICollection<IVariable> LocalVariables {
38      get { return localVariables.AsReadOnly(); }
39    }
40
41    public IFunction Function {
42      get { return function; }
43    }
44
45    public IVariable GetLocalVariable(string name) {
46      foreach(IVariable var in localVariables) {
47        if(var.Name == name) return var;
48      }
49      return null;
50    }
51
52    public void AddVariable(IVariable variable) {
53      localVariables.Add(variable);
54    }
55
56    public void RemoveVariable(string name) {
57      localVariables.Remove(GetLocalVariable(name));
58    }
59
60    public void AddSubTree(IFunctionTree tree) {
61      subTrees.Add(tree);
62    }
63
64    public void InsertSubTree(int index, IFunctionTree tree) {
65      subTrees.Insert(index, tree);
66    }
67
68    public void RemoveSubTree(int index) {
69      subTrees.RemoveAt(index);
70    }
71
72    public double Evaluate(Dataset dataset, int sampleIndex) {
73      return function.Evaluate(dataset, sampleIndex, subTrees);
74    }
75    #endregion
76
77    public override XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
78      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
79      XmlNode subTreesNode = document.CreateNode(XmlNodeType.Element, "SubTrees", null);
80      for(int i = 0; i < subTrees.Count; i++)
81        subTreesNode.AppendChild(PersistenceManager.Persist(subTrees[i], document, persistedObjects));
82      node.AppendChild(subTreesNode);
83      XmlNode variablesNode = document.CreateNode(XmlNodeType.Element, "Variables", null);
84      foreach(IVariable variable in localVariables)
85        variablesNode.AppendChild(PersistenceManager.Persist(variable, document, persistedObjects));
86      node.AppendChild(variablesNode);
87      return node;
88    }
89
90    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
91      base.Populate(node, restoredObjects);
92      XmlNode subTreesNode = node.SelectSingleNode("SubTrees");
93      for(int i = 0; i < subTreesNode.ChildNodes.Count; i++)
94        AddSubTree((IFunctionTree)PersistenceManager.Restore(subTreesNode.ChildNodes[i], restoredObjects));
95      XmlNode variablesNode = node.SelectSingleNode("Variables");
96      foreach(XmlNode variableNode in variablesNode.ChildNodes)
97        AddVariable((IVariable)PersistenceManager.Restore(variableNode, restoredObjects));
98    }
99
100    public override object Clone(IDictionary<Guid, object> clonedObjects) {
101      FunctionTree clone = (FunctionTree)base.Clone(clonedObjects);
102      foreach(IFunctionTree tree in subTrees) {
103        clone.AddSubTree((IFunctionTree)tree.Clone(clonedObjects));
104      }
105      foreach(IVariable variable in localVariables) {
106        clone.AddVariable((IVariable)variable.Clone(clonedObjects));
107      }
108      return clone;
109    }
110
111    public override IView CreateView() {
112      return new FunctionTreeView(this);
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.