44// SPDX-License-Identifier: Apache-2.0
55package org .sosy_lab .java_smt .utils ;
66
7+ import com .microsoft .z3 .*;
78import com .microsoft .z3 .Context ;
8- import com .microsoft .z3 .Solver ;
9- import edu .stanford .CVC4 .ExprManager ;
10- import edu .stanford .CVC4 .SExpr ;
11- import edu .stanford .CVC4 .SmtEngine ;
129import java .io .IOException ;
1310import java .nio .charset .Charset ;
1411import java .nio .file .Files ;
1512import java .nio .file .Path ;
16- import java .nio .file .Paths ;
1713import java .util .Locale ;
1814import org .sosy_lab .common .ShutdownManager ;
1915import org .sosy_lab .common .configuration .Configuration ;
2824import org .sosy_lab .java_smt .api .SolverContext .ProverOptions ;
2925import org .sosy_lab .java_smt .api .SolverException ;
3026import org .sosy_lab .java_smt .basicimpl .Generator ;
31- import com .microsoft .z3 .*;
32- import io .github .cvc5 .*;
33- import java .io .*;
34- import java .nio .file .*;
35- import io .github .cvc5 .*;
36- import io .github .cvc5 .modes .*;
3727
38- /**
39- * This file is meant for the Evaluation of the Parser/Generator.
40- */
28+ /** This file is meant for the Evaluation of the Parser/Generator. */
4129class ParseGenerateAndReparse {
42- private ParseGenerateAndReparse () {
43- }
30+ private ParseGenerateAndReparse () {}
4431
4532 public static void main (String [] args )
4633 throws InvalidConfigurationException , InterruptedException , SolverException {
47-
34+ String expectedResult =
35+ "(declare-const a (_ FloatingPoint 8 24))\n "
36+ + "(declare-const b (_ FloatingPoint 8 24))\n "
37+ + "(declare-const min (_ FloatingPoint 8 24))\n "
38+ + "(assert (fp.eq (fp.min a b) min))\n " ;
39+ System .out .println (nativeZ3ParseAndIsUnsat (expectedResult ));
4840 if (args .length < 3 ) {
4941 System .err .println ("Usage: java ParseGenerateAndReparseTest <smt2-file> <solver> <mode>" );
5042 System .exit (1 );
5143 }
5244
53-
5445 String smt2FilePath = args [0 ];
5546 String smt2 ;
5647 try {
@@ -83,8 +74,8 @@ public static void main(String[] args)
8374 if (mode .equals ("GENERATE_AND_REPARSE" )) {
8475 // PARSE REGENERATE THEN NATIVE PARSE
8576 try (SolverContext solverLessContext =
86- SolverContextFactory .createSolverContext (
87- config , logger , shutdown .getNotifier (), Solvers .SOLVERLESS )) {
77+ SolverContextFactory .createSolverContext (
78+ config , logger , shutdown .getNotifier (), Solvers .SOLVERLESS )) {
8879
8980 // JAVASMT'S PARSER
9081 BooleanFormula formula =
@@ -93,39 +84,31 @@ public static void main(String[] args)
9384 Generator .assembleConstraint (formula );
9485 String regenerated = Generator .getSMTLIB2String ();
9586 // NATIVE PARSE
96- checkResult (callNativeParseAndIsUnsat ( solver , smt2FilePath ));
87+ checkResult (nativeZ3ParseAndIsUnsat ( regenerated ));
9788 } catch (Exception pE ) {
9889 printError (pE );
9990 }
100-
10191 }
10292 if (mode .equals ("NATIVE" )) {
103- try (SolverContext realSolverContext =
104- SolverContextFactory .createSolverContext (
105- config , logger , shutdown .getNotifier (), solver );
106- ProverEnvironment realProverEnvironment =
107- realSolverContext .newProverEnvironment (ProverOptions .GENERATE_MODELS )) {
108-
109- try {
110- // NATIVE PARSE
111- checkResult (callNativeParseAndIsUnsat (solver , smt2FilePath ));
112- } catch (Exception pE ) {
113- printError (pE );
114- }
93+ try {
94+ // NATIVE PARSE
95+ checkResult (nativeZ3ParseAndIsUnsat (smt2FilePath ));
96+ } catch (Exception pE ) {
97+ printError (pE );
11598 }
11699 }
117100 if (mode .equals ("PARSE" )) {
118101 try (SolverContext realSolverContext =
119- SolverContextFactory .createSolverContext (
120- config , logger , shutdown .getNotifier (), solver );
121- ProverEnvironment realProverEnvironment =
122- realSolverContext .newProverEnvironment (ProverOptions .GENERATE_MODELS )) {
102+ SolverContextFactory .createSolverContext (
103+ config , logger , shutdown .getNotifier (), solver );
104+ ProverEnvironment realProverEnvironment =
105+ realSolverContext .newProverEnvironment (ProverOptions .GENERATE_MODELS )) {
123106 try {
124107 // JAVASMT'S PARSER
125108 BooleanFormula javaSMTParsed =
126109 realSolverContext .getFormulaManager ().universalParseFromString (smt2 );
127110 realProverEnvironment .addConstraint (javaSMTParsed );
128- //JAVASMT'S PARSE
111+ // JAVASMT'S PARSE
129112 checkResult (realProverEnvironment .isUnsat ());
130113 } catch (Exception pE ) {
131114 printError (pE );
@@ -139,7 +122,7 @@ public static void checkResult(boolean isUnsat) {
139122 System .exit (0 );
140123 }
141124
142- public static boolean nativeZ3ParseAndIsUnsat (String smt2 ) throws Exception {
125+ public static boolean nativeZ3ParseAndIsUnsat (String smt2 ) {
143126 try (Context ctx = new Context ()) {
144127 Solver solver = ctx .mkSolver ();
145128 solver .fromString (smt2 );
@@ -148,105 +131,6 @@ public static boolean nativeZ3ParseAndIsUnsat(String smt2) throws Exception {
148131 }
149132 }
150133
151- public static boolean nativeCVC5ParseAndIsUnsat (String smt2 ) throws Exception {
152- // 1. Temporäre Datei erstellen
153- Path tempFile = Files .createTempFile ("cvc5_" , ".smt2" );
154- try {
155- // 2. SMT2-Inhalt in temporäre Datei schreiben
156- Files .write (tempFile , smt2 .getBytes ());
157-
158- // 3. Prozess für CVC5 starten
159- ProcessBuilder pb = new ProcessBuilder (
160- "./dependencies/cvc5" ,
161- "--lang=smt2" ,
162- tempFile .toString ()
163- );
164- pb .redirectErrorStream (true );
165- Process process = pb .start ();
166-
167- // 4. Ausgabe lesen
168- try (BufferedReader reader = new BufferedReader (
169- new InputStreamReader (process .getInputStream ()))) {
170- String line ;
171- while ((line = reader .readLine ()) != null ) {
172- if (line .trim ().equals ("unsat" )) {
173- return true ;
174- } else if (line .trim ().equals ("sat" )) {
175- return false ;
176- }
177- }
178- }
179-
180- // 5. Auf Prozessende warten
181- int exitCode = process .waitFor ();
182- if (exitCode != 0 ) {
183- throw new RuntimeException ("CVC5 exited with code " + exitCode );
184- }
185-
186- return false ; // unknown
187- } finally {
188- // 6. Temporäre Datei löschen
189- Files .deleteIfExists (tempFile );
190- }
191- }
192-
193- public static boolean nativeCVC4ParseAndIsUnsat (String smt2 ) throws Exception {
194- // 1. Temporäre Datei erstellen
195- Path tempFile = Files .createTempFile ("cvc4_" , ".smt2" );
196- try {
197- // 2. SMT2-Inhalt in temporäre Datei schreiben
198- Files .write (tempFile , smt2 .getBytes ());
199-
200- // 3. Prozess für CVC4 starten
201- ProcessBuilder pb = new ProcessBuilder (
202- "./dependencies/cvc4" ,
203- "--lang=smt2" ,
204- tempFile .toString ()
205- );
206- pb .redirectErrorStream (true );
207- Process process = pb .start ();
208-
209- // 4. Ausgabe lesen
210- try (BufferedReader reader = new BufferedReader (
211- new InputStreamReader (process .getInputStream ()))) {
212- String line ;
213- while ((line = reader .readLine ()) != null ) {
214- if (line .trim ().equals ("unsat" )) {
215- return true ;
216- } else if (line .trim ().equals ("sat" )) {
217- return false ;
218- }
219- }
220- }
221-
222- // 5. Auf Prozessende warten
223- int exitCode = process .waitFor ();
224- if (exitCode != 0 ) {
225- throw new RuntimeException ("CVC4 exited with code " + exitCode );
226- }
227-
228- return false ; // unknown
229- } finally {
230- // 6. Temporäre Datei löschen
231- Files .deleteIfExists (tempFile );
232- }
233- }
234-
235- public static boolean callNativeParseAndIsUnsat (Solvers solverName , String smt2 )
236- throws Exception {
237- switch (solverName ) {
238- case Z3 :
239- return nativeZ3ParseAndIsUnsat (smt2 );
240- case CVC4 :
241- return nativeCVC4ParseAndIsUnsat (smt2 );
242- case CVC5 :
243- return nativeCVC5ParseAndIsUnsat (smt2 );
244- default :
245- throw new UnsupportedOperationException ("Unsupported solver: " + solverName );
246- }
247- }
248-
249-
250134 public static void printError (Exception pE ) {
251135 System .out .println ("ERROR: " );
252136 pE .printStackTrace ();
0 commit comments