[10731] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[10731] | 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 System.Threading;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 |
|
---|
| 30 | namespace 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";
|
---|
[11787] | 37 | protected override string CodeTemplate { get { return ScriptTemplates.CSharpScriptTemplate; } }
|
---|
[10731] | 38 | #endregion
|
---|
| 39 |
|
---|
| 40 | #region Fields & Properties
|
---|
[10857] | 41 | private CSharpScriptBase compiledScript;
|
---|
[10731] | 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) {
|
---|
[11058] | 57 | variableStore = cloner.Clone(original.variableStore);
|
---|
[10731] | 58 | }
|
---|
| 59 | public CSharpScript() {
|
---|
| 60 | variableStore = new VariableStore();
|
---|
[10857] | 61 | Code = CodeTemplate;
|
---|
[10731] | 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() {
|
---|
[10857] | 74 | if (compiledScript != null)
|
---|
| 75 | compiledScript.ConsoleOutputChanged += CompiledScriptOnConsoleOutputChanged;
|
---|
[10731] | 76 | }
|
---|
| 77 |
|
---|
| 78 | protected virtual void DeregisterScriptEvents() {
|
---|
[10857] | 79 | if (compiledScript != null)
|
---|
| 80 | compiledScript.ConsoleOutputChanged -= CompiledScriptOnConsoleOutputChanged;
|
---|
[10731] | 81 | }
|
---|
| 82 |
|
---|
| 83 | #region Compilation
|
---|
| 84 |
|
---|
| 85 | public override Assembly Compile() {
|
---|
[10857] | 86 | DeregisterScriptEvents();
|
---|
| 87 | compiledScript = null;
|
---|
[10731] | 88 | var assembly = base.Compile();
|
---|
| 89 | var types = assembly.GetTypes();
|
---|
[10857] | 90 | compiledScript = (CSharpScriptBase)Activator.CreateInstance(types.Single(x => typeof(CSharpScriptBase).IsAssignableFrom(x)));
|
---|
[10731] | 91 | RegisterScriptEvents();
|
---|
| 92 | return assembly;
|
---|
| 93 | }
|
---|
| 94 | #endregion
|
---|
| 95 |
|
---|
[10857] | 96 | private Thread scriptThread;
|
---|
[11734] | 97 | public virtual void ExecuteAsync() {
|
---|
[10857] | 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 {
|
---|
[11834] | 107 | scriptThread = null;
|
---|
[10857] | 108 | OnScriptExecutionFinished(ex);
|
---|
| 109 | }
|
---|
| 110 | });
|
---|
[11514] | 111 | scriptThread.SetApartmentState(ApartmentState.STA);
|
---|
[10857] | 112 | scriptThread.Start();
|
---|
[10731] | 113 | }
|
---|
| 114 |
|
---|
| 115 | public virtual void Kill() {
|
---|
[11834] | 116 | if (scriptThread == null) return;
|
---|
| 117 | scriptThread.Abort();
|
---|
[10731] | 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 | }
|
---|