Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/20/08 21:44:59 (16 years ago)
Author:
gkronber
Message:

fixed two loose ends from r525 (#263):

  • update all SubOperatorTypeConstraints when an operator is removed from the operator library
  • invalidate the cached list of allowed sub functions in FunctionBase when one of the constraints is changed
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Functions/FunctionBase.cs

    r528 r529  
    2828using HeuristicLab.DataAnalysis;
    2929using HeuristicLab.Constraints;
     30using System.Diagnostics;
    3031
    3132namespace HeuristicLab.Functions {
     
    8081    public IList<IFunction> AllowedSubFunctions(int index) {
    8182      if(allowedSubFunctions == null) {
     83        // first time: analyze the constraint and create a cached copy of the allowed sub-functions
    8284        allowedSubFunctions = new List<IFunction>[MaxArity];
    8385        for(int i = 0; i < MaxArity; i++) {
    84           foreach(IConstraint constraint in Constraints) {
    85             if(constraint is SubOperatorTypeConstraint) {
    86               SubOperatorTypeConstraint subOpConstraint = constraint as SubOperatorTypeConstraint;
    87               if(subOpConstraint.SubOperatorIndex.Data == i) {
    88                 allowedSubFunctions[i] = new List<IFunction>();
    89                 foreach(IFunction f in subOpConstraint.AllowedSubOperators) allowedSubFunctions[i].Add(f);
    90                 break;
    91               }
    92             } else if(constraint is AllSubOperatorsTypeConstraint) {
    93               AllSubOperatorsTypeConstraint subOpConstraint = constraint as AllSubOperatorsTypeConstraint;
    94               allowedSubFunctions[i] = new List<IFunction>();
    95               foreach(IFunction f in subOpConstraint.AllowedSubOperators) allowedSubFunctions[i].Add(f);
    96               break;
    97             }
    98           }
     86          allowedSubFunctions[i] = GetAllowedSubFunctions(i);
    9987        }
    10088      }
    10189      return allowedSubFunctions[index];
     90    }
     91
     92    private List<IFunction> GetAllowedSubFunctions(int index) {
     93      List<IFunction> allowedSubFunctions = new List<IFunction>();
     94      foreach(IConstraint constraint in Constraints) {
     95        if(constraint is SubOperatorTypeConstraint) {
     96          SubOperatorTypeConstraint subOpConstraint = constraint as SubOperatorTypeConstraint;
     97          if(subOpConstraint.SubOperatorIndex.Data == index) {
     98            foreach(IFunction f in subOpConstraint.AllowedSubOperators) allowedSubFunctions.Add(f);
     99            subOpConstraint.Changed += new EventHandler(subOpConstraint_Changed); // register an event-handler to invalidate the cache on constraing changes
     100            return allowedSubFunctions;
     101          }
     102        } else if(constraint is AllSubOperatorsTypeConstraint) {
     103          AllSubOperatorsTypeConstraint subOpConstraint = constraint as AllSubOperatorsTypeConstraint;
     104          foreach(IFunction f in subOpConstraint.AllowedSubOperators) allowedSubFunctions.Add(f);
     105          subOpConstraint.Changed += new EventHandler(subOpConstraint_Changed); // register an event-handler to invalidate the cache on constraint changes
     106          return allowedSubFunctions;
     107        }
     108      }
     109      return allowedSubFunctions;
     110    }
     111
     112    private void subOpConstraint_Changed(object sender, EventArgs e) {
     113      allowedSubFunctions = null;
    102114    }
    103115
Note: See TracChangeset for help on using the changeset viewer.