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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Text;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using System.Collections;
|
---|
28 | using HeuristicLab.CEDMA.DB.Interfaces;
|
---|
29 | using System.Xml;
|
---|
30 | using System.Runtime.Serialization;
|
---|
31 | using System.IO;
|
---|
32 | using HeuristicLab.PluginInfrastructure;
|
---|
33 | using HeuristicLab.Data;
|
---|
34 | using HeuristicLab.DataAnalysis;
|
---|
35 | using System.Drawing;
|
---|
36 |
|
---|
37 | namespace 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 Dictionary<string, Dictionary<object, double>> categoricalValueIndices = new Dictionary<string, Dictionary<object, double>>();
|
---|
69 |
|
---|
70 | public Results(IStore store) {
|
---|
71 | this.store = store;
|
---|
72 | }
|
---|
73 |
|
---|
74 | private List<ResultsEntry> entries = null;
|
---|
75 | private bool cached = false;
|
---|
76 | public IEnumerable<ResultsEntry> GetEntries() {
|
---|
77 | if (!cached)
|
---|
78 | return SelectRows();
|
---|
79 | return entries.AsEnumerable();
|
---|
80 | }
|
---|
81 |
|
---|
82 | private IEnumerable<ResultsEntry> SelectRows() {
|
---|
83 | int page = 0;
|
---|
84 | int resultsReturned = 0;
|
---|
85 | bool newEntry = false;
|
---|
86 | if (store == null) yield break;
|
---|
87 | entries = new List<ResultsEntry>();
|
---|
88 | do {
|
---|
89 | var allBindings = store.Query("?Model <" + Ontology.InstanceOf + "> <" + Ontology.TypeModel + "> ." + Environment.NewLine +
|
---|
90 | "?Model ?Attribute ?Value .", page, PAGE_SIZE);
|
---|
91 | var allModelBindings = allBindings.GroupBy(x => (Entity)x.Get("Model"));
|
---|
92 | resultsReturned = allBindings.Count;
|
---|
93 |
|
---|
94 | foreach (var modelBindings in allModelBindings) {
|
---|
95 | ResultsEntry entry = entries.FirstOrDefault(x => x.Uri == modelBindings.Key.Uri);
|
---|
96 | newEntry = false;
|
---|
97 | if (entry == null) {
|
---|
98 | entry = new ResultsEntry();
|
---|
99 | entry.Uri = modelBindings.Key.Uri;
|
---|
100 | entries.Add(entry);
|
---|
101 | newEntry = true;
|
---|
102 | }
|
---|
103 | foreach (var binding in modelBindings) {
|
---|
104 | if (binding.Get("Value") is Literal) {
|
---|
105 | string name = ((Entity)binding.Get("Attribute")).Uri.Replace(Ontology.CedmaNameSpace, "");
|
---|
106 | if (entry.Get(name) == null) {
|
---|
107 | object value = ((Literal)binding.Get("Value")).Value;
|
---|
108 | entry.Set(name, value);
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 | if (newEntry) yield return entry;
|
---|
113 | }
|
---|
114 | page++;
|
---|
115 | } while (resultsReturned == PAGE_SIZE);
|
---|
116 |
|
---|
117 | FireChanged();
|
---|
118 | cached = true;
|
---|
119 | }
|
---|
120 |
|
---|
121 | internal IEnumerable<string> SelectModelAttributes() {
|
---|
122 | return CategoricalVariables.Concat(OrdinalVariables);
|
---|
123 | }
|
---|
124 |
|
---|
125 | private void LoadModelAttributes() {
|
---|
126 | this.ordinalVariables =
|
---|
127 | store
|
---|
128 | .Query(
|
---|
129 | "?ModelAttribute <" + Ontology.InstanceOf + "> <" + Ontology.TypeModelAttribute + "> ." + Environment.NewLine +
|
---|
130 | "?ModelAttribute <" + Ontology.InstanceOf + "> <" + Ontology.TypeOrdinalAttribute + "> .", 0, 100)
|
---|
131 | .Select(s => ((Entity)s.Get("ModelAttribute")).Uri.Replace(Ontology.CedmaNameSpace, ""))
|
---|
132 | .Distinct()
|
---|
133 | .ToArray();
|
---|
134 | this.categoricalVariables =
|
---|
135 | store
|
---|
136 | .Query(
|
---|
137 | "?ModelAttribute <" + Ontology.InstanceOf + "> <" + Ontology.TypeModelAttribute + "> ." + Environment.NewLine +
|
---|
138 | "?ModelAttribute <" + Ontology.InstanceOf + "> <" + Ontology.TypeCategoricalAttribute + "> .", 0, 100)
|
---|
139 | .Select(s => ((Entity)s.Get("ModelAttribute")).Uri.Replace(Ontology.CedmaNameSpace, ""))
|
---|
140 | .Distinct()
|
---|
141 | .ToArray();
|
---|
142 | }
|
---|
143 |
|
---|
144 | public double IndexOfCategoricalValue(string variable, object value) {
|
---|
145 | if (value == null) return double.NaN;
|
---|
146 | Dictionary<object, double> valueToIndexMap;
|
---|
147 | if (categoricalValueIndices.ContainsKey(variable)) {
|
---|
148 | valueToIndexMap = categoricalValueIndices[variable];
|
---|
149 | } else {
|
---|
150 | valueToIndexMap = new Dictionary<object, double>();
|
---|
151 | categoricalValueIndices[variable] = valueToIndexMap;
|
---|
152 | }
|
---|
153 | if (!valueToIndexMap.ContainsKey(value)) {
|
---|
154 | if (valueToIndexMap.Values.Count == 0) valueToIndexMap[value] = 1.0;
|
---|
155 | else valueToIndexMap[value] = 1.0 + valueToIndexMap.Values.Max();
|
---|
156 | }
|
---|
157 | return valueToIndexMap[value];
|
---|
158 | }
|
---|
159 | }
|
---|
160 | }
|
---|