在列表上搜索<树状结构> [英] Search on List<Tree-like structure>

查看:52
本文介绍了在列表上搜索<树状结构>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个数据采集系统,它具有与工厂模式相似的树状结构,我很难处理这个结构,更多的代码来找到元素opredelnie。 br $> b $ b

  public   interface  ITag:IRegister 
{
string 名称{ get ; set ; }
string SystemName { get ; }
}

public interface ISignal:IRegister
{
}

public interface IRegister
{
类型GetType { get ; }
}

public interface IGroup:IRegister
{
string 名称{ get ; set ; }
string SystemName { get ; }
}

public interface IDevice:IRegister
{
string 名称{ get ; set ; }
string SystemName { get ; }
}

public interface IServer:IRegister
{
}

public interface INode:IRegister
{
string 名称{ get ; set ; }
string SystemName { get ; }
}

[可序列化]
public class 服务器:IServer
{
public string 名称;
public string SystemName;
public 列表< INode>节点;


public 服务器(字符串名称,List< INode>节点)
{
Name = name;
Nodes = node;
SystemName = Server;
}
public 类型GetType
{
get { return typeof (服务器); }
}
}

[可序列化]
public class TCP:INode
{
public string IPAddress;
public int 端口;
public 列表< IDevice>节点;
public string 名称{ get ; set ; }
public string SystemName { get ; }

public TCP( string name, string ip, int port,List< IDevice> devices)
{
Name = name;
IPAddress = ip;
Port = port;
节点=设备;
SystemName = 节点;
}

public TCP()
{
}

< span class =code-keyword> public
类型GetType
{
get { return typeof (TCP); }
}
}

[可序列化]
public class RTU:INode
{
public string 名称{ get ; set ; }
public string SystemName { get ; }
public string 端口;
public int SpeedRate;
public int DataBits;
public int StopBits;
public Parity Parity;
public 列表< IDevice>设备;

public RTU(字符串名称, int sr, int db, int sb,Parity par, string port,List< IDevice> devices)
{
Name = name;
Port = port;
SpeedRate = sr;
StopBits = sb;
DataBits = db;
Devices = devices;
Parity = par;
SystemName = 节点;
}

public 类型GetType
{
get { return typeof (RTU); }
}
}

[可序列化]
public class 设备:IDevice
{
public int 地址;
public string 名称{ get ; set ; }
public string SystemName { get ; }
public List< IGroup>组;

public 设备( int 地址, string name,List< IGroup> groups)
{
Address = address;
群组=群组;
姓名=姓名;
SystemName = Device;
}

public 类型GetType
{
get { return typeof (设备); }
}

public 类型DeviceType(List< IRegister> list)
{
return list.GetType();
}
}

[可序列化]
public class 组:IGroup
{
public List< ITag>标签;
public string 名称{ get ; set ; }
public string SystemName { get ; }

public 组(字符串名称,列表< ITag>标签)
{
Tags = tags;
姓名=姓名;
SystemName = Group;
}

public 类型GetType
{
get { return typeof (组); ; }
}

}

[可序列化]
public class 标签:ITag,IGroup
{
public ISignal Signal;
public TypeData数据;
public TypeModbus TypeModbus;
// 公共对象值;
// public DateTime Time;
public string 名称{ get ; set ; }
public string SystemName { get ; }

public 标签(ISignal信号,TypeData数据,TypeModbus typeModbus,字符串 n, object value = null
{
信号=信号;
数据=数据;
TypeModbus = typeModbus;
// 值=值;
// 时间=时间;
名称= n;
SystemName = 标签;
}


public 类型GetType
{
获取 {返回 typeof (标签); }
}

}

[可序列化]
public class 模拟:ISignal
{
public int 地址;
public int Address_validaty;
public float MinWarning;
public float MinEmergency;
public float MaxWarning;
public float MaxEmergency;
public bool 控制;
public float Coeficient;
public float Shift;
public bool IsCoeficient;
public string MinWText;
public string MinEText;
public string MaxWText;
public string MaxEText;

public 类型GetType
{
get { return typeof (模拟); }
}
}

[可序列化]
public class 离散:ISignal
{
public int 地址;
public int Address_validaty;
public bool IsAutomat;
public static ITag Tag = null ;
public string TrueText;
public string FalseText;
public 离散( int ad, int adv, bool isautomat, string ft, string tt,ITag tag = null
{
Address = ad;
Address_validaty = adv;
TrueText = tt;
FalseText = ft;
IsAutomat = isautomat;

if (isautomat)
Tag = tag;
}
public 类型GetType
{
get { return typeof (离散); }
}

}

[可序列化]
public class 管理:ISignal
{
public ITag ConnectionRegister;
public int 地址;
public int SecondsReply;


public 类型GetType
{
get { return typeof (管理); }
}
}



在这个例子中,我使用嵌套循环来查找包含整个结构的List上的Tag,并且正在搜索我必须使用结构的每个节点,它只重命名项目,并有删除,搜索重复,重叠等。



< pre lang =C#> if (e.Node.Name.Equals( 标记))
{
foreach (服务器z in List)
{
foreach (INode节点 in z.Nodes)
{
if (node.GetType == typeof ( TCP))
{
TCP _node =(TCP)节点;
foreach (设备设备 in _node.Devices)
{
IEnumerable的< IGroup> d = device.Groups.Where(p = > p.GetType == typeof (组)) ;
foreach (组 d)
{
var n = group .Tags.Where(p = > p.Name == e.Node.Text);
foreach (ITag标记 in n)
{
tag。 Name = newname;
}
}
}
}
其他 如果(node.GetType == typeof (RTU))
{
RTU _node =(RTU)node;
foreach (设备设备 in _node.Devices)
{
IEnumerable的< IGroup> d = device.Groups.Where(p = > p.GetType == typeof (组)) ;
foreach (组 d)
{
var n = group .Tags.Where(p = > p.Name == e.Node.Text);
foreach (ITag标记 in n)
{
tag。 Name = newname;
}
}
}
}
}
}
}



请告诉我如何简化搜索,使其更具可读性。

解决方案

看起来尽管顶部和底部除了节点类型之外都有相同的代码(一个是TCP,另一个RTU)您可以使用接口或基类来编码并消除一半代码吗?



示例:



INode = interface

TCPNode:INode

RTUNode:INode



INode _node = TCP或RTU节点,因为它们都支持接口。



示例:



 INode _node = node; 
foreach(_node.Devices中的设备设备)
{
IEnumerable< IGroup> d = device.Groups.Where(p => p.GetType == typeof(Group));
foreach(d组中的组)
{
var n = group.Tags.Where(p => p.Name == e.Node.Text);
foreach(n中的ITag标签)
{
tag.Name = newname;
}
}
}


I create a data collection system that has a tree-like structure built on the similarity to the pattern of the factory, and I have the difficulty in working with this structure, more stingrays lot of code to find the element opredelnie.

    public interface ITag : IRegister
{
    string Name { get; set; }
    string SystemName { get; }
}

public interface ISignal : IRegister
{
}

public interface IRegister
{
    Type GetType { get; }
}

public interface IGroup : IRegister
{
    string Name { get; set; }
    string SystemName { get; }
}

public interface IDevice : IRegister
{
    string Name { get; set; }
    string SystemName { get; }
}

public interface IServer : IRegister
{
}

public interface INode : IRegister
{
    string Name { get; set; }
    string SystemName { get; }
}

[Serializable]
public class Server : IServer
{
    public string Name;
    public string SystemName;
    public List<INode> Nodes;


    public Server(string name, List<INode> node)
    {
        Name = name;
        Nodes = node;
        SystemName = "Server";
    }
    public Type GetType
    {
        get { return typeof(Server); }
    }
}

[Serializable]
public class TCP : INode
{
    public string IPAddress;
    public int Port;
    public List<IDevice> Nodes;
    public string Name { get; set; }
    public string SystemName { get; }

    public TCP(string name, string ip, int port, List<IDevice> devices)
    {
        Name = name;
        IPAddress = ip;
        Port = port;
        Nodes = devices;
        SystemName = "Node";
    }

    public TCP()
    {
    }

    public Type GetType
    {
        get { return typeof(TCP); }
    }
}

[Serializable]
public class RTU : INode
{
    public string Name { get; set; }
    public string SystemName { get; }
    public string Port;
    public int SpeedRate;
    public int DataBits;
    public int StopBits;
    public Parity Parity;
    public List<IDevice> Devices;

    public RTU(string name, int sr, int db, int sb, Parity par, string port, List<IDevice> devices)
    {
        Name = name;
        Port = port;
        SpeedRate = sr;
        StopBits = sb;
        DataBits = db;
        Devices = devices;
        Parity = par;
        SystemName = "Node";
    }

    public Type GetType
    {
        get { return typeof(RTU); }
    }
}

[Serializable]
public class Device : IDevice
{
    public int Address;
    public string Name { get; set; }
    public string SystemName { get; }
    public List<IGroup> Groups;

    public Device(int address, string name, List<IGroup> groups)
    {
        Address = address;
        Groups = groups;
        Name = name;
        SystemName = "Device";
    }

    public Type GetType
    {
        get { return typeof(Device); }
    }

    public Type DeviceType(List<IRegister> list )
    {
        return list.GetType();
    }
}

[Serializable]
public class Group : IGroup
{
    public List<ITag> Tags;
    public string Name { get; set; }
    public string SystemName { get; }

    public Group(string name, List<ITag> tags)
    {
        Tags = tags;
        Name = name;
        SystemName = "Group";
    }

    public Type GetType
    {
        get { return typeof(Group); ; }
    }

}

[Serializable]
public class Tag : ITag, IGroup
{
    public ISignal Signal;
    public TypeData Data;
    public TypeModbus TypeModbus;
    //public object Value;
    //public DateTime Time;
    public string Name { get; set; }
    public string SystemName { get; }

    public Tag(ISignal signal, TypeData data, TypeModbus typeModbus,string n, object value = null)
    {
        Signal = signal;
        Data = data;
        TypeModbus = typeModbus;
        //Value = value;
        //Time = time;
        Name = n;
        SystemName = "Tag";
    }


    public Type GetType
    {
        get { return typeof(Tag); }
    }

}

[Serializable]
public class Analog : ISignal
{
    public int Address;
    public int Address_validaty;
    public float MinWarning;
    public float MinEmergency;
    public float MaxWarning;
    public float MaxEmergency;
    public bool Control;
    public float Coeficient;
    public float Shift;
    public bool IsCoeficient;
    public string MinWText;
    public string MinEText;
    public string MaxWText;
    public string MaxEText;

    public Type GetType
    {
        get { return typeof(Analog); }
    }
}

[Serializable]
public class Discrete : ISignal
{
    public int Address;
    public int Address_validaty;
    public bool IsAutomat;
    public static ITag Tag = null;
    public string TrueText;
    public string FalseText;
    public Discrete(int ad, int adv,  bool isautomat, string ft, string tt, ITag tag = null)
    {
        Address = ad;
        Address_validaty = adv;
        TrueText = tt;
        FalseText = ft;
        IsAutomat = isautomat;

        if (isautomat)
            Tag = tag;
    }
    public Type GetType
    {
        get { return typeof(Discrete); }
    }

}

[Serializable]
public class Managment : ISignal
{
    public ITag ConnectionRegister;
    public int Address;
    public int SecondsReply;


    public Type GetType
    {
        get { return typeof(Managment); }
    }
}


In this example I'm using nested loops to find Tag on List which contains the entire structure, and are searching for me to have to use for each node of the structure, and it only renames the item, and there is deletion, search duplicate, overlap, and so on.

    if(e.Node.Name.Equals("Tag"))
{
    foreach(Server z in List)
    {
        foreach(INode node in z.Nodes)
        {
            if(node.GetType == typeof(TCP))
            {
                TCP _node = (TCP) node;
                foreach(Device device in _node.Devices)
                {
                    IEnumerable<IGroup> d = device.Groups.Where(p => p.GetType == typeof(Group));
                    foreach(Group group in d)
                    {
                        var n = group.Tags.Where(p => p.Name == e.Node.Text);
                        foreach(ITag tag in n)
                        {
                            tag.Name = newname;
                        }
                    }
                }
            }
            else if(node.GetType == typeof(RTU))
            {
                RTU _node = (RTU) node;
                foreach(Device device in _node.Devices)
                {
                    IEnumerable<IGroup> d = device.Groups.Where(p => p.GetType == typeof(Group));
                    foreach(Group group in d)
                    {
                        var n = group.Tags.Where(p => p.Name == e.Node.Text);
                        foreach(ITag tag in n)
                        {
                            tag.Name = newname;
                        }
                    }
                }
            }
        }
    }
}


Please tell me how to simplify the search, make it more readable.

解决方案

It looks as as though the top and bottom have identical code except for the node type (one being TCP, the other RTU) could you use an interface or base class to code against and eliminate half your code?

Example:

INode = interface
TCPNode : INode
RTUNode : INode

INode _node = Either TCP or RTU node because both support interface.

Example:

INode _node = node;
     foreach(Device device in _node.Devices)
     {
         IEnumerable<IGroup> d = device.Groups.Where(p => p.GetType == typeof(Group));
         foreach(Group group in d)
         {
             var n = group.Tags.Where(p => p.Name == e.Node.Text);
             foreach(ITag tag in n)
             {
                 tag.Name = newname;
             }
         }
     }


这篇关于在列表上搜索&lt;树状结构&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆