Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization/HeuristicLab.Problems.MetaOptimization/3.3/ConstrainedTypeValue.cs @ 7153

Last change on this file since 7153 was 7153, checked in by ascheibe, 13 years ago

#1215

  • removed storing of valid types and valid values. This leads to wrong plugin discovery by the Persistence and Hive. Previously references to types of all available algorithms and problems were saved and needed by Hive for deserializing a MetaOpt task. Now only the actual values are saved and valid types and values are discovered after deserialization.
  • added missing license headers
  • added missing plugin references to plugin file
File size: 2.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7using HeuristicLab.PluginInfrastructure;
8
9namespace HeuristicLab.Problems.MetaOptimization {
10  [Item("ConstrainedTypeValue", "Represents a type with constraints.")]
11  [StorableClass]
12  public class ConstrainedTypeValue : TypeValue {
13
14    private IEnumerable<Type> validTypes;
15    public IEnumerable<Type> ValidTypes {
16      get { return validTypes; }
17      set { validTypes = value; }
18    }
19
20    public override Type Value {
21      set {
22        if (!ValidTypes.Contains(value)) throw new NotSupportedException("Value cannot be set. Type is not enlisted in ValidTypes");
23        base.Value = value;
24      }
25    }
26
27    public ConstrainedTypeValue()
28      : base() {
29      this.readOnly = false;
30    }
31    public ConstrainedTypeValue(Type value)
32      : this() {
33      this.Value = value;
34    }
35
36    [StorableConstructor]
37    protected ConstrainedTypeValue(bool deserializing) : base(deserializing) { }
38    protected ConstrainedTypeValue(ConstrainedTypeValue original, Cloner cloner)
39      : base(original, cloner) {
40      this.validTypes = new List<Type>(original.validTypes);
41    }
42    public override IDeepCloneable Clone(Cloner cloner) {
43      return new ConstrainedTypeValue(this, cloner);
44    }
45  }
46
47  [Item("ConstrainedTypeValue<>", "Represents a type with constraints.")]
48  [StorableClass]
49  public class ConstrainedTypeValue<T> : ConstrainedTypeValue where T : class, IItem {
50
51    public ConstrainedTypeValue()
52      : base() {
53      this.ValidTypes = ApplicationManager.Manager.GetTypes(typeof(T), true).ToList();
54      this.Value = ValidTypes.First();
55    }
56    public ConstrainedTypeValue(Type value)
57      : base() {
58      this.ValidTypes = ApplicationManager.Manager.GetTypes(typeof(T), true).ToList();
59      this.Value = value;
60    }
61
62    [StorableConstructor]
63    protected ConstrainedTypeValue(bool deserializing)
64      : base(deserializing) {
65      if (deserializing) {
66        this.ValidTypes = ApplicationManager.Manager.GetTypes(typeof(T), true).ToList();
67      }
68    }
69
70    protected ConstrainedTypeValue(ConstrainedTypeValue original, Cloner cloner) : base(original, cloner) { }
71    public override IDeepCloneable Clone(Cloner cloner) {
72      return new ConstrainedTypeValue<T>(this, cloner);
73    }
74  }
75}
Note: See TracBrowser for help on using the repository browser.