Free cookie consent management tool by TermsFeed Policy Generator

source: branches/New Persistence Exploration/Persistence/Persistence/Core/PersistenceConfiguration.cs @ 1373

Last change on this file since 1373 was 1373, checked in by epitzer, 15 years ago

Simple GUI configuration for decomposers. (#506)

File size: 4.0 KB
RevLine 
[1360]1using System;
2using System.Collections.Generic;
3using System.Reflection;
4using HeuristicLab.Persistence.Interfaces;
5
6namespace HeuristicLab.Persistence {
7
8  public class Configuration {
9
10    private readonly Dictionary<Type, IFormatter> formatters;
11    private readonly List<IDecomposer> decomposers;
12    private readonly Dictionary<Type, IDecomposer> decomposerCache;
13    public readonly IFormat Format;
14
15    public Configuration(Dictionary<Type, IFormatter> formatters, IEnumerable<IDecomposer> decomposers) {     
16      this.formatters = new Dictionary<Type, IFormatter>();
17      foreach ( var pair in formatters ) {
18        if (Format == null) {
19          Format = pair.Value.Format;
20        } else if (pair.Value.Format != Format ) {
21          throw new ArgumentException("All formatters must have the same IFormat.");
22        }
23        this.formatters.Add(pair.Key, pair.Value);
24      }
25      this.decomposers = new List<IDecomposer>(decomposers);
26      decomposerCache = new Dictionary<Type, IDecomposer>();     
27    }   
28
29    public IFormatter GetFormatter(Type type) {
30      IFormatter formatter;
31      formatters.TryGetValue(type, out formatter);
32      return formatter;
33    }
34
35    public IDecomposer GetDecomposer(Type type) {
36      IDecomposer decomposer;
37      decomposerCache.TryGetValue(type, out decomposer);
38      if (decomposer != null)
39        return decomposer;
40      foreach (IDecomposer d in decomposers) {
41        if (d.CanDecompose(type)) {
42          decomposerCache.Add(type, d);
43          return d;
44        }
45      }
46      return null;
47    }
48   
49  }
50
51  public class ConfigurationService {
52
[1373]53    private static ConfigurationService instance;
[1360]54    private readonly Dictionary<IFormat, List<IFormatter>> formatters;
55    private readonly List<IDecomposer> decomposers;
[1373]56
57    public Dictionary<IFormat, List<IFormatter>> AllFormatters {
58      get {
59        return formatters;
60      }
61    }
62
63    public List<IDecomposer> AllDecomposers {
64      get {
65        return decomposers;
66      }
67    }
[1360]68   
69    public static ConfigurationService Instance {
70      get {
71        if (instance == null) {
72          instance = new ConfigurationService();
73        }
74        return instance;
75      }
76    }
77
78    public ConfigurationService() {
79      formatters = new Dictionary<IFormat, List<IFormatter>>();
80      decomposers = new List<IDecomposer>();
81      Reset();
82    }
83
84    public void Reset() {
85      Assembly defaultAssembly = Assembly.GetExecutingAssembly();
86      DiscoverFrom(defaultAssembly);
87      foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
88        if ( a != defaultAssembly )
89          DiscoverFrom(a);
90    }
91
92    protected void DiscoverFrom(Assembly a) {
93      foreach (Type t in a.GetTypes()) {
94        if (t.GetInterface(typeof (IFormatter).FullName) != null) {
95          try {
96            IFormatter formatter = (IFormatter) Activator.CreateInstance(t, true);
97            if ( ! formatters.ContainsKey(formatter.Format) ) {
98              formatters.Add(formatter.Format, new List<IFormatter>());
99            }
100            formatters[formatter.Format].Add(formatter);
101          } catch (MissingMethodException) {
102            Console.WriteLine("WARNING: Could not instantiate {0}", t.FullName);
103          }         
104        }
105        if (t.GetInterface(typeof (IDecomposer).FullName) != null) {
106          try {
107            decomposers.Add((IDecomposer) Activator.CreateInstance(t, true));
108          } catch (MissingMethodException) {
109            Console.WriteLine("WARNING: Could not instantiate {0}", t.FullName);
110          }
111        }
112      }
113    }
114
115    public Configuration GetDefaultConfig(IFormat format) {
116      Dictionary<Type, IFormatter> formatterConfig = new Dictionary<Type, IFormatter>();
117      foreach ( IFormatter f in formatters[format] ) {
118        if ( ! formatterConfig.ContainsKey(f.Type) )
119          formatterConfig.Add(f.Type, f);
120      }
121      return new Configuration(formatterConfig, decomposers);
122    }
123
124  }
125 
126}
Note: See TracBrowser for help on using the repository browser.