Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/ExpressionTable.cs @ 14744

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

#2665 Renamings due to typos, ManagedPool tests, Skip Noops in Debugger

File size: 7.0 KB
Line 
1namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
2  using System;
3  using System.Collections.Generic;
4  using System.Linq;
5
6  using HeuristicLab.Problems.ProgramSynthesis.Push.Attributes;
7  using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
8
9  public static class ExpressionTable {
10    public static readonly IDictionary<string, Expression> StatelessExpressionTable;
11    public static readonly IDictionary<string, Func<Expression>> StatefullExpressionFactory;
12    public static readonly IDictionary<StackType, IList<string>> StackTypeToNamesTable = new Dictionary<StackType, IList<string>>();
13    public static readonly IDictionary<int, string> IndexToNameTable = new Dictionary<int, string>();
14    public static readonly IDictionary<Type, string> TypeToNameTable = new Dictionary<Type, string>();
15    public static readonly string[] ExpressionNames;
16
17    public static readonly int Count;
18
19    static ExpressionTable() {
20      StatelessExpressionTable = GetStatelessExpressionTable();
21      StatefullExpressionFactory = GetStatefullExpressionFactory();
22      ExpressionNames = StatelessExpressionTable.Keys.Concat(StatefullExpressionFactory.Keys).ToArray();
23
24      Count = StatelessExpressionTable.Count + StatefullExpressionFactory.Count;
25    }
26
27    public static int StatelessCount
28    {
29      get
30      {
31        return StatelessExpressionTable.Count;
32      }
33    }
34
35    public static int StatefullCount
36    {
37      get
38      {
39        return StatefullExpressionFactory.Count;
40      }
41    }
42
43    private static Dictionary<string, Expression> GetStatelessExpressionTable() {
44      var dictionary = new Dictionary<string, Expression>();
45      var expressionTypes = GetExpressionTypes(typeof(StatelessExpression));
46
47      foreach (var type in expressionTypes) {
48        var expression = Activator.CreateInstance(type) as Expression;
49        var attribute = (PushExpressionAttribute)Attribute.GetCustomAttribute(type, typeof(PushExpressionAttribute));
50
51        dictionary.Add(attribute.ExpressionName, expression);
52        IndexToNameTable.Add(IndexToNameTable.Keys.Count, attribute.ExpressionName);
53        TypeToNameTable.Add(type, attribute.ExpressionName);
54
55        if (!StackTypeToNamesTable.ContainsKey(attribute.StackType)) {
56          StackTypeToNamesTable.Add(attribute.StackType, new List<string>());
57        }
58
59        StackTypeToNamesTable[attribute.StackType].Add(attribute.ExpressionName);
60      }
61
62      return dictionary;
63    }
64
65    private static Dictionary<string, Func<Expression>> GetStatefullExpressionFactory() {
66      var dictionary = new Dictionary<string, Func<Expression>>();
67      var statefullExpressionType = typeof(StatefulExpression<>);
68      var expressionTypes = GetExpressionTypes(statefullExpressionType);
69
70      foreach (var type in expressionTypes) {
71        // Make a NewExpression that calls the ctor
72        var newExp = System.Linq.Expressions.Expression.New(type);
73
74        // Create a lambda with the New expression as body
75        var creator = System.Linq.Expressions.Expression.Lambda<Func<Expression>>(newExp).Compile();
76        var attribute = (PushExpressionAttribute)Attribute.GetCustomAttribute(type, typeof(PushExpressionAttribute));
77
78        dictionary.Add(attribute.ExpressionName, creator);
79        IndexToNameTable.Add(IndexToNameTable.Keys.Count, attribute.ExpressionName);
80        TypeToNameTable.Add(type, attribute.ExpressionName);
81
82        if (!StackTypeToNamesTable.ContainsKey(attribute.StackType)) {
83          StackTypeToNamesTable.Add(attribute.StackType, new List<string>());
84        }
85
86        StackTypeToNamesTable[attribute.StackType].Add(attribute.ExpressionName);
87      }
88
89      return dictionary;
90    }
91
92    private static bool IsSubclassOf(Type type, Type baseType) {
93      if (type == null || baseType == null || type == baseType)
94        return false;
95
96      if (baseType.IsGenericType == false) {
97        if (type.IsGenericType == false)
98          return type.IsSubclassOf(baseType);
99      } else {
100        baseType = baseType.GetGenericTypeDefinition();
101      }
102
103      type = type.BaseType;
104      var objectType = typeof(object);
105      while (type != objectType && type != null) {
106        var curentType = type.IsGenericType ? type.GetGenericTypeDefinition() : type;
107        if (curentType == baseType)
108          return true;
109
110        type = type.BaseType;
111      }
112
113      return false;
114    }
115
116    private static IEnumerable<Type> GetExpressionTypes(Type baseType) {
117      return from domainAssembly in AppDomain.CurrentDomain.GetAssemblies()
118             from assemblyType in domainAssembly.GetTypes()
119             where !assemblyType.IsAbstract &&
120                   IsSubclassOf(assemblyType, baseType) &&
121                   assemblyType.GetConstructor(Type.EmptyTypes) != null
122             select assemblyType;
123    }
124
125    public static ExecExpandExpression GetProgram(int[] index) {
126      var expressions = new Expression[index.Length];
127
128      for (var i = 0; i < index.Length; i++) {
129        expressions[i] = GetExpression(index[i]);
130      }
131
132      var program = new PushProgram(expressions);
133      return new ExecExpandExpression(program);
134    }
135
136    public static Expression GetExpression(int index) {
137      return GetExpression(IndexToNameTable[index]);
138    }
139
140    public static Expression GetExpression(string name) {
141      Expression expression;
142
143      if (!TryGetStatelessExpression(name, out expression) &&
144          !TryGetStatefullExpression(name, out expression))
145        throw new InvalidOperationException(string.Format("Expression with name {0} not found", name));
146
147      return expression;
148    }
149
150    public static Expression GetStatelessExpression<T>() where T : StatelessExpression {
151      return GetStatelessExpression(TypeToNameTable[typeof(T)]);
152    }
153
154    public static Expression GetStatelessExpression(string name) {
155      Expression expression;
156      if (StatelessExpressionTable.TryGetValue(name, out expression))
157        return expression;
158      throw new NotSupportedException("Expression not supported: " + name);
159    }
160
161    public static bool TryGetStatelessExpression(string name, out Expression expression) {
162      return StatelessExpressionTable.TryGetValue(name, out expression);
163    }
164
165    public static Expression GetStatefullExpression<T>() {
166      return GetStatefullExpression(TypeToNameTable[typeof(T)]);
167    }
168
169    public static Expression GetStatefullExpression(string name) {
170      Func<Expression> creator;
171      if (StatefullExpressionFactory.TryGetValue(name, out creator))
172        return creator();
173      throw new NotSupportedException("Expression not supported: " + name);
174    }
175
176    public static bool TryGetStatefullExpression(string name, out Expression expression) {
177      Func<Expression> creator;
178
179      if (StatefullExpressionFactory.TryGetValue(name, out creator)) {
180        expression = creator();
181        return true;
182      }
183
184      expression = default(Expression);
185      return false;
186    }
187  }
188}
Note: See TracBrowser for help on using the repository browser.