| 1 | # Copyright (C) 2007 Adriano Monteiro Marques <py.adriano@gmail.com> |
|---|
| 2 | # |
|---|
| 3 | # Author: Joao Paulo de Souza Medeiros <ignotus21@gmail.com> |
|---|
| 4 | # |
|---|
| 5 | # This program is free software; you can redistribute it and/or modify |
|---|
| 6 | # it under the terms of the GNU General Public License as published by |
|---|
| 7 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | # (at your option) any later version. |
|---|
| 9 | # |
|---|
| 10 | # This program is distributed in the hope that it will be useful, |
|---|
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | # GNU General Public License for more details. |
|---|
| 14 | # |
|---|
| 15 | # You should have received a copy of the GNU General Public License |
|---|
| 16 | # along with this program; if not, write to the Free Software |
|---|
| 17 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | class Node(object): |
|---|
| 21 | """ |
|---|
| 22 | Node class |
|---|
| 23 | """ |
|---|
| 24 | |
|---|
| 25 | def __init__(self, id=None): |
|---|
| 26 | """ |
|---|
| 27 | Constructor method of Node class |
|---|
| 28 | @type : integer |
|---|
| 29 | @param : Node identificator |
|---|
| 30 | """ |
|---|
| 31 | self.__id = id |
|---|
| 32 | """Node identificator""" |
|---|
| 33 | self.__information = None |
|---|
| 34 | """Hash with general information""" |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | def getID(self): |
|---|
| 38 | """ |
|---|
| 39 | Get node ID |
|---|
| 40 | @rtype: number |
|---|
| 41 | @return: Node identificator |
|---|
| 42 | """ |
|---|
| 43 | return self.__id |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | def setID(self, id): |
|---|
| 47 | """ |
|---|
| 48 | Set node ID |
|---|
| 49 | @type : number |
|---|
| 50 | @param : Node identificator |
|---|
| 51 | """ |
|---|
| 52 | self.__id = id |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | def getInformation(self, info): |
|---|
| 56 | """ |
|---|
| 57 | Get general information about node |
|---|
| 58 | @type : string |
|---|
| 59 | @param : Information name |
|---|
| 60 | @rtype: mixed |
|---|
| 61 | @return: The requested information |
|---|
| 62 | """ |
|---|
| 63 | return self.__information[ info ] |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | def setInformation(self, info): |
|---|
| 67 | """ |
|---|
| 68 | Set general information |
|---|
| 69 | @type : dict |
|---|
| 70 | @param : General information dictionary |
|---|
| 71 | """ |
|---|
| 72 | self.__information = info |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | class Graph: |
|---|
| 77 | """ |
|---|
| 78 | Network Graph class |
|---|
| 79 | """ |
|---|
| 80 | |
|---|
| 81 | def __init__(self, nodes=None): |
|---|
| 82 | """ |
|---|
| 83 | Constructor method of Graph class |
|---|
| 84 | @type : list |
|---|
| 85 | @param : List of nodes |
|---|
| 86 | """ |
|---|
| 87 | self.__mainNodeID = 0 |
|---|
| 88 | """""" |
|---|
| 89 | self.__numberOfNodes = 0 |
|---|
| 90 | """""" |
|---|
| 91 | self.__listOfNodes = [] |
|---|
| 92 | """""" |
|---|
| 93 | self.__connections = set() |
|---|
| 94 | """List with id's describing connections""" |
|---|
| 95 | |
|---|
| 96 | if nodes != None: |
|---|
| 97 | self.__numberOfNodes = len(nodes) |
|---|
| 98 | self.__createHashNode(nodes) |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | def __createHashNode(self, nodes): |
|---|
| 102 | """ |
|---|
| 103 | Create hash of nodes by node ID |
|---|
| 104 | """ |
|---|
| 105 | self.__listOfNodes = range(self.__numberOfNodes) |
|---|
| 106 | |
|---|
| 107 | for i in range(self.__numberOfNodes): |
|---|
| 108 | self.__listOfNodes[nodes[i].getID()] = nodes[i] |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | def getNumberOfNodes(self): |
|---|
| 112 | """ |
|---|
| 113 | Get the number of nodes in graph |
|---|
| 114 | @rtype: number |
|---|
| 115 | @return: The number of nodes in the graph |
|---|
| 116 | """ |
|---|
| 117 | return self.__numberOfNodes |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | def setMainNodeByID(self, id): |
|---|
| 121 | """ |
|---|
| 122 | Set the main node by ID |
|---|
| 123 | @type : number |
|---|
| 124 | @param : The node ID |
|---|
| 125 | """ |
|---|
| 126 | self.__mainNodeID = id |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | def getNodeByID(self, id): |
|---|
| 130 | """ |
|---|
| 131 | Get one node of graph by your ID |
|---|
| 132 | @type : number |
|---|
| 133 | @param : The node ID |
|---|
| 134 | @rtype: Node |
|---|
| 135 | @return: The node |
|---|
| 136 | """ |
|---|
| 137 | return self.__listOfNodes[id] |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | def getMainNode(self): |
|---|
| 141 | """ |
|---|
| 142 | Get the main node |
|---|
| 143 | @rtype: Node |
|---|
| 144 | @return: The main node |
|---|
| 145 | """ |
|---|
| 146 | return self.__listOfNodes[self.__mainNodeID] |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | def getMainNodeID(self): |
|---|
| 150 | """ |
|---|
| 151 | Get the main node ID |
|---|
| 152 | @rtype: number |
|---|
| 153 | @return: The main node ID |
|---|
| 154 | """ |
|---|
| 155 | return self.__mainNodeID |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | def setConnections(self, nodeIDA, nodeIDB): |
|---|
| 159 | """ |
|---|
| 160 | Set node connections |
|---|
| 161 | @type : list |
|---|
| 162 | @param : List of connections |
|---|
| 163 | """ |
|---|
| 164 | connection = (nodeIDA, nodeIDB) |
|---|
| 165 | self.__connections.add(connection) |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | def getConnections(self): |
|---|
| 169 | """ |
|---|
| 170 | """ |
|---|
| 171 | return self.__connections |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | def getNodeConnections(self, node, exceptNodes=[]): |
|---|
| 175 | """ |
|---|
| 176 | """ |
|---|
| 177 | nodeID = node.getID() |
|---|
| 178 | connections = [] |
|---|
| 179 | |
|---|
| 180 | for (a, b) in self.__connections: |
|---|
| 181 | if a == nodeID and b not in exceptNodes: |
|---|
| 182 | connections.append(b) |
|---|
| 183 | if b == nodeID and a not in exceptNodes: |
|---|
| 184 | connections.append(a) |
|---|
| 185 | |
|---|
| 186 | return connections |
|---|
| 187 | |
|---|