Search in sources :

Example 1 with EvaluationCommand

use of de.prob.animator.command.EvaluationCommand 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;
}
Also used : HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) AbstractEvalResult(de.prob.animator.domainobjects.AbstractEvalResult) EvaluationCommand(de.prob.animator.command.EvaluationCommand) ArrayList(java.util.ArrayList) IEvalElement(de.prob.animator.domainobjects.IEvalElement) HashMap(java.util.HashMap) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap) ComposedCommand(de.prob.animator.command.ComposedCommand)

Aggregations

ComposedCommand (de.prob.animator.command.ComposedCommand)1 EvaluationCommand (de.prob.animator.command.EvaluationCommand)1 AbstractEvalResult (de.prob.animator.domainobjects.AbstractEvalResult)1 IEvalElement (de.prob.animator.domainobjects.IEvalElement)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 WeakHashMap (java.util.WeakHashMap)1