Search in sources :

Example 1 with CliError

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");
    }
}
Also used : CliError(de.prob.exception.CliError) AbstractCommand(de.prob.animator.command.AbstractCommand) ErrorItem(de.prob.animator.domainobjects.ErrorItem)

Example 2 with CliError

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());
        }
    }
}
Also used : CliError(de.prob.exception.CliError) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 3 with CliError

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;
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CliError(de.prob.exception.CliError) BufferedReader(java.io.BufferedReader)

Aggregations

CliError (de.prob.exception.CliError)3 IOException (java.io.IOException)2 AbstractCommand (de.prob.animator.command.AbstractCommand)1 ErrorItem (de.prob.animator.domainobjects.ErrorItem)1 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1