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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using System.Reflection;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.Scripting;
|
---|
29 |
|
---|
30 | namespace 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 {
|
---|
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 | 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 = (ProblemDefinition)Activator.CreateInstance(types.First(x => typeof(ProblemDefinition).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 void OnInstanceChanged() {
|
---|
102 | var handler = InstanceChanged;
|
---|
103 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|