TraceLab Component Library
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
ResultsController.cs
Go to the documentation of this file.
1 // TraceLab Component Library
2 // Copyright © 2012-2013 SEMERU
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 using System;
18 using System.Collections.Generic;
19 using System.Linq;
20 using System.Text;
21 using TraceLab.Components.DevelopmentKit.Metrics;
22 using TraceLabSDK;
23 using TraceLabSDK.Types.Contests;
24 
25 namespace TraceLab.Components.Library.Metrics.Controller
26 {
27  public sealed class ResultsController
28  {
29  #region Singleton utilities
30 
31  private static readonly ResultsController _instance;
32 
37  static ResultsController()
38  {
39  _instance = new ResultsController();
40  }
41 
45  public static ResultsController Instance
46  {
47  get
48  {
49  return _instance;
50  }
51  }
52 
59  {
60  return new ResultsController();
61  }
62 
63  #endregion
64 
65  #region Class definition
66 
67  #region Members
68 
69  private Object _lock = new Object();
70 
74  private ResultsController()
75  {
76  _techniques = new HashSet<string>();
77  _datasets = new HashSet<string>();
78  _results = new Dictionary<string, List<IMetricComputation>>();
79  }
80 
81  private HashSet<string> _techniques;
82  private HashSet<string> _datasets;
83  private Dictionary<string, List<IMetricComputation>> _results;
84 
88  public IEnumerable<string> Techniques
89  {
90  get
91  {
92  lock (_lock)
93  {
94  return new HashSet<string>(_techniques);
95  }
96  }
97  }
98 
102  public IEnumerable<string> Datasets
103  {
104  get
105  {
106  lock (_lock)
107  {
108  return new HashSet<string>(_datasets);
109  }
110  }
111  }
112 
113  #endregion
114 
122  public void AddResult(string technique, string dataset, IMetricComputation computation)
123  {
124  if (String.IsNullOrWhiteSpace(technique))
125  {
126  throw new ComponentException("Technique name cannot be empty.");
127  }
128  if (String.IsNullOrWhiteSpace(dataset))
129  {
130  throw new ComponentException("Dataset name cannot be empty.");
131  }
132  if (computation == null)
133  {
134  throw new ComponentException("Computation cannot be null.");
135  }
136  lock (_lock)
137  {
138  _techniques.Add(technique);
139  _datasets.Add(dataset);
140  string key = ComputeKey(technique, dataset);
141  if (!_results.ContainsKey(key))
142  {
143  _results.Add(key, new List<IMetricComputation>());
144  }
145  _results[key].Add(computation);
146  }
147  }
148 
156  public IEnumerable<IMetricComputation> GetResults(string technique, string dataset)
157  {
158  if (String.IsNullOrWhiteSpace(technique))
159  {
160  throw new ComponentException("Technique name cannot be empty.");
161  }
162  if (String.IsNullOrWhiteSpace(dataset))
163  {
164  throw new ComponentException("Dataset name cannot be empty.");
165  }
166  lock (_lock)
167  {
168  if (!_techniques.Contains(technique))
169  {
170  throw new ComponentException("Collection does not contain technique \"" + technique + "\"");
171  }
172  if (!_datasets.Contains(dataset))
173  {
174  throw new ComponentException("Collection does not contain dataset \"" + dataset + "\"");
175  }
176  List<IMetricComputation> list = null;
177  string key = ComputeKey(technique, dataset);
178  _results.TryGetValue(key, out list);
179  if (list == null)
180  {
181  throw new ComponentException("Could not retrieve data from collection: (" + technique + ", " + dataset + ")");
182  }
183  return new List<IMetricComputation>(list);
184  }
185  }
186 
192  public TLExperimentsResultsCollection GenerateSummaryResults()
193  {
194  lock (_lock)
195  {
196  TLExperimentsResultsCollection ExperimentsResultsCollection = new TLExperimentsResultsCollection();
197  // iterate over techniques
198  foreach (string technique in _techniques)
199  {
200  TLExperimentResults TechniqueResults = new TLExperimentResults(technique);
201  // iterate over datasets
202  foreach (string dataset in _datasets)
203  {
204  // get list of results for technique + dataset
205  List<IMetricComputation> list = null;
206  string key = ComputeKey(technique, dataset);
207  _results.TryGetValue(key, out list);
208  if (list != null)
209  {
210  DatasetResults data = new DatasetResults(dataset);
211  // add results to dataset
212  foreach (IMetricComputation computation in list)
213  {
214  if (!computation.HasRun)
215  {
216  computation.Compute();
217  }
218  data.AddMetric(computation.GenerateSummary());
219  }
220  // add dataset to technique
221  if (data.Metrics.Count() > 0)
222  {
223  TechniqueResults.AddDatasetResult(data);
224  }
225  }
226  }
227  // add technique to collection
228  if (TechniqueResults.DatasetsResults.Count() > 0)
229  {
230  ExperimentsResultsCollection.Add(TechniqueResults);
231  }
232  }
233  return ExperimentsResultsCollection;
234  }
235  }
236 
243  private string ComputeKey(string technique, string dataset)
244  {
245  return technique + "_" + dataset;
246  }
247 
248  #endregion
249  }
250 }