Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6421 was 5337, checked in by cneumuel, 13 years ago

#1215

  • made all IAlgorithm types compatible to be loaded into MetaOptimization.
  • valid problem types are now automatically set
File size: 2.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using System.Drawing;
8using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9using HeuristicLab.Common;
10using HeuristicLab.PluginInfrastructure;
11
12namespace HeuristicLab.Problems.MetaOptimization {
13  [Item("ConstrainedTypeValue", "Represents a type with constraints.")]
14  [StorableClass]
15  public class ConstrainedTypeValue : TypeValue {
16
17    [Storable]
18    private IEnumerable<Type> validTypes;
19    public IEnumerable<Type> ValidTypes {
20      get { return validTypes; }
21      set { validTypes = value; }
22    }
23
24    public override Type Value {
25      set {
26        if (!ValidTypes.Contains(value)) throw new NotSupportedException("Value cannot be set. Type is not enlisted in ValidTypes");
27        base.Value = value;
28      }
29    }
30
31    public ConstrainedTypeValue() : base() {
32      this.readOnly = false;
33    }
34    public ConstrainedTypeValue(Type value) : this() {
35      this.Value = value;
36    }
37
38    [StorableConstructor]
39    protected ConstrainedTypeValue(bool deserializing) : base(deserializing) { }
40    protected ConstrainedTypeValue(ConstrainedTypeValue original, Cloner cloner)
41      : base(original, cloner) {
42      this.validTypes = new List<Type>(original.validTypes);
43    }
44    public override IDeepCloneable Clone(Cloner cloner) {
45      return new ConstrainedTypeValue(this, cloner);
46    }
47  }
48
49  [Item("ConstrainedTypeValue<>", "Represents a type with constraints.")]
50  [StorableClass]
51  public class ConstrainedTypeValue<T> : ConstrainedTypeValue where T : class, IItem {
52
53    public ConstrainedTypeValue() : base() {
54      this.ValidTypes = ApplicationManager.Manager.GetTypes(typeof(T), true).ToList();
55      this.Value = ValidTypes.First();
56    }
57    public ConstrainedTypeValue(Type value) : base() {
58      this.ValidTypes = ApplicationManager.Manager.GetTypes(typeof(T), true).ToList();
59      this.Value = value;
60    }
61    [StorableConstructor]
62    protected ConstrainedTypeValue(bool deserializing) : base(deserializing) { }
63    protected ConstrainedTypeValue(ConstrainedTypeValue original, Cloner cloner) : base(original, cloner) { }
64    public override IDeepCloneable Clone(Cloner cloner) {
65      return new ConstrainedTypeValue<T>(this, cloner);
66    }
67  }
68}
Note: See TracBrowser for help on using the repository browser.