-
Notifications
You must be signed in to change notification settings - Fork 0
/
Requirements.py
289 lines (271 loc) · 10.1 KB
/
Requirements.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import os
import re
class node():
"""docstring for node"""
def __init__(self, Id, Content):
self.Id = Id
self.Content = Content
self.Pointing = set()
self.Pointed = set()
self.Childs = set()
self.Parents = set()
def addPointing(self, pointingId):
self.Pointing.add(pointingId)
def addPointed(self, pointedId):
self.Pointed.add(pointedId)
def removePointed(self, pointedId):
if pointedId in self.Pointed:
self.Pointed.remove(pointedId)
def removePointing(self, pointingId):
if pointingId in self.Pointing:
self.Pointing.remove(pointingId)
def addParent(self, ParentsId):
self.Parents.add(ParentsId)
def removeParent(self, ParentsId):
if ParentsId in self.Parents:
self.Parents.remove(ParentsId)
def addChild(self, ChildsId):
self.Childs.add(ChildsId)
def removeChild(self, ChildsId):
if ChildsId in self.Childs:
self.Childs.remove(ChildsId)
def getlabel(self):
labelStr=""
if "label" in self.Content.keys():
labelStr=self.Content["label"]
else:
labelStr = self.Id
return labelStr
def getDir(self):
IdStr=self.Id
for item in self.Parents:
IdStr=str(item)+":"+IdStr
break
return IdStr
def writeNode(self,container=None):
nodeStr= ""
# only write if is a parent
if len(self.Parents)== 0:
nodeStr= self.Id+" ["
for key in self.Content.keys():
nodeStr+= str(key)+'="'+str(self.Content[key])
# add childs as in UML
if key == "label":
for child in self.Childs:
nodeStr+= "|<" + container.getNode(child).Id + ">" + container.getNode(child).getlabel()
# finish label
nodeStr+= '" '
if len(self.Childs) > 0:
nodeStr+="shape=record "
nodeStr+="];"
return nodeStr
# this class is in charge of handling the nodes relationships in order to be consistence
class nodesContainer():
"""docstring for nodesContainer"""
def __init__(self):
self.Nodes = {}
def addNode(self,Id,Content):
# if node already exists override Content but leave relations
Id = str(Id)
if Id in self.Nodes:
self.Nodes[Id].Content = Content
else:
self.Nodes[Id] = node(Id,Content)
def removeNode(self, Id):
Id = str(Id)
if Id in self.Nodes:
for node in self.Nodes[Id].Pointing :
self.removeRelation(Id, node)
for node in self.Nodes[Id].Pointed :
self.removeRelation(node, Id)
del self.Nodes[Id]
def addRelation(self,pointingID,pointedId):
# if not present add empty node
pointingID = str(pointingID)
pointedId = str(pointedId )
if pointingID not in self.Nodes:
self.addNode(pointingID,{"label":pointingID})
if pointedId not in self.Nodes:
self.addNode(pointedId,{"label":pointedId})
self.Nodes[pointingID].addPointing(pointedId)
self.Nodes[pointedId].addPointed(pointingID)
def removeRelation(self,pointingID,pointedId):
pointingID = str(pointingID)
pointedId = str(pointedId )
if pointingID in self.Nodes:
if pointedId in self.Nodes:
self.Nodes[pointingID].removePointing(pointedId)
self.Nodes[pointedId].removePointed(pointingID)
def getNode(self,Id):
return self.Nodes[str(Id)]
def getNodesArray(self):
return sorted(self.Nodes.keys())
def addParentship(self,ChildId,ParentId):
# if not present add empty node
ChildId = str(ChildId)
ParentId = str(ParentId )
if ChildId not in self.Nodes:
self.addNode(ChildId,{"label":ChildId})
if ParentId not in self.Nodes:
self.addNode(ParentId,{"label":ParentId})
self.Nodes[ChildId].addParent(ParentId)
self.Nodes[ParentId].addChild(ChildId)
def removeParentship(self,ChildId,ParentId):
ChildId = str(ChildId)
ParentId = str(ParentId )
if ChildId in self.Nodes:
if ParentId in self.Nodes:
self.Nodes[ChildId].removeParent(ParentId)
self.Nodes[ParentId].removeChild(ChildId)
def getSetOfNodes(self,regexp):
regular = re.compile(regexp)
nodes = self.getNodesArray()
def filterFunction (x) : return regular.search(x)
return filter(filterFunction, nodes)
class fileParser(object):
"""This object is in charge of parsing a file ,
is to be inherit to customize the line parsinfg methods"""
def __init__(self, filename):
self.file = filename
self.loaded = False
def loadFile(self):
f = open(self.file)
g = f.read()
self.data = g.split("\n")
self.loaded = True
def parseFile(self):
if not self.loaded:
self.loadFile()
for line in self.data:
if self.lineParseCriteria(line):
self.parseLine(line)
def lineParseCriteria(self,line):
return False
def parseLine(self, line):
pass
def __str__(self):
return "This is the parsing of %s \n Loaded status %l" % (self.file, self.loaded)
# This class reads the files with nodes relations and loads them in the desired container
class relationsParser(fileParser):
"""docstring for relationsParser"""
def __init__(self, filename):
fileParser.__init__(self,filename)
self.relations=[]
self.parentships =[]
def lineParseCriteria(self,line):
return not (line.strip() == '' or line.strip()[0] =='#')
def parseLine(self, line):
position = line.find("->")
if position >= 0:
fromId = line[0:position].strip()
toId = line[position+2:].strip()
# check parent - child
position = toId.find(":")
if position >= 0:
parentID = toId[0:position].strip()
toId = toId[position+1:].strip()
self.parentships.append([toId.strip(), parentID.strip()])
self.relations.append([fromId.strip(), toId.strip()])
def loadNodeContainer(self, nodeContainer):
for relation in self.relations:
nodeContainer.addRelation(relation[0],relation[1])
for parentship in self.parentships:
nodeContainer.addParentship(parentship[0],parentship[1])
class nodesParser(fileParser):
"""docstring for nodesParser"""
def __init__(self, filename):
fileParser.__init__(self,filename)
self.Nodes={}
self.Relations=[]
def lineParseCriteria(self,line):
return (len(line.strip())>1 and line.strip()[0] =='#')
def parseLine(self, line):
line = line.strip()
position = line.find(":")
if position >= 0:
Id = line[1:position].strip()
Description = line[position+2:].strip()
self.Nodes[Id]= Description
def loadNodeContainer(self, nodeContainer):
for node in self.Nodes.keys():
nodeContainer.addNode(node,{"label":self.Nodes[node]})
class dotGenerator():
def __init__(self, container, fileName):
self.container = container
self.fileName = fileName
self.name = ""
def loadFile(self):
f = open(self.fileName,"w")
f.write("""
digraph g {
graph [
ranksep = 4.0;
rankdir = "LR"
];
node [
fontsize = "16"
shape = "box"
];
edge [
];
\n
""")
for node in self.container.Nodes.keys():
NodeObj = self.container.getNode(node)
f.write(NodeObj.writeNode(self.container) +"\n")
idString =NodeObj.getDir()
# for reverse change to for pointed in NodeObj.Pointed:
for pointed in NodeObj.Pointed:
toObj = self.container.getNode(pointed)
f.write(idString+" -> "+ toObj.getDir() +"; \n" )
#f.write(self.Groupnodes(['SR','UR']))
f.write("""
}
""")
f.close()
os.system("dot "+self.fileName+" -Tpng -o outfile.png")
def Groupnodes(self,regulars):
string =""
for regular in regulars:
group = self.container.getSetOfNodes(regular)
if group:
string += "{ \n rank=same; \n "
dummy = reduce(joinNodes,group)
string += dummy + ' [style="invis"] ; \n };\n'
return string
class tableGenerator():
def __init__(self, container, fileName):
self.container = container
self.fileName = fileName
self.name = ""
def loadFile(self):
f = open(self.fileName,"w")
f.write("""
<table>
<tr>
<th>User Requirements</th>
<th>Software Requirements</th>
</tr>
""")
for node in self.container.getSetOfNodes("UR"):
f.write(self.writeElements(node))
f.write("""
</table>
""")
f.close()
def writeElements(self,node):
string =" <tr>\n"
NodeObj = self.container.getNode(node)
string += " <td>"+ NodeObj.getDir() +"</td>\n <td>"
for element in NodeObj.Pointing:
string += self.container.getNode(element).getDir()+ "<br>"
string +="</td>\n </tr>\n"
return string
def joinNodes(x,y):
return x+" -> "+y
class HTMLcontainerParser():
"""docstring for HTMLcontainerParser"""
def __init__(self, container, filename):
self.html = ""
self.container = container
self.filename = filename