3 ##############################################################################
4 # Copyright (c) 2017 Politecnico di Torino and others.
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
12 from pprint import pprint
13 from code_generator import CodeGeneratorBackend
22 print 'test_generator.py -i <inputfile> -o <outputfile> -s <source> -d <destination>'
24 #initialize command line arguments values
29 #parse command line arguments and exit if there is an error
31 opts, args = getopt.getopt(argv,"hi:o:s:d:",["ifile=","ofile=","source=","destination="])
32 except getopt.GetoptError:
33 print 'test_generator.py -i <inputfile> -o <outputfile> -s <source> -d <destination>'
37 print 'test_generator.py -i <inputfile> -o <outputfile> -s <source> -d <destination>'
39 elif opt in ("-i", "--ifile"):
41 elif opt in ("-o", "--ofile"):
43 elif opt in ("-s", "--source"):
45 elif opt in ("-d", "--destination"):
48 logging.basicConfig(stream=sys.stderr, level=logging.INFO)
49 #capitalize ouput filename
50 dirname = os.path.dirname(outputfile)
51 basename = os.path.basename(outputfile)
52 basename = os.path.splitext(basename)[0]
53 basename = basename[0].upper() + basename[1:]
56 logging.debug('Input file is', inputfile)
57 logging.debug('Output file is', dirname + "/" + basename)
58 logging.debug('Source node is', source)
59 logging.debug('Destination node is', destination)
61 #begin file generation
62 with smart_open(dirname + "/" + basename + ".java") as f:
63 c = CodeGeneratorBackend()
65 c.writeln("package tests;")
66 c.writeln("import java.util.Calendar;")
67 c.writeln("import java.util.Date;")
68 c.writeln("import java.util.HashMap;")
70 c.writeln("import com.microsoft.z3.Context;")
71 c.writeln("import com.microsoft.z3.FuncDecl;")
72 c.writeln("import com.microsoft.z3.Model;")
73 c.writeln("import com.microsoft.z3.Status;")
74 c.writeln("import com.microsoft.z3.Z3Exception;")
75 c.writeln("import mcnet.components.IsolationResult;")
78 inputfile = os.path.basename(inputfile)
79 c.writeln("import tests.scenarios." + os.path.splitext(inputfile)[0] + ";")
80 c.writeln("public class " + basename + "{")
83 c.writeln("Context ctx;")
85 c.write("public void resetZ3() throws Z3Exception{\n\
86 HashMap<String, String> cfg = new HashMap<String, String>();\n\
87 cfg.put(\"model\", \"true\");\n\
88 ctx = new Context(cfg);\n\
91 c.write("public void printVector (Object[] array){\n\
93 System.out.println( \"*** Printing vector ***\");\n\
94 for (Object a : array){\n\
96 System.out.println( \"#\"+i);\n\
97 System.out.println(a);\n\
98 System.out.println( \"*** \"+ i+ \" elements printed! ***\");\n\
102 c.write("public void printModel (Model model) throws Z3Exception{\n\
103 for (FuncDecl d : model.getFuncDecls()){\n\
104 System.out.println(d.getName() +\" = \"+ d.toString());\n\
105 System.out.println(\"\");\n\
109 c.writeln("public int run() throws Z3Exception{")
112 c.writeln(basename + " p = new " + basename + "();")
114 #adding time estimation
115 #c.writeln("int k = 0;")
116 #c.writeln("long t = 0;")
118 #c.writeln("for(;k<1;k++){")
121 c.writeln("p.resetZ3();")
123 c.write(os.path.splitext(inputfile)[0] + " model = new " + os.path.splitext(inputfile)[0] + "(p.ctx);\n")
125 #c.writeln("Calendar cal = Calendar.getInstance();")
126 #c.writeln("Date start_time = cal.getTime();")
128 c.write("IsolationResult ret =model.check.checkIsolationProperty(model.")
129 c.append(source + ", model." + destination + ");\n")
130 #c.writeln("Calendar cal2 = Calendar.getInstance();")
131 #c.writeln("t = t+(cal2.getTime().getTime() - start_time.getTime());")
133 c.writeln("if (ret.result == Status.UNSATISFIABLE){\n\
134 System.out.println(\"UNSAT\");\n\
136 }else if (ret.result == Status.SATISFIABLE){\n\
137 System.out.println(\"SAT\");\n\
140 System.out.println(\"UNKNOWN\");\n\
148 #c.writeln("System.out.printf(\"Mean execution time " + source + " -> " + destination + ": %.16f\", ((float) t/(float)1000)/k);")
157 logging.debug("File " + os.path.abspath(dirname + "/" + basename + ".java") + " has been successfully generated!!")
159 if __name__ == "__main__":