1 | namespace HeuristicLab.Problems.ProgramSynthesis.Push.Configuration {
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Linq;
|
---|
5 | using Attributes;
|
---|
6 | using Common;
|
---|
7 | using Core;
|
---|
8 | using Erc;
|
---|
9 | using Erc.Interfaces;
|
---|
10 | using Expressions;
|
---|
11 | using Persistence.Default.CompositeSerializers.Storable;
|
---|
12 | using Stack;
|
---|
13 |
|
---|
14 | [StorableClass]
|
---|
15 | public class PushConfiguration : ParameterizedNamedItem, IReadOnlyPushConfiguration, IEnabledExpressionsConfiguration {
|
---|
16 |
|
---|
17 |
|
---|
18 |
|
---|
19 | public PushConfiguration() {
|
---|
20 | Name = "Push Configuration";
|
---|
21 |
|
---|
22 | enabledExpressions = ExpressionTable.ExpressionNames.ToList();
|
---|
23 | EnabledStacks = new Dictionary<StackTypes, bool>();
|
---|
24 |
|
---|
25 | ErcOptions = new ErcOptions();
|
---|
26 | EvalPushLimit = 1024;
|
---|
27 | MaxPointsInProgram = 128;
|
---|
28 | TopLevelPushCode = true;
|
---|
29 | TopLevelPopCode = false;
|
---|
30 | MaxPointsInRandomExpression = 64;
|
---|
31 | MaxStringLength = 32;
|
---|
32 | MaxVectorLength = 64;
|
---|
33 | MaxDepth = 32;
|
---|
34 |
|
---|
35 | InitEnabledStacks();
|
---|
36 | }
|
---|
37 |
|
---|
38 | private void InitEnabledStacks(bool state = true) {
|
---|
39 | foreach (StackTypes type in Enum.GetValues(typeof(StackTypes))) {
|
---|
40 | if (!EnabledStacks.ContainsKey(type))
|
---|
41 | EnabledStacks.Add(type, state);
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | [StorableConstructor]
|
---|
46 | public PushConfiguration(bool deserializing)
|
---|
47 | : base(deserializing) {
|
---|
48 | }
|
---|
49 |
|
---|
50 | public PushConfiguration(PushConfiguration origin, Cloner cloner) : base(origin, cloner) {
|
---|
51 | enabledExpressions = origin.EnabledExpressions.ToList();
|
---|
52 | EnabledStacks = origin.EnabledStacks.ToDictionary(x => x.Key, x => x.Value);
|
---|
53 |
|
---|
54 | ErcOptions = cloner.Clone(origin.ErcOptions);
|
---|
55 | EvalPushLimit = origin.EvalPushLimit;
|
---|
56 | MaxPointsInProgram = origin.MaxPointsInProgram;
|
---|
57 | MaxPointsInRandomExpression = origin.MaxPointsInRandomExpression;
|
---|
58 | TopLevelPushCode = origin.TopLevelPushCode;
|
---|
59 | TopLevelPopCode = origin.TopLevelPopCode;
|
---|
60 | MaxStringLength = origin.MaxStringLength;
|
---|
61 | MaxVectorLength = origin.MaxVectorLength;
|
---|
62 | MaxDepth = origin.MaxDepth;
|
---|
63 | }
|
---|
64 |
|
---|
65 | [StorableHook(HookType.AfterDeserialization)]
|
---|
66 | // ReSharper disable once UnusedMember.Local
|
---|
67 | private void AfterDeserialization() {
|
---|
68 | // Ensures that all types added after last serialization are available in enabledStacks
|
---|
69 | InitEnabledStacks(false);
|
---|
70 | }
|
---|
71 |
|
---|
72 | [Storable]
|
---|
73 | public IDictionary<StackTypes, bool> EnabledStacks { get; set; }
|
---|
74 |
|
---|
75 | IReadOnlyDictionary<StackTypes, bool> IReadOnlyPushConfiguration.EnabledStacks { get { return EnabledStacks as IReadOnlyDictionary<StackTypes, bool>; } }
|
---|
76 |
|
---|
77 | public event EventHandler<EnabledExpressionsChangedEventArgs> EnabledExpressionsChanged;
|
---|
78 |
|
---|
79 | [Storable]
|
---|
80 | private readonly List<string> enabledExpressions;
|
---|
81 |
|
---|
82 | public IList<string> EnabledExpressions
|
---|
83 | {
|
---|
84 | get { return enabledExpressions; }
|
---|
85 | set
|
---|
86 | {
|
---|
87 | var removedExpressions = enabledExpressions.ToArray();
|
---|
88 | enabledExpressions.Clear();
|
---|
89 | enabledExpressions.AddRange(value);
|
---|
90 |
|
---|
91 | if (EnabledExpressionsChanged != null) {
|
---|
92 | EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(value, removedExpressions));
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | IReadOnlyList<string> IReadOnlyPushConfiguration.EnabledExpressions { get { return EnabledExpressions as IReadOnlyList<string>; } }
|
---|
98 |
|
---|
99 | [Storable]
|
---|
100 | public ErcOptions ErcOptions { get; set; }
|
---|
101 |
|
---|
102 | IReadOnlyErcOptions IReadOnlyPushConfiguration.ErcOptions { get { return ErcOptions; } }
|
---|
103 |
|
---|
104 | /// <summary>
|
---|
105 | /// This is the maximum allowed number of "executions" in a single top-level call to the interpreter.
|
---|
106 | /// The execution of a single Push instruction counts as one execution, as does the processing of a single literal,
|
---|
107 | /// as does the descent into one layer of parentheses (that is, the processing of the "(" counts as one execution).
|
---|
108 | /// When this limit is exceeded the interpreter aborts immediately, leaving its stacks in the states they were in prior
|
---|
109 | /// to the abort (so they may still be examined by a calling program). Whether or not this counts as an "abnormal"
|
---|
110 | /// termination
|
---|
111 | /// is up to the calling program.
|
---|
112 | /// </summary>
|
---|
113 | [Storable]
|
---|
114 | public int EvalPushLimit { get; set; }
|
---|
115 |
|
---|
116 | /// <summary>
|
---|
117 | /// This is the maximum of depth a push program can have. Expressions, which lead to exceed this limit are interpreted as NOOP.
|
---|
118 | /// </summary>
|
---|
119 | [Storable]
|
---|
120 | public int MaxDepth { get; set; }
|
---|
121 |
|
---|
122 | /// <summary>
|
---|
123 | /// This is the maximum size of an item on the CODE stack, expressed as a number of points.
|
---|
124 | /// A point is an instruction, a literal, or a pair of parentheses. Any instruction that would cause this limit to be
|
---|
125 | /// exceeded
|
---|
126 | /// should instead act as a NOOP, leaving all stacks in the states that they were in before the execution of the
|
---|
127 | /// instruction.
|
---|
128 | /// </summary>
|
---|
129 | [Storable]
|
---|
130 | public int MaxPointsInProgram { get; set; }
|
---|
131 |
|
---|
132 | /// <summary>
|
---|
133 | /// The maximum number of points in an expression produced by the CODE.RAND instruction.
|
---|
134 | /// </summary>
|
---|
135 | [Storable]
|
---|
136 | public int MaxPointsInRandomExpression { get; set; }
|
---|
137 |
|
---|
138 | /// <summary>
|
---|
139 | /// When TRUE (which is the default), code passed to the top level of the interpreter
|
---|
140 | /// will be pushed onto the CODE stack prior to execution.
|
---|
141 | /// </summary>
|
---|
142 | [Storable]
|
---|
143 | public bool TopLevelPushCode { get; set; }
|
---|
144 |
|
---|
145 | /// <summary>
|
---|
146 | /// When TRUE, the CODE stack will be popped at the end of top level calls to the interpreter. The default is FALSE.
|
---|
147 | /// </summary>
|
---|
148 | [Storable]
|
---|
149 | public bool TopLevelPopCode { get; set; }
|
---|
150 |
|
---|
151 | [Storable]
|
---|
152 | public int MaxStringLength { get; set; }
|
---|
153 |
|
---|
154 | [Storable]
|
---|
155 | public int MaxVectorLength { get; set; }
|
---|
156 |
|
---|
157 | public void EnableExpressionOfStack(StackTypes types) {
|
---|
158 | var names = ExpressionTable.StackTypeToNamesTable[types]
|
---|
159 | .Except(EnabledExpressions)
|
---|
160 | .ToArray();
|
---|
161 |
|
---|
162 | foreach (var name in names)
|
---|
163 | EnabledExpressions.Add(name);
|
---|
164 |
|
---|
165 | if (EnabledExpressionsChanged != null) {
|
---|
166 | EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(names, new string[0]));
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | public void DisableExpressionOfStack(StackTypes types) {
|
---|
171 | var names = ExpressionTable.StackTypeToNamesTable[types]
|
---|
172 | .Intersect(EnabledExpressions)
|
---|
173 | .ToArray();
|
---|
174 |
|
---|
175 | foreach (var name in names)
|
---|
176 | EnabledExpressions.Remove(name);
|
---|
177 |
|
---|
178 | if (EnabledExpressionsChanged != null) {
|
---|
179 | EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(new string[0], names));
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | public void EnableExpression(string name, bool enableStackIfDisabled = false) {
|
---|
184 | if (EnabledExpressions.Contains(name)) return;
|
---|
185 |
|
---|
186 | EnabledExpressions.Add(name);
|
---|
187 |
|
---|
188 | if (enableStackIfDisabled) {
|
---|
189 | var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == name).Key;
|
---|
190 | var attribute = ExpressionTable.TypeToAttributeTable[type];
|
---|
191 | EnabledStacks[attribute.StackType] = true;
|
---|
192 | }
|
---|
193 |
|
---|
194 | if (EnabledExpressionsChanged != null) {
|
---|
195 | EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(
|
---|
196 | new[] { name },
|
---|
197 | new string[0]));
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | public void DisableExpression(string name, bool disableStackIfEnabled = false) {
|
---|
202 | if (!EnabledExpressions.Contains(name)) return;
|
---|
203 |
|
---|
204 | EnabledExpressions.Remove(name);
|
---|
205 |
|
---|
206 | if (disableStackIfEnabled) {
|
---|
207 | var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == name).Key;
|
---|
208 | var attribute = ExpressionTable.TypeToAttributeTable[type];
|
---|
209 | EnabledStacks[attribute.StackType] = false;
|
---|
210 | }
|
---|
211 |
|
---|
212 | if (EnabledExpressionsChanged != null) {
|
---|
213 | EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(
|
---|
214 | new string[0],
|
---|
215 | new[] { name }));
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | public void EnableExpression<T>(bool enableStackIfDisabled = false) where T : Expression {
|
---|
220 | var attribute = (PushExpressionAttribute)Attribute.GetCustomAttribute(typeof(T), typeof(PushExpressionAttribute));
|
---|
221 | EnableExpression(attribute.ExpressionName, enableStackIfDisabled);
|
---|
222 | }
|
---|
223 |
|
---|
224 | public void DisableExpression<T>(bool disableStackIfEnabled = false) where T : Expression {
|
---|
225 | var attribute = (PushExpressionAttribute)Attribute.GetCustomAttribute(typeof(T), typeof(PushExpressionAttribute));
|
---|
226 | DisableExpression(attribute.ExpressionName, disableStackIfEnabled);
|
---|
227 | }
|
---|
228 |
|
---|
229 | public void SetExpression(string name, bool state, bool cascadeForStack = false) {
|
---|
230 | if (state) EnableExpression(name, cascadeForStack);
|
---|
231 | else DisableExpression(name, cascadeForStack);
|
---|
232 | }
|
---|
233 |
|
---|
234 | public void SetExpression<T>(bool state, bool cascadeForStack = false) where T : Expression {
|
---|
235 | if (state) EnableExpression<T>(cascadeForStack);
|
---|
236 | else DisableExpression<T>(cascadeForStack);
|
---|
237 | }
|
---|
238 |
|
---|
239 | public void EnableStack(StackTypes type, bool enableExpressions = false) {
|
---|
240 | EnabledStacks[type] = true;
|
---|
241 |
|
---|
242 | if (enableExpressions)
|
---|
243 | EnableExpressionOfStack(type);
|
---|
244 | }
|
---|
245 |
|
---|
246 | public void DisableStack(StackTypes type, bool disableExpressions = false) {
|
---|
247 | EnabledStacks[type] = false;
|
---|
248 |
|
---|
249 | if (disableExpressions)
|
---|
250 | DisableExpressionOfStack(type);
|
---|
251 | }
|
---|
252 |
|
---|
253 | public void SetStack(StackTypes type, bool state, bool cascadeForExpressions = false) {
|
---|
254 | if (state) EnableStack(type, cascadeForExpressions);
|
---|
255 | else DisableStack(type, cascadeForExpressions);
|
---|
256 | }
|
---|
257 |
|
---|
258 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
259 | return new PushConfiguration(this, cloner);
|
---|
260 | }
|
---|
261 | }
|
---|
262 | } |
---|