Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Refactoring-Ticket419/HeuristicLab.CEDMA.Charting/ResultList.cs @ 1070

Last change on this file since 1070 was 988, checked in by gkronber, 16 years ago

worked on #419 (Refactor CEDMA plugins)

File size: 7.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.Logging;
34using HeuristicLab.Data;
35using HeuristicLab.DataAnalysis;
36using HeuristicLab.Charting.Data;
37using System.Drawing;
38using HeuristicLab.GP;
39
40namespace HeuristicLab.CEDMA.Charting {
41
42  public class RecordAddedEventArgs : EventArgs {
43    private Record record;
44    public Record Record { get { return record; } }
45    public RecordAddedEventArgs(Record r)
46      : base() {
47      this.record = r;
48    }
49  }
50
51  public class ResultList : ItemBase {
52    private Dictionary<Record, Dataset> datasets;
53
54    private IStore store;
55    public IStore Store {
56      get { return store; }
57      set {
58        store = value;
59        Action reloadList = ReloadList;
60        reloadList.BeginInvoke(null, null);
61      }
62    }
63
64    private List<string> variableNames = new List<string>() { Record.TARGET_VARIABLE, Record.TREE_SIZE, Record.TREE_HEIGHT, Record.SELECTIONPRESSURE,
65    Record.MAPE_TRAINING, Record.MAPE_VALIDATION, Record.MAPE_TEST,
66    Record.R2_TRAINING, Record.R2_VALIDATION, Record.R2_TEST};
67    public string[] VariableNames {
68      get {
69        return variableNames.ToArray();
70      }
71    }
72
73    private Dictionary<Entity, string> predicateToVariableName;
74
75    public event EventHandler<RecordAddedEventArgs> OnRecordAdded;
76
77    private List<Record> records;
78    public List<Record> Records {
79      get {
80        List<Record> result = new List<Record>();
81        lock(records) {
82          result.AddRange(records);
83        }
84        return result;
85      }
86    }
87    private void ReloadList() {
88      var results = store.Select(new Statement(Ontology.AnyEntity, Ontology.PredicateInstanceOf, Ontology.TypeGeneticProgrammingFunctionTree))
89      .Select(x => store.Select(new SelectFilter(
90        new Entity[] { new Entity(x.Subject.Uri) },
91        new Entity[] { Ontology.PredicateModelAttribute },
92          new Resource[] { Ontology.AnyEntity })));
93
94      Random random = new Random();
95      foreach(Statement[] ss in results) {
96        if(ss.Length > 0) {
97          Record r = new Record(this, ss[0].Subject.Uri);
98          r.Set(Record.X_JITTER, random.NextDouble() * 2.0 - 1.0);
99          r.Set(Record.Y_JITTER, random.NextDouble() * 2.0 - 1.0);
100          foreach(Statement s in ss) {
101            string varName;
102            predicateToVariableName.TryGetValue(s.Predicate, out varName);
103            if(varName != null) {
104              if(varName == Record.TREE_HEIGHT || varName == Record.TREE_SIZE || varName == Record.TARGET_VARIABLE) {
105                r.Set(varName, (double)(int)((Literal)s.Property).Value);
106              } else {
107                r.Set(varName, (double)((Literal)s.Property).Value);
108              }
109            }
110          }
111          lock(records) {
112            records.Add(r);
113          }
114          FireRecordAdded(r);
115        }
116      }
117      FireChanged();
118    }
119
120    private void FireRecordAdded(Record r) {
121      if(OnRecordAdded != null) OnRecordAdded(this, new RecordAddedEventArgs(r));
122    }
123
124    public ResultList()
125      : base() {
126      records = new List<Record>();
127      datasets = new Dictionary<Record, Dataset>();
128      //predicateToVariableName = new Dictionary<Entity, string>();
129      //predicateToVariableName[targetVariablePredicate] = Record.TARGET_VARIABLE;
130      //predicateToVariableName[treeSizePredicate] = Record.TREE_SIZE;
131      //predicateToVariableName[treeHeightPredicate] = Record.TREE_HEIGHT;
132      //predicateToVariableName[selectionPressurePredicate] = Record.SELECTIONPRESSURE;
133      //predicateToVariableName[trainingMAPEPredicate] = Record.MAPE_TRAINING;
134      //predicateToVariableName[validationMAPEPredicate] = Record.MAPE_VALIDATION;
135      //predicateToVariableName[testMAPEPredicate] = Record.MAPE_TEST;
136      //predicateToVariableName[trainingR2Predicate] = Record.R2_TRAINING;
137      //predicateToVariableName[validationR2Predicate] = Record.R2_VALIDATION;
138      //predicateToVariableName[testR2Predicate] = Record.R2_TEST;
139    }
140
141    public override IView CreateView() {
142      return new ResultListView(this);
143    }
144
145    internal void OpenModel(Record record) {
146      IList<Statement> modelResults = store.Select(new Statement(new Entity(record.Uri), Ontology.PredicateSerializedData, Ontology.AnyEntity));
147      if(modelResults.Count == 1) {
148        string rawData = ((SerializedLiteral)modelResults[0].Property).RawData;
149        XmlDocument doc = new XmlDocument();
150        doc.LoadXml(rawData);
151        IFunctionTree tree = (IFunctionTree)PersistenceManager.Restore(doc.ChildNodes[1], new Dictionary<Guid, IStorable>());
152        int targetVariable = (int)record.Get(Record.TARGET_VARIABLE);
153        Dataset dataset = GetDataset(record);
154
155        ModelView modelView = new ModelView(record, dataset, tree, targetVariable);
156        PluginManager.ControlManager.ShowControl(modelView);
157      }
158    }
159
160    private Dataset GetDataset(Record record) {
161      if(!datasets.ContainsKey(record)) {
162        IList<Statement> result = store.Select(new Statement(Ontology.AnyEntity, Ontology.PredicateHasModel, new Entity(record.Uri)));
163        if(result.Count == 1) {
164          IList<Statement> datasetResult = store.Select(new Statement(result[0].Subject, Ontology.PredicateSerializedData, Ontology.AnyEntity));
165          if(datasetResult.Count == 1) {
166            string rawData = ((SerializedLiteral)datasetResult[0].Property).RawData;
167            XmlDocument doc = new XmlDocument();
168            doc.LoadXml(rawData);
169            Dataset dataset = (Dataset)PersistenceManager.Restore(doc.ChildNodes[1], new Dictionary<Guid, IStorable>());
170            datasets.Add(record, dataset);
171          }
172        }
173      }
174      return datasets[record];
175    }
176
177    internal void OpenAlgorithm(Record record) {
178      IList<Statement> generatedBy = store.Select(new Statement(new Entity(record.Uri), Ontology.PredicateGeneratedBy, Ontology.AnyEntity));
179      if(generatedBy.Count == 1) {
180        IList<Statement> algoResult = store.Select(new Statement((Entity)generatedBy[0].Property, Ontology.PredicateSerializedData, Ontology.AnyEntity));
181        if(algoResult.Count == 1) {
182          string rawData = ((SerializedLiteral)algoResult[0].Property).RawData;
183          XmlDocument doc = new XmlDocument();
184          doc.LoadXml(rawData);
185          IItem algo = (IItem)PersistenceManager.Restore(doc.ChildNodes[1], new Dictionary<Guid, IStorable>());
186          PluginManager.ControlManager.ShowControl(algo.CreateView());
187        }
188      }
189    }
190  }
191}
Note: See TracBrowser for help on using the repository browser.