动态计算依赖关系

本文关键字:关系 依赖 计算 动态 | 更新日期: 2023-09-27 18:01:24

我在为我的应用程序开发算法时遇到困难。应用程序需要非常灵活,以便考虑到多种设备和多种风格。

以下是其中一个设备的规则:

Only for styles 1,2,3,8,9,10
If slope = I2t
  Range: 2-24
  Step 0.5
If slope = I4t
  Range: 1-5
  Step 0.5
If slope = Mod Inv 
  Range: 0.1-5.0
  Step 0.1
If slope = Very Inv or Ext Inv
    Range 0.2-5.0
    Step 0.1
Ony for Styles 4,5,6,7
If slope = I2t
    Range: 2-24
    Step 0.5
If slope = I4t
    Range: 1-5
    Step 0.5
If slope = IEC-A
    Range: 0.05 - 1.00
    Step 0.05
If slope = IEC-B
    Range: 0.10 - 1.00
    Step 0.05
If slope = IEC-C
    Range: 0.20-1.00
    Step 0.05

现在我可以为这个特定的设备编写代码,但是有多个设备具有不同的语句集。目前,我正在从文件中读取这些值。这允许我在不更改代码的情况下更新程序支持的设备数量,因此减少了维护。

有什么建议吗?

动态计算依赖关系

您可以使用表格方式保存输入值和相应的配置设置,然后查看字典。像这样:

struct ConfigKey
{
     public readonly int Style;
     public readonly string Slope;
     public ConfigKey(int style, string slope) 
     {
        this.Style = style;
        this.Slope= slope;
     }
     public override bool Equals(object obj)
     {
         if (!(obj is ConfigKey))
         {
             return false;
         }
         ConfigKey other = (ConfigKey)obj;
         return this.Style == other.Style && this.Slope == other.Slope;
     }
     public override int GetHashCode()
     {
         return Style.GetHashCode() ^ Slope.GetHashCode();
     }
}
struct Config
{
    String  Range {get; set;}
    Double Step { get; set; }
}
Dictionary<ConfigKey, Config> conf = new Dictionary<ConfigKey, Config>();
public Config GetConfig(int style, string slope)
{
    return conf[new ConfigKey(style, slope)];
}