[560] | 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;
|
---|
[562] | 32 | using HeuristicLab.PluginInfrastructure;
|
---|
[567] | 33 | using HeuristicLab.Data;
|
---|
| 34 | using HeuristicLab.DataAnalysis;
|
---|
| 35 | using System.Drawing;
|
---|
[560] | 36 |
|
---|
[1073] | 37 | namespace HeuristicLab.CEDMA.Core {
|
---|
[1287] | 38 | public class Results : ItemBase {
|
---|
[1417] | 39 | private const int PAGE_SIZE = 1000;
|
---|
[1287] | 40 | private string[] categoricalVariables = null;
|
---|
| 41 | public string[] CategoricalVariables {
|
---|
| 42 | get {
|
---|
| 43 | if (categoricalVariables == null) {
|
---|
| 44 | LoadModelAttributes();
|
---|
| 45 | }
|
---|
| 46 | return categoricalVariables;
|
---|
| 47 | }
|
---|
| 48 | }
|
---|
[560] | 49 |
|
---|
[1287] | 50 | private string[] ordinalVariables = null;
|
---|
| 51 | public string[] OrdinalVariables {
|
---|
| 52 | get {
|
---|
| 53 | if (ordinalVariables == null) {
|
---|
| 54 | LoadModelAttributes();
|
---|
| 55 | }
|
---|
| 56 | return ordinalVariables;
|
---|
| 57 | }
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[560] | 60 | private IStore store;
|
---|
| 61 | public IStore Store {
|
---|
| 62 | get { return store; }
|
---|
| 63 | set {
|
---|
| 64 | store = value;
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
[561] | 67 |
|
---|
[1426] | 68 | private Dictionary<string, Dictionary<object, double>> categoricalValueIndices = new Dictionary<string, Dictionary<object, double>>();
|
---|
| 69 |
|
---|
[1287] | 70 | private Entity dataSetEntity;
|
---|
[560] | 71 |
|
---|
[1287] | 72 | public Results(IStore store) {
|
---|
| 73 | this.store = store;
|
---|
[560] | 74 | }
|
---|
| 75 |
|
---|
[1287] | 76 | internal void FilterDataSet(Entity entity) {
|
---|
| 77 | this.dataSetEntity = entity;
|
---|
[560] | 78 | }
|
---|
| 79 |
|
---|
[1287] | 80 | private List<ResultsEntry> entries = null;
|
---|
| 81 | private bool cached = false;
|
---|
| 82 | public IEnumerable<ResultsEntry> GetEntries() {
|
---|
| 83 | if (!cached)
|
---|
| 84 | return SelectRows();
|
---|
| 85 | return entries.AsEnumerable();
|
---|
[560] | 86 | }
|
---|
| 87 |
|
---|
[1287] | 88 | private IEnumerable<ResultsEntry> SelectRows() {
|
---|
[1417] | 89 | int page = 0;
|
---|
| 90 | int resultsReturned = 0;
|
---|
| 91 | bool newEntry = false;
|
---|
[1287] | 92 | if (store == null) yield break;
|
---|
| 93 | entries = new List<ResultsEntry>();
|
---|
[1417] | 94 | do {
|
---|
| 95 | var allBindings = store.Query("<" + dataSetEntity + "> <" + Ontology.PredicateHasModel + "> ?Model ." + Environment.NewLine +
|
---|
| 96 | "?Model ?Attribute ?Value .", page, PAGE_SIZE);
|
---|
| 97 | var allModelBindings = allBindings.GroupBy(x => (Entity)x.Get("Model"));
|
---|
| 98 | resultsReturned = allBindings.Count;
|
---|
| 99 |
|
---|
| 100 | foreach (var modelBindings in allModelBindings) {
|
---|
| 101 | ResultsEntry entry = entries.FirstOrDefault(x => x.Uri == modelBindings.Key.Uri);
|
---|
| 102 | newEntry = false;
|
---|
| 103 | if (entry == null) {
|
---|
| 104 | entry = new ResultsEntry();
|
---|
| 105 | entry.Uri = modelBindings.Key.Uri;
|
---|
| 106 | entries.Add(entry);
|
---|
| 107 | newEntry = true;
|
---|
| 108 | }
|
---|
| 109 | foreach (var binding in modelBindings) {
|
---|
| 110 | if (binding.Get("Value") is Literal) {
|
---|
| 111 | string name = ((Entity)binding.Get("Attribute")).Uri.Replace(Ontology.CedmaNameSpace, "");
|
---|
| 112 | if (entry.Get(name) == null) {
|
---|
| 113 | object value = ((Literal)binding.Get("Value")).Value;
|
---|
| 114 | entry.Set(name, value);
|
---|
| 115 | }
|
---|
[1287] | 116 | }
|
---|
| 117 | }
|
---|
[1417] | 118 | if (newEntry) yield return entry;
|
---|
[1287] | 119 | }
|
---|
[1417] | 120 | page++;
|
---|
| 121 | } while (resultsReturned == PAGE_SIZE);
|
---|
[567] | 122 |
|
---|
[1287] | 123 | FireChanged();
|
---|
| 124 | cached = true;
|
---|
[560] | 125 | }
|
---|
[567] | 126 |
|
---|
[1287] | 127 | internal IEnumerable<string> SelectModelAttributes() {
|
---|
| 128 | return CategoricalVariables.Concat(OrdinalVariables);
|
---|
[567] | 129 | }
|
---|
[576] | 130 |
|
---|
[1287] | 131 | private void LoadModelAttributes() {
|
---|
| 132 | this.ordinalVariables =
|
---|
[1417] | 133 | store.Query("?ModelAttribute <" + Ontology.PredicateInstanceOf + "> <" + Ontology.TypeOrdinalAttribute + "> .", 0, 100)
|
---|
[1287] | 134 | .Select(s => ((Entity)s.Get("ModelAttribute")).Uri.Replace(Ontology.CedmaNameSpace, ""))
|
---|
| 135 | .ToArray();
|
---|
| 136 | this.categoricalVariables =
|
---|
[1417] | 137 | store.Query("?ModelAttribute <" + Ontology.PredicateInstanceOf + "> <" + Ontology.TypeCategoricalAttribute + "> .", 0, 100)
|
---|
[1287] | 138 | .Select(s => ((Entity)s.Get("ModelAttribute")).Uri.Replace(Ontology.CedmaNameSpace, ""))
|
---|
| 139 | .ToArray();
|
---|
[576] | 140 | }
|
---|
[1424] | 141 |
|
---|
| 142 | public double IndexOfCategoricalValue(string variable, object value) {
|
---|
[1426] | 143 | if (value == null) return double.NaN;
|
---|
| 144 | Dictionary<object, double> valueToIndexMap;
|
---|
| 145 | if (categoricalValueIndices.ContainsKey(variable)) {
|
---|
| 146 | valueToIndexMap = categoricalValueIndices[variable];
|
---|
| 147 | } else {
|
---|
| 148 | valueToIndexMap = new Dictionary<object, double>();
|
---|
| 149 | categoricalValueIndices[variable] = valueToIndexMap;
|
---|
| 150 | }
|
---|
| 151 | if (!valueToIndexMap.ContainsKey(value)) {
|
---|
| 152 | if (valueToIndexMap.Values.Count == 0) valueToIndexMap[value] = 1.0;
|
---|
| 153 | else valueToIndexMap[value] = 1.0 + valueToIndexMap.Values.Max();
|
---|
| 154 | }
|
---|
| 155 | return valueToIndexMap[value];
|
---|
[1424] | 156 | }
|
---|
[560] | 157 | }
|
---|
| 158 | }
|
---|