[15017] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 |
|
---|
| 5 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Configuration {
|
---|
| 6 | using HeuristicLab.Common;
|
---|
| 7 | using HeuristicLab.Core;
|
---|
| 8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 9 | using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions;
|
---|
[15032] | 10 | using HeuristicLab.Problems.ProgramSynthesis.Push.Extensions;
|
---|
[15017] | 11 | using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
|
---|
| 12 |
|
---|
| 13 | [StorableClass]
|
---|
[15273] | 14 | public abstract class PushConfigurationBase : NamedItem, IExpressionsConfiguration {
|
---|
[15017] | 15 |
|
---|
| 16 | protected PushConfigurationBase() {
|
---|
| 17 | Name = "Push Configuration";
|
---|
| 18 |
|
---|
| 19 | enabledExpressions = ExpressionTable.ExpressionNames.ToList();
|
---|
[15032] | 20 | expressionsPerStackCount = new Dictionary<StackTypes, int>();
|
---|
| 21 | InitExpressionsPerStackCount();
|
---|
[15017] | 22 | }
|
---|
| 23 |
|
---|
| 24 | [StorableConstructor]
|
---|
| 25 | protected PushConfigurationBase(bool deserializing) : base(deserializing) { }
|
---|
| 26 |
|
---|
| 27 | protected PushConfigurationBase(PushConfigurationBase origin, Cloner cloner) : base(origin, cloner) {
|
---|
| 28 | enabledExpressions = origin.EnabledExpressions.ToList();
|
---|
[15032] | 29 | expressionsPerStackCount = origin.expressionsPerStackCount.ToDictionary(x => x.Key, x => x.Value);
|
---|
[15273] | 30 | InExpressionCount = origin.InExpressionCount;
|
---|
| 31 | Seed = origin.Seed;
|
---|
[15017] | 32 | }
|
---|
| 33 |
|
---|
| 34 | [Storable]
|
---|
[15273] | 35 | public int Seed { get; set; }
|
---|
| 36 |
|
---|
| 37 | [Storable]
|
---|
| 38 | public int InExpressionCount { get; private set; }
|
---|
| 39 |
|
---|
| 40 | [Storable]
|
---|
[15032] | 41 | private readonly Dictionary<StackTypes, int> expressionsPerStackCount;
|
---|
[15017] | 42 |
|
---|
[15273] | 43 | public IReadOnlyDictionary<StackTypes, int> ExpressionsPerStackCount { get { return expressionsPerStackCount; } }
|
---|
| 44 |
|
---|
[15017] | 45 | public event EventHandler<EnabledExpressionsChangedEventArgs> EnabledExpressionsChanged;
|
---|
| 46 |
|
---|
| 47 | [Storable]
|
---|
| 48 | protected readonly List<string> enabledExpressions;
|
---|
| 49 |
|
---|
[15032] | 50 | public bool IsStackEnabled(StackTypes type) {
|
---|
| 51 | int count;
|
---|
| 52 | return expressionsPerStackCount.TryGetValue(type, out count) && count > 0;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | public IReadOnlyList<string> EnabledExpressions
|
---|
[15017] | 56 | {
|
---|
| 57 | get { return enabledExpressions; }
|
---|
| 58 | set
|
---|
| 59 | {
|
---|
[15189] | 60 | var removedExpressions = enabledExpressions.ToList();
|
---|
[15017] | 61 | enabledExpressions.Clear();
|
---|
| 62 | enabledExpressions.AddRange(value);
|
---|
| 63 |
|
---|
| 64 | if (EnabledExpressionsChanged != null) {
|
---|
| 65 | EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(value, removedExpressions));
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
[15032] | 70 | private void InitExpressionsPerStackCount() {
|
---|
[15017] | 71 | foreach (StackTypes type in Enum.GetValues(typeof(StackTypes))) {
|
---|
[15032] | 72 | if (type == StackTypes.None) continue;
|
---|
| 73 | expressionsPerStackCount.Add(type, 0);
|
---|
[15017] | 74 | }
|
---|
[15032] | 75 |
|
---|
| 76 | foreach (var expressionName in ExpressionTable.ExpressionNames) {
|
---|
| 77 | var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == expressionName).Key;
|
---|
| 78 | var attribute = ExpressionTable.TypeToAttributeTable[type];
|
---|
| 79 |
|
---|
[15189] | 80 | if (expressionsPerStackCount.ContainsKey(attribute.StackType)) {
|
---|
| 81 | expressionsPerStackCount[attribute.StackType]++;
|
---|
| 82 | }
|
---|
[15032] | 83 |
|
---|
| 84 | foreach (var additionalStackType in attribute.AdditionalStackDependencies.ToValues())
|
---|
| 85 | expressionsPerStackCount[additionalStackType]++;
|
---|
| 86 | }
|
---|
[15017] | 87 | }
|
---|
| 88 |
|
---|
| 89 | public void SetEnabledStacks(StackTypes types) {
|
---|
| 90 | // Disable all
|
---|
[15032] | 91 | enabledExpressions.Clear();
|
---|
[15017] | 92 |
|
---|
[15032] | 93 | foreach (StackTypes type in Enum.GetValues(typeof(StackTypes))) {
|
---|
[15017] | 94 | if (type == StackTypes.None) continue;
|
---|
[15032] | 95 | expressionsPerStackCount[type] = 0;
|
---|
[15017] | 96 | }
|
---|
| 97 |
|
---|
[15032] | 98 | var expressionNames = ExpressionTable.StackDependencyToNamesTable
|
---|
| 99 | .Where(pair => types.HasFlag(pair.Key))
|
---|
| 100 | .SelectMany(pair => pair.Value);
|
---|
| 101 |
|
---|
| 102 | EnableExpressions(expressionNames);
|
---|
[15017] | 103 | }
|
---|
| 104 |
|
---|
| 105 | public void EnableExpressionOfStack(StackTypes types) {
|
---|
| 106 | EnableExpressions(ExpressionTable.StackTypeToNamesTable[types]);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | public void EnableExpressionDependentOnStack(StackTypes types) {
|
---|
| 110 | var names = ExpressionTable.StackDependencyToNamesTable[types].Where(
|
---|
| 111 | name => {
|
---|
| 112 | var type = ExpressionTable.NameToTypeTable[name];
|
---|
| 113 | var attribute = ExpressionTable.TypeToAttributeTable[type];
|
---|
| 114 |
|
---|
| 115 | return (attribute.StackType | attribute.AdditionalStackDependencies)
|
---|
| 116 | .ToEnumerable()
|
---|
[15032] | 117 | .All(IsStackEnabled);
|
---|
[15017] | 118 | });
|
---|
| 119 |
|
---|
| 120 | EnableExpressions(names);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[15032] | 123 | private void EnableExpressions(IEnumerable<string> expressionNames) {
|
---|
[15273] | 124 | var addedExpressions = expressionNames as IList<string> ?? expressionNames.ToList();
|
---|
| 125 |
|
---|
| 126 | foreach (var expressionName in addedExpressions.Except(EnabledExpressions))
|
---|
[15032] | 127 | EnableExpression(expressionName, false);
|
---|
[15017] | 128 |
|
---|
| 129 | if (EnabledExpressionsChanged != null) {
|
---|
[15273] | 130 | EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(addedExpressions, new string[0]));
|
---|
[15017] | 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | public void DisableExpressionsOfStack(StackTypes types) {
|
---|
| 135 | DisableExpressions(ExpressionTable.StackTypeToNamesTable[types]);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | public void DisableExpressionsDependentOnStack(StackTypes types) {
|
---|
| 139 | var names = ExpressionTable.StackDependencyToNamesTable
|
---|
| 140 | .Where(pair => pair.Key.HasFlag(types))
|
---|
| 141 | .SelectMany(pair => pair.Value);
|
---|
| 142 |
|
---|
| 143 | DisableExpressions(names);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[15032] | 146 | private void DisableExpressions(IEnumerable<string> expressionNames) {
|
---|
[15273] | 147 | var removedExpressions = expressionNames as IList<string> ?? expressionNames.ToList();
|
---|
| 148 |
|
---|
| 149 | foreach (var expressionName in removedExpressions.Intersect(EnabledExpressions))
|
---|
[15032] | 150 | DisableExpression(expressionName, false);
|
---|
[15017] | 151 |
|
---|
| 152 | if (EnabledExpressionsChanged != null) {
|
---|
[15273] | 153 | EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(new string[0], removedExpressions));
|
---|
[15017] | 154 | }
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[15032] | 157 | public void EnableExpression(string expressionName) {
|
---|
| 158 | EnableExpression(expressionName, true);
|
---|
| 159 | }
|
---|
[15017] | 160 |
|
---|
[15032] | 161 | private void EnableExpression(string expressionName, bool triggerExpressionChanged) {
|
---|
| 162 | if (enabledExpressions.Contains(expressionName)) return;
|
---|
[15017] | 163 |
|
---|
[15032] | 164 | var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == expressionName).Key;
|
---|
| 165 | var attribute = ExpressionTable.TypeToAttributeTable[type];
|
---|
[15017] | 166 |
|
---|
[15032] | 167 | enabledExpressions.Add(expressionName);
|
---|
[15189] | 168 |
|
---|
| 169 | if (expressionsPerStackCount.ContainsKey(attribute.StackType))
|
---|
| 170 | expressionsPerStackCount[attribute.StackType]++;
|
---|
| 171 |
|
---|
| 172 | foreach (var additionalStackType in attribute.AdditionalStackDependencies.ToValues().Where(expressionsPerStackCount.ContainsKey))
|
---|
[15032] | 173 | expressionsPerStackCount[additionalStackType]++;
|
---|
| 174 |
|
---|
| 175 | if (EnabledExpressionsChanged != null && triggerExpressionChanged) {
|
---|
[15017] | 176 | EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(
|
---|
[15032] | 177 | new[] { expressionName },
|
---|
[15017] | 178 | new string[0]));
|
---|
| 179 | }
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[15032] | 182 | public void DisableExpression(string expressionName) {
|
---|
| 183 | DisableExpression(expressionName, true);
|
---|
| 184 | }
|
---|
[15017] | 185 |
|
---|
[15032] | 186 | public void DisableExpression(string expressionName, bool triggerExpressionChanged) {
|
---|
| 187 | if (!EnabledExpressions.Contains(expressionName)) return;
|
---|
[15017] | 188 |
|
---|
[15032] | 189 | var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == expressionName).Key;
|
---|
| 190 | var attribute = ExpressionTable.TypeToAttributeTable[type];
|
---|
[15017] | 191 |
|
---|
[15032] | 192 | enabledExpressions.Remove(expressionName);
|
---|
[15189] | 193 |
|
---|
| 194 | if (expressionsPerStackCount.ContainsKey(attribute.StackType))
|
---|
| 195 | expressionsPerStackCount[attribute.StackType]--;
|
---|
| 196 |
|
---|
| 197 | foreach (var additionalStackType in attribute.AdditionalStackDependencies.ToValues().Where(expressionsPerStackCount.ContainsKey))
|
---|
[15032] | 198 | expressionsPerStackCount[additionalStackType]--;
|
---|
| 199 |
|
---|
| 200 | if (EnabledExpressionsChanged != null && triggerExpressionChanged) {
|
---|
[15017] | 201 | EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(
|
---|
| 202 | new string[0],
|
---|
[15032] | 203 | new[] { expressionName }));
|
---|
[15017] | 204 | }
|
---|
| 205 | }
|
---|
| 206 |
|
---|
[15032] | 207 | public void EnableExpression<T>() where T : Expression {
|
---|
[15017] | 208 | var attribute = ExpressionTable.TypeToAttributeTable[typeof(T)];
|
---|
[15032] | 209 | EnableExpression(attribute.Name);
|
---|
[15017] | 210 | }
|
---|
| 211 |
|
---|
[15032] | 212 | public void DisableExpression<T>() where T : Expression {
|
---|
[15017] | 213 | var attribute = ExpressionTable.TypeToAttributeTable[typeof(T)];
|
---|
[15032] | 214 | DisableExpression(attribute.Name);
|
---|
[15017] | 215 | }
|
---|
| 216 |
|
---|
[15032] | 217 | public void SetExpression(string name, bool state) {
|
---|
| 218 | if (state) EnableExpression(name);
|
---|
| 219 | else DisableExpression(name);
|
---|
[15017] | 220 | }
|
---|
| 221 |
|
---|
[15032] | 222 | public void SetExpression<T>(bool state) where T : Expression {
|
---|
| 223 | if (state) EnableExpression<T>();
|
---|
| 224 | else DisableExpression<T>();
|
---|
[15017] | 225 | }
|
---|
| 226 |
|
---|
[15032] | 227 | public void EnableStack(StackTypes type, bool enableDependencies = false) {
|
---|
| 228 | EnableExpressionOfStack(type);
|
---|
[15017] | 229 |
|
---|
[15032] | 230 | if (enableDependencies)
|
---|
| 231 | EnableExpressionDependentOnStack(type);
|
---|
[15017] | 232 | }
|
---|
| 233 |
|
---|
[15032] | 234 | public void DisableStack(StackTypes type, bool disableDependencies = false) {
|
---|
| 235 | DisableExpressionsOfStack(type);
|
---|
[15017] | 236 |
|
---|
[15032] | 237 | if (disableDependencies)
|
---|
| 238 | DisableExpressionsDependentOnStack(type);
|
---|
[15017] | 239 | }
|
---|
| 240 |
|
---|
[15032] | 241 | public void SetStack(StackTypes type, bool state, bool setDependencies = false) {
|
---|
| 242 | if (state) EnableStack(type, setDependencies);
|
---|
| 243 | else DisableStack(type, setDependencies);
|
---|
[15017] | 244 | }
|
---|
[15273] | 245 |
|
---|
| 246 | public void InitInExpressions(int totalInputArgumentCount) {
|
---|
| 247 | InExpressionCount = totalInputArgumentCount;
|
---|
| 248 |
|
---|
| 249 | for (var i = 0; i < ExpressionTable.InExpressionTable.Count; i++) {
|
---|
| 250 | var expression = ExpressionTable.InExpressionTable[i];
|
---|
| 251 | var expressionName = ExpressionTable.TypeToNameTable[expression.GetType()];
|
---|
| 252 |
|
---|
| 253 | DisableExpression(expressionName);
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | if (totalInputArgumentCount > ExpressionTable.InExpressionTable.Count)
|
---|
| 257 | throw new InvalidOperationException("More input arguments defined as InExpression types");
|
---|
| 258 |
|
---|
| 259 | for (var i = 0; i < totalInputArgumentCount; i++) {
|
---|
| 260 | var expression = ExpressionTable.InExpressionTable[i];
|
---|
| 261 | var expressionName = ExpressionTable.TypeToNameTable[expression.GetType()];
|
---|
| 262 |
|
---|
| 263 | EnableExpression(expressionName);
|
---|
| 264 | }
|
---|
| 265 | }
|
---|
[15017] | 266 | }
|
---|
| 267 | }
|
---|