Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Operators.Programmable/3.3/ProgrammableOperator.cs @ 4857

Last change on this file since 4857 was 4838, checked in by epitzer, 14 years ago

Better reflect single successor operation generation inside user visible code. (#1275)

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