Package ptolemy.domains.fsm.kernel

Examples of ptolemy.domains.fsm.kernel.State


     *  not the initial state, it is removed and the two transitions are
     *  combined into one. The label on the new transition is formed by
     *  <incomingLabel><NAME_CONNECTOR><outgoingLabel>.
     */
    public void combineInternalTransitions() {
        State initialState = (State) getEntity(initialStateName.getExpression());

        try {
            Iterator states = entityList().iterator();

            while (states.hasNext()) {
                State state = (State) states.next();

                ComponentPort inPort = state.incomingPort;
                List transitionList = inPort.linkedRelationList();
                InterfaceAutomatonTransition incomingTransition = null;

                if (transitionList.size() != 1) {
                    continue;
                }

                // just one incoming transition, check if it's internal
                incomingTransition = (InterfaceAutomatonTransition) transitionList
                        .get(0);

                if (incomingTransition.getType() != InterfaceAutomatonTransition._INTERNAL_TRANSITION) {
                    continue;
                }

                ComponentPort outPort = state.outgoingPort;
                transitionList = outPort.linkedRelationList();

                InterfaceAutomatonTransition outgoingTransition = null;

                if (transitionList.size() != 1) {
                    continue;
                }

                // just one outgoing transition, check if it's internal
                outgoingTransition = (InterfaceAutomatonTransition) transitionList
                        .get(0);

                if (outgoingTransition.getType() != InterfaceAutomatonTransition._INTERNAL_TRANSITION) {
                    continue;
                }

                // only has one incoming and one outgoing internal transition,
                // check if this state is initial.
                if (state == initialState) {
                    continue;
                }

                // combine transitions
                State sourceState = incomingTransition.sourceState();
                State destinationState = outgoingTransition.destinationState();
                String incomingLabel = incomingTransition.getLabel();
                String incomingName = incomingLabel.substring(0, incomingLabel
                        .length() - 1);
                String outgoingLabel = outgoingTransition.getLabel();
                String newLabel = incomingName + NAME_CONNECTOR + outgoingLabel;
View Full Code Here


        // Initialize simulation. Use condition 1 in Definitino 14 to
        // (significantly) reduce the size.
        Iterator superStates = entityList().iterator();

        while (superStates.hasNext()) {
            State superState = (State) superStates.next();
            Iterator subStates = subAutomaton.entityList().iterator();

            while (subStates.hasNext()) {
                State subState = (State) subStates.next();

                if (_condition1Satisfied(this, superState, subAutomaton,
                        subState)) {
                    StatePair pair = new StatePair(superState, subState);
                    simulation.add(pair);
                }
            }
        }

        // Repeatedly removing the pairs in simulation using the 2 conditions
        // in Definition 14 of the paper, until no more pairs can be removed
        // or until simulation is empty.
        Set toBeRemoved = new HashSet();

        do {
            toBeRemoved.clear();

            Iterator pairs = simulation.iterator();

            while (pairs.hasNext()) {
                StatePair pair = (StatePair) pairs.next();
                State superState = pair.first();
                State subState = pair.second();

                if (_condition2Satisfied(this, superState, subAutomaton,
                        subState, simulation) == false) {
                    toBeRemoved.add(pair);
                }
View Full Code Here

        Set deadlockStates = new HashSet();
        Iterator states = entityList().iterator();

        while (states.hasNext()) {
            State state = (State) states.next();
            ComponentPort outPort = state.outgoingPort;

            if (outPort.linkedRelationList().size() == 0) {
                deadlockStates.add(state);
            }
View Full Code Here

        // iterate
        while (!frontier.isEmpty()) {
            // there does not seem to be an easy way to remove an arbitrary
            // element, except through Iterator
            Iterator iterator = frontier.iterator();
            State current = (State) iterator.next();
            frontier.remove(current);

            // put all states that are reacheable from current through
            // internal transitions into closure and frontier
            ComponentPort outPort = current.outgoingPort;
            Iterator transitions = outPort.linkedRelationList().iterator();

            while (transitions.hasNext()) {
                InterfaceAutomatonTransition transition = (InterfaceAutomatonTransition) transitions
                        .next();
                int transitionType = transition.getType();

                if (transitionType == InterfaceAutomatonTransition._INTERNAL_TRANSITION) {
                    State destinationState = transition.destinationState();

                    if (!closure.contains(destinationState)) {
                        closure.add(destinationState);
                        frontier.add(destinationState);
                    }
View Full Code Here

        Set destinations = new HashSet();
        Set closure = epsilonClosure(sourceState);
        Iterator sources = closure.iterator();

        while (sources.hasNext()) {
            State source = (State) sources.next();
            ComponentPort outPort = source.outgoingPort;
            Iterator transitions = outPort.linkedRelationList().iterator();

            while (transitions.hasNext()) {
                InterfaceAutomatonTransition transition = (InterfaceAutomatonTransition) transitions
                        .next();

                if (transition.getLabel().equals(transitionLabel)) {
                    State destination = transition.destinationState();
                    destinations.add(destination);
                }
            }
        }
View Full Code Here

        closure.remove(state);

        Iterator states = closure.iterator();

        while (states.hasNext()) {
            State nextState = (State) states.next();
            Set labels = _transitionLabelsFrom(nextState,
                    InterfaceAutomatonTransition._INPUT_TRANSITION);
            transitionLabels.retainAll(labels);
        }
View Full Code Here

        closure.remove(state);

        Iterator states = closure.iterator();

        while (states.hasNext()) {
            State nextState = (State) states.next();
            Set labels = _transitionLabelsFrom(nextState,
                    InterfaceAutomatonTransition._OUTPUT_TRANSITION);
            transitionLabels.addAll(labels);
        }
View Full Code Here

        //         if (p, q') is in alternatingSimulation
        //             if (p, q') is not already in reacheableSimulation
        //                 put it in both reacheableSimulation and frontier
        //
        //     stop until frontier is empty
        State superInitial = superAutomaton.getInitialState();
        State subInitial = subAutomaton.getInitialState();
        StatePair pair = new StatePair(superInitial, subInitial);

        if (!alternatingSimulation.contains(pair)) {
            // initial states not in alternating simulation, return
            // an empty set.
            return new HashSet();
        }

        // initial states in alternating simulation
        Set reacheableSimulation = new HashSet();
        Set frontier = new HashSet();
        reacheableSimulation.add(pair);
        frontier.add(pair);

        // repeat
        while (!frontier.isEmpty()) {
            // pick a state from frontier. It seems that there isn't an
            // easy way to pick an arbitrary entry from a HashSet, except
            // through Iterator
            Iterator iterator = frontier.iterator();
            StatePair currentPair = (StatePair) iterator.next();
            frontier.remove(currentPair);

            State superState = currentPair.first();
            State subState = currentPair.second();

            ComponentPort superPort = superState.outgoingPort;
            Iterator superTransitions = superPort.linkedRelationList()
                    .iterator();

            while (superTransitions.hasNext()) {
                InterfaceAutomatonTransition superTransition = (InterfaceAutomatonTransition) superTransitions
                        .next();
                State superDestination = superTransition.destinationState();
                String superLabel = superTransition.getLabel();

                int transitionType = superTransition.getType();

                if ((transitionType == InterfaceAutomatonTransition._INPUT_TRANSITION)
                        || (transitionType == InterfaceAutomatonTransition._OUTPUT_TRANSITION)) {
                    // check whether sub automaton has same transition
                    ComponentPort subPort = subState.outgoingPort;
                    Iterator subTransitions = subPort.linkedRelationList()
                            .iterator();

                    while (subTransitions.hasNext()) {
                        InterfaceAutomatonTransition subTransition = (InterfaceAutomatonTransition) subTransitions
                                .next();
                        String subLabel = subTransition.getLabel();

                        if (superLabel.equals(subLabel)) {
                            State subDestination = subTransition
                                    .destinationState();
                            StatePair newPair = new StatePair(superDestination,
                                    subDestination);

                            if (alternatingSimulation.contains(newPair)
                                    && (!reacheableSimulation.contains(newPair))) {
                                reacheableSimulation.add(newPair);
                                frontier.add(newPair);
                            }
                        }
                    }
                } else {
                    // internal transition in super automaton
                    StatePair newPair = new StatePair(superDestination,
                            subState);

                    if (alternatingSimulation.contains(newPair)
                            && (!reacheableSimulation.contains(newPair))) {
                        reacheableSimulation.add(newPair);
                        frontier.add(newPair);
                    }
                }
            }

            // explore internal transitions from subState
            ComponentPort subPort = subState.outgoingPort;
            Iterator subTransitions = subPort.linkedRelationList().iterator();

            while (subTransitions.hasNext()) {
                InterfaceAutomatonTransition subTransition = (InterfaceAutomatonTransition) subTransitions
                        .next();

                int transitionType = subTransition.getType();

                if ((transitionType == InterfaceAutomatonTransition._INTERNAL_TRANSITION)) {
                    State subDestination = subTransition.destinationState();
                    StatePair newPair = new StatePair(superState,
                            subDestination);

                    if (alternatingSimulation.contains(newPair)
                            && (!reacheableSimulation.contains(newPair))) {
View Full Code Here

    private State _addState(InterfaceAutomaton product, State stateInThis,
            State stateInArgument, HashMap frontier)
            throws IllegalActionException, NameDuplicationException {
        String name = stateInThis.getName() + NAME_CONNECTOR
                + stateInArgument.getName();
        State state = (State) product.getEntity(name);

        if (state == null) {
            // not in product
            state = new State(product, name);

            Triple triple = new Triple(state, stateInThis, stateInArgument);
            frontier.put(name, triple);
        }
View Full Code Here

                    + automaton.getName());

            HashMap frontier = new HashMap();

            // create initial state
            State stateInThis = this.getInitialState();
            State stateInArgument = automaton.getInitialState();
            String name = stateInThis.getName() + NAME_CONNECTOR
                    + stateInArgument.getName();
            State stateInProduct = new State(product, name);
            product.initialStateName.setExpression(name);

            Triple triple = new Triple(stateInProduct, stateInThis,
                    stateInArgument);
            frontier.put(name, triple);

            // iterate
            while (!frontier.isEmpty()) {
                // pick a state from frontier. It seems that there isn't an
                // easy way to pick an arbitrary entry from a HashMap, except
                // through Iterator
                Iterator iterator = frontier.keySet().iterator();
                name = (String) iterator.next();
                triple = (Triple) frontier.remove(name);
                stateInProduct = triple._stateInProduct;
                stateInThis = triple._stateInThis;
                stateInArgument = triple._stateInArgument;

                boolean isStateInProductIllegal = false;

                if ((!considerTransient)
                        || ((!_isTransient(stateInThis)) && (!_isTransient(stateInArgument)))) {
                    // extend frontier from state in this automaton
                    ComponentPort outPort = stateInThis.outgoingPort;
                    Iterator transitions = outPort.linkedRelationList()
                            .iterator();

                    while (transitions.hasNext() && !isStateInProductIllegal) {
                        InterfaceAutomatonTransition transition = (InterfaceAutomatonTransition) transitions
                                .next();

                        State destinationInThis = transition.destinationState();

                        // get transitionLabel, transitionName and relation name
                        // for later use
                        String transitionLabel = transition.getLabel();

                        // remove ending "?", "!", or ";"
                        String transitionName = transitionLabel.substring(0,
                                transitionLabel.length() - 1);

                        // switch depending on type of transition
                        int transitionType = transition.getType();

                        if (transitionType == InterfaceAutomatonTransition._INPUT_TRANSITION) {
                            // case 1
                            if (_inputNames.contains(transitionName)) {
                                // case 1A. Add transition to product as input
                                // transition
                                State destinationInProduct = _addState(product,
                                        destinationInThis, stateInArgument,
                                        frontier);
                                _addTransition(product, this.getName(),
                                        stateInProduct, destinationInProduct,
                                        transitionLabel);
                            } else {
                                // case 1B. transition is shared in product
                                String outName = transitionName + "!";
                                Set destinationsInArgument = _getDestinationStates(
                                        stateInArgument, outName);

                                if (!destinationsInArgument.isEmpty()) {
                                    // case 1Ba. q has T output. Add T to
                                    // product as internal transition
                                    Iterator destinations = destinationsInArgument
                                            .iterator();

                                    while (destinations.hasNext()) {
                                        State destinationInArgument = (State) destinations
                                                .next();
                                        State destinationInProduct = _addState(
                                                product, destinationInThis,
                                                destinationInArgument, frontier);
                                        _addTransition(product, this.getName()
                                                + NAME_CONNECTOR
                                                + automaton.getName(),
                                                stateInProduct,
                                                destinationInProduct,
                                                transitionName + ";");
                                    }
                                } else {
                                    // case 1Bb. q does not have T output.
                                    // Transition cannot happen, ignore.
                                }
                            }
                        } else if (transitionType == InterfaceAutomatonTransition._OUTPUT_TRANSITION) {
                            // case 2. T is output for p.
                            if (_outputNames.contains(transitionName)) {
                                // case 2A. T is output of product. Add T to
                                // product as output transition
                                State destinationInProduct = _addState(product,
                                        destinationInThis, stateInArgument,
                                        frontier);
                                _addTransition(product, this.getName(),
                                        stateInProduct, destinationInProduct,
                                        transitionLabel);
                            } else {
                                // case 2B. transition is shared in product
                                String inName = transitionName + "?";
                                Set destinationsInArgument = _getDestinationStates(
                                        stateInArgument, inName);

                                if (!destinationsInArgument.isEmpty()) {
                                    // case 2Ba. q has T input. Need to add T
                                    // to product as internal transition.
                                    // However, to avoid adding this transition
                                    // twice, leave the code that explores
                                    // state q to add the transition
                                } else {
                                    // case 2Bb. q does not have T input.
                                    // stateInProduct is illegal
                                    _illegalStates.add(stateInProduct);
                                    isStateInProductIllegal = true;
                                }
                            }
                        } else if (transitionType == InterfaceAutomatonTransition._INTERNAL_TRANSITION) {
                            // case 3. T is internal for p. Add T to product
                            State destinationInProduct = _addState(product,
                                    destinationInThis, stateInArgument,
                                    frontier);
                            _addTransition(product, this.getName(),
                                    stateInProduct, destinationInProduct,
                                    transitionLabel);
                        } else {
                            throw new InternalErrorException(
                                    "InterfaceAutomaton._computeProduct: "
                                            + "unrecognized transition type.");
                        }
                    } // end explore from state p

                    // extend frontier from state in the argument automaton
                    outPort = stateInArgument.outgoingPort;
                    transitions = outPort.linkedRelationList().iterator();

                    while (transitions.hasNext() && !isStateInProductIllegal) {
                        InterfaceAutomatonTransition transition = (InterfaceAutomatonTransition) transitions
                                .next();

                        State destinationInArgument = transition
                                .destinationState();

                        // get transitionLabel, transitionName and relation name
                        // for later use
                        String transitionLabel = transition.getLabel();

                        // remove ending "?", "!", or ";"
                        String transitionName = transitionLabel.substring(0,
                                transitionLabel.length() - 1);

                        // switch depending on type of transition
                        int transitionType = transition.getType();

                        if (transitionType == InterfaceAutomatonTransition._INPUT_TRANSITION) {
                            // case 1
                            if (_inputNames.contains(transitionName)) {
                                // case 1A. Add transition to product as input
                                // transition
                                State destinationInProduct = _addState(product,
                                        stateInThis, destinationInArgument,
                                        frontier);
                                _addTransition(product, automaton.getName(),
                                        stateInProduct, destinationInProduct,
                                        transitionLabel);
                            } else {
                                // case 1B. transition is shared in product
                                String outName = transitionName + "!";
                                Set destinationsInThis = _getDestinationStates(
                                        stateInThis, outName);

                                if (!destinationsInThis.isEmpty()) {
                                    // case 1Ba. p has T output. Add T to
                                    // product as internal transition
                                    Iterator destinations = destinationsInThis
                                            .iterator();

                                    while (destinations.hasNext()) {
                                        State destinationInThis = (State) destinations
                                                .next();
                                        State destinationInProduct = _addState(
                                                product, destinationInThis,
                                                destinationInArgument, frontier);
                                        _addTransition(product, this.getName()
                                                + NAME_CONNECTOR
                                                + automaton.getName(),
                                                stateInProduct,
                                                destinationInProduct,
                                                transitionName + ";");
                                    }
                                } else {
                                    // case 1Bb. p does not have T output.
                                    // Transition cannot happen, ignore.
                                }
                            }
                        } else if (transitionType == InterfaceAutomatonTransition._OUTPUT_TRANSITION) {
                            // case 2. T is output for q.
                            if (_outputNames.contains(transitionName)) {
                                // case 2A. T is output of product. Add T to
                                // product as output transition
                                State destinationInProduct = _addState(product,
                                        stateInThis, destinationInArgument,
                                        frontier);
                                _addTransition(product, automaton.getName(),
                                        stateInProduct, destinationInProduct,
                                        transitionLabel);
                            } else {
                                // case 2B. transition is shared in product
                                String inName = transitionName + "?";
                                Set destinationsInThis = _getDestinationStates(
                                        stateInThis, inName);

                                if (!destinationsInThis.isEmpty()) {
                                    // case 2Ba. p has T input. Need to add T
                                    // to product as internal transition.
                                    // However, to avoid adding this transition
                                    // twice, leave the code that explores
                                    // state p to add the transition
                                } else {
                                    // case 2Bb. p does not have T input.
                                    // stateInProduct is illegal
                                    _illegalStates.add(stateInProduct);
                                    isStateInProductIllegal = true;
                                }
                            }
                        } else if (transitionType == InterfaceAutomatonTransition._INTERNAL_TRANSITION) {
                            // case 3. T is internal for q. Add T to product
                            State destinationInProduct = _addState(product,
                                    stateInThis, destinationInArgument,
                                    frontier);
                            _addTransition(product, automaton.getName(),
                                    stateInProduct, destinationInProduct,
                                    transitionLabel);
                        } else {
                            throw new InternalErrorException(
                                    "InterfaceAutomaton._computeProduct: "
                                            + "unrecognized transition type.");
                        }
                    } // end explore from state q
                } else {
                    // one or both of stateInThis and stateInArgument are
                    // transient
                    if (_isTransient(stateInThis)
                            && _isTransient(stateInArgument)) {
                        throw new IllegalActionException("Cannot compute "
                                + "product since both states are transient: "
                                + stateInThis.getName() + " and "
                                + stateInArgument.getName());
                    }

                    if (_isTransient(stateInThis)) {
                        // extend frontier from transient state in this
                        // automaton
                        ComponentPort outPort = stateInThis.outgoingPort;
                        Iterator transitions = outPort.linkedRelationList()
                                .iterator();

                        while (transitions.hasNext()
                                && !isStateInProductIllegal) {
                            InterfaceAutomatonTransition transition = (InterfaceAutomatonTransition) transitions
                                    .next();

                            State destinationInThis = transition
                                    .destinationState();

                            // get transitionLabel, transitionName and
                            // relation name for later use
                            String transitionLabel = transition.getLabel();

                            // remove ending "?", "!", or ";"
                            String transitionName = transitionLabel.substring(
                                    0, transitionLabel.length() - 1);

                            // switch depending on type of transition
                            int transitionType = transition.getType();

                            if (transitionType == InterfaceAutomatonTransition._INPUT_TRANSITION) {
                                // case 1
                                throw new IllegalActionException("Transient "
                                        + "state " + stateInThis.getName()
                                        + " in " + getName() + " has input "
                                        + "transition " + transitionLabel);
                            } else if (transitionType == InterfaceAutomatonTransition._OUTPUT_TRANSITION) {
                                // case 2. T is output for p.
                                if (_outputNames.contains(transitionName)) {
                                    // case 2A. T is output of product. Add T
                                    // to product as output transition
                                    State destinationInProduct = _addState(
                                            product, destinationInThis,
                                            stateInArgument, frontier);
                                    _addTransition(product, this.getName(),
                                            stateInProduct,
                                            destinationInProduct,
                                            transitionLabel);
                                } else {
                                    // case 2B. transition is shared in product
                                    String inName = transitionName + "?";
                                    Set destinationsInArgument = _getDestinationStates(
                                            stateInArgument, inName);

                                    if (!destinationsInArgument.isEmpty()) {
                                        // case 2Ba. q has T input. Add T to
                                        // product as internal transition.
                                        Iterator destinations = destinationsInArgument
                                                .iterator();

                                        while (destinations.hasNext()) {
                                            State destinationInArgument = (State) destinations
                                                    .next();
                                            State destinationInProduct = _addState(
                                                    product, destinationInThis,
                                                    destinationInArgument,
                                                    frontier);
                                            _addTransition(product, this
                                                    .getName()
                                                    + NAME_CONNECTOR
                                                    + automaton.getName(),
                                                    stateInProduct,
                                                    destinationInProduct,
                                                    transitionName + ";");
                                        }
                                    } else {
                                        // case 2Bb. q does not have T input.
                                        // stateInProduct is illegal
                                        _illegalStates.add(stateInProduct);
                                        isStateInProductIllegal = true;
                                    }
                                }
                            } else if (transitionType == InterfaceAutomatonTransition._INTERNAL_TRANSITION) {
                                // case 3. T is internal for p. Add T to product
                                State destinationInProduct = _addState(product,
                                        destinationInThis, stateInArgument,
                                        frontier);
                                _addTransition(product, this.getName(),
                                        stateInProduct, destinationInProduct,
                                        transitionLabel);
                            } else {
                                throw new InternalErrorException(
                                        "InterfaceAutomaton._computeProduct: "
                                                + "unrecognized transition type.");
                            }
                        } // end explore from transient state p
                    } else {
                        // stateInArgument is transient.
                        // extend frontier from state in the argument automaton
                        ComponentPort outPort = stateInArgument.outgoingPort;
                        Iterator transitions = outPort.linkedRelationList()
                                .iterator();

                        while (transitions.hasNext()
                                && !isStateInProductIllegal) {
                            InterfaceAutomatonTransition transition = (InterfaceAutomatonTransition) transitions
                                    .next();

                            State destinationInArgument = transition
                                    .destinationState();

                            // get transitionLabel, transitionName and
                            // relation name for later use
                            String transitionLabel = transition.getLabel();

                            // remove ending "?", "!", or ";"
                            String transitionName = transitionLabel.substring(
                                    0, transitionLabel.length() - 1);

                            // switch depending on type of transition
                            int transitionType = transition.getType();

                            if (transitionType == InterfaceAutomatonTransition._INPUT_TRANSITION) {
                                throw new IllegalActionException("Transient "
                                        + "state " + stateInArgument.getName()
                                        + " in " + automaton.getName()
                                        + " has input transition "
                                        + transitionLabel);
                            } else if (transitionType == InterfaceAutomatonTransition._OUTPUT_TRANSITION) {
                                // case 2. T is output for q.
                                if (_outputNames.contains(transitionName)) {
                                    // case 2A. T is output of product. Add T to
                                    // product as output transition
                                    State destinationInProduct = _addState(
                                            product, stateInThis,
                                            destinationInArgument, frontier);
                                    _addTransition(product,
                                            automaton.getName(),
                                            stateInProduct,
                                            destinationInProduct,
                                            transitionLabel);
                                } else {
                                    // case 2B. transition is shared in product
                                    String inName = transitionName + "?";
                                    Set destinationsInThis = _getDestinationStates(
                                            stateInThis, inName);

                                    if (!destinationsInThis.isEmpty()) {
                                        // case 2Ba. p has T input. Add T
                                        // to product as internal transition.
                                        Iterator destinations = destinationsInThis
                                                .iterator();

                                        while (destinations.hasNext()) {
                                            State destinationInThis = (State) destinations
                                                    .next();
                                            State destinationInProduct = _addState(
                                                    product, destinationInThis,
                                                    destinationInArgument,
                                                    frontier);
                                            _addTransition(product, this
                                                    .getName()
                                                    + NAME_CONNECTOR
                                                    + automaton.getName(),
                                                    stateInProduct,
                                                    destinationInProduct,
                                                    transitionName + ";");
                                        }
                                    } else {
                                        // case 2Bb. p does not have T input.
                                        // stateInProduct is illegal
                                        _illegalStates.add(stateInProduct);
                                        isStateInProductIllegal = true;
                                    }
                                }
                            } else if (transitionType == InterfaceAutomatonTransition._INTERNAL_TRANSITION) {
                                // case 3. T is internal for q. Add T to product
                                State destinationInProduct = _addState(product,
                                        stateInThis, destinationInArgument,
                                        frontier);
                                _addTransition(product, automaton.getName(),
                                        stateInProduct, destinationInProduct,
                                        transitionLabel);
View Full Code Here

TOP

Related Classes of ptolemy.domains.fsm.kernel.State

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.