Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Operators.Programmable/3.3/ProgrammableOperator.cs @ 18199

Last change on this file since 18199 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 16.4 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2]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;
[4068]23using System.CodeDom;
24using System.CodeDom.Compiler;
[2]25using System.Collections.Generic;
26using System.IO;
[694]27using System.Linq;
[2]28using System.Reflection;
[4068]29using System.Text;
[2]30using System.Text.RegularExpressions;
[4068]31using HeuristicLab.Collections;
[3376]32using HeuristicLab.Common;
[5042]33using HeuristicLab.Common.Resources;
[2]34using HeuristicLab.Core;
[4068]35using HeuristicLab.Persistence.Auxiliary;
[17097]36using HEAL.Attic;
[2799]37using HeuristicLab.PluginInfrastructure;
[4068]38using Microsoft.CSharp;
[2]39
40namespace HeuristicLab.Operators.Programmable {
41
[2877]42  [Item("ProgrammableOperator", "An operator that can be programmed for arbitrary needs.")]
[17097]43  [StorableType("741279E4-4C38-4D66-B9F1-03E0F8B47A78")]
[12686]44  public class ProgrammableOperator : Operator, IParameterizedNamedItem, IStorableContent, IProgrammableItem {
[1872]45
[2799]46    #region Fields & Properties
47
[4419]48    public string Filename { get; set; }
49
[3014]50    public new ParameterCollection Parameters {
[3008]51      get { return base.Parameters; }
52    }
53
[7201]54    public static new System.Drawing.Image StaticItemImage { get { return VSImageLibrary.Script; } }
[5042]55
[2799]56    private MethodInfo executeMethod;
57    public CompilerErrorCollection CompileErrors { get; private set; }
58    public string CompilationUnitCode { get; private set; }
[2897]59
[1872]60    [Storable]
[2799]61    private string code;
[2]62    public string Code {
[2799]63      get { return code; }
[2]64      set {
[2799]65        if (value != code) {
66          code = value;
[2]67          executeMethod = null;
68          OnCodeChanged();
69        }
70      }
71    }
72
[1211]73    private object syncRoot = new object();
74
[2799]75    private static object initLock = new object();
76    private static Dictionary<string, List<Assembly>> defaultPluginDict;
77    private static Dictionary<Assembly, bool> defaultAssemblyDict;
78
79    public readonly Dictionary<string, List<Assembly>> Plugins;
80    protected Dictionary<Assembly, bool> Assemblies;
81
[4068]82    [Storable(Name = "SelectedAssemblies")]
[3903]83    private List<string> _selectedAssemblyNames_persistence {
[2897]84      get {
[4068]85        return Assemblies.Where(a => a.Value).Select(a => a.Key.FullName).ToList();
[2799]86      }
87      set {
[5781]88        var selectedAssemblyNames = new HashSet<string>(value.Select(n => new AssemblyName(n).Name));
[2799]89        foreach (var a in Assemblies.Keys.ToList()) {
[5781]90          Assemblies[a] = selectedAssemblyNames.Contains(new AssemblyName(a.FullName).Name);
[2897]91        }
[2799]92      }
[2]93    }
94
[2799]95    public IEnumerable<Assembly> AvailableAssemblies {
96      get { return Assemblies.Keys; }
97    }
98
99    public IEnumerable<Assembly> SelectedAssemblies {
100      get { return Assemblies.Where(kvp => kvp.Value).Select(kvp => kvp.Key); }
101    }
102
103    [Storable]
104    private HashSet<string> namespaces;
105    public IEnumerable<string> Namespaces {
106      get { return namespaces; }
107    }
108
[2877]109    public override bool CanChangeDescription {
[3024]110      get { return true; }
111    }
[2877]112
[2799]113    #endregion
114
115    #region Extended Accessors
116
117    public void SelectAssembly(Assembly a) {
[3903]118      if (a != null && Assemblies.ContainsKey(a) && !Assemblies[a]) {
[2799]119        Assemblies[a] = true;
[3903]120      }
[2799]121    }
122
123    public void UnselectAssembly(Assembly a) {
[3903]124      if (a != null && Assemblies.ContainsKey(a) && Assemblies[a]) {
[2799]125        Assemblies[a] = false;
[3903]126      }
[2799]127    }
128
129    public void SelectNamespace(string ns) {
130      namespaces.Add(ns);
131    }
132
133    public void UnselectNamespace(string ns) {
134      namespaces.Remove(ns);
135    }
136
137    public IEnumerable<string> GetAllNamespaces(bool selectedAssembliesOnly) {
138      var namespaces = new HashSet<string>();
139      foreach (var a in Assemblies) {
140        if (!selectedAssembliesOnly || a.Value) {
141          foreach (var t in a.Key.GetTypes()) {
142            if (t.IsPublic) {
143              foreach (string ns in GetNamespaceHierachy(t.Namespace)) {
144                namespaces.Add(ns);
145              }
146            }
147          }
148        }
[2]149      }
[2799]150      return namespaces;
[2]151    }
152
[2799]153    private IEnumerable<string> GetNamespaceHierachy(string ns) {
[5011]154      if (ns != null) {
155        for (int i = ns.Length; i != -1; i = ns.LastIndexOf('.', i - 1)) {
156          yield return ns.Substring(0, i);
157        }
[2799]158      }
159    }
[2]160
[2799]161    #endregion
[2]162
[2799]163    #region Construction & Initialization
[4762]164
[4722]165    [StorableConstructor]
[17097]166    protected ProgrammableOperator(StorableConstructorFlag _) : base(_) {
[4762]167      ProgrammableOperator.StaticInitialize();
168      Assemblies = defaultAssemblyDict.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
169      Plugins = defaultPluginDict.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.ToList());
[4722]170    }
[2]171
[4722]172    protected ProgrammableOperator(ProgrammableOperator original, Cloner cloner)
173      : base(original, cloner) {
174      code = original.Code;
175      executeMethod = original.executeMethod;
176      Assemblies = original.Assemblies.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
[4762]177      Plugins = original.Plugins.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
178      namespaces = new HashSet<string>(original.namespaces);
[4722]179      CompilationUnitCode = original.CompilationUnitCode;
[4764]180      if (original.CompileErrors != null)
181        CompileErrors = new CompilerErrorCollection(original.CompileErrors);
[4722]182      RegisterEvents();
183    }
184
[2897]185    public ProgrammableOperator() {
186      code = "";
[2799]187      executeMethod = null;
188      ProgrammableOperator.StaticInitialize();
[3903]189      Assemblies = defaultAssemblyDict.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
190      Plugins = defaultPluginDict.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.ToList());
[3014]191      namespaces = new HashSet<string>(DiscoverNamespaces());
192      RegisterEvents();
[2799]193    }
194
[4762]195    [StorableHook(HookType.AfterDeserialization)]
196    private void AfterDeserialization() {
[4838]197      // ensure default namespaces and assemblies are present if deserializing old operators
198      namespaces.Add("HeuristicLab.Operators.Programmable");
199      Assemblies[typeof(HeuristicLab.Operators.Operator).Assembly] = true;
200      Assemblies[typeof(HeuristicLab.Operators.Programmable.ProgrammableOperator).Assembly] = true;
[5173]201      Assemblies[typeof(System.Threading.Barrier).Assembly] = true; // ensure new System assembly is selected (4.0.0.0)
[4762]202      RegisterEvents();
203    }
[4722]204
[4762]205    public override IDeepCloneable Clone(Cloner cloner) {
206      return new ProgrammableOperator(this, cloner);
207    }
208
[3014]209    private void RegisterEvents() {
[3903]210      Parameters.ItemsAdded += Parameters_Changed;
211      Parameters.ItemsRemoved += Parameters_Changed;
212      Parameters.ItemsReplaced += Parameters_Changed;
213      Parameters.CollectionReset += Parameters_Changed;
[3014]214    }
215
[3903]216    private void Parameters_Changed(object sender, CollectionItemsChangedEventArgs<IParameter> args) {
217      OnSignatureChanged();
[2897]218    }
219
[3903]220    protected void OnSignatureChanged() {
[4828]221      executeMethod = null;
[3903]222      EventHandler handler = SignatureChanged;
[4722]223      if (handler != null) handler(this, EventArgs.Empty);
[3903]224    }
225
[2799]226    private static void StaticInitialize() {
227      lock (initLock) {
[3903]228        if (defaultPluginDict != null && defaultAssemblyDict != null)
229          return;
[2799]230        defaultAssemblyDict = DiscoverAssemblies();
231        defaultPluginDict = GroupAssemblies(defaultAssemblyDict.Keys);
232      }
233    }
234
235    private static Dictionary<string, List<Assembly>> GroupAssemblies(IEnumerable<Assembly> assemblies) {
236      var plugins = new Dictionary<string, List<Assembly>>();
237      var locationTable = assemblies.ToDictionary(a => a.Location, a => a);
[3234]238
[3303]239      foreach (var plugin in ApplicationManager.Manager.Plugins) {
240        var aList = new List<Assembly>();
241        foreach (var aName in from file in plugin.Files
242                              where file.Type == PluginFileType.Assembly
243                              select file.Name) {
244          Assembly a;
245          locationTable.TryGetValue(aName, out a);
246          if (a != null) {
247            aList.Add(a);
248            locationTable.Remove(aName);
[2799]249          }
250        }
[3303]251        plugins[plugin.Name] = aList;
[2799]252      }
[3234]253
[2799]254      plugins["other"] = locationTable.Values.ToList();
255      return plugins;
256    }
257
[2897]258    protected static List<Assembly> defaultAssemblies = new List<Assembly>() {
[5216]259      // mscorlib automatically included (would cause duplicate)
260      typeof(System.ComponentModel.INotifyPropertyChanged).Assembly, // System.dll
261      typeof(System.Linq.Enumerable).Assembly,  // System.Core.dll
262      typeof(System.Data.Linq.DataContext).Assembly, // System.Data.Linq.dll
[3454]263      typeof(HeuristicLab.Common.IDeepCloneable).Assembly,
[2897]264      typeof(HeuristicLab.Core.Item).Assembly,
[3048]265      typeof(HeuristicLab.Data.IntValue).Assembly,
[3903]266      typeof(HeuristicLab.Parameters.ValueParameter<IItem>).Assembly,
267      typeof(HeuristicLab.Collections.ObservableList<IItem>).Assembly,
[4838]268      typeof(HeuristicLab.Operators.Operator).Assembly,
269      typeof(HeuristicLab.Operators.Programmable.ProgrammableOperator).Assembly,
[2799]270    };
271
272    protected static Dictionary<Assembly, bool> DiscoverAssemblies() {
273      var assemblies = new Dictionary<Assembly, bool>();
274      foreach (var a in AppDomain.CurrentDomain.GetAssemblies()) {
275        try {
276          if (File.Exists(a.Location)) {
277            assemblies.Add(a, false);
278          }
[12686]279        } catch (NotSupportedException) {
[2799]280          // NotSupportedException is thrown while accessing
281          // the Location property of the anonymously hosted
282          // dynamic methods assembly, which is related to
283          // LINQ queries
284        }
285      }
286      foreach (var a in defaultAssemblies) {
287        if (assemblies.ContainsKey(a)) {
288          assemblies[a] = true;
289        } else {
290          assemblies.Add(a, true);
291        }
292      }
293      return assemblies;
294    }
295
296    protected static List<string> DiscoverNamespaces() {
297      return new List<string>() {
[3903]298        "HeuristicLab.Common",
299        "HeuristicLab.Core",
300        "HeuristicLab.Data",
301        "HeuristicLab.Parameters",
[4838]302        "HeuristicLab.Operators.Programmable",
[2799]303        "System",
304        "System.Collections.Generic",
305        "System.Text",
306        "System.Linq",
307        "System.Data.Linq",
308      };
309    }
310
311    #endregion
312
313    #region Compilation
314
315    private static CSharpCodeProvider codeProvider =
316      new CSharpCodeProvider(
317        new Dictionary<string, string>() {
[5406]318          { "CompilerVersion", "v4.0" },  // support C# 4.0 syntax
[2799]319        });
320
321    private CompilerResults DoCompile() {
[2]322      CompilerParameters parameters = new CompilerParameters();
323      parameters.GenerateExecutable = false;
324      parameters.GenerateInMemory = true;
325      parameters.IncludeDebugInformation = false;
[2799]326      parameters.ReferencedAssemblies.AddRange(SelectedAssemblies.Select(a => a.Location).ToArray());
327      var unit = CreateCompilationUnit();
328      var writer = new StringWriter();
329      codeProvider.GenerateCodeFromCompileUnit(
330        unit,
331        writer,
332        new CodeGeneratorOptions() {
333          BracingStyle = "C",
334          ElseOnClosing = true,
335          IndentString = "  ",
336        });
337      CompilationUnitCode = writer.ToString();
338      return codeProvider.CompileAssemblyFromDom(parameters, unit);
339    }
[2]340
[2799]341    public virtual void Compile() {
342      var results = DoCompile();
[2]343      executeMethod = null;
[5042]344      CompileErrors = results.Errors;
[2]345      if (results.Errors.HasErrors) {
[2799]346        StringBuilder sb = new StringBuilder();
[2]347        foreach (CompilerError error in results.Errors) {
[2799]348          sb.Append(error.Line).Append(':')
349            .Append(error.Column).Append(": ")
350            .AppendLine(error.ErrorText);
[2]351        }
[2799]352        throw new Exception(string.Format(
353          "Compilation of \"{0}\" failed:{1}{2}",
354          Name, Environment.NewLine,
355          sb.ToString()));
[2]356      } else {
357        Assembly assembly = results.CompiledAssembly;
358        Type[] types = assembly.GetTypes();
359        executeMethod = types[0].GetMethod("Execute");
360      }
361    }
362
[2799]363    private CodeCompileUnit CreateCompilationUnit() {
364      CodeNamespace ns = new CodeNamespace("HeuristicLab.Operators.Programmable.CustomOperators");
365      ns.Types.Add(CreateType());
366      ns.Imports.AddRange(
367        GetSelectedAndValidNamespaces()
368        .Select(n => new CodeNamespaceImport(n))
369        .ToArray());
370      CodeCompileUnit unit = new CodeCompileUnit();
371      unit.Namespaces.Add(ns);
372      return unit;
[2]373    }
374
[2799]375    public IEnumerable<string> GetSelectedAndValidNamespaces() {
376      var possibleNamespaces = new HashSet<string>(GetAllNamespaces(true));
377      foreach (var ns in Namespaces)
378        if (possibleNamespaces.Contains(ns))
379          yield return ns;
380    }
381
382    public static readonly Regex SafeTypeNameCharRegex = new Regex("[_a-zA-Z0-9]+");
383    public static readonly Regex SafeTypeNameRegex = new Regex("[_a-zA-Z][_a-zA-Z0-9]*");
384
385    public string CompiledTypeName {
386      get {
387        var sb = new StringBuilder();
388        foreach (string s in SafeTypeNameCharRegex.Matches(Name).Cast<Match>().Select(m => m.Value)) {
389          sb.Append(s);
[1211]390        }
[2799]391        return SafeTypeNameRegex.Match(sb.ToString()).Value;
[116]392      }
[2799]393    }
[2]394
[2799]395    private CodeTypeDeclaration CreateType() {
396      CodeTypeDeclaration typeDecl = new CodeTypeDeclaration(CompiledTypeName) {
397        IsClass = true,
398        TypeAttributes = TypeAttributes.Public,
399      };
400      typeDecl.Members.Add(CreateMethod());
401      return typeDecl;
402    }
403
404    public string Signature {
405      get {
406        var sb = new StringBuilder()
[3903]407        .Append("public static IOperation Execute(")
[4838]408        .Append(TypeNameParser.Parse(GetType().FullName).GetTypeNameInCode(namespaces))
[3903]409        .Append(" op, ")
410        .Append(TypeNameParser.Parse(typeof(IExecutionContext).FullName).GetTypeNameInCode(namespaces))
411        .Append(" context");
[2799]412        foreach (IParameter param in Parameters) {
[3903]413          sb.Append(String.Format(", {0} {1}",
414            TypeNameParser.Parse(param.GetType().FullName).GetTypeNameInCode(namespaces),
415            param.Name));
[2897]416        }
[2799]417        return sb.Append(")").ToString();
[2]418      }
[2799]419    }
[2]420
[2897]421    public event EventHandler SignatureChanged;
422
[2799]423    private static Regex lineSplitter = new Regex(@"\r\n|\r|\n");
424
425    private CodeMemberMethod CreateMethod() {
426      CodeMemberMethod method = new CodeMemberMethod();
427      method.Name = "Execute";
[2897]428      method.ReturnType = new CodeTypeReference(typeof(IOperation));
[2799]429      method.Attributes = MemberAttributes.Public | MemberAttributes.Static;
[4838]430      method.Parameters.Add(new CodeParameterDeclarationExpression(GetType(), "op"));
[2897]431      method.Parameters.Add(new CodeParameterDeclarationExpression(typeof(IExecutionContext), "context"));
[2799]432      foreach (var param in Parameters)
[3903]433        method.Parameters.Add(new CodeParameterDeclarationExpression(param.GetType(), param.Name));
[2799]434      string[] codeLines = lineSplitter.Split(code);
435      for (int i = 0; i < codeLines.Length; i++) {
436        codeLines[i] = string.Format("#line {0} \"ProgrammableOperator\"{1}{2}", i + 1, "\r\n", codeLines[i]);
437      }
[4838]438      method.Statements.Add(new CodeSnippetStatement(string.Join("\r\n", codeLines) + MethodSuffix));
[2799]439      return method;
[2]440    }
441
[4838]442    public virtual string MethodSuffix {
443      get { return "return null;"; }
444    }
445
[2799]446    #endregion
447
448    #region HeuristicLab interfaces
[2877]449
450    public override IOperation Apply() {
[2799]451      lock (syncRoot) {
452        if (executeMethod == null) {
453          Compile();
454        }
455      }
456
[2897]457      var parameters = new List<object>() { this, ExecutionContext };
[3903]458      parameters.AddRange(Parameters.Select(p => (object)p));
[2877]459      return (IOperation)executeMethod.Invoke(null, parameters.ToArray());
[2]460    }
[2897]461
[2]462    public event EventHandler CodeChanged;
463    protected virtual void OnCodeChanged() {
[4722]464      EventHandler handler = CodeChanged;
465      if (handler != null) handler(this, EventArgs.Empty);
[2]466    }
[2799]467
468    #endregion
[2]469  }
470}
Note: See TracBrowser for help on using the repository browser.