Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/ProblemDefinitionScript.cs @ 11944

Last change on this file since 11944 was 11944, checked in by abeham, 9 years ago

#2174:

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