-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_cld.py
193 lines (176 loc) · 6.25 KB
/
generate_cld.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
"""
generate_cld.py
Generates causal loop diagramm in HTML format based on input file
Hans Rauch
(c) MIT LICENSE
2022-09-02
Version 0.86
"""
import random
import re
import sys
causal_raw = """
R = 60
Regen (1;1;Der Regen ist die Basis allen Lebens)
Weide (3;2;Nahrungsgrundlage für viele Beutetiere)
Beute (4;4;Nahrungsgrundlage für viele Jäger)
Jäger (1;4;Spitze der Nahrungspyramide;#a00000)
Regen +> Weide (Der Regen fördert das Wachstum der Pflanzen;2)
Weide +> Beute (Eine üppige Wiese kann viele Beutetiere ernähren;2)
Beute ++> Jäger (Viele Beutetiere sind eine gute Nahrungsgrundlage für Jäger)
Jäger --> Beute (Jäger hält die Anzahl der Beutetiere im Gleichgewicht)
"""
def get_node_id(nodes: list, name: str) -> int:
"""
find the node-id for <name>
"""
for n in nodes:
if n[5] == name:
return n[0]
return -1
def generate_causal_diagram(raw: str) -> str:
"""
generate nodes and edges
"""
nodes = []
edges = []
UID = 'r' + str(random.random())[2:11]
base_color = '#e0e0ff'
R = 60
lines = raw.strip().split('\n')
for line in lines:
line = line.strip()
# read radius
n_pos = line.find('=')
if n_pos > 0 and line.startswith('R'):
line = line[n_pos+1:]
try:
R = int(line.strip())
except:
pass
line = ""
if line:
com = re.search(r'[-,+]+>+', line)
if com:
bend = 50
url = ''
info = ''
name_from = line[:com.start()].strip().strip()
name_from_id = get_node_id(nodes, name_from)
strength = len(line[com.start():com.end()].replace('>', ''))
if line[com.start()] == '+':
strength = 1 * strength
else:
strength = -1 * strength
line = line[com.end():].strip()
com = re.search(r'[\( ]', line)
if not com:
name_to = line.strip()
name_to_id = get_node_id(nodes, name_to)
else:
n_start = com.start()
name_to = line[:n_start].strip()
name_to_id = get_node_id(nodes, name_to)
line = line[n_start:].strip()
com = re.search(r'\(.*\)', line)
if com:
line = line[1:-1]
items = line.split(";")
info = items[0].strip()
try:
bend = int(items[1]) * 10
except:
pass
if len(items) > 2:
url = items[1].strip()
else: #if info:
url = 'javascript://'
edge = [name_from_id, name_to_id, bend, strength, url, info]
edges.append(edge)
else:
n_pos = line.find('(')
if n_pos > 0:
name = line[:n_pos].strip()
com = re.search(r'\(.*\)', line)
if com:
raw_params = line[com.start()+1:com.end()-1].split(';')
params = []
params.append(int(raw_params[0]))
params.append(int(raw_params[1]))
for p in raw_params[2:]:
params.append(p.strip())
if len(raw_params) < 3:
params.append('')
params.append(base_color)
params.append('')
elif len(raw_params) < 4:
params.append(base_color)
params.append('')
elif len(raw_params) < 5:
params.append(base_color)
if not params[4] or params[4] == '#':
params[4] = 'javascript://'
d = (R - 10)*2
node = [len(nodes), d*params[0], d*params[1], params[3], params[4], name, params[2]]
nodes.append(node)
# HTML template
data = f"""
<div class="row mt-3">
<div class="col-md-9">
<svg id="svg_{UID}" width="600px" height="400px" viewbox="0 0 600 400"></svg>
</div>
<div class="col-md-3">
<div id="svg_{UID}_info"></div>
</div>
</div>
<script>
var rcld_{UID} = new RoughCld("svg_{UID}")
nodes = {nodes}
edges = {edges}
rcld_{UID}.draw_cld(nodes, edges, true)
</script>
----------------------------------------------------------
<div class="row mt-3">
<div class="col-md-9">
<svg id="svg_{UID}" width="800px" height="600px"></svg>
</div>
<div class="col-md-3">
<div id="svg_{UID}_info"></div>
</div>
</div>
<div class="col-12">
<div class="btn-group" role="group" aria-label="forward / backward">
<button class="btn btn-light" onclick="rcld_{UID}.prev_edge()">
<span class="fas fa-chevron-circle-left"></span>
</button>
<button class="btn btn-light" onclick="rcld_{UID}.next_edge()">
<span class="fas fa-chevron-circle-right"></span>
</button>
</div>
</div>
<script>
var rcld_{UID} = new RoughCld("svg_{UID}")
nodes = {nodes}
edges = {edges}
rcld_{UID}.draw_cld(nodes, edges, true)
</script>
"""
return data.replace("'", '"')
# read input file and generate output file
if __name__ == "__main__":
if len(sys.argv) < 2:
print ('Usage: python generate_cld.py <filename>')
else:
filename = sys.argv[1]
n_pos = filename.rfind('.')
if n_pos > 0:
f_name = filename[:n_pos]
else:
f_name = filename
f_name += '.html'
with open(filename, 'r') as f:
causal_raw = f.read()
causal_text = generate_causal_diagram(causal_raw)
with open(f_name, 'w') as f:
f.write(causal_text)
print (f'Result: {f_name}')