Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3303 was 3303, checked in by swagner, 12 years ago

Removed unnecessary checks if the application manager is not null (#954).

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