-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
130 lines (109 loc) · 4.27 KB
/
test.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
# -*- coding: utf-8 -*-
# This file is part of sf2dve.
#
# sf2dve is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 2.1 of the License, or
# (at your option) any later version.
#
# sf2dve is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with sf2dve. If not, see <http://www.gnu.org/licenses/>.
"""
Created on Sat Dec 27 14:10:02 2014
@author: pavla
"""
import sys, subprocess, re
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("model", help="DVE file extended with process " +\
"feed_inputs", type=argparse.FileType('r'))
parser.add_argument("input", help="file with input sequence for each " +\
"input variable. Each variable is on new line and " +\
"in format: variable_name = [n1 n2 n3 ...]",
type=argparse.FileType('r'))
parser.add_argument("output", help="output file for storing sequence " +\
"of values of given variable",
type=argparse.FileType('w'))
parser.add_argument("variable", help="name of the output variable",
type=str)
args = parser.parse_args()
model = ''.join(args.model.readlines())
byteMin = 0
byteMax = 1
intMin = 0
intMax = 7
byteSize = byteMax - byteMin + 1
intSize = intMax - intMin + 1
byteVars = []
intVars = []
for line in args.input:
varName = re.search(r"(.+?)(\[|=)", line).group(1).strip()
varInputs = re.search(r"\[(.+)\]", line).group(1).strip().split()
varType = re.search(r"(.*)%s" % varName, model).group(1).strip()
if varType == "byte":
byteVars.append((varName, varInputs))
elif varType == "int":
intVars.append((varName, varInputs))
else:
print("Wrong variable type in the model: %s" % varType)
if byteVars != []:
maxInput = len(byteVars[0][1])
else:
maxInput = len(intVars[0][1])
byteVars = sorted(byteVars, key=lambda x:model.find(x[0]))
intVars = sorted(intVars, key=lambda x:model.find(x[0]))
args.model.seek(0, 0)
inputTrace = "--trace=1,"
maxVarCom = intSize**len(intVars) * byteSize**len(byteVars)
firstCatched = False
for i in range(0, maxInput):
for j in range(0, maxVarCom):
l = j
match = True
for varName, varInputs in byteVars:
if varInputs[i] != str(int(l % byteSize + byteMin)):
match = False
l = (l - l % byteSize) / byteSize
for varName, varInputs in intVars:
if varInputs[i] != str(int(l % intSize + intMin)):
match = False
l = (l - l % intSize) / intSize
if match:
if firstCatched:
inputTrace += str(j + 1) + ","
else:
firstCatched = True
break
inputTrace += str(maxVarCom + 1) + ","
inputTrace = inputTrace[:-1]
p = subprocess.Popen(["divine", "simulate", "--no-reduce", inputTrace, args.model.name],
stdout=subprocess.PIPE, stderr=open("/dev/null", "w"))
blocks = []
block = []
second = False
for line in p.stdout.readlines():
line = line.decode()
block.append(line)
if line == "\n":
if second:
second = False
else:
blocks.append(block)
second = True
block = []
blocks = blocks[1:]
output_sequence = ""
for block in blocks:
for line in block:
if line.startswith("process_"):
number = re.search(args.variable + " = ([^,]+)", line).group(1)
output_sequence += number + "\n"
args.output.write(output_sequence)
if __name__ == "__main__":
sys.exit(main())