Changeset 6299 for branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs
- Timestamp:
- 05/26/11 15:11:48 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GP.Grammar.Editor/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs
r6296 r6299 130 130 #region protected grammar manipulation methods 131 131 protected void AddSymbol(ISymbol symbol) { 132 AddSymbolToDictionaries(symbol); 133 var groupSymbol = symbol as GroupSymbol; 134 if (groupSymbol != null) { 135 foreach (ISymbol s in groupSymbol.Flatten()) 136 AddSymbolToDictionaries(s); 137 } 138 139 ClearCaches(); 140 OnChanged(); 141 } 142 private void AddSymbolToDictionaries(ISymbol symbol) { 132 143 symbols.Add(symbol.Name, symbol); 133 144 symbolSubtreeCount.Add(symbol.Name, Tuple.Create(0, 0)); 134 145 RegisterSymbolEvents(symbol); 135 146 } 147 148 protected void RemoveSymbol(ISymbol symbol) { 149 RemoveSymbolFromDictionaries(symbol); 136 150 var groupSymbol = symbol as GroupSymbol; 137 if (groupSymbol != null) AddSymbol(groupSymbol); 151 if (groupSymbol != null) { 152 foreach (ISymbol s in groupSymbol.Flatten()) 153 RemoveSymbolFromDictionaries(s); 154 } 138 155 139 156 ClearCaches(); 140 157 OnChanged(); 141 158 } 142 143 private void AddSymbol(GroupSymbol groupSymbol) { 144 foreach (ISymbol symbol in groupSymbol.Symbols) { 145 symbols.Add(symbol.Name, symbol); 146 symbolSubtreeCount.Add(symbol.Name, Tuple.Create(0, 0)); 147 RegisterSymbolEvents(symbol); 148 149 var childGroup = symbol as GroupSymbol; 150 if (childGroup != null) AddSymbol(childGroup); 151 } 152 } 153 154 protected void RemoveSymbol(ISymbol symbol) { 159 private void RemoveSymbolFromDictionaries(ISymbol symbol) { 155 160 symbols.Remove(symbol.Name); 156 161 allowedChildSymbols.Remove(symbol.Name); … … 170 175 } 171 176 DeregisterSymbolEvents(symbol); 172 ClearCaches();173 OnChanged();174 177 } 175 178 … … 181 184 182 185 protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child) { 186 List<ISymbol> parents; 187 List<ISymbol> childs; 188 bool changed = false; 189 190 var parentGroup = parent as GroupSymbol; 191 if (parentGroup != null) parents = parentGroup.Flatten().Where(p => !(p is GroupSymbol)).ToList(); 192 else parents = new List<ISymbol>() { parent }; 193 var childGroup = child as GroupSymbol; 194 if (childGroup != null) childs = childGroup.Flatten().Where(c => !(c is GroupSymbol)).ToList(); 195 else childs = new List<ISymbol>() { child }; 196 197 foreach (ISymbol p in parents) { 198 foreach (ISymbol c in childs) { 199 changed |= AddAllowedChildSymbolToDictionaries(p, c); 200 } 201 } 202 203 if (changed) { 204 ClearCaches(); 205 OnChanged(); 206 } 207 } 208 209 private bool AddAllowedChildSymbolToDictionaries(ISymbol parent, ISymbol child) { 210 List<string> childSymbols; 211 if (!allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) { 212 childSymbols = new List<string>(); 213 allowedChildSymbols.Add(parent.Name, childSymbols); 214 } 215 if (childSymbols.Contains(child.Name)) return false; 216 183 217 suppressEvents = true; 184 218 for (int argumentIndex = 0; argumentIndex < GetMaximumSubtreeCount(parent); argumentIndex++) … … 186 220 suppressEvents = false; 187 221 188 List<string> childSymbols; 189 if (!allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) { 190 childSymbols = new List<string>(); 191 allowedChildSymbols.Add(parent.Name, childSymbols); 192 } 193 194 if (!childSymbols.Contains(child.Name)) { 195 childSymbols.Add(child.Name); 222 childSymbols.Add(child.Name); 223 return true; 224 } 225 226 protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 227 List<ISymbol> parents; 228 List<ISymbol> childs; 229 bool changed = false; 230 231 var parentGroup = parent as GroupSymbol; 232 if (parentGroup != null) parents = parentGroup.Flatten().Where(p => !(p is GroupSymbol)).ToList(); 233 else parents = new List<ISymbol>() { parent }; 234 var childGroup = child as GroupSymbol; 235 if (childGroup != null) childs = childGroup.Flatten().Where(c => !(c is GroupSymbol)).ToList(); 236 else childs = new List<ISymbol>() { child }; 237 238 foreach (ISymbol p in parents) { 239 foreach (ISymbol c in childs) { 240 changed |= AddAllowedChildSymbolToDictionaries(p, c, argumentIndex); 241 } 242 } 243 244 if (changed) { 196 245 ClearCaches(); 197 246 OnChanged(); … … 199 248 } 200 249 201 protected void AddAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) { 250 251 private bool AddAllowedChildSymbolToDictionaries(ISymbol parent, ISymbol child, int argumentIndex) { 202 252 List<string> childSymbols; 203 253 if (allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) 204 if (childSymbols.Contains(child.Name)) return ;254 if (childSymbols.Contains(child.Name)) return false; 205 255 206 256 var key = Tuple.Create(parent.Name, argumentIndex); … … 209 259 allowedChildSymbolsPerIndex.Add(key, childSymbols); 210 260 } 211 212 if (!childSymbols.Contains(child.Name)) { 213 childSymbols.Add(child.Name); 214 ClearCaches(); 215 OnChanged(); 216 } 261 if (childSymbols.Contains(child.Name)) return false; 262 263 childSymbols.Add(child.Name); 264 return true; 217 265 } 218 266 … … 221 269 List<string> childSymbols; 222 270 if (allowedChildSymbols.TryGetValue(child.Name, out childSymbols)) { 223 if (allowedChildSymbols[parent.Name].Remove(child.Name)) changed = true;271 changed |= childSymbols.Remove(child.Name); 224 272 } 225 273 226 274 for (int argumentIndex = 0; argumentIndex < GetMaximumSubtreeCount(parent); argumentIndex++) { 227 275 var key = Tuple.Create(parent.Name, argumentIndex); 228 if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) { 229 if (allowedChildSymbolsPerIndex[key].Remove(child.Name)) changed = true; 230 } 276 if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) 277 changed |= childSymbols.Remove(child.Name); 231 278 } 232 279 … … 240 287 bool changed = false; 241 288 289 suppressEvents = true; 242 290 List<string> childSymbols; 243 291 if (allowedChildSymbols.TryGetValue(parent.Name, out childSymbols)) { 244 292 if (childSymbols.Remove(child.Name)) { 245 suppressEvents = true;246 293 for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) { 247 294 if (i != argumentIndex) AddAllowedChildSymbol(parent, child, i); 248 295 } 249 suppressEvents = false;250 296 changed = true; 251 297 } 252 298 } 299 suppressEvents = false; 253 300 254 301 var key = Tuple.Create(parent.Name, argumentIndex); 255 if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) { 256 if (allowedChildSymbolsPerIndex[key].Remove(child.Name)) 257 changed = true; 258 } 302 if (allowedChildSymbolsPerIndex.TryGetValue(key, out childSymbols)) 303 changed |= childSymbols.Remove(child.Name); 259 304 260 305 if (changed) { … … 265 310 266 311 protected void SetSubtreeCount(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) { 312 var groupSymbol = symbol as GroupSymbol; 313 if (groupSymbol != null) 314 foreach (ISymbol s in groupSymbol.Flatten().Where(s => !(s is GroupSymbol))) 315 SetSubTreeCountInDictionaries(s, minimumSubtreeCount, maximumSubtreeCount); 316 else 317 SetSubTreeCountInDictionaries(symbol, minimumSubtreeCount, maximumSubtreeCount); 318 319 ClearCaches(); 320 OnChanged(); 321 } 322 323 private void SetSubTreeCountInDictionaries(ISymbol symbol, int minimumSubtreeCount, int maximumSubtreeCount) { 267 324 for (int i = GetMaximumSubtreeCount(symbol) - 1; i >= maximumSubtreeCount; i--) { 268 325 var key = Tuple.Create(symbol.Name, i); … … 271 328 272 329 symbolSubtreeCount[symbol.Name] = Tuple.Create(minimumSubtreeCount, maximumSubtreeCount); 273 ClearCaches();274 OnChanged();275 330 } 276 331 #endregion … … 291 346 List<string> temp; 292 347 if (allowedChildSymbols.TryGetValue(parent.Name, out temp)) 293 if (temp.Contains(child.Name)) return true;348 return temp.Contains(child.Name); 294 349 return false; 295 350 }
Note: See TracChangeset
for help on using the changeset viewer.