Search in sources :

Example 1 with RegisterFormulaCommand

use of de.prob.animator.command.RegisterFormulaCommand 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)1 ComposedCommand (de.prob.animator.command.ComposedCommand)1 RegisterFormulaCommand (de.prob.animator.command.RegisterFormulaCommand)1 CSP (de.prob.animator.domainobjects.CSP)1 IEvalElement (de.prob.animator.domainobjects.IEvalElement)1 ArrayList (java.util.ArrayList)1 WeakHashMap (java.util.WeakHashMap)1