Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization (trunk integration)/HeuristicLab.Problems.MetaOptimization/3.3/ConstrainedTypeValue.cs @ 8589

Last change on this file since 8589 was 8576, checked in by jkarder, 12 years ago

#1853: created branch for MetaOptimization (trunk integration)

File size: 3.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.ParameterConfigurationEncoding;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Problems.MetaOptimization {
32  [Item("ConstrainedTypeValue", "Represents a type with constraints.")]
33  [StorableClass]
34  public class ConstrainedTypeValue : TypeValue {
35    private IEnumerable<Type> validTypes;
36    public IEnumerable<Type> ValidTypes {
37      get { return validTypes; }
38      set { validTypes = value; }
39    }
40
41    public override Type Value {
42      set {
43        if (!ValidTypes.Contains(value)) throw new NotSupportedException("Value cannot be set. Type is not enlisted in ValidTypes");
44        base.Value = value;
45      }
46    }
47
48    #region Constructors and Cloning
49    [StorableConstructor]
50    protected ConstrainedTypeValue(bool deserializing) : base(deserializing) { }
51    protected ConstrainedTypeValue(ConstrainedTypeValue original, Cloner cloner)
52      : base(original, cloner) {
53      if (original.validTypes != null)
54        this.validTypes = new List<Type>(original.validTypes);
55    }
56    public ConstrainedTypeValue()
57      : base() {
58      this.readOnly = false;
59    }
60    public ConstrainedTypeValue(Type value)
61      : this() {
62      this.Value = value;
63    }
64    public override IDeepCloneable Clone(Cloner cloner) {
65      return new ConstrainedTypeValue(this, cloner);
66    }
67    #endregion
68  }
69
70  [Item("ConstrainedTypeValue<>", "Represents a type with constraints.")]
71  [StorableClass]
72  public class ConstrainedTypeValue<T> : ConstrainedTypeValue where T : class, IItem {
73    [StorableConstructor]
74    protected ConstrainedTypeValue(bool deserializing)
75      : base(deserializing) {
76      if (deserializing) {
77        this.ValidTypes = ApplicationManager.Manager.GetTypes(typeof(T), true).ToList();
78      }
79    }
80    protected ConstrainedTypeValue(ConstrainedTypeValue original, Cloner cloner) : base(original, cloner) { }
81    public ConstrainedTypeValue()
82      : base() {
83      this.ValidTypes = ApplicationManager.Manager.GetTypes(typeof(T), true).ToList();
84      this.Value = ValidTypes.First();
85    }
86    public ConstrainedTypeValue(Type value)
87      : base() {
88      this.ValidTypes = ApplicationManager.Manager.GetTypes(typeof(T), true).ToList();
89      this.Value = value;
90    }
91    public override IDeepCloneable Clone(Cloner cloner) {
92      return new ConstrainedTypeValue<T>(this, cloner);
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.