use of de.prob.exception.CliError in project prob2 by bendisposto.
the class AnimatorImpl method execute.
@SuppressWarnings("unused")
@Override
public synchronized void execute(final AbstractCommand command) {
if (cli == null) {
logger.error("Probcli is missing. Try \"upgrade\".");
throw new CliError("no cli found");
}
if (DEBUG && !command.getSubcommands().isEmpty()) {
List<AbstractCommand> cmds = command.getSubcommands();
for (AbstractCommand abstractCommand : cmds) {
execute(abstractCommand);
}
}
if (command.blockAnimator()) {
logger.trace("Blocking animator");
startTransaction();
}
logger.trace("Starting execution of {}", command);
do {
IPrologResult result = processor.sendCommand(command);
final List<ErrorItem> errorItems = getErrorItems();
if (result instanceof YesResult && errorItems.isEmpty()) {
logger.trace("Execution successful, processing result");
try {
command.processResult(((YesResult) result).getBindings());
} catch (RuntimeException e) {
this.kill();
throw new CliError("Exception while processing command result", e);
}
} else {
logger.trace("Execution unsuccessful, processing error");
command.processErrorResult(result, errorItems);
}
logger.trace("Executed {} (completed: {}, interrupted: {})", command, command.isCompleted(), command.isInterrupted());
if (!command.isCompleted() && Thread.currentThread().isInterrupted()) {
logger.info("Stopping execution of {} because this thread was interrupted", command);
break;
}
} while (!command.isCompleted());
logger.trace("Done executing {}", command);
if (command.blockAnimator()) {
endTransaction();
logger.trace("Unblocked animator");
}
}
use of de.prob.exception.CliError in project prob2 by bendisposto.
the class ProBInstanceProvider method analyseStdout.
private static void analyseStdout(final BufferedReader input, final Collection<? extends AbstractCliPattern<?>> patterns) {
final List<AbstractCliPattern<?>> patternsList = new ArrayList<>(patterns);
try {
String line;
do {
line = input.readLine();
if (line == null) {
break;
}
logger.debug("Apply cli detection patterns to {}", line);
applyPatterns(patternsList, line);
} while (!patternsList.isEmpty() && !line.contains("starting command loop"));
} catch (IOException e) {
final String message = "Problem while starting ProB. Cannot read from input stream.";
logger.error(message);
logger.debug(message, e);
throw new CliError(message, e);
}
for (AbstractCliPattern<?> p : patternsList) {
p.notifyNotFound();
if (p.notFoundIsFatal()) {
throw new CliError("Missing info from CLI " + p.getClass().getSimpleName());
}
}
}
use of de.prob.exception.CliError in project prob2 by bendisposto.
the class ProBInstanceProvider method startProlog.
private ProBInstance startProlog() {
ProcessHandle processTuple = processProvider.get();
Process process = processTuple.getProcess();
String key = processTuple.getKey();
final BufferedReader stream = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.forName("utf8")));
final Map<Class<? extends AbstractCliPattern<?>>, AbstractCliPattern<?>> cliInformation;
try {
cliInformation = extractCliInformation(stream);
} catch (CliError e) {
// Check if the CLI exited while extracting the information.
final Integer exitCode = getOptionalProcessExitCode(process);
if (exitCode == null) {
// CLI didn't exit, just rethrow the error.
throw e;
} else {
// CLI exited, report the exit code.
throw new CliError("CLI exited with status " + exitCode + " while matching output patterns", e);
}
}
Integer port = ((PortPattern) cliInformation.get(PortPattern.class)).getValue();
Long userInterruptReference = ((InterruptRefPattern) cliInformation.get(InterruptRefPattern.class)).getValue();
ProBConnection connection = new ProBConnection(key, port);
try {
processCounter.incrementAndGet();
connection.connect();
ProBInstance cli = new ProBInstance(process, stream, userInterruptReference, connection, home, osInfo, processCounter);
processes.add(new WeakReference<>(cli));
return cli;
} catch (IOException e) {
processCounter.decrementAndGet();
logger.error("Error connecting to Prolog binary.", e);
return null;
}
}
Aggregations