-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboolparser.py
172 lines (142 loc) · 3.33 KB
/
boolparser.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
# Compiler for constructing a causal logic network from boolean rules
import ply.yacc as yacc
import networkx as nx
# Get the token map from the lexer.
from tokenizer import tokens
def new_node(node, network):
#print 'Adding node', node, type(node)
network.add_node(node)
network.node[node]['label']=node
return network
class Node():
def __init__(self, name):
self.name = name
def __repr__(self):
return "Node %s, color %s, tail %s" % (self.name, self.Color, self.Tail)
#setting default color and edge style
Color = 'black'
Tail = 'normal'
#construct an edge from Node to a given target node
def draw_edge(self,target,G):
G.add_edge(self.name,target)
G[self.name][target]['color'] = self.Color
G[self.name][target]['arrowhead'] = self.Tail
return G
#G = nx.DiGraph()
#invert the color of an edge if it is inhibitory
def colorswap(e,G):
if G[e[0]][e[1]]['color'] == 'red':
G[e[0]][e[1]]['color'] = 'blue'
elif G[e[0]][e[1]]['color'] == 'blue':
G[e[0]][e[1]]['color'] = 'red'
return G
def p_rule(p):
'rule : ID ASSIGN expression'
new_node(p[1], G)
p[0] = p[3]
if isinstance(p[3],Node):
p[3].draw_edge(p[1],G)
else:
for i in list(p[3]):
i.draw_edge(p[1],G)
return p
def p_expression_and(p):
'expression : expression AND term'
flag = 0
if isinstance(p[1],Node):
p[1].Color = 'blue'
else:
flag = 1
p[1].append(p[3])
if isinstance(p[3],Node):
p[3].Color = 'blue'
if flag:
p[0] = p[1]
else:
p[0] = [p[1],p[3]]
return p
def p_expression_or(p):
'expression : expression OR term'
flag = 0
if isinstance(p[1],Node):
p[1].Color = 'red'
else:
flag = 1
p[1].append(p[3])
if isinstance(p[3],Node):
p[3].Color = 'red'
if flag:
p[0] = p[1]
else:
p[0] = [p[1],p[3]]
return p
def p_expression_term(p):
'expression : term'
p[0] = p[1]
return p
def p_term_not(p):
'term : NOT term'
if isinstance(p[2],Node):
p[2].Tail = 'tee'
p[0] = p[2]
return p
def p_term(p):
'term : ID'
p[0] = Node(p[1])
new_node(p[1], G)
#print G.nodes()
#print 'p[1] is', p[1], type(p[1])
return p
def p_term_expr(p):
'term : LPAREN expression RPAREN'
fakenode =''
for i in p[2]:
fakenode +=(i.name+'-')
new_node(fakenode, G)
for i in p[2]:
i.draw_edge(fakenode,G)
p[0] = Node(fakenode)
return p
# Error rule for syntax errors
def p_error(p):
print "Syntax error in input!", p
# Build the parser
parser = yacc.yacc()
def readfile(filename):
f = open(filename, 'r')
text = f.read()
text_iter = iter(text.splitlines())
#run the parser for each line in the file
global G
G = nx.DiGraph()
for s in text_iter:
edges = {}
parser.parse(s)
#invert the color of inhibitory edges
for e in G.edges_iter(data=True):
if G[e[0]][e[1]]['arrowhead'] == 'tee':
colorswap(e,G)
return G
def readstr(text):
#print 'Into boolparser function, the rules are:', text
text_iter = iter(text.splitlines())
#run the parser for each line in the file
global G
G = nx.DiGraph()
for s in text_iter:
edges = {}
parser.parse(s)
#parser.restart()
#invert the color of inhibitory edges
for e in G.edges_iter(data=True):
if G[e[0]][e[1]]['arrowhead'] == 'tee':
colorswap(e,G)
return G
'''
ifile = '/home/parul/Dropbox/codes/rules/forply/main.txt'
G = readfile(ifile)
#Write to a graph
gfile = '/home/parul/Dropbox/codes/EMT_complt/byply/main.graphml'
#nx.write_graphml(G,gfile)
print 'Graph created at', gfile
'''