Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.Programmable/3.3/ProblemDefinitionScript.cs @ 13869

Last change on this file since 13869 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: 5.3 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.Core;
27using HeuristicLab.Optimization;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Scripting;
30
31namespace HeuristicLab.Problems.Programmable {
32  [Item("ProblemDefinitionScript", "Script that defines the parameter vector and evaluates the solution for a programmable problem.")]
33  [StorableClass]
34  public abstract class ProblemDefinitionScript : Script, IProblemDefinition {
35    protected bool SuppressEvents { get; set; }
36
37    [Storable]
38    private VariableStore variableStore;
39    public VariableStore VariableStore {
40      get { return variableStore; }
41    }
42
43    [Storable]
44    private bool codeChanged;
45
46    [StorableConstructor]
47    protected ProblemDefinitionScript(bool deserializing) : base(deserializing) { }
48    protected ProblemDefinitionScript(ProblemDefinitionScript original, Cloner cloner)
49      : base(original, cloner) {
50      variableStore = cloner.Clone(original.variableStore);
51      codeChanged = original.codeChanged;
52    }
53    protected ProblemDefinitionScript()
54      : base() {
55      variableStore = new VariableStore();
56    }
57    protected ProblemDefinitionScript(string code)
58      : base(code) {
59      variableStore = new VariableStore();
60    }
61
62    IEncoding IProblemDefinition.Encoding {
63      get { return CompiledProblemDefinition.Encoding; }
64    }
65
66    private readonly object compileLock = new object();
67    private volatile IProblemDefinition compiledProblemDefinition;
68    protected IProblemDefinition CompiledProblemDefinition {
69      get {
70        // double checked locking pattern
71        if (compiledProblemDefinition == null) {
72          lock (compileLock) {
73            if (compiledProblemDefinition == null) {
74              if (codeChanged) throw new ProblemDefinitionScriptException("The code has been changed, but was not recompiled.");
75              Compile(false);
76            }
77          }
78        }
79        return compiledProblemDefinition;
80      }
81    }
82
83    public sealed override Assembly Compile() {
84      return Compile(true);
85    }
86
87    private Assembly Compile(bool fireChanged) {
88      var assembly = base.Compile();
89      var types = assembly.GetTypes();
90      if (!types.Any(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x)))
91        throw new ProblemDefinitionScriptException("The compiled code doesn't contain a problem definition." + Environment.NewLine + "The problem definition must be a subclass of CompiledProblemDefinition.");
92      if (types.Count(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x)) > 1)
93        throw new ProblemDefinitionScriptException("The compiled code contains multiple problem definitions." + Environment.NewLine + "Only one subclass of CompiledProblemDefinition is allowed.");
94
95      CompiledProblemDefinition inst;
96      try {
97        inst = (CompiledProblemDefinition)Activator.CreateInstance(types.Single(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x)));
98      } catch (Exception e) {
99        compiledProblemDefinition = null;
100        throw new ProblemDefinitionScriptException("Instantiating the problem definition failed." + Environment.NewLine + "Check your default constructor.", e);
101      }
102
103      try {
104        inst.vars = new Variables(VariableStore);
105        inst.Initialize();
106      } catch (Exception e) {
107        compiledProblemDefinition = null;
108        throw new ProblemDefinitionScriptException("Initializing the problem definition failed." + Environment.NewLine + "Check your Initialize() method.", e);
109      }
110
111      try {
112        compiledProblemDefinition = inst;
113        if (fireChanged) OnProblemDefinitionChanged();
114      } catch (Exception e) {
115        compiledProblemDefinition = null;
116        throw new ProblemDefinitionScriptException("Using the problem definition in the problem failed." + Environment.NewLine + "Examine this error message carefully (often there is an issue with the defined encoding).", e);
117      }
118
119      codeChanged = false;
120      return assembly;
121    }
122
123    protected override void OnCodeChanged() {
124      base.OnCodeChanged();
125      compiledProblemDefinition = null;
126      codeChanged = true;
127    }
128
129    public event EventHandler ProblemDefinitionChanged;
130    protected virtual void OnProblemDefinitionChanged() {
131      var handler = ProblemDefinitionChanged;
132      if (handler != null) handler(this, EventArgs.Empty);
133    }
134  }
135}
Note: See TracBrowser for help on using the repository browser.