Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.CEDMA.Server/3.3/ExecuterBase.cs @ 4042

Last change on this file since 4042 was 2530, checked in by mkommend, 15 years ago

changed modeling database backend to use a shared read connection => notification by exception if another program is connected to the database (ticket #759)

File size: 3.7 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.Text;
25using System.Windows.Forms;
26using HeuristicLab.PluginInfrastructure;
27using System.Net;
28using System.ServiceModel;
29using System.ServiceModel.Description;
30using System.Linq;
31using HeuristicLab.Data;
32using HeuristicLab.Grid;
33using System.Diagnostics;
34using HeuristicLab.Core;
35using System.Threading;
36using HeuristicLab.Modeling;
37using HeuristicLab.Modeling.Database;
38
39namespace HeuristicLab.CEDMA.Server {
40  public abstract class ExecuterBase : IExecuter {
41    internal event EventHandler Changed;
42
43    private IDispatcher dispatcher;
44    protected IDispatcher Dispatcher {
45      get { return dispatcher; }
46    }
47    private IModelingDatabase databaseService;
48
49    private int maxActiveJobs;
50    public int MaxActiveJobs {
51      get { return maxActiveJobs; }
52      set {
53        if (value < 0) throw new ArgumentException("Only positive values are allowed for MaxActiveJobs");
54        maxActiveJobs = value;
55        OnChanged();
56      }
57    }
58
59    public int CalculatedJobs {
60      get;
61      set;
62    }
63    public int StoredJobs {
64      get;
65      set;
66    }
67
68    public ExecuterBase(IDispatcher dispatcher, IModelingDatabase databaseService) {
69      CalculatedJobs = 0;
70      maxActiveJobs = 10;
71      this.dispatcher = dispatcher;
72      this.databaseService = databaseService;
73      this.databaseService.Connect();
74      StoredJobs = databaseService.GetAllModels().Count();
75      this.databaseService.Disconnect();
76    }
77
78    public void Start() {
79      new Thread(StartJobs).Start();
80    }
81
82    protected abstract void StartJobs();
83
84    protected void SetResults(IScope src, IScope target) {
85      foreach (HeuristicLab.Core.IVariable v in src.Variables) {
86        target.AddVariable(v);
87      }
88      foreach (IScope subScope in src.SubScopes) {
89        target.AddSubScope(subScope);
90      }
91      foreach (KeyValuePair<string, string> alias in src.Aliases) {
92        target.AddAlias(alias.Key, alias.Value);
93      }
94    }
95
96    protected void StoreResults(HeuristicLab.Modeling.IAlgorithm finishedAlgorithm) {
97      CalculatedJobs++;
98      databaseService.Connect();
99      if (databaseService.GetDataset() == null) {
100        databaseService.PersistProblem(finishedAlgorithm.Dataset);
101        databaseService.Commit();
102        databaseService.Disconnect();
103        databaseService.Connect();
104      }
105      databaseService.Persist(finishedAlgorithm.Model,finishedAlgorithm.Name, finishedAlgorithm.Description);
106      databaseService.Commit();
107      databaseService.Disconnect();
108      StoredJobs++;
109      OnChanged();
110    }
111
112    public abstract string[] GetJobs();
113
114    protected internal void OnChanged() {
115      if (Changed != null) Changed(this, new EventArgs());
116    }
117
118    #region IViewable Members
119
120    public IView CreateView() {
121      return new ExecuterView(this);
122    }
123
124    #endregion
125  }
126}
Note: See TracBrowser for help on using the repository browser.