Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2665 Added Dictionary of stacks to interperter, clear all stacks

File size: 1.8 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        var stackName = Enum.GetName(stackTypesType, type);
37        var stack = interpreter.Stacks[type];
38
39        if (stack.IsEmpty || !stack.IsEnabled) return;
40        Console.WriteLine("--------- {0} ---------\n{1}\n", stackName, stack);
41      }
42
43      if (interpreter.CustomExpressions.Count > 0) {
44        Console.WriteLine("--------- Custom Expressions ---------");
45        foreach (var ce in interpreter.CustomExpressions) {
46          Console.WriteLine("{0}: {1}", ce.Key, ce.Value);
47        }
48      }
49    }
50  }
51}
52
Note: See TracBrowser for help on using the repository browser.