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
RevLine 
[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
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;
[562]32using HeuristicLab.PluginInfrastructure;
[567]33using HeuristicLab.Logging;
34using HeuristicLab.Data;
35using HeuristicLab.DataAnalysis;
36using HeuristicLab.Charting.Data;
37using System.Drawing;
[656]38using HeuristicLab.GP;
[560]39
40namespace HeuristicLab.CEDMA.Charting {
[562]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
[560]51  public class ResultList : ItemBase {
[567]52    private Dictionary<Record, Dataset> datasets;
[560]53
54    private IStore store;
55    public IStore Store {
56      get { return store; }
57      set {
58        store = value;
[561]59        Action reloadList = ReloadList;
60        reloadList.BeginInvoke(null, null);
[560]61      }
62    }
[561]63
[575]64    private List<string> variableNames = new List<string>() { Record.TARGET_VARIABLE, Record.TREE_SIZE, Record.TREE_HEIGHT, Record.SELECTIONPRESSURE,
[566]65    Record.MAPE_TRAINING, Record.MAPE_VALIDATION, Record.MAPE_TEST,
66    Record.R2_TRAINING, Record.R2_VALIDATION, Record.R2_TEST};
[560]67    public string[] VariableNames {
68      get {
69        return variableNames.ToArray();
70      }
71    }
72
[566]73    private Dictionary<Entity, string> predicateToVariableName;
74
[562]75    public event EventHandler<RecordAddedEventArgs> OnRecordAdded;
[560]76
[562]77    private List<Record> records;
[566]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    }
[562]87    private void ReloadList() {
[988]88      var results = store.Select(new Statement(Ontology.AnyEntity, Ontology.PredicateInstanceOf, Ontology.TypeGeneticProgrammingFunctionTree))
[560]89      .Select(x => store.Select(new SelectFilter(
90        new Entity[] { new Entity(x.Subject.Uri) },
[988]91        new Entity[] { Ontology.PredicateModelAttribute },
92          new Resource[] { Ontology.AnyEntity })));
[560]93
[566]94      Random random = new Random();
[560]95      foreach(Statement[] ss in results) {
[562]96        if(ss.Length > 0) {
[567]97          Record r = new Record(this, ss[0].Subject.Uri);
[566]98          r.Set(Record.X_JITTER, random.NextDouble() * 2.0 - 1.0);
99          r.Set(Record.Y_JITTER, random.NextDouble() * 2.0 - 1.0);
[561]100          foreach(Statement s in ss) {
[566]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              }
[561]109            }
[560]110          }
[566]111          lock(records) {
112            records.Add(r);
113          }
[562]114          FireRecordAdded(r);
[560]115        }
116      }
[566]117      FireChanged();
[560]118    }
119
[562]120    private void FireRecordAdded(Record r) {
121      if(OnRecordAdded != null) OnRecordAdded(this, new RecordAddedEventArgs(r));
[560]122    }
123
124    public ResultList()
125      : base() {
[562]126      records = new List<Record>();
[567]127      datasets = new Dictionary<Record, Dataset>();
[988]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;
[560]139    }
140
141    public override IView CreateView() {
142      return new ResultListView(this);
143    }
144
[567]145    internal void OpenModel(Record record) {
[988]146      IList<Statement> modelResults = store.Select(new Statement(new Entity(record.Uri), Ontology.PredicateSerializedData, Ontology.AnyEntity));
[567]147      if(modelResults.Count == 1) {
148        string rawData = ((SerializedLiteral)modelResults[0].Property).RawData;
[562]149        XmlDocument doc = new XmlDocument();
150        doc.LoadXml(rawData);
[567]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
[576]155        ModelView modelView = new ModelView(record, dataset, tree, targetVariable);
[567]156        PluginManager.ControlManager.ShowControl(modelView);
[561]157      }
[560]158    }
[567]159
160    private Dataset GetDataset(Record record) {
161      if(!datasets.ContainsKey(record)) {
[988]162        IList<Statement> result = store.Select(new Statement(Ontology.AnyEntity, Ontology.PredicateHasModel, new Entity(record.Uri)));
[567]163        if(result.Count == 1) {
[988]164          IList<Statement> datasetResult = store.Select(new Statement(result[0].Subject, Ontology.PredicateSerializedData, Ontology.AnyEntity));
[567]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    }
[576]176
177    internal void OpenAlgorithm(Record record) {
[988]178      IList<Statement> generatedBy = store.Select(new Statement(new Entity(record.Uri), Ontology.PredicateGeneratedBy, Ontology.AnyEntity));
[576]179      if(generatedBy.Count == 1) {
[988]180        IList<Statement> algoResult = store.Select(new Statement((Entity)generatedBy[0].Property, Ontology.PredicateSerializedData, Ontology.AnyEntity));
[576]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    }
[560]190  }
191}
Note: See TracBrowser for help on using the repository browser.