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
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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;
27using System.Linq;
28using System.Reflection;
29using System.CodeDom;
30using System.CodeDom.Compiler;
31using Microsoft.CSharp;
32using System.Text.RegularExpressions;
33using HeuristicLab.Common;
34using HeuristicLab.Core;
35using HeuristicLab.Data;
36using System.Data.Linq;
37using System.Xml.XPath;
38using HeuristicLab.PluginInfrastructure;
39using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
40using HeuristicLab.Collections;
41
42namespace HeuristicLab.Operators.Programmable {
43
44  [Item("ProgrammableOperator", "An operator that can be programmed for arbitrary needs.")]
45  [StorableClass] 
46  public class ProgrammableOperator : Operator, IParameterizedNamedItem {
47
48    #region Fields & Properties
49
50    public new ParameterCollection Parameters {
51      get { return base.Parameters; }
52    }
53
54    private MethodInfo executeMethod;
55    public CompilerErrorCollection CompileErrors { get; private set; }
56    public string CompilationUnitCode { get; private set; }
57
58    [Storable]
59    private string code;
60    public string Code {
61      get { return code; }
62      set {
63        if (value != code) {
64          code = value;
65          executeMethod = null;
66          OnCodeChanged();
67        }
68      }
69    }
70
71    private object syncRoot = new object();
72
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]
82    private List<string> _persistedAssemblyNames {
83      get {
84        return Assemblies.Keys.Select(a => a.FullName).ToList();
85      }
86      set {
87        var selectedAssemblyNames = new HashSet<string>(value);
88        foreach (var a in Assemblies.Keys.ToList()) {
89          Assemblies[a] = selectedAssemblyNames.Contains(a.FullName);
90        }
91      }
92    }
93
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
108    public override bool CanChangeDescription {
109      get { return true; }
110    }
111
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
134    public void SetDescription(string description) {
135      if (description == null)
136        throw new NullReferenceException("description must not be null");
137      Description = description;
138    }
139
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        }
152      }
153      return namespaces;
154    }
155
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    }
161
162    #endregion
163
164    #region Construction & Initialization
165
166    public ProgrammableOperator() {
167      code = "";
168      executeMethod = null;
169      ProgrammableOperator.StaticInitialize();
170      Assemblies = defaultAssemblyDict;
171      Plugins = defaultPluginDict;
172      namespaces = new HashSet<string>(DiscoverNamespaces());
173      RegisterEvents();
174    }
175
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
184    protected void OnSignatureChanged(object sender, CollectionItemsChangedEventArgs<IParameter> args) {
185      if (SignatureChanged != null)
186        SignatureChanged(sender, EventArgs.Empty);
187    }
188
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);
200
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);
211          }
212        }
213        plugins[plugin.Name] = aList;
214      }
215
216      plugins["other"] = locationTable.Values.ToList();
217      return plugins;
218    }
219
220    protected static List<Assembly> defaultAssemblies = new List<Assembly>() {
221      typeof(System.Linq.Enumerable).Assembly,  // add reference to version 3.5 of System.dll
222      typeof(System.Collections.Generic.List<>).Assembly,
223      typeof(System.Text.StringBuilder).Assembly,
224      typeof(System.Data.Linq.DataContext).Assembly,
225      typeof(HeuristicLab.Core.Item).Assembly,
226      typeof(HeuristicLab.Data.IntValue).Assembly,
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          }
236        } catch (NotSupportedException) {
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() {
276      CompilerParameters parameters = new CompilerParameters();
277      parameters.GenerateExecutable = false;
278      parameters.GenerateInMemory = true;
279      parameters.IncludeDebugInformation = false;
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    }
294
295    public virtual void Compile() {
296      var results = DoCompile();
297      executeMethod = null;
298      if (results.Errors.HasErrors) {
299        CompileErrors = results.Errors;
300        StringBuilder sb = new StringBuilder();
301        foreach (CompilerError error in results.Errors) {
302          sb.Append(error.Line).Append(':')
303            .Append(error.Column).Append(": ")
304            .AppendLine(error.ErrorText);
305        }
306        throw new Exception(string.Format(
307          "Compilation of \"{0}\" failed:{1}{2}",
308          Name, Environment.NewLine,
309          sb.ToString()));
310      } else {
311        CompileErrors = null;
312        Assembly assembly = results.CompiledAssembly;
313        Type[] types = assembly.GetTypes();
314        executeMethod = types[0].GetMethod("Execute");
315      }
316    }
317
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;
328    }
329
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);
345        }
346        return SafeTypeNameRegex.Match(sb.ToString()).Value;
347      }
348    }
349
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()
362        .Append("public static IOperation Execute(IOperator op, IExecutionContext context");
363        foreach (IParameter param in Parameters) {
364          sb.Append(String.Format(", {0} {1}", param.DataType.Name, param.Name));
365        }
366        return sb.Append(")").ToString();
367      }
368    }
369
370    public event EventHandler SignatureChanged;
371
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";
377      method.ReturnType = new CodeTypeReference(typeof(IOperation));
378      method.Attributes = MemberAttributes.Public | MemberAttributes.Static;
379      method.Parameters.Add(new CodeParameterDeclarationExpression(typeof(IOperator), "op"));
380      method.Parameters.Add(new CodeParameterDeclarationExpression(typeof(IExecutionContext), "context"));
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;
391    }
392
393    #endregion
394
395    #region HeuristicLab interfaces
396
397    public override IOperation Apply() {
398      lock (syncRoot) {
399        if (executeMethod == null) {
400          Compile();
401        }
402      }
403
404      var parameters = new List<object>() { this, ExecutionContext };
405      parameters.AddRange(Parameters.Select(p => (object)p.ActualValue));
406      return (IOperation)executeMethod.Invoke(null, parameters.ToArray());
407    }
408
409    public event EventHandler CodeChanged;
410    protected virtual void OnCodeChanged() {
411      if (CodeChanged != null)
412        CodeChanged(this, new EventArgs());
413    }
414
415    #endregion
416
417    #region Cloning
418
419    public override IDeepCloneable Clone(Cloner cloner) {
420      ProgrammableOperator clone = (ProgrammableOperator)base.Clone(cloner);
421      clone.description = description;
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;
428      clone.RegisterEvents();
429      return clone;
430    }
431
432    #endregion
433
434  }
435}
Note: See TracBrowser for help on using the repository browser.