Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

C#
    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.

C#
    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.
Posted
Comments
John C Rayan 30-Sep-15 8:07am    
Just a thought ...Think about making the tree structure as XML and you could use XPath to search the entire structure.
Nathan Minier 30-Sep-15 13:45pm    
I don't think there's anything wrong with it. Add comments if you're concerned about keeping track of the nests.
Maciej Los 30-Sep-15 16:36pm    
What you mean by "simplify"? Is there any issue?
Maciej Los 31-Oct-15 18:58pm    
You have to implement IEqualityComparer to be able "simplify" the search.

1 solution

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;
             }
         }
     }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900