Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/Extensions.cs @ 15017

Last change on this file since 15017 was 15017, checked in by pkimmesw, 7 years ago

#2665 Fixed Benchmark Problem Definition, Converted LoopExpressions to stateless expressions, Added several unit test to ensure funcionality, Fixed UI bugs

File size: 2.0 KB
Line 
1using System.Collections.Generic;
2using System.Linq;
3
4namespace HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter {
5  using System;
6  using System.Collections;
7  using System.Reflection;
8
9  using Attributes;
10
11  using Stack;
12
13  public static class Extensions {
14    private static readonly IDictionary<StackTypes, PropertyInfo> StackProperties = typeof(PushInterpreter)
15        .GetProperties()
16        .Select(p => new { Property = p, Attribute = p.GetCustomAttribute<PushStackAttribute>() })
17        .Where(x => x.Attribute != null)
18        .ToDictionary(x => x.Attribute.StackType, x => x.Property);
19
20    public static IEnumerable<T> GetStackEntriesByType<T>(this IPushInterpreter interpreter, StackTypes stackType) {
21      return StackProperties.ContainsKey(stackType)
22        ? ((IEnumerable)StackProperties[stackType].GetValue(interpreter)).OfType<T>()
23        : null;
24    }
25
26    public static Type GetStackEntryType(this StackTypes stackType) {
27      return StackProperties.ContainsKey(stackType)
28        ? StackProperties[stackType].PropertyType.GetGenericArguments().Single()
29        : null;
30    }
31
32    public static void PrintStacks(this IPushInterpreter interpreter) {
33      var stackTypesType = typeof(StackTypes);
34
35      foreach (StackTypes type in Enum.GetValues(stackTypesType)) {
36        if (!interpreter.Stacks.ContainsKey(type)) continue;
37
38        var stackName = Enum.GetName(stackTypesType, type);
39        var stack = interpreter.Stacks[type];
40
41        if (stack.IsEmpty || !stack.IsEnabled) continue;
42
43        var stackString = string.Join(" ", interpreter.StringifyStack(type).Reverse());
44        Console.WriteLine("--------- {0} ---------\n{1}\n", stackName, stackString);
45      }
46
47      if (interpreter.CustomExpressions.Count > 0) {
48        Console.WriteLine("--------- Custom Expressions ---------");
49        foreach (var ce in interpreter.CustomExpressions) {
50          Console.WriteLine("{0}: {1}", ce.Key, ce.Value);
51        }
52      }
53    }
54  }
55}
56
Note: See TracBrowser for help on using the repository browser.