source: trunk/sources/HeuristicLab.Scripting/3.3/CSharpScript.cs @ 11056

Last change on this file since 11056 was 11056, checked in by jkarder, 5 years ago

#2203: used the persistence to clone the VariableStore

File size: 5.9 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.IO;
24using System.Linq;
25using System.Reflection;
26using System.Threading;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Persistence.Core;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Persistence.Default.Xml;
32
33namespace HeuristicLab.Scripting {
34  [Item("C# Script", "An empty C# script.")]
35  [Creatable("Scripts")]
36  [StorableClass]
37  public class CSharpScript : Script, IStorableContent {
38    #region Constants
39    protected const string ExecuteMethodName = "Execute";
40    protected override string CodeTemplate {
41      get {
42        return @"// use 'vars' to access variables in the script's variable store (e.g. vars.x = 5)
43// use 'vars.Contains(string)' to check if a variable exists
44// use 'vars.Clear()' to remove all variables
45// use 'foreach (KeyValuePair<string, object> v in vars) { ... }' to iterate over all variables
46
47using System;
48using System.Linq;
49using System.Collections.Generic;
50using HeuristicLab.Common;
51using HeuristicLab.Core;
52using HeuristicLab.Data;
53
54public class MyScript : HeuristicLab.Scripting.CSharpScriptBase {
55  public override void Main() {
56    // type your code here
57  }
58
59  // implement further classes and methods
60
61}";
62      }
63    }
64    #endregion
65
66    #region Fields & Properties
67    private CSharpScriptBase compiledScript;
68
69    public string Filename { get; set; }
70
71    [Storable]
72    private VariableStore variableStore;
73    public VariableStore VariableStore {
74      get { return variableStore; }
75    }
76    #endregion
77
78    #region Construction & Initialization
79    [StorableConstructor]
80    protected CSharpScript(bool deserializing) : base(deserializing) { }
81    protected CSharpScript(CSharpScript original, Cloner cloner)
82      : base(original, cloner) {
83      try {
84        using (var serializerStream = new MemoryStream()) {
85          XmlGenerator.Serialize(original.variableStore, serializerStream);
86          var bytes = serializerStream.GetBuffer();
87          using (var deserializerStream = new MemoryStream(bytes)) {
88            variableStore = XmlParser.Deserialize<VariableStore>(deserializerStream);
89          }
90        }
91      } catch (PersistenceException pe) {
92        throw new NotSupportedException("Could not clone the script because some of its variables could not be cloned.", pe);
93      }
94    }
95    public CSharpScript() {
96      variableStore = new VariableStore();
97      Code = CodeTemplate;
98    }
99    public CSharpScript(string code)
100      : base(code) {
101      variableStore = new VariableStore();
102    }
103
104    public override IDeepCloneable Clone(Cloner cloner) {
105      return new CSharpScript(this, cloner);
106    }
107    #endregion
108
109    protected virtual void RegisterScriptEvents() {
110      if (compiledScript != null)
111        compiledScript.ConsoleOutputChanged += CompiledScriptOnConsoleOutputChanged;
112    }
113
114    protected virtual void DeregisterScriptEvents() {
115      if (compiledScript != null)
116        compiledScript.ConsoleOutputChanged -= CompiledScriptOnConsoleOutputChanged;
117    }
118
119    #region Compilation
120
121    public override Assembly Compile() {
122      DeregisterScriptEvents();
123      compiledScript = null;
124      var assembly = base.Compile();
125      var types = assembly.GetTypes();
126      compiledScript = (CSharpScriptBase)Activator.CreateInstance(types.Single(x => typeof(CSharpScriptBase).IsAssignableFrom(x)));
127      RegisterScriptEvents();
128      return assembly;
129    }
130    #endregion
131
132    private Thread scriptThread;
133    public virtual void Execute() {
134      if (compiledScript == null) return;
135      scriptThread = new Thread(() => {
136        Exception ex = null;
137        try {
138          OnScriptExecutionStarted();
139          compiledScript.Execute(VariableStore);
140        } catch (ThreadAbortException) {
141          // the execution was cancelled by the user
142        } catch (Exception e) {
143          ex = e;
144        } finally {
145          OnScriptExecutionFinished(ex);
146        }
147      });
148      scriptThread.Start();
149    }
150
151    public virtual void Kill() {
152      if (scriptThread.IsAlive)
153        scriptThread.Abort();
154    }
155
156    protected virtual void CompiledScriptOnConsoleOutputChanged(object sender, EventArgs<string> e) {
157      OnConsoleOutputChanged(e.Value);
158    }
159
160    public event EventHandler ScriptExecutionStarted;
161    protected virtual void OnScriptExecutionStarted() {
162      var handler = ScriptExecutionStarted;
163      if (handler != null) handler(this, EventArgs.Empty);
164    }
165
166    public event EventHandler<EventArgs<Exception>> ScriptExecutionFinished;
167    protected virtual void OnScriptExecutionFinished(Exception e) {
168      var handler = ScriptExecutionFinished;
169      if (handler != null) handler(this, new EventArgs<Exception>(e));
170    }
171
172    public event EventHandler<EventArgs<string>> ConsoleOutputChanged;
173    protected virtual void OnConsoleOutputChanged(string args) {
174      var handler = ConsoleOutputChanged;
175      if (handler != null) handler(this, new EventArgs<string>(args));
176    }
177  }
178}
Note: See TracBrowser for help on using the repository browser.