Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4065 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
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;
[3903]40using HeuristicLab.Persistence.Auxiliary;
[2932]41using HeuristicLab.Collections;
[2]42
43namespace HeuristicLab.Operators.Programmable {
44
[2877]45  [Item("ProgrammableOperator", "An operator that can be programmed for arbitrary needs.")]
[3903]46  [StorableClass]
[3008]47  public class ProgrammableOperator : Operator, IParameterizedNamedItem {
[1872]48
[2799]49    #region Fields & Properties
50
[3014]51    public new ParameterCollection Parameters {
[3008]52      get { return base.Parameters; }
53    }
54
[2799]55    private MethodInfo executeMethod;
56    public CompilerErrorCollection CompileErrors { get; private set; }
57    public string CompilationUnitCode { get; private set; }
[2897]58
[1872]59    [Storable]
[2799]60    private string code;
[2]61    public string Code {
[2799]62      get { return code; }
[2]63      set {
[2799]64        if (value != code) {
65          code = value;
[2]66          executeMethod = null;
67          OnCodeChanged();
68        }
69      }
70    }
71
[1211]72    private object syncRoot = new object();
73
[2799]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
[3903]82    [Storable(Name="SelectedAssemblies")]
83    private List<string> _selectedAssemblyNames_persistence {
[2897]84      get {
[3903]85        return Assemblies.Where(a => a.Value).Select(a => a.Key.FullName).ToList();       
[2799]86      }
87      set {
[2897]88        var selectedAssemblyNames = new HashSet<string>(value);
[2799]89        foreach (var a in Assemblies.Keys.ToList()) {
90          Assemblies[a] = selectedAssemblyNames.Contains(a.FullName);
[2897]91        }
[2799]92      }
[2]93    }
94
[2799]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
[2877]109    public override bool CanChangeDescription {
[3024]110      get { return true; }
111    }
[2877]112
[2799]113    #endregion
114
115    #region Extended Accessors
116
117    public void SelectAssembly(Assembly a) {
[3903]118      if (a != null && Assemblies.ContainsKey(a) && !Assemblies[a]) {
[2799]119        Assemblies[a] = true;
[3903]120      }
[2799]121    }
122
123    public void UnselectAssembly(Assembly a) {
[3903]124      if (a != null && Assemblies.ContainsKey(a) && Assemblies[a]) {
[2799]125        Assemblies[a] = false;
[3903]126      }
[2799]127    }
128
129    public void SelectNamespace(string ns) {
130      namespaces.Add(ns);
[3903]131      OnSignatureChanged();
[2799]132    }
133
134    public void UnselectNamespace(string ns) {
135      namespaces.Remove(ns);
[3903]136      OnSignatureChanged();
[2799]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        }
[2]151      }
[2799]152      return namespaces;
[2]153    }
154
[2799]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    }
[2]160
[2799]161    #endregion
[2]162
[2799]163    #region Construction & Initialization
[2]164
[2897]165    public ProgrammableOperator() {
166      code = "";
[2799]167      executeMethod = null;
168      ProgrammableOperator.StaticInitialize();
[3903]169      Assemblies = defaultAssemblyDict.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
170      Plugins = defaultPluginDict.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.ToList());
[3014]171      namespaces = new HashSet<string>(DiscoverNamespaces());
172      RegisterEvents();
[2799]173    }
174
[3014]175    [StorableHook(HookType.AfterDeserialization)]
176    private void RegisterEvents() {
[3903]177      Parameters.ItemsAdded += Parameters_Changed;
178      Parameters.ItemsRemoved += Parameters_Changed;
179      Parameters.ItemsReplaced += Parameters_Changed;
180      Parameters.CollectionReset += Parameters_Changed;
[3014]181    }
182
[3903]183    private void Parameters_Changed(object sender, CollectionItemsChangedEventArgs<IParameter> args) {
184      OnSignatureChanged();
[2897]185    }
186
[3903]187    protected void OnSignatureChanged() {
188      EventHandler handler = SignatureChanged;
189      if (handler != null)
190        handler(this, EventArgs.Empty);
191    }
192
[2799]193    private static void StaticInitialize() {
194      lock (initLock) {
[3903]195        if (defaultPluginDict != null && defaultAssemblyDict != null)
196          return;
[2799]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);
[3234]205
[3303]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);
[2799]216          }
217        }
[3303]218        plugins[plugin.Name] = aList;
[2799]219      }
[3234]220
[2799]221      plugins["other"] = locationTable.Values.ToList();
222      return plugins;
223    }
224
[2897]225    protected static List<Assembly> defaultAssemblies = new List<Assembly>() {
[2799]226      typeof(System.Linq.Enumerable).Assembly,  // add reference to version 3.5 of System.dll
227      typeof(System.Collections.Generic.List<>).Assembly,
[2897]228      typeof(System.Text.StringBuilder).Assembly,
[2799]229      typeof(System.Data.Linq.DataContext).Assembly,
[3454]230      typeof(HeuristicLab.Common.IDeepCloneable).Assembly,
[2897]231      typeof(HeuristicLab.Core.Item).Assembly,
[3048]232      typeof(HeuristicLab.Data.IntValue).Assembly,
[3903]233      typeof(HeuristicLab.Parameters.ValueParameter<IItem>).Assembly,
234      typeof(HeuristicLab.Collections.ObservableList<IItem>).Assembly,
235      typeof(System.ComponentModel.INotifyPropertyChanged).Assembly,
236
[2799]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          }
[2897]246        } catch (NotSupportedException) {
[2799]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>() {
[3903]265        "HeuristicLab.Common",
266        "HeuristicLab.Core",
267        "HeuristicLab.Data",
268        "HeuristicLab.Parameters",
[2799]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() {
[2]288      CompilerParameters parameters = new CompilerParameters();
289      parameters.GenerateExecutable = false;
290      parameters.GenerateInMemory = true;
291      parameters.IncludeDebugInformation = false;
[2799]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    }
[2]306
[2799]307    public virtual void Compile() {
308      var results = DoCompile();
[2]309      executeMethod = null;
310      if (results.Errors.HasErrors) {
[2799]311        CompileErrors = results.Errors;
312        StringBuilder sb = new StringBuilder();
[2]313        foreach (CompilerError error in results.Errors) {
[2799]314          sb.Append(error.Line).Append(':')
315            .Append(error.Column).Append(": ")
316            .AppendLine(error.ErrorText);
[2]317        }
[2799]318        throw new Exception(string.Format(
319          "Compilation of \"{0}\" failed:{1}{2}",
320          Name, Environment.NewLine,
321          sb.ToString()));
[2]322      } else {
[2799]323        CompileErrors = null;
[2]324        Assembly assembly = results.CompiledAssembly;
325        Type[] types = assembly.GetTypes();
326        executeMethod = types[0].GetMethod("Execute");
327      }
328    }
329
[2799]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;
[2]340    }
341
[2799]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);
[1211]357        }
[2799]358        return SafeTypeNameRegex.Match(sb.ToString()).Value;
[116]359      }
[2799]360    }
[2]361
[2799]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()
[3903]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");
[2799]379        foreach (IParameter param in Parameters) {
[3903]380          sb.Append(String.Format(", {0} {1}",
381            TypeNameParser.Parse(param.GetType().FullName).GetTypeNameInCode(namespaces),
382            param.Name));
[2897]383        }
[2799]384        return sb.Append(")").ToString();
[2]385      }
[2799]386    }
[2]387
[2897]388    public event EventHandler SignatureChanged;
389
[2799]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";
[2897]395      method.ReturnType = new CodeTypeReference(typeof(IOperation));
[2799]396      method.Attributes = MemberAttributes.Public | MemberAttributes.Static;
[2897]397      method.Parameters.Add(new CodeParameterDeclarationExpression(typeof(IOperator), "op"));
398      method.Parameters.Add(new CodeParameterDeclarationExpression(typeof(IExecutionContext), "context"));
[2799]399      foreach (var param in Parameters)
[3903]400        method.Parameters.Add(new CodeParameterDeclarationExpression(param.GetType(), param.Name));
[2799]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;
[2]409    }
410
[2799]411    #endregion
412
413    #region HeuristicLab interfaces
[2877]414
415    public override IOperation Apply() {
[2799]416      lock (syncRoot) {
417        if (executeMethod == null) {
418          Compile();
419        }
420      }
421
[2897]422      var parameters = new List<object>() { this, ExecutionContext };
[3903]423      parameters.AddRange(Parameters.Select(p => (object)p));
[2877]424      return (IOperation)executeMethod.Invoke(null, parameters.ToArray());
[2]425    }
[2897]426
[2]427    public event EventHandler CodeChanged;
428    protected virtual void OnCodeChanged() {
429      if (CodeChanged != null)
430        CodeChanged(this, new EventArgs());
431    }
[2799]432
433    #endregion
434
[2897]435    #region Cloning
[2799]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;
[3014]445      clone.RegisterEvents();
[2799]446      return clone;
447    }
[2897]448
[2799]449    #endregion
[2897]450
[2]451  }
452}
Note: See TracBrowser for help on using the repository browser.