2011-07-09 67 views
0

我正在編寫一個函數來解決功能管理子系統的依賴關係。請有人看看我提出的僞代碼,並告訴我是否錯過了某些東西(我可能有)。僞代碼審查 - 功能依賴性檢查

var featuresUnderAnalysis = new List<Feature>(); 

bool DependenciesAreMet(Feature feature) 
{ 
    if(featuresUnderAnalysis.Contains(feature)) //see note 1 
    { 
     throw new CircularDependencyDetectedException();  
    } 

    featuresUnderAnalysis.Add(feature); 

    if feature has no dependencies then 
    { 
     featuresUnderAnalysis.Remove(feature); 
     return true;  
    } 
    else 
    { 
     foreach dependency feature has 
     { 
      if(!DependenciesAreMet(dependency)) return false; 
     } 

     featuresUnderAnalysis.Remove(feature); 
     return true; 
    } 
} 


//note1: we maintain a list of features we have met and not 
//yet resolved the dependencies for. If we come across 
//a feature and find it in the featuresUnderAnalysis 
//list then we realise that resolution of a feature's 
//dependency graph depends on resolution of it's graph 
//and hence we cannot complete, and we throw an exception. 

回答

0

在C#中實現算法並創建單元測試表明它的行爲如預期。

/// <remarks> 
    /// NOTE 1: BA; we maintain a list of features we have met and not 
    /// yet resolved the dependencies for. If we come across 
    /// a feature and find it in the featuresUnderAnalysis 
    /// list then we realise that resolution of a feature's 
    /// dependency graph depends on resolution of it's graph 
    /// and hence we cannot complete, and we throw an exception. 
    /// </remarks> 
    public class FeatureSettingDependencyChecker 
    { 
     public static bool DependenciesAreMet(FeatureSetting featureSettingToCheck,            
               FeatureSetting[] allFeatureSettings, 
               List<FeatureSetting> featuresCurrentlyUnderAnalysis = null) 
     { 
      featuresCurrentlyUnderAnalysis = featuresCurrentlyUnderAnalysis ?? new List<FeatureSetting>(); 
      if (featuresCurrentlyUnderAnalysis.Contains(featureSettingToCheck)) //see note 1 
      { 
       throw new CircularFeatureSettingDependencyException(); 
      } 

      featuresCurrentlyUnderAnalysis.Add(featureSettingToCheck); 

      if (!featureSettingToCheck.Dependencies.Any()) 
      { 
       featuresCurrentlyUnderAnalysis.Remove(featureSettingToCheck); 

       return featureSettingToCheck.IsEnabled; 
      } 

      foreach (var dependency in featureSettingToCheck.Dependencies) 
      { 
       try 
       { 
        var dependency1 = dependency; 
        var dependencySetting = allFeatureSettings.First(s => s.Feature == dependency1); 
        if (!DependenciesAreMet(dependencySetting, allFeatureSettings, featuresCurrentlyUnderAnalysis)) 
        { 
         return false; 
        } 
       } 
       catch(InvalidOperationException e) 
       { 
        throw new FeatureDependencyConfigurationException("Ensure all features have configurations.", e); 
       } 
      } 

      featuresCurrentlyUnderAnalysis.Remove(featureSettingToCheck); 

      return featureSettingToCheck.IsEnabled; 
     } 
    }