source: branches/CEDMA-Refactoring-Ticket419/HeuristicLab.CEDMA.Server/Dispatcher.cs @ 1044

Last change on this file since 1044 was 1044, checked in by gkronber, 13 years ago

worked on dispatcher for CEDMA agents. #419 (Refactor CEDMA plugins)

File size: 4.0 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 HeuristicLab.CEDMA.DB.Interfaces;
30using HeuristicLab.CEDMA.DB;
31using System.ServiceModel.Description;
32using System.Linq;
33using HeuristicLab.CEDMA.Core;
34
35namespace HeuristicLab.CEDMA.Server {
36  public class Dispatcher {
37    private List<string> dispatchQueue;
38    public IList<string> DispatchQueue {
39      get { return dispatchQueue.AsReadOnly(); }
40    }
41
42    private IStore store;
43
44    public Dispatcher(IStore store) {
45      this.store = store;
46      this.dispatchQueue = new List<string>();
47    }
48
49    private void FillDispatchQueue() {
50      Dictionary<Entity, Dictionary<int, int>> numberOfModelsOfTargetVariableOfDataSet = new Dictionary<Entity, Dictionary<int, int>>();
51      IList<Statement> datasetStatements = store.Select(new Statement(Ontology.AnyEntity, Ontology.PredicateInstanceOf, Ontology.TypeDataSet));
52      foreach (Statement datasetStatement in datasetStatements) {
53        numberOfModelsOfTargetVariableOfDataSet.Add(datasetStatement.Subject, new Dictionary<int, int>());
54        IList<Statement> modelStatements = store.Select(new Statement(datasetStatement.Subject, Ontology.PredicateHasModel, Ontology.AnyEntity));
55        foreach (Statement modelStatement in modelStatements) {
56          IList<Statement> modelAttributeStatements = store.Select(new Statement((Entity)modelStatement.Property, Ontology.PredicateModelAttribute, Ontology.AnyEntity));
57          foreach (Statement modelAttrStatement in modelAttributeStatements) {
58            var targetVariableStatements = store.Select(new Statement((Entity)modelAttrStatement.Property, Ontology.PredicateModelAttributeName, Ontology.AnyEntity))
59               .Where(t => (string)((Literal)t.Property).Value == "TargetVariable")
60               .SelectMany(t => store.Select(new Statement((Entity)modelAttrStatement.Property, Ontology.PredicateModelAttributeValue, Ontology.AnyEntity)))
61               .GroupBy(t => (int)((Literal)t.Property).Value);
62            foreach (var targetVariable in targetVariableStatements) {
63              numberOfModelsOfTargetVariableOfDataSet[datasetStatement.Subject].Add(targetVariable.Key, targetVariable.Count());
64            }
65          }
66        }
67      }
68      foreach(KeyValuePair<Entity, Dictionary<int, int>> dataSetEntry in numberOfModelsOfTargetVariableOfDataSet) {
69        DataSet dataSet = new DataSet(store, dataSetEntry.Key);
70        foreach(int targetVariable in dataSet.Problem.AllowedTargetVariables)
71          if (!dataSetEntry.Value.ContainsKey(targetVariable) || dataSetEntry.Value[targetVariable] < 10) {
72            QueueJob(dataSet.Name + " - target variable: " + targetVariable);
73          }
74      }
75    }
76
77    private void QueueJob(string job) {
78      dispatchQueue.Add(job);
79    }
80
81    private string GetNextJob() {
82      if (dispatchQueue.Count == 0) FillDispatchQueue();
83      string next = dispatchQueue[0];
84      dispatchQueue.RemoveAt(0);
85      return next;
86    }
87
88    internal void Start() {
89      FillDispatchQueue();
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.