Search in sources :

Example 1 with AbstractCommand

use of de.prob.animator.command.AbstractCommand 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 AbstractCommand

use of de.prob.animator.command.AbstractCommand in project prob2 by bendisposto.

the class StateSpaceProvider method loadFromCommand.

public StateSpace loadFromCommand(final AbstractModel model, final AbstractElement mainComponent, final Map<String, String> preferences, final AbstractCommand loadCmd) {
    StateSpace s = ssProvider.get();
    s.setModel(model, mainComponent);
    List<AbstractCommand> cmds = new ArrayList<>();
    for (Entry<String, String> pref : preferences.entrySet()) {
        cmds.add(new SetPreferenceCommand(pref.getKey(), pref.getValue()));
    }
    try {
        s.execute(new ComposedCommand(cmds));
        s.execute(loadCmd);
        s.execute(new StartAnimationCommand());
    } catch (Exception e) {
        s.kill();
        throw e;
    }
    return s;
}
Also used : StartAnimationCommand(de.prob.animator.command.StartAnimationCommand) StateSpace(de.prob.statespace.StateSpace) AbstractCommand(de.prob.animator.command.AbstractCommand) ArrayList(java.util.ArrayList) SetPreferenceCommand(de.prob.animator.command.SetPreferenceCommand) ComposedCommand(de.prob.animator.command.ComposedCommand)

Example 3 with AbstractCommand

use of de.prob.animator.command.AbstractCommand in project prob2 by bendisposto.

the class StateSpace method unsubscribe.

public boolean unsubscribe(final Object subscriber, final Collection<? extends IEvalElement> formulas, boolean unregister) {
    boolean success = false;
    final List<AbstractCommand> unsubscribeCmds = new ArrayList<>();
    for (IEvalElement formula : formulas) {
        if (formulaRegistry.containsKey(formula)) {
            final WeakHashMap<Object, Object> subscribers = formulaRegistry.get(formula);
            subscribers.remove(subscriber);
            if (subscribers.isEmpty() && unregister) {
                unsubscribeCmds.add(new UnregisterFormulaCommand(formula));
            }
            success = true;
        }
    }
    if (!unsubscribeCmds.isEmpty()) {
        execute(new ComposedCommand(unsubscribeCmds));
    }
    return success;
}
Also used : AbstractCommand(de.prob.animator.command.AbstractCommand) ArrayList(java.util.ArrayList) IEvalElement(de.prob.animator.domainobjects.IEvalElement) UnregisterFormulaCommand(de.prob.animator.command.UnregisterFormulaCommand) ComposedCommand(de.prob.animator.command.ComposedCommand)

Example 4 with AbstractCommand

use of de.prob.animator.command.AbstractCommand in project prob2 by bendisposto.

the class StateSpace method subscribe.

/**
 * This method lets ProB know that the subscriber is interested in the
 * specified formulas. ProB will then evaluate the formulas for every state
 * (after which the values can be retrieved from the
 * {@link State#getValues()} method).
 *
 * @param subscriber
 *            who is interested in the given formulas
 * @param formulas
 *            that are of interest
 * @return whether or not the subscription was successful (will return true
 *         if at least one of the formulas was successfully subscribed)
 */
public boolean subscribe(final Object subscriber, final Collection<? extends IEvalElement> formulas) {
    boolean success = false;
    List<AbstractCommand> subscribeCmds = new ArrayList<>();
    for (IEvalElement formulaOfInterest : formulas) {
        if (formulaOfInterest instanceof CSP) {
            logger.info("CSP formula {} not subscribed because CSP evaluation is not state based. Use eval method instead", formulaOfInterest.getCode());
        } else {
            if (formulaRegistry.containsKey(formulaOfInterest)) {
                formulaRegistry.get(formulaOfInterest).put(subscriber, new WeakReference<Object>(formulaOfInterest));
                success = true;
            } else {
                WeakHashMap<Object, Object> subscribers = new WeakHashMap<>();
                subscribers.put(subscriber, new WeakReference<Object>(subscriber));
                formulaRegistry.put(formulaOfInterest, subscribers);
                subscribeCmds.add(new RegisterFormulaCommand(formulaOfInterest));
                success = true;
            }
        }
    }
    if (!subscribeCmds.isEmpty()) {
        execute(new ComposedCommand(subscribeCmds));
    }
    return success;
}
Also used : CSP(de.prob.animator.domainobjects.CSP) AbstractCommand(de.prob.animator.command.AbstractCommand) ArrayList(java.util.ArrayList) IEvalElement(de.prob.animator.domainobjects.IEvalElement) RegisterFormulaCommand(de.prob.animator.command.RegisterFormulaCommand) ComposedCommand(de.prob.animator.command.ComposedCommand) WeakHashMap(java.util.WeakHashMap)

Aggregations

AbstractCommand (de.prob.animator.command.AbstractCommand)4 ComposedCommand (de.prob.animator.command.ComposedCommand)3 ArrayList (java.util.ArrayList)3 IEvalElement (de.prob.animator.domainobjects.IEvalElement)2 RegisterFormulaCommand (de.prob.animator.command.RegisterFormulaCommand)1 SetPreferenceCommand (de.prob.animator.command.SetPreferenceCommand)1 StartAnimationCommand (de.prob.animator.command.StartAnimationCommand)1 UnregisterFormulaCommand (de.prob.animator.command.UnregisterFormulaCommand)1 CSP (de.prob.animator.domainobjects.CSP)1 ErrorItem (de.prob.animator.domainobjects.ErrorItem)1 CliError (de.prob.exception.CliError)1 StateSpace (de.prob.statespace.StateSpace)1 WeakHashMap (java.util.WeakHashMap)1