Changeset 11739 for branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/ProblemDefinitionScript.cs
- Timestamp:
- 01/08/15 15:52:05 (9 years ago)
- Location:
- branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New
- Files:
-
- 2 added
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/ProgrammableProblem/HeuristicLab.Problems.Programmable/3.3/New/Scripts/ProblemDefinitionScript.cs
r11738 r11739 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Linq; 24 25 using System.Reflection; … … 31 32 [Item("ProblemDefinitionScript", "Script that defines the parameter vector and evaluates the solution for a programmable problem.")] 32 33 [StorableClass] 33 public abstract class Problem Script : Script{34 public abstract class ProblemDefinitionScript : Script, IProblemDefinition { 34 35 protected bool SuppressEvents { get; set; } 35 36 … … 41 42 42 43 [StorableConstructor] 43 protected Problem Script(bool deserializing) : base(deserializing) { }44 protected Problem Script(ProblemScript original, Cloner cloner)44 protected ProblemDefinitionScript(bool deserializing) : base(deserializing) { } 45 protected ProblemDefinitionScript(ProblemDefinitionScript original, Cloner cloner) 45 46 : base(original, cloner) { 46 47 variableStore = cloner.Clone(original.variableStore); 47 48 } 48 protected Problem Script(){49 protected ProblemDefinitionScript() :base(){ 49 50 variableStore = new VariableStore(); 50 51 } 51 52 52 private volatile IProblemDefinition instance; 53 private object locker = new object(); 54 public IProblemDefinition Instance { 53 IEncoding IProblemDefinition.Encoding { 54 get { return CompiledProblemDefinition != null ? CompiledProblemDefinition.Encoding : null; } 55 } 56 IEnumerable<Individual> IProblemDefinition.GetNeighbors(Individual individual, IRandom random) { 57 return CompiledProblemDefinition.GetNeighbors(individual, random); 58 } 59 60 private readonly object locker = new object(); 61 private volatile IProblemDefinition compiledProblemDefinition; 62 protected IProblemDefinition CompiledProblemDefinition { 55 63 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 } 64 lock (locker) { 65 if (compiledProblemDefinition == null) { 66 Compile(); 66 67 } 67 68 } 68 if (compilationNecessary && (oldInstance != null || instance != null)) 69 OnInstanceChanged(); 70 return instance; 71 } finally { 72 SuppressEvents = false; 73 } 69 return compiledProblemDefinition; 74 70 } 75 pr otectedset {76 instance= value;77 if (!SuppressEvents) OnInstanceChanged();71 private set { 72 compiledProblemDefinition = value; 73 OnProblemDefinitionChanged(); 78 74 } 79 75 } … … 83 79 var types = assembly.GetTypes(); 84 80 try { 85 var inst = ( ProblemScriptBase)Activator.CreateInstance(types.First(x => typeof(ProblemScriptBase).IsAssignableFrom(x)));81 var inst = (CompiledProblemDefinition)Activator.CreateInstance(types.First(x => typeof(CompiledProblemDefinition).IsAssignableFrom(x))); 86 82 inst.vars = new Variables(VariableStore); 87 83 inst.Initialize(); 88 Instance = inst; 89 } catch { 90 Instance = null; 84 CompiledProblemDefinition = inst; 85 } 86 catch { 87 compiledProblemDefinition = null; 91 88 } 92 89 return assembly; … … 95 92 protected override void OnCodeChanged() { 96 93 base.OnCodeChanged(); 97 instance= null;94 compiledProblemDefinition = null; 98 95 } 99 96 100 public event EventHandler InstanceChanged;101 protected virtual void On InstanceChanged() {102 var handler = InstanceChanged;97 public event EventHandler ProblemDefinitionChanged; 98 protected virtual void OnProblemDefinitionChanged() { 99 var handler = ProblemDefinitionChanged; 103 100 if (handler != null) handler(this, EventArgs.Empty); 104 101 }
Note: See TracChangeset
for help on using the changeset viewer.