forked from sventhijssen/compact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BDDParser.py
62 lines (52 loc) · 2.22 KB
/
BDDParser.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
import os
import subprocess
from BenchmarkGraph import BenchmarkGraph
from abc import ABC, abstractmethod
from networkx import Graph, DiGraph
from Benchmark import Benchmark
class BDDParser(ABC):
def __init__(self, benchmark: Benchmark):
self.benchmark = benchmark
self.benchmark_graph = BenchmarkGraph(benchmark.input_variables, benchmark.output_variables)
self.undirected_graph = Graph()
self.directed_graph = DiGraph()
self.reversed_graph = DiGraph()
self.bdd_construct_time = None
self.bdd_show_times = dict()
self.log = ''
def _write_dot_file(self, graph, dot_file_name):
content = ""
content += "graph sbdd {\n"
for (u, d) in graph.nodes(data=True):
if d["root"]:
variable = d["variable"]
output_variable = d["output_variables"]
content += '{}[shape="plaintext"];\n'.format(output_variable)
content += '{}[label="{}"];\n'.format(u, variable)
content += '{} -- {};\n'.format(output_variable, u)
elif d["terminal"]:
variable = d["variable"]
content += '{}[label="{}",shape="box"];\n'.format(u, variable)
else:
variable = d["variable"]
content += '{}[label="{}"];\n'.format(u, variable)
for (u, v, d) in graph.edges(data=True):
if not d["positive"]:
content += '{} -- {} [style="dotted"];\n'.format(u, v)
else:
content += '{} -- {};\n'.format(u, v)
content += '}\n'
with open(dot_file_name, 'w') as f:
f.write(content)
def draw(self):
dot_file_name = "{}.dot".format(self.benchmark.name)
png_file_name = "{}.png".format(self.benchmark.name)
self._write_dot_file(self.directed_graph, dot_file_name)
bash_cmd = 'dot -Tpng {} -o {}'.format(dot_file_name, png_file_name)
process = subprocess.Popen(['bash'], cwd=os.getcwd(), stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
process.communicate(str.encode(bash_cmd))
@abstractmethod
def parse(self) -> Graph:
pass
def get_log(self) -> str:
return self.log