1 package it.unical.mat.embasp.platforms.desktop;
3 import java.io.BufferedReader;
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.io.PrintWriter;
10 import it.unical.mat.embasp.base.Callback;
11 import it.unical.mat.embasp.base.InputProgram;
12 import it.unical.mat.embasp.base.OptionDescriptor;
13 import it.unical.mat.embasp.base.Output;
14 import it.unical.mat.embasp.base.Service;
25 protected String load_from_STDIN_option;
31 public String getExePath() {
35 abstract protected Output getOutput(String output, String error);
53 public void startAsync(
final Callback callback,
final List<InputProgram> programs,
final List<OptionDescriptor> options) {
58 callback.callback(
startSync(programs, options));
70 public Output startSync(
final List<InputProgram> programs,
final List<OptionDescriptor> options) {
72 String option =
new String();
75 option += o.getOptions();
76 option += o.getSeparator();
80 String files_paths =
new String();
81 String final_program =
new String();
85 final_program += p.getPrograms();
86 for(
final String program_file: p.getFilesPaths()){
87 File f =
new File(program_file);
88 if(f.exists() && !f.isDirectory()) {
89 files_paths += program_file;
93 System.err.println(
"Warning : the file " + f.getAbsolutePath() +
" does not exists.");
96 System.err.println(
"Warning : wrong " +
InputProgram.class.getName());
98 final StringBuffer solverOutput =
new StringBuffer();
99 final StringBuffer solverError =
new StringBuffer();
103 final long startTime = System.nanoTime();
106 return new Output(
"",
"Error: executable not found");
108 final StringBuffer stringBuffer =
new StringBuffer();
109 stringBuffer.append(
exe_path).append(
" ").append(option).append(
" ").append(files_paths);
111 if (!final_program.isEmpty()){
112 stringBuffer.append(this.load_from_STDIN_option);
115 System.err.println(stringBuffer.toString());
117 final Process solver_process = Runtime.getRuntime().exec(stringBuffer.toString());
119 if(!final_program.isEmpty()) {
120 final PrintWriter writer =
new PrintWriter(solver_process.getOutputStream());
121 writer.println(final_program);
124 solver_process.waitFor();
127 Thread threadOutput=
new Thread() {
132 final BufferedReader bufferedReaderOutput =
new BufferedReader(
new InputStreamReader(solver_process.getInputStream()));
136 while ((currentLine = bufferedReaderOutput.readLine()) !=
null)
137 solverOutput.append(currentLine +
"\n");
138 }
catch (
final IOException e) {
144 threadOutput.start();
147 Thread threadError =
new Thread() {
152 final BufferedReader bufferedReaderError =
new BufferedReader(
new InputStreamReader(solver_process.getErrorStream()));
154 String currentErrLine;
155 while ((currentErrLine = bufferedReaderError.readLine()) !=
null)
156 solverError.append(currentErrLine +
"\n");
158 }
catch (
final IOException e) {
167 final long stopTime = System.nanoTime();
168 System.err.println(
"Total time : " + (stopTime - startTime));
170 return getOutput(solverOutput.toString(), solverError.toString());
172 }
catch (
final IOException e2) {
173 e2.printStackTrace();
174 }
catch (
final InterruptedException e) {
178 return getOutput(
"",
"");