Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceOverhaul/HeuristicLab.Problems.ExternalEvaluation/3.4/Programmable/OptimizationSupportScript.cs @ 14711

Last change on this file since 14711 was 13218, checked in by jkarder, 9 years ago

#2298: applied some of the changes suggested by mkommend in comment:14:ticket:2298

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Linq;
24using System.Reflection;
25using HeuristicLab.Common;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Scripting;
28
29namespace HeuristicLab.Problems.ExternalEvaluation.Programmable {
30  public abstract class OptimizationSupportScript<T> : Script
31    where T : class {
32
33    [Storable]
34    private VariableStore variableStore;
35    public VariableStore VariableStore {
36      get { return variableStore; }
37    }
38
39    [StorableConstructor]
40    protected OptimizationSupportScript(bool deserializing) : base(deserializing) { }
41    protected OptimizationSupportScript(OptimizationSupportScript<T> original, Cloner cloner)
42      : base(original, cloner) {
43      variableStore = cloner.Clone(original.variableStore);
44    }
45
46    protected OptimizationSupportScript()
47      : base() {
48      variableStore = new VariableStore();
49    }
50
51    protected OptimizationSupportScript(string code)
52      : base(code) {
53      variableStore = new VariableStore();
54    }
55
56    private readonly object compileLock = new object();
57    private T compiledInstance;
58    protected T CompiledInstance {
59      get {
60        if (compiledInstance == null) {
61          lock (compileLock) {
62            if (compiledInstance == null) {
63              Compile();
64            }
65          }
66        }
67        return compiledInstance;
68      }
69      private set { compiledInstance = value; }
70    }
71
72    public override Assembly Compile() {
73      var assembly = base.Compile();
74      var types = assembly.GetTypes();
75      if (!types.Any(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x)))
76        throw new OptimizationSupportException("The compiled code doesn't contain an optimization support." + Environment.NewLine + "The support class must be a subclass of CompiledOptimizationSupport.");
77      if (types.Count(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x)) > 1)
78        throw new OptimizationSupportException("The compiled code contains multiple support classes." + Environment.NewLine + "Only one subclass of CompiledOptimizationSupport is allowed.");
79
80      CompiledOptimizationSupport inst;
81      try {
82        inst = (CompiledOptimizationSupport)Activator.CreateInstance(types.Single(x => typeof(CompiledOptimizationSupport).IsAssignableFrom(x)));
83        inst.vars = new Variables(VariableStore);
84      } catch (Exception e) {
85        compiledInstance = null;
86        throw new OptimizationSupportException("Instantiating the optimization support class failed." + Environment.NewLine + "Check your default constructor.", e);
87      }
88
89      var concreteInst = inst as T;
90      if (concreteInst == null)
91        throw new OptimizationSupportException("The optimization support class does not implement ISingleObjectiveOptimizationSupport." + Environment.NewLine + "Please implement that interface in the subclass of CompiledOptimizationSupport.");
92
93      CompiledInstance = concreteInst;
94
95      return assembly;
96    }
97
98    protected override void OnCodeChanged() {
99      base.OnCodeChanged();
100      compiledInstance = null;
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.