Free cookie consent management tool by TermsFeed Policy Generator

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

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

Restricted name changes to CombinedOperator, Placeholder and ProgrammableOperator (#893)

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