Search in sources :

Example 11 with IEvalElement

use of de.prob.animator.domainobjects.IEvalElement in project prob2 by bendisposto.

the class GetSvgForVisualizationCommand method writeCommand.

@Override
public void writeCommand(IPrologTermOutput pto) {
    pto.openTerm(PROLOG_COMMAND_NAME);
    pto.printAtomOrNumber(id.getId());
    pto.printAtom(item.getCommand());
    pto.openList();
    for (IEvalElement formula : formulas) {
        formula.printProlog(pto);
    }
    pto.closeList();
    pto.printAtom("svg");
    pto.printAtom(file.getAbsolutePath());
    pto.closeTerm();
}
Also used : IEvalElement(de.prob.animator.domainobjects.IEvalElement)

Example 12 with IEvalElement

use of de.prob.animator.domainobjects.IEvalElement 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)

Example 13 with IEvalElement

use of de.prob.animator.domainobjects.IEvalElement in project prob2 by bendisposto.

the class StateSpace method transitionFromPredicate.

/**
 * Takes the name of an operation and a predicate and finds Operations that
 * satisfy the name and predicate at the given stateId. New Operations are
 * added to the graph. This is only valid for ClassicalB predicates.
 *
 * @param stateId
 *            {@link State} from which the operation should be found
 * @param opName
 *            name of the operation that should be executed
 * @param predicate
 *            an additional guard for the operation. This usually describes
 *            the parameters
 * @param nrOfSolutions
 *            int number of solutions that should be found for the given
 *            predicate
 * @return list of operations calculated by ProB
 */
public List<Transition> transitionFromPredicate(final State stateId, final String opName, final String predicate, final int nrOfSolutions) {
    final IEvalElement pred = model.parseFormula(predicate, FormulaExpand.EXPAND);
    final GetOperationByPredicateCommand command = new GetOperationByPredicateCommand(this, stateId.getId(), opName, pred, nrOfSolutions);
    execute(command);
    if (command.hasErrors()) {
        throw new IllegalArgumentException("Executing operation " + opName + " with predicate " + predicate + " produced errors: " + Joiner.on(", ").join(command.getErrors()));
    }
    return command.getNewTransitions();
}
Also used : IEvalElement(de.prob.animator.domainobjects.IEvalElement) GetOperationByPredicateCommand(de.prob.animator.command.GetOperationByPredicateCommand)

Example 14 with IEvalElement

use of de.prob.animator.domainobjects.IEvalElement 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 15 with IEvalElement

use of de.prob.animator.domainobjects.IEvalElement 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

IEvalElement (de.prob.animator.domainobjects.IEvalElement)15 AbstractEvalResult (de.prob.animator.domainobjects.AbstractEvalResult)4 ArrayList (java.util.ArrayList)4 ComposedCommand (de.prob.animator.command.ComposedCommand)3 ListPrologTerm (de.prob.prolog.term.ListPrologTerm)3 PrologTerm (de.prob.prolog.term.PrologTerm)3 HashMap (java.util.HashMap)3 AbstractCommand (de.prob.animator.command.AbstractCommand)2 GetOperationByPredicateCommand (de.prob.animator.command.GetOperationByPredicateCommand)2 ClassicalB (de.prob.animator.domainobjects.ClassicalB)2 EvalResult (de.prob.animator.domainobjects.EvalResult)2 CompoundPrologTerm (de.prob.prolog.term.CompoundPrologTerm)2 Map (java.util.Map)2 WeakHashMap (java.util.WeakHashMap)2 Test (org.junit.Test)2 AbstractOperation (de.be4.classicalb.core.parser.rules.AbstractOperation)1 ComputationOperation (de.be4.classicalb.core.parser.rules.ComputationOperation)1 RuleOperation (de.be4.classicalb.core.parser.rules.RuleOperation)1 EvaluationCommand (de.prob.animator.command.EvaluationCommand)1 OperationInfo (de.prob.animator.command.GetMachineOperationInfos.OperationInfo)1