Changeset 14777 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/ExpressionTable.cs
- Timestamp:
- 03/23/17 01:11:18 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Expressions/ExpressionTable.cs
r14746 r14777 10 10 public static class ExpressionTable { 11 11 public static readonly IDictionary<string, Expression> StatelessExpressionTable; 12 public static readonly IDictionary<string, Func<Expression>> Stateful lExpressionFactory;13 public static readonly IDictionary<StackType , IList<string>> StackTypeToNamesTable = new Dictionary<StackType, IList<string>>();12 public static readonly IDictionary<string, Func<Expression>> StatefulExpressionFactory; 13 public static readonly IDictionary<StackTypes, IList<string>> StackTypeToNamesTable = new Dictionary<StackTypes, IList<string>>(); 14 14 public static readonly IDictionary<int, string> IndexToNameTable = new Dictionary<int, string>(); 15 15 public static readonly IDictionary<Type, string> TypeToNameTable = new Dictionary<Type, string>(); 16 public static readonly IDictionary<StackTypes, IList<string>> StackDependencyToNamesTable = new Dictionary<StackTypes, IList<string>>(); 16 17 public static readonly string[] ExpressionNames; 17 18 19 public static readonly Type[] StatelessExpressionTypes; 20 public static readonly Type[] StatefulExpressionTypes; 21 18 22 public static readonly int Count; 19 23 20 24 static ExpressionTable() { 25 StatelessExpressionTypes = GetExpressionTypes(typeof(StatelessExpression)).ToArray(); 26 StatefulExpressionTypes = GetExpressionTypes(typeof(StatefulExpression<>)).ToArray(); 27 21 28 StatelessExpressionTable = GetStatelessExpressionTable(); 22 Stateful lExpressionFactory = GetStatefullExpressionFactory();23 ExpressionNames = StatelessExpressionTable.Keys.Concat(Stateful lExpressionFactory.Keys).ToArray();24 25 Count = StatelessExpressionTable.Count + Stateful lExpressionFactory.Count;29 StatefulExpressionFactory = GetStatefulExpressionFactory(); 30 ExpressionNames = StatelessExpressionTable.Keys.Concat(StatefulExpressionFactory.Keys).ToArray(); 31 32 Count = StatelessExpressionTable.Count + StatefulExpressionFactory.Count; 26 33 } 27 34 … … 38 45 get 39 46 { 40 return Stateful lExpressionFactory.Count;47 return StatefulExpressionFactory.Count; 41 48 } 42 49 } … … 44 51 private static Dictionary<string, Expression> GetStatelessExpressionTable() { 45 52 var dictionary = new Dictionary<string, Expression>(); 46 var expressionTypes = GetExpressionTypes(typeof(StatelessExpression)); 47 48 foreach (var type in expressionTypes) { 53 54 foreach (var type in StatelessExpressionTypes) { 49 55 var expression = Activator.CreateInstance(type) as Expression; 50 56 var attribute = (PushExpressionAttribute)Attribute.GetCustomAttribute(type, typeof(PushExpressionAttribute)); … … 54 60 TypeToNameTable.Add(type, attribute.ExpressionName); 55 61 56 if (!StackTypeToNamesTable.ContainsKey(attribute.StackType)) { 57 StackTypeToNamesTable.Add(attribute.StackType, new List<string>()); 58 } 59 60 StackTypeToNamesTable[attribute.StackType].Add(attribute.ExpressionName); 62 if (!StackTypeToNamesTable.ContainsKey(attribute.StackTypes)) { 63 StackTypeToNamesTable.Add(attribute.StackTypes, new List<string>()); 64 } 65 66 StackTypeToNamesTable[attribute.StackTypes].Add(attribute.ExpressionName); 67 68 var dependencies = attribute.StackTypes | attribute.AdditionalStackDependencies; 69 if (!StackDependencyToNamesTable.ContainsKey(dependencies)) { 70 StackDependencyToNamesTable.Add(dependencies, new List<string>()); 71 } 72 73 StackDependencyToNamesTable[dependencies].Add(attribute.ExpressionName); 61 74 } 62 75 … … 64 77 } 65 78 66 private static Dictionary<string, Func<Expression>> GetStateful lExpressionFactory() {79 private static Dictionary<string, Func<Expression>> GetStatefulExpressionFactory() { 67 80 var dictionary = new Dictionary<string, Func<Expression>>(); 68 var statefullExpressionType = typeof(StatefulExpression<>); 69 var expressionTypes = GetExpressionTypes(statefullExpressionType); 70 71 foreach (var type in expressionTypes) { 81 82 foreach (var type in StatefulExpressionTypes) { 72 83 // Make a NewExpression that calls the ctor 73 84 var newExp = System.Linq.Expressions.Expression.New(type); … … 81 92 TypeToNameTable.Add(type, attribute.ExpressionName); 82 93 83 if (!StackTypeToNamesTable.ContainsKey(attribute.StackType)) { 84 StackTypeToNamesTable.Add(attribute.StackType, new List<string>()); 85 } 86 87 StackTypeToNamesTable[attribute.StackType].Add(attribute.ExpressionName); 94 if (!StackTypeToNamesTable.ContainsKey(attribute.StackTypes)) { 95 StackTypeToNamesTable.Add(attribute.StackTypes, new List<string>()); 96 } 97 98 StackTypeToNamesTable[attribute.StackTypes].Add(attribute.ExpressionName); 99 100 var dependencies = attribute.StackTypes | attribute.AdditionalStackDependencies; 101 if (!StackDependencyToNamesTable.ContainsKey(dependencies)) { 102 StackDependencyToNamesTable.Add(dependencies, new List<string>()); 103 } 104 105 StackDependencyToNamesTable[dependencies].Add(attribute.ExpressionName); 88 106 } 89 107 … … 124 142 } 125 143 144 public static IReadOnlyList<string> GetEnabledExpressionsByStackTypes(StackTypes allowedTypes) { 145 return StackDependencyToNamesTable 146 .Where(entry => (entry.Key & ~allowedTypes) == StackTypes.None) 147 .SelectMany(entry => entry.Value) 148 .ToArray(); 149 } 150 126 151 public static PushProgram GetProgram(IManagedPool<PushProgram> pool, int[] index) { 127 152 var expressions = new Expression[index.Length]; … … 142 167 143 168 if (!TryGetStatelessExpression(name, out expression) && 144 !TryGetStateful lExpression(name, out expression))169 !TryGetStatefulExpression(name, out expression)) 145 170 throw new InvalidOperationException(string.Format("Expression with name {0} not found", name)); 146 171 … … 153 178 154 179 public static Expression GetStatelessExpression(string name) { 155 Expression expression; 156 if (StatelessExpressionTable.TryGetValue(name, out expression)) 157 return expression; 180 if (StatelessExpressionTable.ContainsKey(name)) 181 return StatelessExpressionTable[name]; 158 182 throw new NotSupportedException("Expression not supported: " + name); 159 183 } 160 184 161 185 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) { 186 if (!StatelessExpressionTable.ContainsKey(name)) { 187 expression = null; 188 return false; 189 } 190 191 expression = StatelessExpressionTable[name]; 192 193 return true; 194 } 195 196 public static Expression GetStatefulExpression<T>() { 197 return GetStatefulExpression(TypeToNameTable[typeof(T)]); 198 } 199 200 public static Expression GetStatefulExpression(string name) { 170 201 Func<Expression> creator; 171 if (Stateful lExpressionFactory.TryGetValue(name, out creator))202 if (StatefulExpressionFactory.TryGetValue(name, out creator)) 172 203 return creator(); 173 204 throw new NotSupportedException("Expression not supported: " + name); 174 205 } 175 206 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(); 207 public static bool TryGetStatefulExpression(string name, out Expression expression) { 208 if (StatefulExpressionFactory.ContainsKey(name)) { 209 expression = StatefulExpressionFactory[name](); 181 210 return true; 182 211 }
Note: See TracChangeset
for help on using the changeset viewer.