Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11753 was 11753, checked in by mkommend, 9 years ago

#2174: First working version of refactored programmable problem.

File size: 3.5 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    [StorableConstructor]
43    protected ProblemDefinitionScript(bool deserializing) : base(deserializing) { }
44    protected ProblemDefinitionScript(ProblemDefinitionScript original, Cloner cloner)
45      : base(original, cloner) {
46      variableStore = cloner.Clone(original.variableStore);
47    }
48    protected ProblemDefinitionScript()
49      : base() {
50      variableStore = new VariableStore();
51    }
52
53    IEncoding IProblemDefinition.Encoding {
54      get { return CompiledProblemDefinition.Encoding; }
55    }
56
57    private readonly object locker = new object();
58    private volatile IProblemDefinition compiledProblemDefinition;
59    protected IProblemDefinition CompiledProblemDefinition {
60      get {
61        //lock (locker) {
62        //  if (compiledProblemDefinition == null) {
63        //    Compile();
64        //  }
65        //}
66        if (compiledProblemDefinition == null) throw new InvalidOperationException("The problem definition script is not compiled and cannot be used.");
67        return compiledProblemDefinition;
68      }
69      private set {
70        compiledProblemDefinition = value;
71        OnProblemDefinitionChanged();
72      }
73    }
74
75    public override Assembly Compile() {
76      var assembly = base.Compile();
77      var types = assembly.GetTypes();
78      try {
79        var inst = (CompiledProblemDefinition)Activator.CreateInstance(types.First(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x)));
80        inst.vars = new Variables(VariableStore);
81        inst.Initialize();
82        CompiledProblemDefinition = inst;
83      }
84      catch {
85        compiledProblemDefinition = null;
86      }
87      return assembly;
88    }
89
90    protected override void OnCodeChanged() {
91      base.OnCodeChanged();
92      compiledProblemDefinition = null;
93    }
94
95    public event EventHandler ProblemDefinitionChanged;
96    protected virtual void OnProblemDefinitionChanged() {
97      var handler = ProblemDefinitionChanged;
98      if (handler != null) handler(this, EventArgs.Empty);
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.