Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.CEDMA.Core/DatabaseOperatorLibrary.cs @ 13365

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

Fixed the problem that sometimes an operator link was not patched before a view was created (which resulted in a NullException) by adding a property for the source database in OperatorLink! and updating this property recursively whenever an operator is loaded from the CEDMA DB.

The method for recursively setting the property is in static class OperatorLinkPatcher because the code is needed in Agent, DatabaseOperatorLibrary and OperatorLink.

(ticket #211)

File size: 3.9 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      // set the DB source for each operator
88      foreach(IOperator op in group.Operators) {
89        OperatorLinkPatcher.LinkDatabase(op, Database);
90      }
91    }
92
93    private IOperator FindOperator(long id) {
94      foreach(KeyValuePair<IOperator, long> p in knownOperators) if(p.Value == id) return p.Key;
95      return null;
96    }
97
98    public override System.Xml.XmlNode GetXmlNode(string name, System.Xml.XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
99      throw new NotSupportedException();
100    }
101
102    public override void Populate(System.Xml.XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
103      throw new NotSupportedException();
104    }
105
106    public override object Clone(IDictionary<Guid, object> clonedObjects) {
107      throw new NotSupportedException();
108    }
109
110    public override IView CreateView() {
111      Restore();
112      return new DatabaseOperatorLibraryView(this);
113    }
114
115    internal long GetId(IOperator op) {
116      return knownOperators[op];
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.