Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/ProblemScript.cs @ 11571

Last change on this file since 11571 was 11484, checked in by abeham, 10 years ago

#2174: Major refactoring

  • Removed ProblemDefinitionHosts
  • Renamed ParameterVector to Individual
  • Renamed Configuration to Encoding
  • Changed handling of existing operators that they will not be removed and recreated, but only rewired
File size: 3.4 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 ProblemScript : Script {
34    protected bool SuppressEvents { get; set; }
35
36    [Storable]
37    private VariableStore variableStore;
38    public VariableStore VariableStore {
39      get { return variableStore; }
40    }
41
42    [StorableConstructor]
43    protected ProblemScript(bool deserializing) : base(deserializing) { }
44    protected ProblemScript(ProblemScript original, Cloner cloner)
45      : base(original, cloner) {
46      variableStore = cloner.Clone(original.variableStore);
47    }
48    protected ProblemScript() {
49      variableStore = new VariableStore();
50    }
51
52    private volatile IProblemDefinition instance;
53    private object locker = new object();
54    public IProblemDefinition Instance {
55      get {
56        SuppressEvents = true;
57        try {
58          var oldInstance = instance;
59          var compilationNecessary = false;
60          if (instance == null) {
61            lock (locker) {
62              if (instance == null) {
63                compilationNecessary = true;
64                Compile();
65              }
66            }
67          }
68          if (compilationNecessary && (oldInstance != null || instance != null))
69            OnInstanceChanged();
70          return instance;
71        } finally {
72          SuppressEvents = false;
73        }
74      }
75      protected set {
76        instance = value;
77        if (!SuppressEvents) OnInstanceChanged();
78      }
79    }
80
81    public override Assembly Compile() {
82      var assembly = base.Compile();
83      var types = assembly.GetTypes();
84      try {
85        var inst = (ProblemScriptBase)Activator.CreateInstance(types.First(x => typeof(ProblemScriptBase).IsAssignableFrom(x)));
86        inst.vars = new Variables(VariableStore);
87        inst.Initialize();
88        Instance = inst;
89      } catch {
90        Instance = null;
91      }
92      return assembly;
93    }
94
95    protected override void OnCodeChanged() {
96      base.OnCodeChanged();
97      instance = null;
98    }
99
100    public event EventHandler InstanceChanged;
101    protected virtual void OnInstanceChanged() {
102      var handler = InstanceChanged;
103      if (handler != null) handler(this, EventArgs.Empty);
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.