Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.CEDMA.Core/DatabaseOperatorLibrary.cs @ 486

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

added missing GPL license headers for new files (#211)

File size: 4.3 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;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.CEDMA.DB.Interfaces;
28using HeuristicLab.Operators;
29
30namespace HeuristicLab.CEDMA.Core {
31  public class DatabaseOperatorLibrary : ItemBase, IOperatorLibrary {
32
33    private OperatorGroup group;
34    public IOperatorGroup Group {
35      get { return group; }
36    }
37
38    private IDatabase database;
39    public IDatabase Database {
40      get { return database; }
41      set { this.database = value; }
42    }
43
44    private Dictionary<IOperator, long> knownOperators;
45
46    public DatabaseOperatorLibrary()
47      : base() {
48      group = new OperatorGroup();
49      knownOperators = new Dictionary<IOperator, long>();
50    }
51
52    public void Save() {
53      Dictionary<IOperator, long> newKnownOperators = new Dictionary<IOperator, long>();
54      foreach(IOperator op in group.Operators) {
55        if(knownOperators.ContainsKey(op)) {
56          // update
57          long id = knownOperators[op];
58          Database.UpdateOperator(id, op.Name, PersistenceManager.SaveToGZip(op));
59          knownOperators.Remove(op);
60          newKnownOperators.Add(op, id);
61        } else {
62          // create new
63          long id = Database.InsertOperator(op.Name, PersistenceManager.SaveToGZip(op));
64          newKnownOperators.Add(op, id);
65        }
66      }
67      // delete operators from the table that are not present in the group anymore (remaining entries)
68      foreach(long id in knownOperators.Values) {
69        Database.DeleteOperator(id);
70      }
71
72      knownOperators = newKnownOperators;
73    }
74
75    public void Restore() {
76      foreach(IOperator op in knownOperators.Keys) {
77        group.RemoveOperator(op);
78      }
79      knownOperators.Clear();
80      if(database == null) return;
81      foreach(OperatorEntry e in Database.GetOperators()) {
82        IOperator op = (IOperator)PersistenceManager.RestoreFromGZip(e.RawData);
83        knownOperators.Add(op, e.Id);
84        group.AddOperator(op);
85      }
86
87      // patch all OperatorLinks
88      foreach(IOperator op in group.Operators) {
89        PatchLinks(op);
90      }
91    }
92
93    private void PatchLinks(IOperator op) {
94      if(op is OperatorLink) {
95        OperatorLink link = op as OperatorLink;
96        link.Operator = FindOperator(link.Id);
97      }
98      else if(op is CombinedOperator) {
99        CombinedOperator combinedOp = op as CombinedOperator;
100        foreach(IOperator internalOp in combinedOp.OperatorGraph.Operators) {
101          PatchLinks(internalOp);
102        }
103      }
104    }
105
106    private IOperator FindOperator(long id) {
107      foreach(KeyValuePair<IOperator, long> p in knownOperators) if(p.Value == id) return p.Key;
108      return null;
109    }
110
111    public override System.Xml.XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
112      throw new NotSupportedException();
113    }
114
115    public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
116      throw new NotSupportedException();
117    }
118
119    public override object Clone(IDictionary<Guid, object> clonedObjects) {
120      throw new NotSupportedException();
121    }
122
123    public override IView CreateView() {
124      Restore();
125      return new DatabaseOperatorLibraryView(this);
126    }
127
128    internal long GetId(IOperator op) {
129      return knownOperators[op];
130    }
131  }
132}
Note: See TracBrowser for help on using the repository browser.