Free cookie consent management tool by TermsFeed Policy Generator

Changeset 750


Ignore:
Timestamp:
11/13/08 16:50:18 (15 years ago)
Author:
gkronber
Message:

implemented #339 (Persist meta-information about required plugins in all documents)
old documents can still be loaded new documents have a list of necessary plugins at the top XML.

Location:
trunk/sources
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Core/PersistenceManager.cs

    r687 r750  
    2626using System.IO;
    2727using System.IO.Compression;
     28using HeuristicLab.PluginInfrastructure;
    2829
    2930namespace HeuristicLab.Core {
     
    4041    }
    4142    public static XmlNode Persist(string name, IStorable instance, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
    42       if (persistedObjects.ContainsKey(instance.Guid)) {
     43      if(persistedObjects.ContainsKey(instance.Guid)) {
    4344        XmlNode node = document.CreateNode(XmlNodeType.Element, name, null);
    4445        XmlAttribute guidAttribute = document.CreateAttribute("GUID");
     
    5253      }
    5354    }
    54     public static IStorable Restore(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
     55    public static IStorable Restore(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
    5556      Guid guid = new Guid(node.Attributes["GUID"].Value);
    56       if (restoredObjects.ContainsKey(guid)) {
     57      if(restoredObjects.ContainsKey(guid)) {
    5758        return restoredObjects[guid];
    5859      } else {
     
    7273    public static void Save(IStorable instance, Stream stream) {
    7374      XmlDocument document = PersistenceManager.CreateXmlDocument();
    74 
    75       document.AppendChild(Persist(instance, document, new Dictionary<Guid, IStorable>()));
     75      Dictionary<Guid, IStorable> dictionary = new Dictionary<Guid, IStorable>();
     76      XmlNode rootNode = document.CreateElement("Root");
     77      document.AppendChild(rootNode);
     78      XmlNode necessaryPluginsNode = document.CreateElement("NecessaryPlugins");
     79      rootNode.AppendChild(necessaryPluginsNode);
     80      rootNode.AppendChild(Persist(instance, document, dictionary));
     81      // determine the list of necessary plugins for this document
     82      DiscoveryService service = new DiscoveryService();
     83      List<PluginInfo> plugins = new List<PluginInfo>();
     84      foreach(IStorable storeable in dictionary.Values) {
     85        PluginInfo pluginInfo = service.GetDeclaringPlugin(storeable.GetType());
     86        if(!plugins.Contains(pluginInfo)) plugins.Add(pluginInfo);
     87      }
     88      foreach(PluginInfo uniquePlugin in plugins) {
     89        XmlNode necessaryPluginNode = document.CreateElement("Plugin");
     90        XmlAttribute nameAttr = document.CreateAttribute("Name");
     91        nameAttr.Value = uniquePlugin.Name;
     92        XmlAttribute versionAttr = document.CreateAttribute("Version");
     93        versionAttr.Value = uniquePlugin.Version.ToString();
     94        necessaryPluginNode.Attributes.Append(nameAttr);
     95        necessaryPluginNode.Attributes.Append(versionAttr);
     96        necessaryPluginsNode.AppendChild(necessaryPluginNode);
     97      }
    7698      document.Save(stream);
    7799    }
     
    86108      XmlDocument doc = new XmlDocument();
    87109      doc.Load(stream);
    88       return PersistenceManager.Restore(doc.ChildNodes[1], new Dictionary<Guid, IStorable>());
     110      XmlNode rootNode = doc.ChildNodes[1];
     111      if(rootNode.Name == "Root" && rootNode.ChildNodes.Count == 2) {
     112        // load documents that have a list of necessary plugins at the top
     113        return PersistenceManager.Restore(rootNode.ChildNodes[1], new Dictionary<Guid, IStorable>());
     114      } else {
     115        // compatibility to load documents without list of necessary plugins
     116        return PersistenceManager.Restore(rootNode, new Dictionary<Guid, IStorable>());
     117      }
    89118    }
    90119
     
    111140      builder.Append(type.Name);
    112141      Type[] args = type.GetGenericArguments();
    113       if (args.Length > 0) {
     142      if(args.Length > 0) {
    114143        builder.Append("[[");
    115144        builder.Append(BuildTypeString(args[0]));
    116145        builder.Append("]");
    117         for (int i = 1; i < args.Length; i++) {
     146        for(int i = 1; i < args.Length; i++) {
    118147          builder.Append(",[");
    119148          builder.Append(BuildTypeString(args[i]));
  • trunk/sources/HeuristicLab.PluginInfrastructure/DiscoveryService.cs

    r29 r750  
    117117      return types.ToArray();
    118118    }
     119
     120    public PluginInfo GetDeclaringPlugin(Type type) {
     121      IPlugin[] plugins = GetInstances<IPlugin>(type.Assembly);
     122      if(plugins.Length != 1) return null;
     123      IPlugin plugin = plugins[0];
     124      foreach(PluginInfo info in PluginManager.Manager.LoadedPlugins) {
     125        if(info.Name == plugin.Name && info.Version == plugin.Version) return info;
     126      }
     127      return null;
     128    }
    119129  }
    120130}
Note: See TracChangeset for help on using the changeset viewer.