Changeset 10892 for stable/HeuristicLab.Scripting
- Timestamp:
- 05/26/14 16:41:12 (11 years ago)
- Location:
- stable
- Files:
-
- 4 edited
- 4 copied
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
/trunk/sources merged: 10510-10512,10566,10577,10642,10727,10731,10747,10761,10857,10865
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Scripting/3.3/CSharpScript.cs
r10731 r10892 62 62 63 63 #region Fields & Properties 64 pr otected CSharpScriptBase CompiledScript;64 private CSharpScriptBase compiledScript; 65 65 66 66 public string Filename { get; set; } … … 82 82 public CSharpScript() { 83 83 variableStore = new VariableStore(); 84 Code = CodeTemplate; 84 85 } 85 86 public CSharpScript(string code) … … 94 95 95 96 protected virtual void RegisterScriptEvents() { 96 if ( CompiledScript != null)97 CompiledScript.ConsoleOutputChanged += CompiledScriptOnConsoleOutputChanged;97 if (compiledScript != null) 98 compiledScript.ConsoleOutputChanged += CompiledScriptOnConsoleOutputChanged; 98 99 } 99 100 100 101 protected virtual void DeregisterScriptEvents() { 101 if ( CompiledScript != null)102 CompiledScript.ConsoleOutputChanged -= CompiledScriptOnConsoleOutputChanged;102 if (compiledScript != null) 103 compiledScript.ConsoleOutputChanged -= CompiledScriptOnConsoleOutputChanged; 103 104 } 104 105 … … 106 107 107 108 public override Assembly Compile() { 108 CompiledScript = null; 109 DeregisterScriptEvents(); 110 compiledScript = null; 109 111 var assembly = base.Compile(); 110 112 var types = assembly.GetTypes(); 111 DeregisterScriptEvents(); 112 CompiledScript = (CSharpScriptBase)Activator.CreateInstance(types.First(x => typeof(CSharpScriptBase).IsAssignableFrom(x))); 113 compiledScript = (CSharpScriptBase)Activator.CreateInstance(types.Single(x => typeof(CSharpScriptBase).IsAssignableFrom(x))); 113 114 RegisterScriptEvents(); 114 115 return assembly; … … 116 117 #endregion 117 118 118 pr otected Thread ScriptThread;119 private Thread scriptThread; 119 120 public virtual void Execute() { 120 if (CompiledScript == null) return; 121 var executeMethod = typeof(CSharpScriptBase).GetMethod(ExecuteMethodName, BindingFlags.NonPublic | BindingFlags.Instance); 122 if (executeMethod != null) { 123 ScriptThread = new Thread(() => { 124 Exception ex = null; 125 try { 126 OnScriptExecutionStarted(); 127 executeMethod.Invoke(CompiledScript, new object[] { VariableStore }); 128 } catch (ThreadAbortException) { 129 // the execution was cancelled by the user 130 } catch (TargetInvocationException e) { 131 ex = e.InnerException; 132 } finally { 133 OnScriptExecutionFinished(ex); 134 } 135 }); 136 ScriptThread.Start(); 137 } 121 if (compiledScript == null) return; 122 scriptThread = new Thread(() => { 123 Exception ex = null; 124 try { 125 OnScriptExecutionStarted(); 126 compiledScript.Execute(VariableStore); 127 } catch (ThreadAbortException) { 128 // the execution was cancelled by the user 129 } catch (Exception e) { 130 ex = e; 131 } finally { 132 OnScriptExecutionFinished(ex); 133 } 134 }); 135 scriptThread.Start(); 138 136 } 139 137 140 138 public virtual void Kill() { 141 if ( ScriptThread.IsAlive)142 ScriptThread.Abort();139 if (scriptThread.IsAlive) 140 scriptThread.Abort(); 143 141 } 144 142 -
stable/HeuristicLab.Scripting/3.3/CSharpScriptBase.cs
r10731 r10892 52 52 53 53 protected class EventWriter : TextWriter { 54 private readonly CSharpScriptBase usb;54 private readonly CSharpScriptBase script; 55 55 56 public EventWriter(CSharpScriptBase usb) {57 this. usb = usb;56 public EventWriter(CSharpScriptBase script) { 57 this.script = script; 58 58 } 59 59 … … 64 64 #region Write/WriteLine Overrides 65 65 #region Write 66 public override void Write(bool value) { usb.OnConsoleOutputChanged(value.ToString()); }67 public override void Write(char value) { usb.OnConsoleOutputChanged(value.ToString()); }68 public override void Write(char[] buffer) { usb.OnConsoleOutputChanged(new string(buffer)); }69 public override void Write(char[] buffer, int index, int count) { usb.OnConsoleOutputChanged(new string(buffer, index, count)); }70 public override void Write(decimal value) { usb.OnConsoleOutputChanged(value.ToString()); }71 public override void Write(double value) { usb.OnConsoleOutputChanged(value.ToString()); }72 public override void Write(float value) { usb.OnConsoleOutputChanged(value.ToString()); }73 public override void Write(int value) { usb.OnConsoleOutputChanged(value.ToString()); }74 public override void Write(long value) { usb.OnConsoleOutputChanged(value.ToString()); }75 public override void Write(object value) { usb.OnConsoleOutputChanged(value.ToString()); }76 public override void Write(string value) { usb.OnConsoleOutputChanged(value); }77 public override void Write(string format, object arg0) { usb.OnConsoleOutputChanged(string.Format(format, arg0)); }78 public override void Write(string format, object arg0, object arg1) { usb.OnConsoleOutputChanged(string.Format(format, arg0, arg0)); }79 public override void Write(string format, object arg0, object arg1, object arg2) { usb.OnConsoleOutputChanged(string.Format(format, arg0, arg1, arg2)); }80 public override void Write(string format, params object[] arg) { usb.OnConsoleOutputChanged(string.Format(format, arg)); }81 public override void Write(uint value) { usb.OnConsoleOutputChanged(value.ToString()); }82 public override void Write(ulong value) { usb.OnConsoleOutputChanged(value.ToString()); }66 public override void Write(bool value) { script.OnConsoleOutputChanged(value.ToString()); } 67 public override void Write(char value) { script.OnConsoleOutputChanged(value.ToString()); } 68 public override void Write(char[] buffer) { script.OnConsoleOutputChanged(new string(buffer)); } 69 public override void Write(char[] buffer, int index, int count) { script.OnConsoleOutputChanged(new string(buffer, index, count)); } 70 public override void Write(decimal value) { script.OnConsoleOutputChanged(value.ToString()); } 71 public override void Write(double value) { script.OnConsoleOutputChanged(value.ToString()); } 72 public override void Write(float value) { script.OnConsoleOutputChanged(value.ToString()); } 73 public override void Write(int value) { script.OnConsoleOutputChanged(value.ToString()); } 74 public override void Write(long value) { script.OnConsoleOutputChanged(value.ToString()); } 75 public override void Write(object value) { script.OnConsoleOutputChanged(value.ToString()); } 76 public override void Write(string value) { script.OnConsoleOutputChanged(value); } 77 public override void Write(string format, object arg0) { script.OnConsoleOutputChanged(string.Format(format, arg0)); } 78 public override void Write(string format, object arg0, object arg1) { script.OnConsoleOutputChanged(string.Format(format, arg0, arg0)); } 79 public override void Write(string format, object arg0, object arg1, object arg2) { script.OnConsoleOutputChanged(string.Format(format, arg0, arg1, arg2)); } 80 public override void Write(string format, params object[] arg) { script.OnConsoleOutputChanged(string.Format(format, arg)); } 81 public override void Write(uint value) { script.OnConsoleOutputChanged(value.ToString()); } 82 public override void Write(ulong value) { script.OnConsoleOutputChanged(value.ToString()); } 83 83 #endregion 84 84 85 85 #region WriteLine 86 public override void WriteLine() { usb.OnConsoleOutputChanged(Environment.NewLine); }87 public override void WriteLine(bool value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }88 public override void WriteLine(char value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }89 public override void WriteLine(char[] buffer) { usb.OnConsoleOutputChanged(new string(buffer) + Environment.NewLine); }90 public override void WriteLine(char[] buffer, int index, int count) { usb.OnConsoleOutputChanged(new string(buffer, index, count) + Environment.NewLine); }91 public override void WriteLine(decimal value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }92 public override void WriteLine(double value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }93 public override void WriteLine(float value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }94 public override void WriteLine(int value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }95 public override void WriteLine(long value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }96 public override void WriteLine(object value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }97 public override void WriteLine(string value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }98 public override void WriteLine(string format, object arg0) { usb.OnConsoleOutputChanged(string.Format(format, arg0) + Environment.NewLine); }99 public override void WriteLine(string format, object arg0, object arg1) { usb.OnConsoleOutputChanged(string.Format(format, arg0, arg1) + Environment.NewLine); }100 public override void WriteLine(string format, object arg0, object arg1, object arg2) { usb.OnConsoleOutputChanged(string.Format(format, arg0, arg1, arg2) + Environment.NewLine); }101 public override void WriteLine(string format, params object[] arg) { usb.OnConsoleOutputChanged(string.Format(format, arg) + Environment.NewLine); }102 public override void WriteLine(uint value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }103 public override void WriteLine(ulong value) { usb.OnConsoleOutputChanged(value + Environment.NewLine); }86 public override void WriteLine() { script.OnConsoleOutputChanged(Environment.NewLine); } 87 public override void WriteLine(bool value) { script.OnConsoleOutputChanged(value + Environment.NewLine); } 88 public override void WriteLine(char value) { script.OnConsoleOutputChanged(value + Environment.NewLine); } 89 public override void WriteLine(char[] buffer) { script.OnConsoleOutputChanged(new string(buffer) + Environment.NewLine); } 90 public override void WriteLine(char[] buffer, int index, int count) { script.OnConsoleOutputChanged(new string(buffer, index, count) + Environment.NewLine); } 91 public override void WriteLine(decimal value) { script.OnConsoleOutputChanged(value + Environment.NewLine); } 92 public override void WriteLine(double value) { script.OnConsoleOutputChanged(value + Environment.NewLine); } 93 public override void WriteLine(float value) { script.OnConsoleOutputChanged(value + Environment.NewLine); } 94 public override void WriteLine(int value) { script.OnConsoleOutputChanged(value + Environment.NewLine); } 95 public override void WriteLine(long value) { script.OnConsoleOutputChanged(value + Environment.NewLine); } 96 public override void WriteLine(object value) { script.OnConsoleOutputChanged(value + Environment.NewLine); } 97 public override void WriteLine(string value) { script.OnConsoleOutputChanged(value + Environment.NewLine); } 98 public override void WriteLine(string format, object arg0) { script.OnConsoleOutputChanged(string.Format(format, arg0) + Environment.NewLine); } 99 public override void WriteLine(string format, object arg0, object arg1) { script.OnConsoleOutputChanged(string.Format(format, arg0, arg1) + Environment.NewLine); } 100 public override void WriteLine(string format, object arg0, object arg1, object arg2) { script.OnConsoleOutputChanged(string.Format(format, arg0, arg1, arg2) + Environment.NewLine); } 101 public override void WriteLine(string format, params object[] arg) { script.OnConsoleOutputChanged(string.Format(format, arg) + Environment.NewLine); } 102 public override void WriteLine(uint value) { script.OnConsoleOutputChanged(value + Environment.NewLine); } 103 public override void WriteLine(ulong value) { script.OnConsoleOutputChanged(value + Environment.NewLine); } 104 104 #endregion 105 105 #endregion -
stable/HeuristicLab.Scripting/3.3/HeuristicLab.Scripting-3.3.csproj
r10506 r10892 85 85 </ItemGroup> 86 86 <ItemGroup> 87 <Compile Include="CSharpScript.cs" /> 88 <Compile Include="Variables.cs" /> 87 89 <Compile Include="VariableStore.cs" /> 88 90 <None Include="Plugin.cs.frame" /> 89 91 <Compile Include="Script.cs" /> 90 <Compile Include=" UserScriptBase.cs" />92 <Compile Include="CSharpScriptBase.cs" /> 91 93 <Compile Include="Plugin.cs" /> 92 94 <Compile Include="Properties\AssemblyInfo.cs" /> -
stable/HeuristicLab.Scripting/3.3/Script.cs
r10506 r10892 29 29 using System.Reflection; 30 30 using System.Text; 31 using System.Text.RegularExpressions;32 using System.Threading;33 31 using HeuristicLab.Common; 34 32 using HeuristicLab.Common.Resources; … … 38 36 39 37 namespace HeuristicLab.Scripting { 40 [Item("Script", "An empty C# script.")]41 [Creatable("Scripts")]42 38 [StorableClass] 43 public sealed class Script : NamedItem, IStorableContent { 44 #region Constants 45 private const string ExecuteMethodName = "Execute"; 46 private const string CodeTemplate = 47 @"// use 'vars' to access global variables in the variable store 48 49 using System; 50 using System.Linq; 51 using System.Collections.Generic; 52 using HeuristicLab.Common; 53 using HeuristicLab.Core; 54 using HeuristicLab.Data; 55 56 public class UserScript : HeuristicLab.Scripting.UserScriptBase { 57 public override void Main() { 58 // type your code here 59 } 60 61 // further classes and methods 62 63 }"; 64 #endregion 39 public class Script : NamedItem { 40 protected virtual string CodeTemplate { 41 get { return string.Empty; } 42 } 65 43 66 44 #region Fields & Properties 67 private UserScriptBase compiledScript;68 69 public string Filename { get; set; }70 71 45 public static new Image StaticItemImage { 72 46 get { return VSImageLibrary.Script; } 73 }74 75 [Storable]76 private VariableStore variableStore;77 public VariableStore VariableStore {78 get { return variableStore; }79 47 } 80 48 … … 86 54 if (value == code) return; 87 55 code = value; 88 compiledScript = null;89 56 OnCodeChanged(); 90 57 } 91 }92 93 private string compilationUnitCode;94 public string CompilationUnitCode {95 get { return compilationUnitCode; }96 58 } 97 59 … … 108 70 #region Construction & Initialization 109 71 [StorableConstructor] 110 pr ivateScript(bool deserializing) : base(deserializing) { }111 pr ivateScript(Script original, Cloner cloner)72 protected Script(bool deserializing) : base(deserializing) { } 73 protected Script(Script original, Cloner cloner) 112 74 : base(original, cloner) { 113 75 code = original.code; 114 variableStore = new VariableStore();115 compilationUnitCode = original.compilationUnitCode;116 76 if (original.compileErrors != null) 117 77 compileErrors = new CompilerErrorCollection(original.compileErrors); 118 78 } 119 120 79 public Script() 121 : base("Script", "A HeuristicLab script.") { 122 code = CodeTemplate; 123 variableStore = new VariableStore(); 80 : base("Script", "An empty script.") { 81 code = string.Empty; 82 } 83 public Script(string code) 84 : this() { 85 this.code = code; 124 86 } 125 87 … … 129 91 #endregion 130 92 131 private void RegisterScriptEvents() { 132 if (compiledScript == null) return; 133 compiledScript.ConsoleOutputChanged += compiledScript_ConsoleOutputChanged; 93 #region Compilation 94 protected virtual CSharpCodeProvider CodeProvider { 95 get { 96 return new CSharpCodeProvider( 97 new Dictionary<string, string> { 98 {"CompilerVersion", "v4.0"}, // support C# 4.0 syntax 99 }); 100 } 134 101 } 135 102 136 private void DeregisterScriptEvents() { 137 if (compiledScript == null) return; 138 compiledScript.ConsoleOutputChanged -= compiledScript_ConsoleOutputChanged; 139 } 140 141 #region Compilation 142 private CSharpCodeProvider codeProvider = 143 new CSharpCodeProvider( 144 new Dictionary<string, string> { 145 { "CompilerVersion", "v4.0" }, // support C# 4.0 syntax 146 }); 147 148 private CompilerResults DoCompile() { 103 protected virtual CompilerResults DoCompile() { 149 104 var parameters = new CompilerParameters { 150 105 GenerateExecutable = false, … … 159 114 var unit = CreateCompilationUnit(); 160 115 var writer = new StringWriter(); 161 codeProvider.GenerateCodeFromCompileUnit(116 CodeProvider.GenerateCodeFromCompileUnit( 162 117 unit, 163 118 writer, … … 166 121 IndentString = " ", 167 122 }); 168 compilationUnitCode = writer.ToString(); 169 return codeProvider.CompileAssemblyFromDom(parameters, unit); 123 return CodeProvider.CompileAssemblyFromDom(parameters, unit); 170 124 } 171 125 172 public v oidCompile() {126 public virtual Assembly Compile() { 173 127 var results = DoCompile(); 174 compiledScript = null;175 128 CompileErrors = results.Errors; 176 129 if (results.Errors.HasErrors) { … … 181 134 .AppendLine(error.ErrorText); 182 135 } 183 throw new Exception(string.Format( 184 "Compilation of \"{0}\" failed:{1}{2}", 185 Name, Environment.NewLine, 186 sb.ToString())); 136 throw new Exception(string.Format("Compilation of \"{0}\" failed:{1}{2}", 137 Name, Environment.NewLine, sb.ToString())); 187 138 } else { 188 var assembly = results.CompiledAssembly; 189 var types = assembly.GetTypes(); 190 DeregisterScriptEvents(); 191 compiledScript = (UserScriptBase)Activator.CreateInstance(types[0]); 192 RegisterScriptEvents(); 139 return results.CompiledAssembly; 193 140 } 194 141 } 195 142 196 public IEnumerable<Assembly> GetAssemblies() {143 public virtual IEnumerable<Assembly> GetAssemblies() { 197 144 var assemblies = new List<Assembly>(); 198 145 foreach (var a in AppDomain.CurrentDomain.GetAssemblies()) { … … 210 157 } 211 158 212 private readonly Regex SafeTypeNameCharRegex = new Regex("[_a-zA-Z0-9]+"); 213 private readonly Regex SafeTypeNameRegex = new Regex("[_a-zA-Z][_a-zA-Z0-9]*"); 214 215 private CodeCompileUnit CreateCompilationUnit() { 159 protected virtual CodeCompileUnit CreateCompilationUnit() { 216 160 var unit = new CodeSnippetCompileUnit(code); 217 161 return unit; 218 162 } 219 220 public string CompiledTypeName {221 get {222 var sb = new StringBuilder();223 var strings = SafeTypeNameCharRegex.Matches(Name)224 .Cast<Match>()225 .Select(m => m.Value);226 foreach (string s in strings)227 sb.Append(s);228 return SafeTypeNameRegex.Match(sb.ToString()).Value;229 }230 }231 163 #endregion 232 164 233 private Thread scriptThread;234 public void Execute() {235 if (compiledScript == null) return;236 var executeMethod = typeof(UserScriptBase).GetMethod(ExecuteMethodName, BindingFlags.NonPublic | BindingFlags.Instance);237 if (executeMethod != null) {238 scriptThread = new Thread(() => {239 Exception ex = null;240 try {241 OnScriptExecutionStarted();242 executeMethod.Invoke(compiledScript, new[] { VariableStore });243 } catch (ThreadAbortException) {244 // the execution was cancelled by the user245 } catch (TargetInvocationException e) {246 ex = e.InnerException;247 } finally {248 OnScriptExecutionFinished(ex);249 }250 });251 scriptThread.Start();252 }253 }254 255 public void Kill() {256 if (scriptThread.IsAlive)257 scriptThread.Abort();258 }259 260 private void compiledScript_ConsoleOutputChanged(object sender, EventArgs<string> e) {261 OnConsoleOutputChanged(e.Value);262 }263 264 165 public event EventHandler CodeChanged; 265 pr ivatevoid OnCodeChanged() {166 protected virtual void OnCodeChanged() { 266 167 var handler = CodeChanged; 267 168 if (handler != null) handler(this, EventArgs.Empty); … … 269 170 270 171 public event EventHandler CompileErrorsChanged; 271 pr ivatevoid OnCompileErrorsChanged() {172 protected virtual void OnCompileErrorsChanged() { 272 173 var handler = CompileErrorsChanged; 273 174 if (handler != null) handler(this, EventArgs.Empty); 274 175 } 275 276 public event EventHandler ScriptExecutionStarted;277 private void OnScriptExecutionStarted() {278 var handler = ScriptExecutionStarted;279 if (handler != null) handler(this, EventArgs.Empty);280 }281 282 public event EventHandler<EventArgs<Exception>> ScriptExecutionFinished;283 private void OnScriptExecutionFinished(Exception e) {284 var handler = ScriptExecutionFinished;285 if (handler != null) handler(this, new EventArgs<Exception>(e));286 }287 288 public event EventHandler<EventArgs<string>> ConsoleOutputChanged;289 private void OnConsoleOutputChanged(string args) {290 var handler = ConsoleOutputChanged;291 if (handler != null) handler(this, new EventArgs<string>(args));292 }293 176 } 294 177 } -
stable/HeuristicLab.Scripting/3.3/UserScriptBase.cs
r10512 r10892 52 52 var handler = ConsoleOutputChanged; 53 53 if (handler != null) handler(null, new EventArgs<string>(args)); 54 }55 56 protected class Variables : DynamicObject, IEnumerable<KeyValuePair<string, object>> {57 private readonly VariableStore variableStore;58 59 public ICollection<string> Keys {60 get { return variableStore.Keys; }61 }62 63 public ICollection<object> Values {64 get { return variableStore.Values; }65 }66 67 public Variables(VariableStore variableStore) {68 this.variableStore = variableStore;69 }70 71 public override bool TryGetMember(GetMemberBinder binder, out object result) {72 return variableStore.TryGetValue(binder.Name, out result);73 }74 75 public override bool TrySetMember(SetMemberBinder binder, object value) {76 variableStore[binder.Name] = value;77 return true;78 }79 80 public bool Contains(string variableName) {81 return variableStore.ContainsKey(variableName);82 }83 84 public IEnumerator<KeyValuePair<string, object>> GetEnumerator() {85 return variableStore.GetEnumerator();86 }87 88 IEnumerator IEnumerable.GetEnumerator() {89 return GetEnumerator();90 }91 54 } 92 55 -
stable/HeuristicLab.Scripting/3.3/Variables.cs
r10566 r10892 6 6 public class Variables : DynamicObject, IEnumerable<KeyValuePair<string, object>> { 7 7 private readonly VariableStore variableStore; 8 9 public ICollection<string> Keys {10 get { return variableStore.Keys; }11 }12 13 public ICollection<object> Values {14 get { return variableStore.Values; }15 }16 8 17 9 public Variables(VariableStore variableStore) { … … 32 24 } 33 25 26 public void Clear() { 27 variableStore.Clear(); 28 } 29 34 30 public IEnumerator<KeyValuePair<string, object>> GetEnumerator() { 35 31 return variableStore.GetEnumerator();
Note: See TracChangeset
for help on using the changeset viewer.