Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Operators.Programmable/3.3/ProgrammableOperator.cs @ 11923

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

Merged cloning refactoring branch back into trunk (#922)

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