Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4040 was 3903, checked in by epitzer, 14 years ago

Incorporate changes suggested by abeham in 842#comment:28 (#842)

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