Changeset 15032 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Configuration/PushConfiguration.cs
- Timestamp:
- 06/12/17 14:11:43 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Configuration/PushConfiguration.cs
r15017 r15032 1 1 namespace HeuristicLab.Problems.ProgramSynthesis.Push.Configuration { 2 using System;3 2 using System.Collections.Generic; 4 using System.Linq;5 3 using Base.Erc; 6 4 using Common; 7 using Core;8 using Expressions;9 5 using Persistence.Default.CompositeSerializers.Storable; 10 using Stack;11 6 12 7 [StorableClass] 13 public class PushConfiguration : P arameterizedNamedItem, IReadOnlyPushConfiguration, IEnabledExpressionsConfiguration {8 public class PushConfiguration : PushConfigurationBase, IReadOnlyPushConfiguration { 14 9 public PushConfiguration() { 15 10 Name = "Push Configuration"; 16 17 enabledExpressions = ExpressionTable.ExpressionNames.ToList();18 enabledStacks = new Dictionary<StackTypes, bool>();19 11 20 12 ErcOptions = new ErcOptions(); … … 31 23 MaxParenthesesClose = 4; 32 24 ParenthesesCloseBiasLevel = 1; 33 34 InitEnabledStacks();35 25 } 36 26 37 private void InitEnabledStacks(bool state = true) {38 foreach (StackTypes type in Enum.GetValues(typeof(StackTypes))) {39 if (!enabledStacks.ContainsKey(type) && type != StackTypes.None)40 enabledStacks.Add(type, state);41 }42 }43 27 44 28 [StorableConstructor] … … 48 32 49 33 public PushConfiguration(PushConfiguration origin, Cloner cloner) : base(origin, cloner) { 50 enabledExpressions = origin.EnabledExpressions.ToList();51 enabledStacks = origin.EnabledStacks.ToDictionary(x => x.Key, x => x.Value);52 34 53 35 ErcOptions = cloner.Clone(origin.ErcOptions); … … 66 48 } 67 49 68 [StorableHook(HookType.AfterDeserialization)]69 // ReSharper disable once UnusedMember.Local70 private void AfterDeserialization() {71 // Ensures that all types added after last serialization are available in enabledStacks72 InitEnabledStacks(false);73 }74 75 50 [Storable] 76 51 public string FloatStringFormat { get; set; } 77 52 78 [Storable]79 private readonly Dictionary<StackTypes, bool> enabledStacks;80 53 81 public IReadOnlyDictionary<StackTypes, bool> EnabledStacks { get { return enabledStacks; } }82 83 public event EventHandler<EnabledExpressionsChangedEventArgs> EnabledExpressionsChanged;84 85 [Storable]86 private readonly List<string> enabledExpressions;87 88 public IList<string> EnabledExpressions89 {90 get { return enabledExpressions; }91 set92 {93 var removedExpressions = enabledExpressions.ToArray();94 enabledExpressions.Clear();95 enabledExpressions.AddRange(value);96 97 if (EnabledExpressionsChanged != null) {98 EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(value, removedExpressions));99 }100 }101 }102 54 IReadOnlyList<string> IReadOnlyPushConfiguration.EnabledExpressions { get { return enabledExpressions; } } 103 55 … … 174 126 public int MaxVectorLength { get; set; } 175 127 176 177 public void SetEnabledStacks(StackTypes types) {178 // Disable all179 EnabledExpressions.Clear();180 181 foreach (StackTypes type in Enum.GetValues(types.GetType())) {182 if (type == StackTypes.None) continue;183 enabledStacks[type] = false;184 }185 186 foreach (var pair in ExpressionTable.StackDependencyToNamesTable) {187 if (types.HasFlag(pair.Key)) {188 foreach (var name in pair.Value) {189 EnableExpression(name, true);190 }191 }192 }193 }194 195 public void EnableExpressionOfStack(StackTypes types) {196 EnableExpressions(ExpressionTable.StackTypeToNamesTable[types]);197 }198 199 public void EnableExpressionDependentOnStack(StackTypes types) {200 var names = ExpressionTable.StackDependencyToNamesTable[types].Where(201 name => {202 var type = ExpressionTable.NameToTypeTable[name];203 var attribute = ExpressionTable.TypeToAttributeTable[type];204 205 return (attribute.StackType | attribute.AdditionalStackDependencies)206 .ToEnumerable()207 .All(st => enabledStacks.ContainsKey(st) && enabledStacks[st]);208 });209 210 EnableExpressions(names);211 }212 213 private void EnableExpressions(IEnumerable<string> names) {214 foreach (var name in names.Except(EnabledExpressions))215 EnabledExpressions.Add(name);216 217 if (EnabledExpressionsChanged != null) {218 EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(names, new string[0]));219 }220 }221 222 public void DisableExpressionsOfStack(StackTypes types) {223 DisableExpressions(ExpressionTable.StackTypeToNamesTable[types]);224 }225 226 public void DisableExpressionsDependentOnStack(StackTypes types) {227 var names = ExpressionTable.StackDependencyToNamesTable228 .Where(pair => pair.Key.HasFlag(types))229 .SelectMany(pair => pair.Value);230 231 DisableExpressions(names);232 }233 234 private void DisableExpressions(IEnumerable<string> names) {235 foreach (var name in names.Intersect(EnabledExpressions))236 EnabledExpressions.Remove(name);237 238 if (EnabledExpressionsChanged != null) {239 EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(new string[0], names));240 }241 }242 243 public void EnableExpression(string name, bool enableStackIfDisabled = false) {244 if (EnabledExpressions.Contains(name)) return;245 246 EnabledExpressions.Add(name);247 248 if (enableStackIfDisabled) {249 var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == name).Key;250 var attribute = ExpressionTable.TypeToAttributeTable[type];251 enabledStacks[attribute.StackType] = true;252 }253 254 if (EnabledExpressionsChanged != null) {255 EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(256 new[] { name },257 new string[0]));258 }259 }260 261 public void DisableExpression(string name, bool disableStackIfEnabled = false) {262 if (!EnabledExpressions.Contains(name)) return;263 264 EnabledExpressions.Remove(name);265 266 if (disableStackIfEnabled) {267 var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == name).Key;268 var attribute = ExpressionTable.TypeToAttributeTable[type];269 enabledStacks[attribute.StackType] = false;270 }271 272 if (EnabledExpressionsChanged != null) {273 EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(274 new string[0],275 new[] { name }));276 }277 }278 279 public void EnableExpression<T>(bool enableStackIfDisabled = false) where T : Expression {280 var attribute = ExpressionTable.TypeToAttributeTable[typeof(T)];281 EnableExpression(attribute.ExpressionName, enableStackIfDisabled);282 }283 284 public void DisableExpression<T>(bool disableStackIfEnabled = false) where T : Expression {285 var attribute = ExpressionTable.TypeToAttributeTable[typeof(T)];286 DisableExpression(attribute.ExpressionName, disableStackIfEnabled);287 }288 289 public void SetExpression(string name, bool state, bool cascadeForStack = false) {290 if (state) EnableExpression(name, cascadeForStack);291 else DisableExpression(name, cascadeForStack);292 }293 294 public void SetExpression<T>(bool state, bool cascadeForStack = false) where T : Expression {295 if (state) EnableExpression<T>(cascadeForStack);296 else DisableExpression<T>(cascadeForStack);297 }298 299 public void EnableStack(StackTypes type, bool enableExpressions = true, bool enableDependencies = true) {300 enabledStacks[type] = true;301 302 if (enableExpressions) EnableExpressionOfStack(type);303 if (enableDependencies) EnableExpressionDependentOnStack(type);304 }305 306 public void DisableStack(StackTypes type, bool disableExpressions = true, bool disableDependencies = true) {307 enabledStacks[type] = false;308 309 if (disableExpressions) DisableExpressionsOfStack(type);310 if (disableDependencies) DisableExpressionsDependentOnStack(type);311 }312 313 public void SetStack(StackTypes type, bool state, bool setExpressions = true, bool setDependencies = true) {314 if (state) EnableStack(type, setExpressions, setDependencies);315 else DisableStack(type, setExpressions, setDependencies);316 }317 318 128 public override IDeepCloneable Clone(Cloner cloner) { 319 129 return new PushConfiguration(this, cloner);
Note: See TracChangeset
for help on using the changeset viewer.