Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.CEDMA.Charting/ResultList.cs @ 566

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

implemented 'brushing' and cleaned up code a little bit. #270 (Selection of multiple points)

File size: 6.5 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;
33
34namespace HeuristicLab.CEDMA.Charting {
35
36  public class RecordAddedEventArgs : EventArgs {
37    private Record record;
38    public Record Record { get { return record; } }
39    public RecordAddedEventArgs(Record r)
40      : base() {
41      this.record = r;
42    }
43  }
44
45  public class ResultList : ItemBase {
46    private const string cedmaNS = "http://www.heuristiclab.com/cedma/";
47    private readonly Entity targetVariablePredicate = new Entity(cedmaNS + "TargetVariable");
48    private readonly Entity trainingMAPEPredicate = new Entity(cedmaNS + "MeanAbsolutePercentageErrorTraining");
49    private readonly Entity validationMAPEPredicate = new Entity(cedmaNS + "MeanAbsolutePercentageErrorValidation");
50    private readonly Entity testMAPEPredicate = new Entity(cedmaNS + "MeanAbsolutePercentageErrorTest");
51    private readonly Entity trainingR2Predicate = new Entity(cedmaNS + "CoefficientOfDeterminationTraining");
52    private readonly Entity validationR2Predicate = new Entity(cedmaNS + "CoefficientOfDeterminationValidation");
53    private readonly Entity testR2Predicate = new Entity(cedmaNS + "CoefficientOfDeterminationTest");
54    private readonly Entity treeSizePredicate = new Entity(cedmaNS + "TreeSize");
55    private readonly Entity treeHeightPredicate = new Entity(cedmaNS + "TreeHeight");
56    private readonly Entity rawDataPredicate = new Entity(cedmaNS + "rawData");
57    private readonly Entity anyEntity = new Entity(null);
58
59    private IStore store;
60    public IStore Store {
61      get { return store; }
62      set {
63        store = value;
64        Action reloadList = ReloadList;
65        reloadList.BeginInvoke(null, null);
66      }
67    }
68
69    private List<string> variableNames = new List<string>() { Record.TARGET_VARIABLE, Record.TREE_SIZE, Record.TREE_HEIGHT,
70    Record.MAPE_TRAINING, Record.MAPE_VALIDATION, Record.MAPE_TEST,
71    Record.R2_TRAINING, Record.R2_VALIDATION, Record.R2_TEST};
72    public string[] VariableNames {
73      get {
74        return variableNames.ToArray();
75      }
76    }
77
78    private Dictionary<Entity, string> predicateToVariableName;
79
80    public event EventHandler<RecordAddedEventArgs> OnRecordAdded;
81
82    private List<Record> records;
83    public List<Record> Records {
84      get {
85        List<Record> result = new List<Record>();
86        lock(records) {
87          result.AddRange(records);
88        }
89        return result;
90      }
91    }
92    private void ReloadList() {
93      var results = store.Select(new Statement(anyEntity, new Entity(cedmaNS + "instanceOf"), new Literal("class:GpFunctionTree")))
94      .Select(x => store.Select(new SelectFilter(
95        new Entity[] { new Entity(x.Subject.Uri) },
96        new Entity[] { targetVariablePredicate, treeSizePredicate, treeHeightPredicate,
97          trainingMAPEPredicate, validationMAPEPredicate, testMAPEPredicate,
98          trainingR2Predicate, validationR2Predicate, testR2Predicate },
99          new Resource[] { anyEntity })));
100
101      Random random = new Random();
102      foreach(Statement[] ss in results) {
103        if(ss.Length > 0) {
104          Record r = new Record(ss[0].Subject.Uri);
105          r.Set(Record.X_JITTER, random.NextDouble() * 2.0 - 1.0);
106          r.Set(Record.Y_JITTER, random.NextDouble() * 2.0 - 1.0);
107          foreach(Statement s in ss) {
108            string varName;
109            predicateToVariableName.TryGetValue(s.Predicate, out varName);
110            if(varName != null) {
111              if(varName == Record.TREE_HEIGHT || varName == Record.TREE_SIZE || varName == Record.TARGET_VARIABLE) {
112                r.Set(varName, (double)(int)((Literal)s.Property).Value);
113              } else {
114                r.Set(varName, (double)((Literal)s.Property).Value);
115              }
116            }
117          }
118          lock(records) {
119            records.Add(r);
120          }
121          FireRecordAdded(r);
122        }
123      }
124      FireChanged();
125    }
126
127    private void FireRecordAdded(Record r) {
128      if(OnRecordAdded != null) OnRecordAdded(this, new RecordAddedEventArgs(r));
129    }
130
131    public ResultList()
132      : base() {
133      records = new List<Record>();
134      predicateToVariableName = new Dictionary<Entity, string>();
135      predicateToVariableName[targetVariablePredicate] = Record.TARGET_VARIABLE;
136      predicateToVariableName[treeSizePredicate] = Record.TREE_SIZE;
137      predicateToVariableName[treeHeightPredicate] = Record.TREE_HEIGHT;
138      predicateToVariableName[trainingMAPEPredicate] = Record.MAPE_TRAINING;
139      predicateToVariableName[validationMAPEPredicate] = Record.MAPE_VALIDATION;
140      predicateToVariableName[testMAPEPredicate] = Record.MAPE_TEST;
141      predicateToVariableName[trainingR2Predicate] = Record.R2_TRAINING;
142      predicateToVariableName[validationR2Predicate] = Record.R2_VALIDATION;
143      predicateToVariableName[testR2Predicate] = Record.R2_TEST;
144    }
145
146    public override IView CreateView() {
147      return new ResultListView(this);
148    }
149
150    internal void OpenModel(Record r) {
151      IList<Statement> s = store.Select(new Statement(new Entity(r.Uri), rawDataPredicate, anyEntity));
152      if(s.Count == 1) {
153        string rawData = ((SerializedLiteral)s[0].Property).RawData;
154        XmlDocument doc = new XmlDocument();
155        doc.LoadXml(rawData);
156        IItem item = (IItem)PersistenceManager.Restore(doc.ChildNodes[1], new Dictionary<Guid, IStorable>());
157        PluginManager.ControlManager.ShowControl(item.CreateView());
158      }
159    }
160  }
161}
Note: See TracBrowser for help on using the repository browser.