[421] | 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 |
|
---|
| 22 | using System;
|
---|
[416] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[417] | 27 | using HeuristicLab.CEDMA.DB.Interfaces;
|
---|
[418] | 28 | using HeuristicLab.Operators;
|
---|
[416] | 29 |
|
---|
| 30 | namespace HeuristicLab.CEDMA.Core {
|
---|
| 31 | public class DatabaseOperatorLibrary : ItemBase, IOperatorLibrary {
|
---|
| 32 |
|
---|
| 33 | private OperatorGroup group;
|
---|
| 34 | public IOperatorGroup Group {
|
---|
| 35 | get { return group; }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
[417] | 38 | private IDatabase database;
|
---|
| 39 | public IDatabase Database {
|
---|
| 40 | get { return database; }
|
---|
| 41 | set { this.database = value; }
|
---|
| 42 | }
|
---|
[416] | 43 |
|
---|
[417] | 44 | private Dictionary<IOperator, long> knownOperators;
|
---|
| 45 |
|
---|
| 46 | public DatabaseOperatorLibrary()
|
---|
[416] | 47 | : base() {
|
---|
| 48 | group = new OperatorGroup();
|
---|
[417] | 49 | knownOperators = new Dictionary<IOperator, long>();
|
---|
[416] | 50 | }
|
---|
| 51 |
|
---|
[417] | 52 | public void Save() {
|
---|
[418] | 53 | Dictionary<IOperator, long> newKnownOperators = new Dictionary<IOperator, long>();
|
---|
[417] | 54 | foreach(IOperator op in group.Operators) {
|
---|
| 55 | if(knownOperators.ContainsKey(op)) {
|
---|
| 56 | // update
|
---|
[418] | 57 | long id = knownOperators[op];
|
---|
[417] | 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 | }
|
---|
[418] | 71 |
|
---|
[417] | 72 | knownOperators = newKnownOperators;
|
---|
| 73 | }
|
---|
[416] | 74 |
|
---|
[417] | 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 | }
|
---|
[418] | 86 |
|
---|
[513] | 87 | // set the DB source for each operator
|
---|
[418] | 88 | foreach(IOperator op in group.Operators) {
|
---|
[513] | 89 | OperatorLinkPatcher.LinkDatabase(op, Database);
|
---|
[418] | 90 | }
|
---|
[417] | 91 | }
|
---|
| 92 |
|
---|
[418] | 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 |
|
---|
[416] | 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() {
|
---|
[417] | 111 | Restore();
|
---|
| 112 | return new DatabaseOperatorLibraryView(this);
|
---|
[416] | 113 | }
|
---|
[418] | 114 |
|
---|
| 115 | internal long GetId(IOperator op) {
|
---|
| 116 | return knownOperators[op];
|
---|
| 117 | }
|
---|
[416] | 118 | }
|
---|
| 119 | }
|
---|