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;
|
---|
10 | using HeuristicLab.Problems.ProgramSynthesis.Push.Extensions;
|
---|
11 | using HeuristicLab.Problems.ProgramSynthesis.Push.Stack;
|
---|
12 |
|
---|
13 | [StorableClass]
|
---|
14 | public abstract class PushConfigurationBase : NamedItem, IExpressionsConfiguration {
|
---|
15 |
|
---|
16 | protected PushConfigurationBase() {
|
---|
17 | Name = "Push Configuration";
|
---|
18 |
|
---|
19 | enabledExpressions = ExpressionTable.ExpressionNames.ToList();
|
---|
20 | expressionsPerStackCount = new Dictionary<StackTypes, int>();
|
---|
21 | InitExpressionsPerStackCount();
|
---|
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();
|
---|
29 | expressionsPerStackCount = origin.expressionsPerStackCount.ToDictionary(x => x.Key, x => x.Value);
|
---|
30 | InExpressionCount = origin.InExpressionCount;
|
---|
31 | Seed = origin.Seed;
|
---|
32 | }
|
---|
33 |
|
---|
34 | [Storable]
|
---|
35 | public int Seed { get; set; }
|
---|
36 |
|
---|
37 | [Storable]
|
---|
38 | public int InExpressionCount { get; private set; }
|
---|
39 |
|
---|
40 | [Storable]
|
---|
41 | private readonly Dictionary<StackTypes, int> expressionsPerStackCount;
|
---|
42 |
|
---|
43 | public IReadOnlyDictionary<StackTypes, int> ExpressionsPerStackCount { get { return expressionsPerStackCount; } }
|
---|
44 |
|
---|
45 | public event EventHandler<EnabledExpressionsChangedEventArgs> EnabledExpressionsChanged;
|
---|
46 |
|
---|
47 | [Storable]
|
---|
48 | protected readonly List<string> enabledExpressions;
|
---|
49 |
|
---|
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
|
---|
56 | {
|
---|
57 | get { return enabledExpressions; }
|
---|
58 | set
|
---|
59 | {
|
---|
60 | var removedExpressions = enabledExpressions.ToList();
|
---|
61 | enabledExpressions.Clear();
|
---|
62 | enabledExpressions.AddRange(value);
|
---|
63 |
|
---|
64 | if (EnabledExpressionsChanged != null) {
|
---|
65 | EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(value, removedExpressions));
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | private void InitExpressionsPerStackCount() {
|
---|
71 | foreach (StackTypes type in Enum.GetValues(typeof(StackTypes))) {
|
---|
72 | if (type == StackTypes.None) continue;
|
---|
73 | expressionsPerStackCount.Add(type, 0);
|
---|
74 | }
|
---|
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 |
|
---|
80 | if (expressionsPerStackCount.ContainsKey(attribute.StackType)) {
|
---|
81 | expressionsPerStackCount[attribute.StackType]++;
|
---|
82 | }
|
---|
83 |
|
---|
84 | foreach (var additionalStackType in attribute.AdditionalStackDependencies.ToValues())
|
---|
85 | expressionsPerStackCount[additionalStackType]++;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | public void SetEnabledStacks(StackTypes types) {
|
---|
90 | // Disable all
|
---|
91 | enabledExpressions.Clear();
|
---|
92 |
|
---|
93 | foreach (StackTypes type in Enum.GetValues(typeof(StackTypes))) {
|
---|
94 | if (type == StackTypes.None) continue;
|
---|
95 | expressionsPerStackCount[type] = 0;
|
---|
96 | }
|
---|
97 |
|
---|
98 | var expressionNames = ExpressionTable.StackDependencyToNamesTable
|
---|
99 | .Where(pair => types.HasFlag(pair.Key))
|
---|
100 | .SelectMany(pair => pair.Value);
|
---|
101 |
|
---|
102 | EnableExpressions(expressionNames);
|
---|
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()
|
---|
117 | .All(IsStackEnabled);
|
---|
118 | });
|
---|
119 |
|
---|
120 | EnableExpressions(names);
|
---|
121 | }
|
---|
122 |
|
---|
123 | private void EnableExpressions(IEnumerable<string> expressionNames) {
|
---|
124 | var addedExpressions = expressionNames as IList<string> ?? expressionNames.ToList();
|
---|
125 |
|
---|
126 | foreach (var expressionName in addedExpressions.Except(EnabledExpressions))
|
---|
127 | EnableExpression(expressionName, false);
|
---|
128 |
|
---|
129 | if (EnabledExpressionsChanged != null) {
|
---|
130 | EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(addedExpressions, new string[0]));
|
---|
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 |
|
---|
146 | private void DisableExpressions(IEnumerable<string> expressionNames) {
|
---|
147 | var removedExpressions = expressionNames as IList<string> ?? expressionNames.ToList();
|
---|
148 |
|
---|
149 | foreach (var expressionName in removedExpressions.Intersect(EnabledExpressions))
|
---|
150 | DisableExpression(expressionName, false);
|
---|
151 |
|
---|
152 | if (EnabledExpressionsChanged != null) {
|
---|
153 | EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(new string[0], removedExpressions));
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | public void EnableExpression(string expressionName) {
|
---|
158 | EnableExpression(expressionName, true);
|
---|
159 | }
|
---|
160 |
|
---|
161 | private void EnableExpression(string expressionName, bool triggerExpressionChanged) {
|
---|
162 | if (enabledExpressions.Contains(expressionName)) return;
|
---|
163 |
|
---|
164 | var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == expressionName).Key;
|
---|
165 | var attribute = ExpressionTable.TypeToAttributeTable[type];
|
---|
166 |
|
---|
167 | enabledExpressions.Add(expressionName);
|
---|
168 |
|
---|
169 | if (expressionsPerStackCount.ContainsKey(attribute.StackType))
|
---|
170 | expressionsPerStackCount[attribute.StackType]++;
|
---|
171 |
|
---|
172 | foreach (var additionalStackType in attribute.AdditionalStackDependencies.ToValues().Where(expressionsPerStackCount.ContainsKey))
|
---|
173 | expressionsPerStackCount[additionalStackType]++;
|
---|
174 |
|
---|
175 | if (EnabledExpressionsChanged != null && triggerExpressionChanged) {
|
---|
176 | EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(
|
---|
177 | new[] { expressionName },
|
---|
178 | new string[0]));
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | public void DisableExpression(string expressionName) {
|
---|
183 | DisableExpression(expressionName, true);
|
---|
184 | }
|
---|
185 |
|
---|
186 | public void DisableExpression(string expressionName, bool triggerExpressionChanged) {
|
---|
187 | if (!EnabledExpressions.Contains(expressionName)) return;
|
---|
188 |
|
---|
189 | var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == expressionName).Key;
|
---|
190 | var attribute = ExpressionTable.TypeToAttributeTable[type];
|
---|
191 |
|
---|
192 | enabledExpressions.Remove(expressionName);
|
---|
193 |
|
---|
194 | if (expressionsPerStackCount.ContainsKey(attribute.StackType))
|
---|
195 | expressionsPerStackCount[attribute.StackType]--;
|
---|
196 |
|
---|
197 | foreach (var additionalStackType in attribute.AdditionalStackDependencies.ToValues().Where(expressionsPerStackCount.ContainsKey))
|
---|
198 | expressionsPerStackCount[additionalStackType]--;
|
---|
199 |
|
---|
200 | if (EnabledExpressionsChanged != null && triggerExpressionChanged) {
|
---|
201 | EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(
|
---|
202 | new string[0],
|
---|
203 | new[] { expressionName }));
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | public void EnableExpression<T>() where T : Expression {
|
---|
208 | var attribute = ExpressionTable.TypeToAttributeTable[typeof(T)];
|
---|
209 | EnableExpression(attribute.Name);
|
---|
210 | }
|
---|
211 |
|
---|
212 | public void DisableExpression<T>() where T : Expression {
|
---|
213 | var attribute = ExpressionTable.TypeToAttributeTable[typeof(T)];
|
---|
214 | DisableExpression(attribute.Name);
|
---|
215 | }
|
---|
216 |
|
---|
217 | public void SetExpression(string name, bool state) {
|
---|
218 | if (state) EnableExpression(name);
|
---|
219 | else DisableExpression(name);
|
---|
220 | }
|
---|
221 |
|
---|
222 | public void SetExpression<T>(bool state) where T : Expression {
|
---|
223 | if (state) EnableExpression<T>();
|
---|
224 | else DisableExpression<T>();
|
---|
225 | }
|
---|
226 |
|
---|
227 | public void EnableStack(StackTypes type, bool enableDependencies = false) {
|
---|
228 | EnableExpressionOfStack(type);
|
---|
229 |
|
---|
230 | if (enableDependencies)
|
---|
231 | EnableExpressionDependentOnStack(type);
|
---|
232 | }
|
---|
233 |
|
---|
234 | public void DisableStack(StackTypes type, bool disableDependencies = false) {
|
---|
235 | DisableExpressionsOfStack(type);
|
---|
236 |
|
---|
237 | if (disableDependencies)
|
---|
238 | DisableExpressionsDependentOnStack(type);
|
---|
239 | }
|
---|
240 |
|
---|
241 | public void SetStack(StackTypes type, bool state, bool setDependencies = false) {
|
---|
242 | if (state) EnableStack(type, setDependencies);
|
---|
243 | else DisableStack(type, setDependencies);
|
---|
244 | }
|
---|
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 | }
|
---|
266 | }
|
---|
267 | }
|
---|