Changeset 18195


Ignore:
Timestamp:
01/14/22 09:49:45 (7 days ago)
Author:
mkommend
Message:

#3136: Refactored creation of subfunctions in StructureTemplate.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/StructureTemplate.cs

    r18194 r18195  
    6464        tree = value;
    6565
    66         var newFunctions = GetSubFunctions();
     66        var newFunctions = CreateSubFunctions(tree);
    6767        var oldFunctions = subFunctions?.Intersect(newFunctions)
    6868                           ?? Enumerable.Empty<SubFunction>();
    6969        // adds new functions and keeps the old ones (if they match)
    70         subFunctions = newFunctions.Except(oldFunctions).Concat(oldFunctions).ToList();
     70        var functionsToAdd = newFunctions.Except(oldFunctions);
     71        subFunctions = functionsToAdd.Concat(oldFunctions).ToList();
     72        RegisterSubFunctionEventHandlers(functionsToAdd);
    7173      }
    7274    }
     
    124126      this.applyLinearScaling = original.ApplyLinearScaling;
    125127      this.subFunctions = original.subFunctions.Select(cloner.Clone).ToList();
    126       RegisterEventHandlers();
     128      RegisterSubFunctionEventHandlers(SubFunctions);
    127129    }
    128130
     
    136138      }
    137139
    138       RegisterEventHandlers();
     140      RegisterSubFunctionEventHandlers(SubFunctions);
    139141    }
    140142    #endregion
     
    151153    }
    152154
    153     private IList<SubFunction> GetSubFunctions() {
     155    private static IList<SubFunction> CreateSubFunctions(ISymbolicExpressionTree tree) {
    154156      var subFunctions = new List<SubFunction>();
    155       foreach (var node in Tree.IterateNodesPrefix())
     157      foreach (var node in tree.IterateNodesPrefix())
    156158        if (node is SubFunctionTreeNode subFunctionTreeNode) {
    157159          if (!subFunctionTreeNode.Arguments.Any())
     
    171173              Arguments = subFunctionTreeNode.Arguments
    172174            };
    173             subFunction.Changed += OnSubFunctionChanged;
    174175            subFunctions.Add(subFunction);
    175176          }
     
    178179    }
    179180
    180     private void RegisterEventHandlers() {
    181       foreach (var sf in SubFunctions) {
    182         sf.Changed += OnSubFunctionChanged;
     181    private void RegisterSubFunctionEventHandlers(IEnumerable<SubFunction> subFunctions) {
     182      foreach (var sf in subFunctions) {
     183        sf.Changed += (o, e) => OnChanged();
    183184      }
    184     }
    185     private void OnSubFunctionChanged(object sender, EventArgs e) => OnChanged();
     185    }   
    186186  }
    187187}
Note: See TracChangeset for help on using the changeset viewer.