Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Scripting/3.3/Scripts/CSharp/CSharpScript.cs @ 12087

Last change on this file since 12087 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 System.Threading;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Scripting {
31  [Item("C# Script", "An empty C# script.")]
32  [Creatable("Scripts")]
33  [StorableClass]
34  public class CSharpScript : Script, IStorableContent {
35    #region Constants
36    protected const string ExecuteMethodName = "Execute";
37    protected override string CodeTemplate { get { return ScriptTemplates.CSharpScriptTemplate; } }
38    #endregion
39
40    #region Fields & Properties
41    private CSharpScriptBase compiledScript;
42
43    public string Filename { get; set; }
44
45    [Storable]
46    private VariableStore variableStore;
47    public VariableStore VariableStore {
48      get { return variableStore; }
49    }
50    #endregion
51
52    #region Construction & Initialization
53    [StorableConstructor]
54    protected CSharpScript(bool deserializing) : base(deserializing) { }
55    protected CSharpScript(CSharpScript original, Cloner cloner)
56      : base(original, cloner) {
57      variableStore = cloner.Clone(original.variableStore);
58    }
59    public CSharpScript() {
60      variableStore = new VariableStore();
61      Code = CodeTemplate;
62    }
63    public CSharpScript(string code)
64      : base(code) {
65      variableStore = new VariableStore();
66    }
67
68    public override IDeepCloneable Clone(Cloner cloner) {
69      return new CSharpScript(this, cloner);
70    }
71    #endregion
72
73    protected virtual void RegisterScriptEvents() {
74      if (compiledScript != null)
75        compiledScript.ConsoleOutputChanged += CompiledScriptOnConsoleOutputChanged;
76    }
77
78    protected virtual void DeregisterScriptEvents() {
79      if (compiledScript != null)
80        compiledScript.ConsoleOutputChanged -= CompiledScriptOnConsoleOutputChanged;
81    }
82
83    #region Compilation
84
85    public override Assembly Compile() {
86      DeregisterScriptEvents();
87      compiledScript = null;
88      var assembly = base.Compile();
89      var types = assembly.GetTypes();
90      compiledScript = (CSharpScriptBase)Activator.CreateInstance(types.Single(x => typeof(CSharpScriptBase).IsAssignableFrom(x)));
91      RegisterScriptEvents();
92      return assembly;
93    }
94    #endregion
95
96    private Thread scriptThread;
97    public virtual void ExecuteAsync() {
98      if (compiledScript == null) return;
99      scriptThread = new Thread(() => {
100        Exception ex = null;
101        try {
102          OnScriptExecutionStarted();
103          compiledScript.Execute(VariableStore);
104        } catch (Exception e) {
105          ex = e;
106        } finally {
107          scriptThread = null;
108          OnScriptExecutionFinished(ex);
109        }
110      });
111      scriptThread.SetApartmentState(ApartmentState.STA);
112      scriptThread.Start();
113    }
114
115    public virtual void Kill() {
116      if (scriptThread == null) return;
117      scriptThread.Abort();
118    }
119
120    protected virtual void CompiledScriptOnConsoleOutputChanged(object sender, EventArgs<string> e) {
121      OnConsoleOutputChanged(e.Value);
122    }
123
124    public event EventHandler ScriptExecutionStarted;
125    protected virtual void OnScriptExecutionStarted() {
126      var handler = ScriptExecutionStarted;
127      if (handler != null) handler(this, EventArgs.Empty);
128    }
129
130    public event EventHandler<EventArgs<Exception>> ScriptExecutionFinished;
131    protected virtual void OnScriptExecutionFinished(Exception e) {
132      var handler = ScriptExecutionFinished;
133      if (handler != null) handler(this, new EventArgs<Exception>(e));
134    }
135
136    public event EventHandler<EventArgs<string>> ConsoleOutputChanged;
137    protected virtual void OnConsoleOutputChanged(string args) {
138      var handler = ConsoleOutputChanged;
139      if (handler != null) handler(this, new EventArgs<string>(args));
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.