Changeset 15032 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Configuration/PushConfigurationBase.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/PushConfigurationBase.cs
r15017 r15032 8 8 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 9 9 using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; 10 using HeuristicLab.Problems.ProgramSynthesis.Push.Extensions; 10 11 using HeuristicLab.Problems.ProgramSynthesis.Push.Stack; 11 12 … … 17 18 18 19 enabledExpressions = ExpressionTable.ExpressionNames.ToList(); 19 enabledStacks = new Dictionary<StackTypes, bool>(); 20 21 InitEnabledStacks(); 20 expressionsPerStackCount = new Dictionary<StackTypes, int>(); 21 InitExpressionsPerStackCount(); 22 22 } 23 23 … … 27 27 protected PushConfigurationBase(PushConfigurationBase origin, Cloner cloner) : base(origin, cloner) { 28 28 enabledExpressions = origin.EnabledExpressions.ToList(); 29 e nabledStacks = origin.EnabledStacks.ToDictionary(x => x.Key, x => x.Value);29 expressionsPerStackCount = origin.expressionsPerStackCount.ToDictionary(x => x.Key, x => x.Value); 30 30 } 31 31 32 32 [Storable] 33 private readonly Dictionary<StackTypes, bool> enabledStacks; 34 35 public IReadOnlyDictionary<StackTypes, bool> EnabledStacks { get { return enabledStacks; } } 33 private readonly Dictionary<StackTypes, int> expressionsPerStackCount; 36 34 37 35 public event EventHandler<EnabledExpressionsChangedEventArgs> EnabledExpressionsChanged; … … 40 38 protected readonly List<string> enabledExpressions; 41 39 42 public IList<string> EnabledExpressions 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 43 46 { 44 47 get { return enabledExpressions; } … … 55 58 } 56 59 57 private void InitE nabledStacks(bool state = true) {60 private void InitExpressionsPerStackCount() { 58 61 foreach (StackTypes type in Enum.GetValues(typeof(StackTypes))) { 59 if (!enabledStacks.ContainsKey(type) && type != StackTypes.None) 60 enabledStacks.Add(type, state); 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 expressionsPerStackCount[attribute.StackType]++; 71 72 foreach (var additionalStackType in attribute.AdditionalStackDependencies.ToValues()) 73 expressionsPerStackCount[additionalStackType]++; 61 74 } 62 75 } … … 64 77 public void SetEnabledStacks(StackTypes types) { 65 78 // Disable all 66 EnabledExpressions.Clear();67 68 foreach (StackTypes type in Enum.GetValues(type s.GetType())) {79 enabledExpressions.Clear(); 80 81 foreach (StackTypes type in Enum.GetValues(typeof(StackTypes))) { 69 82 if (type == StackTypes.None) continue; 70 enabledStacks[type] = false; 71 } 72 73 foreach (var pair in ExpressionTable.StackDependencyToNamesTable) { 74 if (types.HasFlag(pair.Key)) { 75 foreach (var name in pair.Value) { 76 EnableExpression(name, true); 77 } 78 } 79 } 83 expressionsPerStackCount[type] = 0; 84 } 85 86 var expressionNames = ExpressionTable.StackDependencyToNamesTable 87 .Where(pair => types.HasFlag(pair.Key)) 88 .SelectMany(pair => pair.Value); 89 90 EnableExpressions(expressionNames); 80 91 } 81 92 … … 92 103 return (attribute.StackType | attribute.AdditionalStackDependencies) 93 104 .ToEnumerable() 94 .All( st => enabledStacks.ContainsKey(st) && enabledStacks[st]);105 .All(IsStackEnabled); 95 106 }); 96 107 … … 98 109 } 99 110 100 private void EnableExpressions(IEnumerable<string> names) {101 foreach (var name in names.Except(EnabledExpressions))102 Enable dExpressions.Add(name);111 private void EnableExpressions(IEnumerable<string> expressionNames) { 112 foreach (var expressionName in expressionNames.Except(EnabledExpressions)) 113 EnableExpression(expressionName, false); 103 114 104 115 if (EnabledExpressionsChanged != null) { 105 EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs( names, new string[0]));116 EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(expressionNames, new string[0])); 106 117 } 107 118 } … … 119 130 } 120 131 121 private void DisableExpressions(IEnumerable<string> names) {122 foreach (var name in names.Intersect(EnabledExpressions))123 EnabledExpressions.Remove(name);132 private void DisableExpressions(IEnumerable<string> expressionNames) { 133 foreach (var expressionName in expressionNames.Intersect(EnabledExpressions)) 134 DisableExpression(expressionName, false); 124 135 125 136 if (EnabledExpressionsChanged != null) { 126 EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(new string[0], names)); 127 } 128 } 129 130 public void EnableExpression(string name, bool enableStackIfDisabled = false) { 131 if (EnabledExpressions.Contains(name)) return; 132 133 EnabledExpressions.Add(name); 134 135 if (enableStackIfDisabled) { 136 var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == name).Key; 137 var attribute = ExpressionTable.TypeToAttributeTable[type]; 138 enabledStacks[attribute.StackType] = true; 139 } 140 141 if (EnabledExpressionsChanged != null) { 137 EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(new string[0], expressionNames)); 138 } 139 } 140 141 public void EnableExpression(string expressionName) { 142 EnableExpression(expressionName, true); 143 } 144 145 private void EnableExpression(string expressionName, bool triggerExpressionChanged) { 146 if (enabledExpressions.Contains(expressionName)) return; 147 148 var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == expressionName).Key; 149 var attribute = ExpressionTable.TypeToAttributeTable[type]; 150 151 enabledExpressions.Add(expressionName); 152 expressionsPerStackCount[attribute.StackType]++; 153 foreach (var additionalStackType in attribute.AdditionalStackDependencies.ToValues()) 154 expressionsPerStackCount[additionalStackType]++; 155 156 if (EnabledExpressionsChanged != null && triggerExpressionChanged) { 142 157 EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs( 143 new[] { name },158 new[] { expressionName }, 144 159 new string[0])); 145 160 } 146 161 } 147 162 148 public void DisableExpression(string name, bool disableStackIfEnabled = false) { 149 if (!EnabledExpressions.Contains(name)) return; 150 151 EnabledExpressions.Remove(name); 152 153 if (disableStackIfEnabled) { 154 var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == name).Key; 155 var attribute = ExpressionTable.TypeToAttributeTable[type]; 156 enabledStacks[attribute.StackType] = false; 157 } 158 159 if (EnabledExpressionsChanged != null) { 163 public void DisableExpression(string expressionName) { 164 DisableExpression(expressionName, true); 165 } 166 167 public void DisableExpression(string expressionName, bool triggerExpressionChanged) { 168 if (!EnabledExpressions.Contains(expressionName)) return; 169 170 var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == expressionName).Key; 171 var attribute = ExpressionTable.TypeToAttributeTable[type]; 172 173 enabledExpressions.Remove(expressionName); 174 expressionsPerStackCount[attribute.StackType]--; 175 foreach (var additionalStackType in attribute.AdditionalStackDependencies.ToValues()) 176 expressionsPerStackCount[additionalStackType]--; 177 178 if (EnabledExpressionsChanged != null && triggerExpressionChanged) { 160 179 EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs( 161 180 new string[0], 162 new[] { name }));163 } 164 } 165 166 public void EnableExpression<T>( bool enableStackIfDisabled = false) where T : Expression {181 new[] { expressionName })); 182 } 183 } 184 185 public void EnableExpression<T>() where T : Expression { 167 186 var attribute = ExpressionTable.TypeToAttributeTable[typeof(T)]; 168 EnableExpression(attribute. ExpressionName, enableStackIfDisabled);169 } 170 171 public void DisableExpression<T>( bool disableStackIfEnabled = false) where T : Expression {187 EnableExpression(attribute.Name); 188 } 189 190 public void DisableExpression<T>() where T : Expression { 172 191 var attribute = ExpressionTable.TypeToAttributeTable[typeof(T)]; 173 DisableExpression(attribute. ExpressionName, disableStackIfEnabled);174 } 175 176 public void SetExpression(string name, bool state , bool cascadeForStack = false) {177 if (state) EnableExpression(name , cascadeForStack);178 else DisableExpression(name , cascadeForStack);179 } 180 181 public void SetExpression<T>(bool state , bool cascadeForStack = false) where T : Expression {182 if (state) EnableExpression<T>( cascadeForStack);183 else DisableExpression<T>( cascadeForStack);184 } 185 186 public void EnableStack(StackTypes type, bool enable Expressions = true, bool enableDependencies = true) {187 enabledStacks[type] = true;188 189 if (enable Expressions) EnableExpressionOfStack(type);190 if (enableDependencies)EnableExpressionDependentOnStack(type);191 } 192 193 public void DisableStack(StackTypes type, bool disable Expressions = true, bool disableDependencies = true) {194 enabledStacks[type] = false;195 196 if (disable Expressions) DisableExpressionsOfStack(type);197 if (disableDependencies)DisableExpressionsDependentOnStack(type);198 } 199 200 public void SetStack(StackTypes type, bool state, bool set Expressions = true, bool setDependencies = true) {201 if (state) EnableStack(type, set Expressions, setDependencies);202 else DisableStack(type, set Expressions, setDependencies);192 DisableExpression(attribute.Name); 193 } 194 195 public void SetExpression(string name, bool state) { 196 if (state) EnableExpression(name); 197 else DisableExpression(name); 198 } 199 200 public void SetExpression<T>(bool state) where T : Expression { 201 if (state) EnableExpression<T>(); 202 else DisableExpression<T>(); 203 } 204 205 public void EnableStack(StackTypes type, bool enableDependencies = false) { 206 EnableExpressionOfStack(type); 207 208 if (enableDependencies) 209 EnableExpressionDependentOnStack(type); 210 } 211 212 public void DisableStack(StackTypes type, bool disableDependencies = false) { 213 DisableExpressionsOfStack(type); 214 215 if (disableDependencies) 216 DisableExpressionsDependentOnStack(type); 217 } 218 219 public void SetStack(StackTypes type, bool state, bool setDependencies = false) { 220 if (state) EnableStack(type, setDependencies); 221 else DisableStack(type, setDependencies); 203 222 } 204 223 }
Note: See TracChangeset
for help on using the changeset viewer.