source: trunk/sources/HeuristicLab.CEDMA.Core/Results.cs @ 1417

Last change on this file since 1417 was 1417, checked in by gkronber, 11 years ago

Introduced paging for queries to fix problems with large WCF messages. #257 (CEDMA console doesn't handle communication- and timeout exceptions correctly)

File size: 4.6 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 System.Collections;
28using HeuristicLab.CEDMA.DB.Interfaces;
29using System.Xml;
30using System.Runtime.Serialization;
31using System.IO;
32using HeuristicLab.PluginInfrastructure;
33using HeuristicLab.Data;
34using HeuristicLab.DataAnalysis;
35using System.Drawing;
36
37namespace HeuristicLab.CEDMA.Core {
38  public class Results : ItemBase {
39    private const int PAGE_SIZE = 1000;
40    private string[] categoricalVariables = null;
41    public string[] CategoricalVariables {
42      get {
43        if (categoricalVariables == null) {
44          LoadModelAttributes();
45        }
46        return categoricalVariables;
47      }
48    }
49
50    private string[] ordinalVariables = null;
51    public string[] OrdinalVariables {
52      get {
53        if (ordinalVariables == null) {
54          LoadModelAttributes();
55        }
56        return ordinalVariables;
57      }
58    }
59
60    private IStore store;
61    public IStore Store {
62      get { return store; }
63      set {
64        store = value;
65      }
66    }
67
68    private Entity dataSetEntity;
69
70    public Results(IStore store) {
71      this.store = store;
72    }
73
74    internal void FilterDataSet(Entity entity) {
75      this.dataSetEntity = entity;
76    }
77
78    private List<ResultsEntry> entries = null;
79    private bool cached = false;
80    public IEnumerable<ResultsEntry> GetEntries() {
81      if (!cached)
82        return SelectRows();
83      return entries.AsEnumerable();
84    }
85
86    private IEnumerable<ResultsEntry> SelectRows() {
87      int page = 0;
88      int resultsReturned = 0;
89      bool newEntry = false;
90      if (store == null) yield break;
91      entries = new List<ResultsEntry>();
92      do {
93        var allBindings = store.Query("<" + dataSetEntity + "> <" + Ontology.PredicateHasModel + "> ?Model ." + Environment.NewLine +
94          "?Model ?Attribute ?Value .", page, PAGE_SIZE);
95        var allModelBindings = allBindings.GroupBy(x => (Entity)x.Get("Model"));
96        resultsReturned = allBindings.Count;
97
98        foreach (var modelBindings in allModelBindings) {
99          ResultsEntry entry = entries.FirstOrDefault(x => x.Uri == modelBindings.Key.Uri);
100          newEntry = false;
101          if (entry == null) {
102            entry = new ResultsEntry();
103            entry.Uri = modelBindings.Key.Uri;
104            entries.Add(entry);
105            newEntry = true;
106          }
107          foreach (var binding in modelBindings) {
108            if (binding.Get("Value") is Literal) {
109              string name = ((Entity)binding.Get("Attribute")).Uri.Replace(Ontology.CedmaNameSpace, "");
110              if (entry.Get(name) == null) {
111                object value = ((Literal)binding.Get("Value")).Value;
112                entry.Set(name, value);
113              }
114            }
115          }
116          if (newEntry) yield return entry;
117        }
118        page++;
119      } while (resultsReturned == PAGE_SIZE);
120
121      FireChanged();
122      cached = true;
123    }
124
125    internal IEnumerable<string> SelectModelAttributes() {
126      return CategoricalVariables.Concat(OrdinalVariables);
127    }
128
129    private void LoadModelAttributes() {
130      this.ordinalVariables =
131        store.Query("?ModelAttribute <" + Ontology.PredicateInstanceOf + "> <" + Ontology.TypeOrdinalAttribute + "> .", 0, 100)
132        .Select(s => ((Entity)s.Get("ModelAttribute")).Uri.Replace(Ontology.CedmaNameSpace, ""))
133        .ToArray();
134      this.categoricalVariables =
135        store.Query("?ModelAttribute <" + Ontology.PredicateInstanceOf + "> <" + Ontology.TypeCategoricalAttribute + "> .", 0, 100)
136        .Select(s => ((Entity)s.Get("ModelAttribute")).Uri.Replace(Ontology.CedmaNameSpace, ""))
137        .ToArray();
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.