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