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, IEnabledExpressionsConfiguration {
|
---|
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 | }
|
---|
31 |
|
---|
32 | [Storable]
|
---|
33 | private readonly Dictionary<StackTypes, int> expressionsPerStackCount;
|
---|
34 |
|
---|
35 | public event EventHandler<EnabledExpressionsChangedEventArgs> EnabledExpressionsChanged;
|
---|
36 |
|
---|
37 | [Storable]
|
---|
38 | protected readonly List<string> enabledExpressions;
|
---|
39 |
|
---|
40 | public bool IsStackEnabled(StackTypes type) {
|
---|
41 | int count;
|
---|
42 | return expressionsPerStackCount.TryGetValue(type, out count) && count > 0;
|
---|
43 | }
|
---|
44 |
|
---|
45 | public IReadOnlyList<string> EnabledExpressions
|
---|
46 | {
|
---|
47 | get { return enabledExpressions; }
|
---|
48 | set
|
---|
49 | {
|
---|
50 | var removedExpressions = enabledExpressions.ToList();
|
---|
51 | enabledExpressions.Clear();
|
---|
52 | enabledExpressions.AddRange(value);
|
---|
53 |
|
---|
54 | if (EnabledExpressionsChanged != null) {
|
---|
55 | EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(value, removedExpressions));
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | private void InitExpressionsPerStackCount() {
|
---|
61 | foreach (StackTypes type in Enum.GetValues(typeof(StackTypes))) {
|
---|
62 | if (type == StackTypes.None) continue;
|
---|
63 | expressionsPerStackCount.Add(type, 0);
|
---|
64 | }
|
---|
65 |
|
---|
66 | foreach (var expressionName in ExpressionTable.ExpressionNames) {
|
---|
67 | var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == expressionName).Key;
|
---|
68 | var attribute = ExpressionTable.TypeToAttributeTable[type];
|
---|
69 |
|
---|
70 | if (expressionsPerStackCount.ContainsKey(attribute.StackType)) {
|
---|
71 | expressionsPerStackCount[attribute.StackType]++;
|
---|
72 | }
|
---|
73 |
|
---|
74 | foreach (var additionalStackType in attribute.AdditionalStackDependencies.ToValues())
|
---|
75 | expressionsPerStackCount[additionalStackType]++;
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | public void SetEnabledStacks(StackTypes types) {
|
---|
80 | // Disable all
|
---|
81 | enabledExpressions.Clear();
|
---|
82 |
|
---|
83 | foreach (StackTypes type in Enum.GetValues(typeof(StackTypes))) {
|
---|
84 | if (type == StackTypes.None) continue;
|
---|
85 | expressionsPerStackCount[type] = 0;
|
---|
86 | }
|
---|
87 |
|
---|
88 | var expressionNames = ExpressionTable.StackDependencyToNamesTable
|
---|
89 | .Where(pair => types.HasFlag(pair.Key))
|
---|
90 | .SelectMany(pair => pair.Value);
|
---|
91 |
|
---|
92 | EnableExpressions(expressionNames);
|
---|
93 | }
|
---|
94 |
|
---|
95 | public void EnableExpressionOfStack(StackTypes types) {
|
---|
96 | EnableExpressions(ExpressionTable.StackTypeToNamesTable[types]);
|
---|
97 | }
|
---|
98 |
|
---|
99 | public void EnableExpressionDependentOnStack(StackTypes types) {
|
---|
100 | var names = ExpressionTable.StackDependencyToNamesTable[types].Where(
|
---|
101 | name => {
|
---|
102 | var type = ExpressionTable.NameToTypeTable[name];
|
---|
103 | var attribute = ExpressionTable.TypeToAttributeTable[type];
|
---|
104 |
|
---|
105 | return (attribute.StackType | attribute.AdditionalStackDependencies)
|
---|
106 | .ToEnumerable()
|
---|
107 | .All(IsStackEnabled);
|
---|
108 | });
|
---|
109 |
|
---|
110 | EnableExpressions(names);
|
---|
111 | }
|
---|
112 |
|
---|
113 | private void EnableExpressions(IEnumerable<string> expressionNames) {
|
---|
114 | foreach (var expressionName in expressionNames.Except(EnabledExpressions))
|
---|
115 | EnableExpression(expressionName, false);
|
---|
116 |
|
---|
117 | if (EnabledExpressionsChanged != null) {
|
---|
118 | EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(expressionNames, new string[0]));
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | public void DisableExpressionsOfStack(StackTypes types) {
|
---|
123 | DisableExpressions(ExpressionTable.StackTypeToNamesTable[types]);
|
---|
124 | }
|
---|
125 |
|
---|
126 | public void DisableExpressionsDependentOnStack(StackTypes types) {
|
---|
127 | var names = ExpressionTable.StackDependencyToNamesTable
|
---|
128 | .Where(pair => pair.Key.HasFlag(types))
|
---|
129 | .SelectMany(pair => pair.Value);
|
---|
130 |
|
---|
131 | DisableExpressions(names);
|
---|
132 | }
|
---|
133 |
|
---|
134 | private void DisableExpressions(IEnumerable<string> expressionNames) {
|
---|
135 | foreach (var expressionName in expressionNames.Intersect(EnabledExpressions))
|
---|
136 | DisableExpression(expressionName, false);
|
---|
137 |
|
---|
138 | if (EnabledExpressionsChanged != null) {
|
---|
139 | EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(new string[0], expressionNames));
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | public void EnableExpression(string expressionName) {
|
---|
144 | EnableExpression(expressionName, true);
|
---|
145 | }
|
---|
146 |
|
---|
147 | private void EnableExpression(string expressionName, bool triggerExpressionChanged) {
|
---|
148 | if (enabledExpressions.Contains(expressionName)) return;
|
---|
149 |
|
---|
150 | var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == expressionName).Key;
|
---|
151 | var attribute = ExpressionTable.TypeToAttributeTable[type];
|
---|
152 |
|
---|
153 | enabledExpressions.Add(expressionName);
|
---|
154 |
|
---|
155 | if (expressionsPerStackCount.ContainsKey(attribute.StackType))
|
---|
156 | expressionsPerStackCount[attribute.StackType]++;
|
---|
157 |
|
---|
158 | foreach (var additionalStackType in attribute.AdditionalStackDependencies.ToValues().Where(expressionsPerStackCount.ContainsKey))
|
---|
159 | expressionsPerStackCount[additionalStackType]++;
|
---|
160 |
|
---|
161 | if (EnabledExpressionsChanged != null && triggerExpressionChanged) {
|
---|
162 | EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(
|
---|
163 | new[] { expressionName },
|
---|
164 | new string[0]));
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | public void DisableExpression(string expressionName) {
|
---|
169 | DisableExpression(expressionName, true);
|
---|
170 | }
|
---|
171 |
|
---|
172 | public void DisableExpression(string expressionName, bool triggerExpressionChanged) {
|
---|
173 | if (!EnabledExpressions.Contains(expressionName)) return;
|
---|
174 |
|
---|
175 | var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == expressionName).Key;
|
---|
176 | var attribute = ExpressionTable.TypeToAttributeTable[type];
|
---|
177 |
|
---|
178 | enabledExpressions.Remove(expressionName);
|
---|
179 |
|
---|
180 | if (expressionsPerStackCount.ContainsKey(attribute.StackType))
|
---|
181 | expressionsPerStackCount[attribute.StackType]--;
|
---|
182 |
|
---|
183 | foreach (var additionalStackType in attribute.AdditionalStackDependencies.ToValues().Where(expressionsPerStackCount.ContainsKey))
|
---|
184 | expressionsPerStackCount[additionalStackType]--;
|
---|
185 |
|
---|
186 | if (EnabledExpressionsChanged != null && triggerExpressionChanged) {
|
---|
187 | EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(
|
---|
188 | new string[0],
|
---|
189 | new[] { expressionName }));
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | public void EnableExpression<T>() where T : Expression {
|
---|
194 | var attribute = ExpressionTable.TypeToAttributeTable[typeof(T)];
|
---|
195 | EnableExpression(attribute.Name);
|
---|
196 | }
|
---|
197 |
|
---|
198 | public void DisableExpression<T>() where T : Expression {
|
---|
199 | var attribute = ExpressionTable.TypeToAttributeTable[typeof(T)];
|
---|
200 | DisableExpression(attribute.Name);
|
---|
201 | }
|
---|
202 |
|
---|
203 | public void SetExpression(string name, bool state) {
|
---|
204 | if (state) EnableExpression(name);
|
---|
205 | else DisableExpression(name);
|
---|
206 | }
|
---|
207 |
|
---|
208 | public void SetExpression<T>(bool state) where T : Expression {
|
---|
209 | if (state) EnableExpression<T>();
|
---|
210 | else DisableExpression<T>();
|
---|
211 | }
|
---|
212 |
|
---|
213 | public void EnableStack(StackTypes type, bool enableDependencies = false) {
|
---|
214 | EnableExpressionOfStack(type);
|
---|
215 |
|
---|
216 | if (enableDependencies)
|
---|
217 | EnableExpressionDependentOnStack(type);
|
---|
218 | }
|
---|
219 |
|
---|
220 | public void DisableStack(StackTypes type, bool disableDependencies = false) {
|
---|
221 | DisableExpressionsOfStack(type);
|
---|
222 |
|
---|
223 | if (disableDependencies)
|
---|
224 | DisableExpressionsDependentOnStack(type);
|
---|
225 | }
|
---|
226 |
|
---|
227 | public void SetStack(StackTypes type, bool state, bool setDependencies = false) {
|
---|
228 | if (state) EnableStack(type, setDependencies);
|
---|
229 | else DisableStack(type, setDependencies);
|
---|
230 | }
|
---|
231 | }
|
---|
232 | }
|
---|