2011-07-09 11 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; 
     } 
    } 
関連する問題