Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 12012 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

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