EmbASP-Java
DesktopService.java
1 package it.unical.mat.embasp.platforms.desktop;
2 
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import java.io.PrintWriter;
8 import java.util.List;
9 
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;
15 
22 public abstract class DesktopService implements Service {
24  protected String exe_path;
25  protected String load_from_STDIN_option;
26 
27  public DesktopService(final String exe_path) {
28  this.exe_path = exe_path;
29  }
30 
31  public String getExePath() {
32  return exe_path;
33  }
34 
35  abstract protected Output getOutput(String output, String error);
36 
43  public void setExePath(final String exe_path) {
44  this.exe_path = exe_path;
45  }
46 
52  @Override
53  public void startAsync(final Callback callback, final List<InputProgram> programs, final List<OptionDescriptor> options) {
54 
55  new Thread() {
56  @Override
57  public void run() {
58  callback.callback(startSync(programs, options));
59  }
60  }.start();
61 
62  }
63 
69  @Override
70  public Output startSync(final List<InputProgram> programs, final List<OptionDescriptor> options) {
71 
72  String option = new String();
73  for (final OptionDescriptor o : options)
74  if (o != null) {
75  option += o.getOptions();
76  option += o.getSeparator();
77  } else
78  System.err.println("Warning : wrong " + OptionDescriptor.class.getName());
79 
80  String files_paths = new String();
81  String final_program = new String();
82 
83  for (final InputProgram p : programs)
84  if (p != null) {
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;
90  files_paths += " ";
91  }
92  else
93  System.err.println("Warning : the file " + f.getAbsolutePath() + " does not exists.");
94  }
95  } else
96  System.err.println("Warning : wrong " + InputProgram.class.getName());
97 
98  final StringBuffer solverOutput = new StringBuffer();
99  final StringBuffer solverError = new StringBuffer();
100 
101  try {
102 
103  final long startTime = System.nanoTime();
104 
105  if (exe_path == null)
106  return new Output("", "Error: executable not found");
107 
108  final StringBuffer stringBuffer = new StringBuffer();
109  stringBuffer.append(exe_path).append(" ").append(option).append(" ").append(files_paths);
110 
111  if (!final_program.isEmpty()){
112  stringBuffer.append(this.load_from_STDIN_option);
113  }
114 
115  System.err.println(stringBuffer.toString());
116 
117  final Process solver_process = Runtime.getRuntime().exec(stringBuffer.toString());
118 
119  if(!final_program.isEmpty()) {
120  final PrintWriter writer = new PrintWriter(solver_process.getOutputStream());
121  writer.println(final_program);
122  if (writer != null)
123  writer.close();
124  solver_process.waitFor();
125  }
126 
127  Thread threadOutput=new Thread() {
128  @Override
129  public void run() {
130  try {
131 
132  final BufferedReader bufferedReaderOutput = new BufferedReader(new InputStreamReader(solver_process.getInputStream()));
133 
134  // Read output of the solver and store in solverOutput
135  String currentLine;
136  while ((currentLine = bufferedReaderOutput.readLine()) != null)
137  solverOutput.append(currentLine + "\n");
138  } catch (final IOException e) {
139  e.printStackTrace();
140  }
141 
142  }
143  };
144  threadOutput.start();
145  threadOutput.join();
146 
147  Thread threadError = new Thread() {
148  @Override
149  public void run() {
150  try {
151 
152  final BufferedReader bufferedReaderError = new BufferedReader(new InputStreamReader(solver_process.getErrorStream()));
153 
154  String currentErrLine;
155  while ((currentErrLine = bufferedReaderError.readLine()) != null)
156  solverError.append(currentErrLine + "\n");
157 
158  } catch (final IOException e) {
159  e.printStackTrace();
160  }
161 
162  }
163  };
164  threadError.start();
165  threadError.join();
166 
167  final long stopTime = System.nanoTime();
168  System.err.println("Total time : " + (stopTime - startTime));
169 
170  return getOutput(solverOutput.toString(), solverError.toString());
171 
172  } catch (final IOException e2) {
173  e2.printStackTrace();
174  } catch (final InterruptedException e) {
175  e.printStackTrace();
176  }
177 
178  return getOutput("", "");
179 
180  }
181 
182 }
it.unical.mat.embasp.platforms.desktop.DesktopService.setExePath
void setExePath(final String exe_path)
Definition: DesktopService.java:43
it.unical.mat.embasp.base.InputProgram
Definition: InputProgram.java:9
it.unical.mat.embasp.platforms.desktop.DesktopService
Definition: DesktopService.java:22
it.unical.mat.embasp.base.Callback
Definition: Callback.java:8
it.unical.mat.embasp.platforms.desktop.DesktopService.startAsync
void startAsync(final Callback callback, final List< InputProgram > programs, final List< OptionDescriptor > options)
Definition: DesktopService.java:53
it.unical.mat.embasp.platforms.desktop.DesktopService.exe_path
String exe_path
Definition: DesktopService.java:24
it.unical.mat.embasp.base.Output
Definition: Output.java:4
it.unical.mat.embasp.base.Service
Definition: Service.java:10
it.unical.mat.embasp.base.OptionDescriptor
Definition: OptionDescriptor.java:4
it.unical.mat.embasp.platforms.desktop.DesktopService.startSync
Output startSync(final List< InputProgram > programs, final List< OptionDescriptor > options)
Definition: DesktopService.java:70