-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreduction.py
75 lines (59 loc) · 2.3 KB
/
reduction.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
'''
Author : Harika Rajavaram
Email : [email protected]
Subject : reducing nodes
Description: Reduction of nodes to minimized number of nodes by updating the dictionary loaded from
the json file and giving new output as json.
'''
import json
graphFile = open('../graph2.json','r')
# loading the file in json format and reading it
readFile = graphFile.read()
file_string = json.loads(readFile)
# newNodes = ["A","B",["C","E"],["D","F"],"G"]
'''
combining all nodes in a block and calling update_json function
'''
def combine(stablility,newNodes):
for node in newNodes:
if(len(node) == 1): # if an array index contains only one element not the list
continue
else:
updated_node = ','.join(map(str, node)) # joining all nodes in a block
equinode = node[0] # calling function update_json with first element of an equivalent block
# print updated_node,equinode
update_nodeName(updated_node,equinode,stablility) #update node to updated_node
'''
updating the input json with removing the initial nodes with the equivalent blocks
'''
def update_nodeName(updated_node,node,stablility):
stable_nodes = file_string[stablility][0].keys()
count = 0
for key in stable_nodes:
if (key in updated_node and count==1): # updating the node with updated_node
del file_string[stablility][0][key]
elif (key in updated_node and count==0): # remove the node if its equivalent node was updated
file_string[stablility][0][updated_node] = file_string[stablility][0][node]
del file_string[stablility][0][node]
count = 1
# print file_string["stable"]
'''
updating the nodes outputs to the updated nodes
'''
def update_toNodes(stablility,stablility_keys,nodeNames):
for key in stablility_keys:
for idx in range(0,2): # as we are working for a DFA for hard coded outputs will be 2
to_val = file_string[stablility][0][key][idx]["to"]
for sub in nodeNames:
if(to_val in sub):
file_string[stablility][0][key][idx]["to"] = sub
#if __name__ == "__main__":
def main(newNodes):
combine("stable",newNodes)
combine("unstable",newNodes)
stable_keys = file_string["stable"][0].keys()
unstable_keys = file_string["unstable"][0].keys()
nodeNames = stable_keys + unstable_keys
update_toNodes("stable",stable_keys,nodeNames)
update_toNodes("unstable",unstable_keys,nodeNames)
return file_string