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

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

Adapted views according the new read-only property (#973)

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