[14727] | 1 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Expressions {
|
---|
| 2 | using System;
|
---|
| 3 | using System.Collections.Generic;
|
---|
| 4 | using System.Linq;
|
---|
| 5 |
|
---|
[14875] | 6 | using Attributes;
|
---|
| 7 | using Data.Pool;
|
---|
| 8 | using Stack;
|
---|
[14727] | 9 |
|
---|
| 10 | public static class ExpressionTable {
|
---|
[14875] | 11 | public static readonly IReadOnlyDictionary<string, Expression> StatelessExpressionTable;
|
---|
| 12 | public static readonly IReadOnlyDictionary<string, Func<Expression>> StatefulExpressionFactory;
|
---|
| 13 | public static readonly IReadOnlyList<string> ExpressionNames;
|
---|
| 14 | public static readonly int ExpressionCount;
|
---|
[14727] | 15 |
|
---|
[14875] | 16 | private static readonly Dictionary<int, string> indexToNameTable = new Dictionary<int, string>();
|
---|
| 17 | private static readonly Dictionary<Type, string> typeToNameTable = new Dictionary<Type, string>();
|
---|
| 18 | private static readonly Dictionary<string, Type> nameToTypeTable = new Dictionary<string, Type>();
|
---|
| 19 | private static readonly Dictionary<StackTypes, IList<string>> stackTypeToNamesTable = new Dictionary<StackTypes, IList<string>>();
|
---|
| 20 | private static readonly Dictionary<StackTypes, IList<string>> stackDependencyToNamesTable = new Dictionary<StackTypes, IList<string>>();
|
---|
| 21 | private static readonly Dictionary<Type, PushExpressionAttribute> typeToAttributeTable = new Dictionary<Type, PushExpressionAttribute>();
|
---|
[14777] | 22 |
|
---|
[14875] | 23 | public static readonly IReadOnlyList<Type> StatelessExpressionTypes;
|
---|
| 24 | public static readonly IReadOnlyList<Type> StatefulExpressionTypes;
|
---|
[14727] | 25 |
|
---|
| 26 | static ExpressionTable() {
|
---|
[14777] | 27 | StatelessExpressionTypes = GetExpressionTypes(typeof(StatelessExpression)).ToArray();
|
---|
| 28 | StatefulExpressionTypes = GetExpressionTypes(typeof(StatefulExpression<>)).ToArray();
|
---|
| 29 |
|
---|
[14727] | 30 | StatelessExpressionTable = GetStatelessExpressionTable();
|
---|
[14777] | 31 | StatefulExpressionFactory = GetStatefulExpressionFactory();
|
---|
| 32 | ExpressionNames = StatelessExpressionTable.Keys.Concat(StatefulExpressionFactory.Keys).ToArray();
|
---|
[14727] | 33 |
|
---|
[14875] | 34 | ExpressionCount = StatelessExpressionTable.Count + StatefulExpressionFactory.Count;
|
---|
[14727] | 35 | }
|
---|
| 36 |
|
---|
[14875] | 37 | public static IReadOnlyDictionary<int, string> IndexToNameTable { get { return indexToNameTable; } }
|
---|
| 38 | public static IReadOnlyDictionary<Type, string> TypeToNameTable { get { return typeToNameTable; } }
|
---|
| 39 | public static IReadOnlyDictionary<string, Type> NameToTypeTable { get { return nameToTypeTable; } }
|
---|
| 40 | public static IReadOnlyDictionary<StackTypes, IList<string>> StackTypeToNamesTable { get { return stackTypeToNamesTable; } }
|
---|
| 41 | public static IReadOnlyDictionary<StackTypes, IList<string>> StackDependencyToNamesTable { get { return stackDependencyToNamesTable; } }
|
---|
| 42 | public static IReadOnlyDictionary<Type, PushExpressionAttribute> TypeToAttributeTable { get { return typeToAttributeTable; } }
|
---|
[14727] | 43 |
|
---|
| 44 | private static Dictionary<string, Expression> GetStatelessExpressionTable() {
|
---|
| 45 | var dictionary = new Dictionary<string, Expression>();
|
---|
| 46 |
|
---|
[14777] | 47 | foreach (var type in StatelessExpressionTypes) {
|
---|
[14727] | 48 | var expression = Activator.CreateInstance(type) as Expression;
|
---|
| 49 | var attribute = (PushExpressionAttribute)Attribute.GetCustomAttribute(type, typeof(PushExpressionAttribute));
|
---|
| 50 |
|
---|
[14875] | 51 | typeToAttributeTable.Add(type, attribute);
|
---|
[14727] | 52 | dictionary.Add(attribute.ExpressionName, expression);
|
---|
[14875] | 53 | indexToNameTable.Add(indexToNameTable.Keys.Count, attribute.ExpressionName);
|
---|
| 54 | typeToNameTable.Add(type, attribute.ExpressionName);
|
---|
| 55 | nameToTypeTable.Add(attribute.ExpressionName, type);
|
---|
[14727] | 56 |
|
---|
[14875] | 57 | if (!stackTypeToNamesTable.ContainsKey(attribute.StackType)) {
|
---|
| 58 | stackTypeToNamesTable.Add(attribute.StackType, new List<string>());
|
---|
[14727] | 59 | }
|
---|
| 60 |
|
---|
[14875] | 61 | stackTypeToNamesTable[attribute.StackType].Add(attribute.ExpressionName);
|
---|
[14777] | 62 |
|
---|
[14834] | 63 | var dependencies = attribute.StackType | attribute.AdditionalStackDependencies;
|
---|
[14875] | 64 | if (!stackDependencyToNamesTable.ContainsKey(dependencies)) {
|
---|
| 65 | stackDependencyToNamesTable.Add(dependencies, new List<string>());
|
---|
[14777] | 66 | }
|
---|
| 67 |
|
---|
[14875] | 68 | stackDependencyToNamesTable[dependencies].Add(attribute.ExpressionName);
|
---|
[14727] | 69 | }
|
---|
| 70 |
|
---|
| 71 | return dictionary;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[14777] | 74 | private static Dictionary<string, Func<Expression>> GetStatefulExpressionFactory() {
|
---|
[14727] | 75 | var dictionary = new Dictionary<string, Func<Expression>>();
|
---|
| 76 |
|
---|
[14777] | 77 | foreach (var type in StatefulExpressionTypes) {
|
---|
[14727] | 78 | // Make a NewExpression that calls the ctor
|
---|
| 79 | var newExp = System.Linq.Expressions.Expression.New(type);
|
---|
| 80 |
|
---|
| 81 | // Create a lambda with the New expression as body
|
---|
| 82 | var creator = System.Linq.Expressions.Expression.Lambda<Func<Expression>>(newExp).Compile();
|
---|
| 83 | var attribute = (PushExpressionAttribute)Attribute.GetCustomAttribute(type, typeof(PushExpressionAttribute));
|
---|
| 84 |
|
---|
[14875] | 85 | typeToAttributeTable.Add(type, attribute);
|
---|
[14727] | 86 | dictionary.Add(attribute.ExpressionName, creator);
|
---|
[14875] | 87 | indexToNameTable.Add(indexToNameTable.Keys.Count, attribute.ExpressionName);
|
---|
| 88 | typeToNameTable.Add(type, attribute.ExpressionName);
|
---|
| 89 | nameToTypeTable.Add(attribute.ExpressionName, type);
|
---|
[14727] | 90 |
|
---|
[14875] | 91 | if (!stackTypeToNamesTable.ContainsKey(attribute.StackType)) {
|
---|
| 92 | stackTypeToNamesTable.Add(attribute.StackType, new List<string>());
|
---|
[14727] | 93 | }
|
---|
| 94 |
|
---|
[14875] | 95 | stackTypeToNamesTable[attribute.StackType].Add(attribute.ExpressionName);
|
---|
[14777] | 96 |
|
---|
[14834] | 97 | var dependencies = attribute.StackType | attribute.AdditionalStackDependencies;
|
---|
[14875] | 98 | if (!stackDependencyToNamesTable.ContainsKey(dependencies)) {
|
---|
| 99 | stackDependencyToNamesTable.Add(dependencies, new List<string>());
|
---|
[14777] | 100 | }
|
---|
| 101 |
|
---|
[14875] | 102 | stackDependencyToNamesTable[dependencies].Add(attribute.ExpressionName);
|
---|
[14727] | 103 | }
|
---|
| 104 |
|
---|
| 105 | return dictionary;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | private static bool IsSubclassOf(Type type, Type baseType) {
|
---|
| 109 | if (type == null || baseType == null || type == baseType)
|
---|
| 110 | return false;
|
---|
| 111 |
|
---|
| 112 | if (baseType.IsGenericType == false) {
|
---|
| 113 | if (type.IsGenericType == false)
|
---|
| 114 | return type.IsSubclassOf(baseType);
|
---|
| 115 | } else {
|
---|
| 116 | baseType = baseType.GetGenericTypeDefinition();
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | type = type.BaseType;
|
---|
| 120 | var objectType = typeof(object);
|
---|
| 121 | while (type != objectType && type != null) {
|
---|
| 122 | var curentType = type.IsGenericType ? type.GetGenericTypeDefinition() : type;
|
---|
| 123 | if (curentType == baseType)
|
---|
| 124 | return true;
|
---|
| 125 |
|
---|
| 126 | type = type.BaseType;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | return false;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | private static IEnumerable<Type> GetExpressionTypes(Type baseType) {
|
---|
| 133 | return from domainAssembly in AppDomain.CurrentDomain.GetAssemblies()
|
---|
| 134 | from assemblyType in domainAssembly.GetTypes()
|
---|
| 135 | where !assemblyType.IsAbstract &&
|
---|
| 136 | IsSubclassOf(assemblyType, baseType) &&
|
---|
| 137 | assemblyType.GetConstructor(Type.EmptyTypes) != null
|
---|
| 138 | select assemblyType;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[14834] | 141 | public static IReadOnlyList<string> GetExpressionsByStackTypes(StackTypes allowedTypes) {
|
---|
[14875] | 142 | return stackDependencyToNamesTable
|
---|
[14909] | 143 | .Where(entry => (entry.Key & ~allowedTypes) == 0x0)
|
---|
[14777] | 144 | .SelectMany(entry => entry.Value)
|
---|
| 145 | .ToArray();
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[14746] | 148 | public static PushProgram GetProgram(IManagedPool<PushProgram> pool, int[] index) {
|
---|
[14744] | 149 | var expressions = new Expression[index.Length];
|
---|
[14727] | 150 |
|
---|
| 151 | for (var i = 0; i < index.Length; i++) {
|
---|
[14744] | 152 | expressions[i] = GetExpression(index[i]);
|
---|
[14727] | 153 | }
|
---|
| 154 |
|
---|
[14746] | 155 | return PushProgram.Create(pool, expressions);
|
---|
[14727] | 156 | }
|
---|
| 157 |
|
---|
| 158 | public static Expression GetExpression(int index) {
|
---|
[14875] | 159 | return GetExpression(indexToNameTable[index]);
|
---|
[14727] | 160 | }
|
---|
| 161 |
|
---|
[14875] | 162 | public static Expression GetExpression(Type type) {
|
---|
| 163 | var name = TypeToNameTable[type];
|
---|
| 164 | return GetExpression(name);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[14727] | 167 | public static Expression GetExpression(string name) {
|
---|
| 168 | Expression expression;
|
---|
| 169 |
|
---|
| 170 | if (!TryGetStatelessExpression(name, out expression) &&
|
---|
[14777] | 171 | !TryGetStatefulExpression(name, out expression))
|
---|
[14727] | 172 | throw new InvalidOperationException(string.Format("Expression with name {0} not found", name));
|
---|
| 173 |
|
---|
| 174 | return expression;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | public static Expression GetStatelessExpression<T>() where T : StatelessExpression {
|
---|
[14875] | 178 | return GetStatelessExpression(typeToNameTable[typeof(T)]);
|
---|
[14727] | 179 | }
|
---|
| 180 |
|
---|
| 181 | public static Expression GetStatelessExpression(string name) {
|
---|
[14777] | 182 | if (StatelessExpressionTable.ContainsKey(name))
|
---|
| 183 | return StatelessExpressionTable[name];
|
---|
[14727] | 184 | throw new NotSupportedException("Expression not supported: " + name);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | public static bool TryGetStatelessExpression(string name, out Expression expression) {
|
---|
[14777] | 188 | if (!StatelessExpressionTable.ContainsKey(name)) {
|
---|
| 189 | expression = null;
|
---|
| 190 | return false;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | expression = StatelessExpressionTable[name];
|
---|
| 194 |
|
---|
| 195 | return true;
|
---|
[14727] | 196 | }
|
---|
| 197 |
|
---|
[14777] | 198 | public static Expression GetStatefulExpression<T>() {
|
---|
[14875] | 199 | return GetStatefulExpression(typeToNameTable[typeof(T)]);
|
---|
[14727] | 200 | }
|
---|
| 201 |
|
---|
[14777] | 202 | public static Expression GetStatefulExpression(string name) {
|
---|
[14727] | 203 | Func<Expression> creator;
|
---|
[14777] | 204 | if (StatefulExpressionFactory.TryGetValue(name, out creator))
|
---|
[14727] | 205 | return creator();
|
---|
| 206 | throw new NotSupportedException("Expression not supported: " + name);
|
---|
| 207 | }
|
---|
| 208 |
|
---|
[14777] | 209 | public static bool TryGetStatefulExpression(string name, out Expression expression) {
|
---|
| 210 | if (StatefulExpressionFactory.ContainsKey(name)) {
|
---|
| 211 | expression = StatefulExpressionFactory[name]();
|
---|
[14727] | 212 | return true;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | expression = default(Expression);
|
---|
| 216 | return false;
|
---|
| 217 | }
|
---|
| 218 | }
|
---|
| 219 | } |
---|