Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3376 was 3376, checked in by swagner, 14 years ago

Moved interfaces and classes for deep cloning from HeuristicLab.Core to HeuristicLab.Common (#975).

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