Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CEDMA-Refactoring-Ticket419/HeuristicLab.CEDMA.Core/ResultList.cs @ 1073

Last change on this file since 1073 was 1073, checked in by gkronber, 15 years ago

worked on CEDMA analysis frontend. #419 (Refactor CEDMA plugins)

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