1 | namespace 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 List<Expression>(index.Length);
|
---|
127 |
|
---|
128 | for (var i = 0; i < index.Length; i++) {
|
---|
129 | expressions.Add(GetExpression(index[i]));
|
---|
130 | }
|
---|
131 |
|
---|
132 | return new ExecExpandExpression(expressions);
|
---|
133 | }
|
---|
134 |
|
---|
135 | public static Expression GetExpression(int index) {
|
---|
136 | return GetExpression(IndexToNameTable[index]);
|
---|
137 | }
|
---|
138 |
|
---|
139 | public static Expression GetExpression(string name) {
|
---|
140 | Expression expression;
|
---|
141 |
|
---|
142 | if (!TryGetStatelessExpression(name, out expression) &&
|
---|
143 | !TryGetStatefullExpression(name, out expression))
|
---|
144 | throw new InvalidOperationException(string.Format("Expression with name {0} not found", name));
|
---|
145 |
|
---|
146 | return expression;
|
---|
147 | }
|
---|
148 |
|
---|
149 | public static Expression GetStatelessExpression<T>() where T : StatelessExpression {
|
---|
150 | return GetStatelessExpression(TypeToNameTable[typeof(T)]);
|
---|
151 | }
|
---|
152 |
|
---|
153 | public static Expression GetStatelessExpression(string name) {
|
---|
154 | Expression expression;
|
---|
155 | if (StatelessExpressionTable.TryGetValue(name, out expression))
|
---|
156 | return expression;
|
---|
157 | throw new NotSupportedException("Expression not supported: " + name);
|
---|
158 | }
|
---|
159 |
|
---|
160 | public static bool TryGetStatelessExpression(string name, out Expression expression) {
|
---|
161 | return StatelessExpressionTable.TryGetValue(name, out expression);
|
---|
162 | }
|
---|
163 |
|
---|
164 | public static Expression GetStatefullExpression<T>() {
|
---|
165 | return GetStatefullExpression(TypeToNameTable[typeof(T)]);
|
---|
166 | }
|
---|
167 |
|
---|
168 | public static Expression GetStatefullExpression(string name) {
|
---|
169 | Func<Expression> creator;
|
---|
170 | if (StatefullExpressionFactory.TryGetValue(name, out creator))
|
---|
171 | return creator();
|
---|
172 | throw new NotSupportedException("Expression not supported: " + name);
|
---|
173 | }
|
---|
174 |
|
---|
175 | public static bool TryGetStatefullExpression(string name, out Expression expression) {
|
---|
176 | Func<Expression> creator;
|
---|
177 |
|
---|
178 | if (StatefullExpressionFactory.TryGetValue(name, out creator)) {
|
---|
179 | expression = creator();
|
---|
180 | return true;
|
---|
181 | }
|
---|
182 |
|
---|
183 | expression = default(Expression);
|
---|
184 | return false;
|
---|
185 | }
|
---|
186 | }
|
---|
187 | } |
---|