Free cookie consent management tool by TermsFeed Policy Generator

Changeset 320


Ignore:
Timestamp:
06/17/08 20:15:09 (16 years ago)
Author:
gkronber
Message:

fixed serialization of function-trees (deactivated with r317) (ticket #168)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Functions/BakedFunctionTree.cs

    r319 r320  
    233233      FlattenTrees();
    234234      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
    235       throw new NotImplementedException();
    236       //XmlAttribute codeAttribute = document.CreateAttribute("LinearRepresentation");
    237       //codeAttribute.Value = GetString<int>(code);
    238       //node.Attributes.Append(codeAttribute);
    239       //return node;
     235      XmlNode linearRepresentationNode = document.CreateElement("LinearRepresentation");
     236      foreach(LightWeightFunction f in linearRepresentation) {
     237        XmlNode entryNode = PersistenceManager.Persist("FunctionType", f.functionType, document, persistedObjects);
     238        XmlAttribute arityAttribute = document.CreateAttribute("Arity");
     239        arityAttribute.Value = f.arity+"";
     240        entryNode.Attributes.Append(arityAttribute);
     241        if(f.data.Count > 0) {
     242          XmlAttribute dataAttribute = document.CreateAttribute("Data");
     243          dataAttribute.Value = GetString<double>(f.data);
     244          entryNode.Attributes.Append(dataAttribute);
     245        }
     246        linearRepresentationNode.AppendChild(entryNode);
     247      }
     248
     249      node.AppendChild(linearRepresentationNode);
     250      return node;
    240251    }
    241252
    242253    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
    243       throw new NotImplementedException();
    244       //base.Populate(node, restoredObjects);
    245       //XmlNode evaluatorNode = node.SelectSingleNode("Evaluator");
    246       //if(evaluatorNode != null) {
    247       //  this.evaluator = (BakedTreeEvaluator)PersistenceManager.Restore(evaluatorNode, restoredObjects);
    248       //}
    249       //code = GetList<int>(node.Attributes["Code"].Value, s => int.Parse(s, CultureInfo.InvariantCulture));
    250       //data = GetList<double>(node.Attributes["Data"].Value, s => double.Parse(s, CultureInfo.InvariantCulture));
    251       //treesExpanded = false;
    252       //variablesExpanded = false;
    253     }
    254 
    255     //private string GetString<T>(IEnumerable<T> xs) where T : IConvertible {
    256     //  StringBuilder builder = new StringBuilder();
    257     //  foreach(T x in xs) {
    258     //    builder.Append(x.ToString(CultureInfo.InvariantCulture) + "; ");
    259     //  }
    260     //  if(builder.Length > 0) builder.Remove(builder.Length - 2, 2);
    261     //  return builder.ToString();
    262     //}
    263 
    264     //private List<T> GetList<T>(string s, Converter<string, T> converter) {
    265     //  List<T> result = new List<T>();
    266     //  string[] tokens = s.Split(new char[] {';',' '}, StringSplitOptions.RemoveEmptyEntries);
    267     //  foreach(string token in tokens) {
    268     //    T x = converter(token.Trim());
    269     //    result.Add(x);
    270     //  }
    271     //  return result;
    272     //}
     254      base.Populate(node, restoredObjects);
     255      XmlNode linearRepresentationNode = node.SelectSingleNode("LinearRepresentation");
     256      foreach(XmlNode entryNode in linearRepresentationNode.ChildNodes) {
     257        LightWeightFunction f = new LightWeightFunction();
     258        f.arity = int.Parse(entryNode.Attributes["Arity"].Value, CultureInfo.InvariantCulture);
     259        if(entryNode.Attributes["Data"]!=null)
     260          f.data = GetList<double>(entryNode.Attributes["Data"].Value, s => double.Parse(s, CultureInfo.InvariantCulture));
     261        f.functionType = (IFunction)PersistenceManager.Restore(entryNode, restoredObjects);
     262        linearRepresentation.Add(f);
     263      }
     264      treesExpanded = false;
     265      variablesExpanded = false;
     266    }
     267
     268    private string GetString<T>(IEnumerable<T> xs) where T : IConvertible {
     269      StringBuilder builder = new StringBuilder();
     270      foreach(T x in xs) {
     271        builder.Append(x.ToString(CultureInfo.InvariantCulture) + "; ");
     272      }
     273      if(builder.Length > 0) builder.Remove(builder.Length - 2, 2);
     274      return builder.ToString();
     275    }
     276
     277    private List<T> GetList<T>(string s, Converter<string, T> converter) {
     278      List<T> result = new List<T>();
     279      string[] tokens = s.Split(new char[] { ';', ' ' }, StringSplitOptions.RemoveEmptyEntries);
     280      foreach(string token in tokens) {
     281        T x = converter(token.Trim());
     282        result.Add(x);
     283      }
     284      return result;
     285    }
    273286
    274287    public override object Clone(IDictionary<Guid, object> clonedObjects) {
Note: See TracChangeset for help on using the changeset viewer.