[4311] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using HeuristicLab.Optimization;
|
---|
| 6 | using HeuristicLab.Data;
|
---|
| 7 | using HeuristicLab.Collections;
|
---|
| 8 | using HeuristicLab.Core;
|
---|
| 9 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 10 | using HeuristicLab.Common.Resources;
|
---|
| 11 | using HeuristicLab.Common;
|
---|
| 12 |
|
---|
| 13 | namespace HeuristicLab.OKB.AlgorithmHost {
|
---|
| 14 |
|
---|
| 15 | [Item("Algorithm Container", "Encapsulates algorithm & parameters for storing in the OKB")]
|
---|
| 16 | [StorableClass]
|
---|
| 17 | public class AlgorithmContainer : ParameterizedNamedItemContainer {
|
---|
| 18 |
|
---|
| 19 | public AlgorithmContainer() {
|
---|
| 20 | ResultMapping = new ObservableDictionary<string, string>();
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | [StorableConstructor]
|
---|
| 24 | public AlgorithmContainer(bool deserializing) : base(deserializing) { }
|
---|
| 25 |
|
---|
| 26 | public IAlgorithm Algorithm {
|
---|
| 27 | get { return (IAlgorithm)Item; }
|
---|
| 28 | set { Item = value; }
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | public override Type ItemType { get { return typeof(IAlgorithm); } }
|
---|
| 32 |
|
---|
| 33 | public override string ItemName { get { return "AlgorithmContainer"; } }
|
---|
| 34 | public override string ItemDescription { get { return "Container for algorithms to be stored in the OKB"; } }
|
---|
| 35 |
|
---|
| 36 |
|
---|
| 37 | [Storable]
|
---|
| 38 | public ObservableDictionary<string, string> ResultMapping { get; private set; }
|
---|
| 39 |
|
---|
| 40 | private ResultCollection results;
|
---|
| 41 |
|
---|
| 42 | public override void InitializeMapping() {
|
---|
| 43 | base.InitializeMapping();
|
---|
| 44 | Results_CollectionReset(this, new CollectionItemsChangedEventArgs<IResult>(Enumerable.Empty<IResult>()));
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | protected override void RegisterItemEvents() {
|
---|
| 48 | base.RegisterItemEvents();
|
---|
| 49 | Algorithm.Prepared += new EventHandler(Algorithm_Prepared);
|
---|
| 50 | RegisterResultEvents();
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | protected override void DeregisterItemEvents() {
|
---|
| 54 | base.DeregisterItemEvents();
|
---|
| 55 | Algorithm.Prepared -= new EventHandler(Algorithm_Prepared);
|
---|
| 56 | DeregisterResultEvents();
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | private void RegisterResultEvents() {
|
---|
| 60 | results = Algorithm.Results;
|
---|
| 61 | results.CollectionReset += new CollectionItemsChangedEventHandler<IResult>(Results_CollectionReset);
|
---|
| 62 | results.ItemsAdded += new CollectionItemsChangedEventHandler<IResult>(Results_ItemsAdded);
|
---|
| 63 | results.ItemsRemoved += new CollectionItemsChangedEventHandler<IResult>(Results_ItemsRemoved);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | private void DeregisterResultEvents() {
|
---|
| 67 | if (results == null) return;
|
---|
| 68 | results.CollectionReset -= new CollectionItemsChangedEventHandler<IResult>(Results_CollectionReset);
|
---|
| 69 | results.ItemsAdded -= new CollectionItemsChangedEventHandler<IResult>(Results_ItemsAdded);
|
---|
| 70 | results.ItemsRemoved -= new CollectionItemsChangedEventHandler<IResult>(Results_ItemsRemoved);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | void Algorithm_Prepared(object sender, EventArgs e) {
|
---|
| 74 | DeregisterResultEvents();
|
---|
| 75 | RegisterResultEvents();
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | void Results_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IResult> e) {
|
---|
| 79 | foreach (var result in e.OldItems) {
|
---|
| 80 | ResultMapping.Remove(result.Name);
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | void Results_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IResult> e) {
|
---|
| 85 | foreach (var result in e.Items) {
|
---|
| 86 | ResultMapping.Add(result.Name, null);
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | void Results_CollectionReset(object sender, CollectionItemsChangedEventArgs<IResult> e) {
|
---|
| 91 | ResultMapping.Clear();
|
---|
| 92 | foreach (Result r in Algorithm.Results) {
|
---|
| 93 | ResultMapping.Add(r.Name, null);
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | public override HeuristicLab.Common.IDeepCloneable Clone(Cloner cloner) {
|
---|
| 98 | AlgorithmContainer clone = (AlgorithmContainer)base.Clone(cloner);
|
---|
| 99 | clone.ResultMapping = new ObservableDictionary<string, string>();
|
---|
| 100 | foreach (var item in ResultMapping) {
|
---|
| 101 | clone.ResultMapping.Add(item.Key, item.Value);
|
---|
| 102 | }
|
---|
| 103 | return clone;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | public void Strip() {
|
---|
| 107 | DeregisterItemEvents();
|
---|
| 108 | DeregisterResultEvents();
|
---|
| 109 | try { Algorithm.Prepare(true); } catch (Exception) { }
|
---|
| 110 | try { Algorithm.Problem = null; } catch (Exception) { }
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | } |
---|