Free cookie consent management tool by TermsFeed Policy Generator

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

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

Removed Creatable test attribute (#935).

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