use of de.prob.animator.command.ComposedCommand 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;
}
use of de.prob.animator.command.ComposedCommand in project prob2 by bendisposto.
the class StateSpace method evaluateForGivenStates.
/**
* Evaluates all of the formulas for every specified state (if they can be
* evaluated). Internally calls {@link #canBeEvaluated(State)}. If the
* formulas are of interest to a class (i.e. the an object has subscribed to
* the formula) the formula is cached.
*
* @param states
* for which the formula is to be evaluated
* @param formulas
* which are to be evaluated
* @return a map of the formulas and their results for all of the specified
* states
*/
public Map<State, Map<IEvalElement, AbstractEvalResult>> evaluateForGivenStates(final Collection<State> states, final List<IEvalElement> formulas) {
Map<State, Map<IEvalElement, AbstractEvalResult>> result = new HashMap<>();
List<EvaluationCommand> cmds = new ArrayList<>();
for (State stateId : states) {
if (stateId.isInitialised()) {
Map<IEvalElement, AbstractEvalResult> res = new HashMap<>();
result.put(stateId, res);
// Check for cached values
Map<IEvalElement, AbstractEvalResult> map = stateId.getValues();
for (IEvalElement f : formulas) {
if (map.containsKey(f)) {
res.put(f, map.get(f));
} else {
cmds.add(f.getCommand(stateId));
}
}
}
}
execute(new ComposedCommand(cmds));
for (EvaluationCommand efCmd : cmds) {
IEvalElement formula = efCmd.getEvalElement();
AbstractEvalResult value = efCmd.getValue();
State id = addState(efCmd.getStateId());
result.get(id).put(formula, value);
}
return result;
}
use of de.prob.animator.command.ComposedCommand 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;
}
use of de.prob.animator.command.ComposedCommand 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;
}
Aggregations