summaryrefslogtreecommitdiffstats
path: root/verigraph/service/src/tests/j-verigraph-generator/test_generator.py
blob: f4629bb19bbd7d820e3f2e7a0394e4cbc6c56884 (plain)
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
#!/usr/bin/python

##############################################################################
# Copyright (c) 2017 Politecnico di Torino and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

from pprint import pprint
from code_generator import CodeGeneratorBackend
import sys, getopt
import contextlib
import os
from utility import *
import logging

def main(argv):
    if len(argv) < 8:
        print 'test_generator.py -i <inputfile> -o <outputfile> -s <source> -d <destination>'
        sys.exit(2)
    #initialize command line arguments values
    inputfile = ''
    outputfile = ''
    source = ''
    destination = ''
    #parse command line arguments and exit if there is an error
    try:
        opts, args = getopt.getopt(argv,"hi:o:s:d:",["ifile=","ofile=","source=","destination="])
    except getopt.GetoptError:
        print 'test_generator.py -i <inputfile> -o <outputfile> -s <source> -d <destination>'
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print 'test_generator.py -i <inputfile> -o <outputfile> -s <source> -d <destination>'
            sys.exit()
        elif opt in ("-i", "--ifile"):
            inputfile = arg
        elif opt in ("-o", "--ofile"):
            outputfile = arg
        elif opt in ("-s", "--source"):
            source = arg
        elif opt in ("-d", "--destination"):
            destination = arg
    #set logging
    logging.basicConfig(stream=sys.stderr, level=logging.INFO)
    #capitalize ouput filename
    dirname = os.path.dirname(outputfile)
    basename = os.path.basename(outputfile)
    basename = os.path.splitext(basename)[0]
    basename = basename[0].upper() + basename[1:]

    #print arguments
    logging.debug('Input file is', inputfile)
    logging.debug('Output file is', dirname + "/" + basename)
    logging.debug('Source node is', source)
    logging.debug('Destination node is', destination)

    #begin file generation
    with smart_open(dirname + "/" + basename + ".java") as f:
        c = CodeGeneratorBackend()
        c.begin(tab="    ")
        c.writeln("package tests;")
        c.writeln("import java.util.Calendar;")
        c.writeln("import java.util.Date;")
        c.writeln("import java.util.HashMap;")

        c.writeln("import com.microsoft.z3.Context;")
        c.writeln("import com.microsoft.z3.FuncDecl;")
        c.writeln("import com.microsoft.z3.Model;")
        c.writeln("import com.microsoft.z3.Status;")
        c.writeln("import com.microsoft.z3.Z3Exception;")
        c.writeln("import mcnet.components.IsolationResult;")


        inputfile = os.path.basename(inputfile)
        c.writeln("import tests.scenarios." + os.path.splitext(inputfile)[0] + ";")
        c.writeln("public class " + basename + "{")

        c.indent()
        c.writeln("Context ctx;")

        c.write("public void resetZ3() throws Z3Exception{\n\
        HashMap<String, String> cfg = new HashMap<String, String>();\n\
        cfg.put(\"model\", \"true\");\n\
        ctx = new Context(cfg);\n\
        \r\t}\n")

        c.write("public void printVector (Object[] array){\n\
        int i=0;\n\
        System.out.println( \"*** Printing vector ***\");\n\
        for (Object a : array){\n\
            i+=1;\n\
            System.out.println( \"#\"+i);\n\
            System.out.println(a);\n\
            System.out.println(  \"*** \"+ i+ \" elements printed! ***\");\n\
        }\n\
        \r\t}\n")

        c.write("public void printModel (Model model) throws Z3Exception{\n\
        for (FuncDecl d : model.getFuncDecls()){\n\
            System.out.println(d.getName() +\" = \"+ d.toString());\n\
              System.out.println(\"\");\n\
        }\n\
        \r\t}\n")

        c.writeln("public int run() throws Z3Exception{")
        c.indent()

        c.writeln(basename + " p = new " + basename + "();")

        #adding time estimation
        #c.writeln("int k = 0;")
        #c.writeln("long t = 0;")

        #c.writeln("for(;k<1;k++){")
        #c.indent()

        c.writeln("p.resetZ3();")

        c.write(os.path.splitext(inputfile)[0] + " model = new " + os.path.splitext(inputfile)[0] + "(p.ctx);\n")

        #c.writeln("Calendar cal = Calendar.getInstance();")
        #c.writeln("Date start_time = cal.getTime();")

        c.write("IsolationResult ret =model.check.checkIsolationProperty(model.")
        c.append(source + ", model." + destination + ");\n")
        #c.writeln("Calendar cal2 = Calendar.getInstance();")
        #c.writeln("t = t+(cal2.getTime().getTime() - start_time.getTime());")

        c.writeln("if (ret.result == Status.UNSATISFIABLE){\n\
            System.out.println(\"UNSAT\");\n\
            return -1;\n\
        }else if (ret.result == Status.SATISFIABLE){\n\
            System.out.println(\"SAT\");\n\
            return 0;\n\
        }else{\n\
            System.out.println(\"UNKNOWN\");\n\
            return -2;\n\
        \r\t\t}")

        #c.dedent()
        #c.writeln("}")

        #c.writeln("")
        #c.writeln("System.out.printf(\"Mean execution time " + source + " -> " + destination + ": %.16f\", ((float) t/(float)1000)/k);")

        c.dedent()
        c.writeln("}")

        c.dedent()
        c.writeln("}")

        print >>f, c.end()
    logging.debug("File " + os.path.abspath(dirname + "/" + basename + ".java") + " has been successfully generated!!")

if __name__ == "__main__":
    main(sys.argv[1:])