Free cookie consent management tool by TermsFeed Policy Generator

Changeset 224


Ignore:
Timestamp:
05/07/08 01:13:58 (17 years ago)
Author:
gkronber
Message:

implemented naive straight-forward way of serialization for baked-trees as tree-structure in XML. It should be possible to serialize and deserialize with the more compact linear representation as well. (ticket #139)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ExperimentalFunctionsBaking/BakedFunctionTree.cs

    r223 r224  
    2727using HeuristicLab.DataAnalysis;
    2828using HeuristicLab.Data;
     29using System.Xml;
    2930
    3031namespace HeuristicLab.Functions {
     
    3334    private List<double> data;
    3435
    35     internal BakedFunctionTree() {
     36    public BakedFunctionTree() {
    3637      code = new List<int>();
    3738      data = new List<double>();
     
    226227
    227228
    228     public override System.Xml.XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
    229       throw new NotImplementedException();
    230     }
    231 
    232     public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
    233       throw new NotImplementedException();
     229    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
     230      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
     231      node.AppendChild(PersistenceManager.Persist("Function", Function, document, persistedObjects));
     232      XmlNode subTreesNode = document.CreateNode(XmlNodeType.Element, "SubTrees", null);
     233      for(int i = 0; i < SubTrees.Count; i++)
     234        subTreesNode.AppendChild(PersistenceManager.Persist(SubTrees[i], document, persistedObjects));
     235      node.AppendChild(subTreesNode);
     236      XmlNode variablesNode = document.CreateNode(XmlNodeType.Element, "Variables", null);
     237      foreach(IVariable variable in LocalVariables)
     238        variablesNode.AppendChild(PersistenceManager.Persist(variable, document, persistedObjects));
     239      node.AppendChild(variablesNode);
     240      return node;
     241    }
     242
     243    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
     244      base.Populate(node, restoredObjects);
     245      IFunction function = (IFunction)PersistenceManager.Restore(node.SelectSingleNode("Function"), restoredObjects);
     246      code.Add(0);
     247      code.Add(BakedTreeEvaluator.MapFunction(function));
     248      code.Add(0);
     249      treesExpanded = true;
     250      subTrees = new List<IFunctionTree>();
     251      variables = new List<IVariable>();
     252      variablesExpanded = true;
     253      XmlNode subTreesNode = node.SelectSingleNode("SubTrees");
     254      for(int i = 0; i < subTreesNode.ChildNodes.Count; i++)
     255        subTrees.Add((IFunctionTree)PersistenceManager.Restore(subTreesNode.ChildNodes[i], restoredObjects));
     256      XmlNode variablesNode = node.SelectSingleNode("Variables");
     257      foreach(XmlNode variableNode in variablesNode.ChildNodes)
     258        variables.Add((IVariable)PersistenceManager.Restore(variableNode, restoredObjects));
    234259    }
    235260
    236261    public override object Clone(IDictionary<Guid, object> clonedObjects) {
    237262      BakedFunctionTree clone = new BakedFunctionTree();
    238       if(treesExpanded || variablesExpanded) throw new InvalidOperationException(); // sanity check
     263      // in case the user (de)serialized the tree between evaluation and selection we have to flatten the tree again.
     264      if(treesExpanded) FlattenTrees();
     265      if(variablesExpanded) FlattenVariables();
    239266      clone.code.AddRange(code);
    240267      clone.data.AddRange(data);
Note: See TracChangeset for help on using the changeset viewer.