-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.py
69 lines (53 loc) · 1.98 KB
/
matrix.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
import sys
import json
def createMatrix():
matrix = []
result = parseFile(matrix)
#printMatrix(result) UNCOMMENT THIS LINE IF YOU WANT 2D FORMAT OF MATRIX
json_data = json.dumps({"matrix": result[0], "preconditions": result[1], "privileges": result[2]}, indent=4)
print(json_data)
return json_data
def addNewPrerequisity(prerequisity, prerequisites, widthOfMatrix, matrix):
prerequisites.append(prerequisity) # STRINGOVY ZOZNAM
matrix.append([0 for _ in range(widthOfMatrix)])
def addNewPrivilege(privilege, privileges, matrix, indexes):
for index, row in enumerate(matrix):
if index in indexes:
row.append(1)
else:
row.append(0)
privileges.append(privilege) # STRINGOVY ZOZNAM
def parseFile(matrix):
file = open(sys.argv[1], "r")
widthOfMatrix = 0
heightOfMatrix = 0
privileges = []
prerequisites = []
for line in file:
tmpL, tmpR = line.split('==>')
tmpL = tmpL.strip()
startVertices = tmpL.split(',')
indexes = [] # na ktory riadok dat 1
for vertex in startVertices:
isTherePrerequisity = prerequisites.index(vertex) if vertex in prerequisites else None
if isTherePrerequisity != None:
indexes.append(isTherePrerequisity)
else:
indexes.append(heightOfMatrix)
heightOfMatrix += 1
addNewPrerequisity(vertex, prerequisites, widthOfMatrix, matrix)
endVertices, supp, conf = tmpR.split("#")
endVertices = endVertices.strip()
endVertices = endVertices.split(',')
for vertex in endVertices:
isTherePrivilege = privileges.index(vertex) if vertex in privileges else None
widthOfMatrix += 1
addNewPrivilege(vertex, privileges, matrix, indexes)
return (matrix, prerequisites, privileges)
def printMatrix(matrix):
print("MATRIX:")
print("--- rows: " + str(len(matrix[1])))
print("--- columns: " + str(len(matrix[2])))
for row in matrix[0]:
print(row)
createMatrix()