Package ptolemy.domains.fsm.kernel

Examples of ptolemy.domains.fsm.kernel.State


            // an destination in the super automaton such that the two
            // destinations are in the current alternating simulation relation.
            Iterator subStates = destinationsInSub.iterator();

            while (subStates.hasNext()) {
                State destinationInSub = (State) subStates.next();
                boolean inCurrentSimulation = false;
                Iterator superStates = destinationsInSuper.iterator();

                while (superStates.hasNext()) {
                    State destinationInSuper = (State) superStates.next();
                    StatePair pair = new StatePair(destinationInSuper,
                            destinationInSub);

                    if (currentSimulation.contains(pair)) {
                        inCurrentSimulation = true;
View Full Code Here


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

            State current = (State) iterator.next();
            frontier.remove(current);

            // make all states that can reach current by output or internal
            // transitions illegal
            ComponentPort inPort = current.incomingPort;
            Iterator transitions = inPort.linkedRelationList().iterator();

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

                if ((transitionType == InterfaceAutomatonTransition._OUTPUT_TRANSITION)
                        || (transitionType == InterfaceAutomatonTransition._INTERNAL_TRANSITION)) {
                    State sourceState = transition.sourceState();

                    if (!_illegalStates.contains(sourceState)) {
                        _illegalStates.add(sourceState);
                        frontier.add(sourceState);
                    }
                }
            }
        }

        // remove all illegalStates from automaton
        iterator = _illegalStates.iterator();

        while (iterator.hasNext()) {
            State state = (State) iterator.next();
            _removeStateAndTransitions(state);
        }
    }
View Full Code Here

    //          end when frontier is empty
    //
    // remove all states not in reacheableStates from this automaton
    private void _removeUnreacheableStates() {
        // init
        State initialState;

        try {
            initialState = getInitialState();
        } catch (IllegalActionException exception) {
            // initial state was removed since it was illegal. remove all
            // states from this automaton to make it empty.
            this.removeAllRelations();
            this.removeAllEntities();
            return;
        }

        Set reacheableStates = new HashSet();
        Set frontier = new HashSet();
        reacheableStates.add(initialState);
        frontier.add(initialState);

        // 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);

            // make all states that are reacheable from current reacheable
            ComponentPort outPort = current.outgoingPort;
            Iterator transitions = outPort.linkedRelationList().iterator();

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

                if (!reacheableStates.contains(destinationState)) {
                    reacheableStates.add(destinationState);
                    frontier.add(destinationState);
                }
            }
        }

        // remove all states not reacheable from initial state
        List states = entityList();

        // the method removeAll() is not supported by this List (it throws
        // UnsupportedOperationException), so manually construct the set
        // of unreacheable states.
        // states.removeAll(reacheableStates);
        Set unreacheableStates = new HashSet();
        Iterator iterator = states.iterator();

        while (iterator.hasNext()) {
            Object state = iterator.next();

            if (!reacheableStates.contains(state)) {
                unreacheableStates.add(state);
            }
        }

        iterator = unreacheableStates.iterator();

        while (iterator.hasNext()) {
            State state = (State) iterator.next();
            _removeStateAndTransitions(state);
        }
    }
View Full Code Here

                        boolean removeIt = true;
                        Iterator states = master.entityList(State.class)
                                .iterator();

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

                            if (state == deleteObj) {
                                continue;
                            }

                            TypedActor[] stateRefinements = null;

                            try {
                                stateRefinements = state.getRefinement();
                            } catch (IllegalActionException ex) {
                                // Ignore, no refinement to check
                            }

                            if (stateRefinements == null) {
View Full Code Here

            code.append("case " + stateCount + ":" + _eol);
            stateCount++;

            depth++;

            State state = (State) states.next();
            Actor[] actors = state.getRefinement();

            if (actors != null) {
                for (int i = 0; i < actors.length; i++) {
                    CodeGeneratorHelper actorHelper = (CodeGeneratorHelper) _getHelper((NamedObj) actors[i]);
View Full Code Here

    public String generateInitializeCode() throws IllegalActionException {
        StringBuffer codeBuffer = new StringBuffer();
        codeBuffer.append(super.generateInitializeCode());

        ptolemy.domains.fsm.kernel.FSMActor fsmActor = (ptolemy.domains.fsm.kernel.FSMActor) getComponent();
        State initialState = fsmActor.getInitialState();

        _updateCurrentState(codeBuffer, initialState, 0);

        return processCode(codeBuffer.toString());
    }
View Full Code Here

            codeBuffer.append(_getIndentPrefix(depth));
            // For each state...
            codeBuffer.append("case " + stateCount + ":" + _eol);
            stateCount++;

            State state = (State) states.next();

            // The transitions (all, preemptive or non-preemptive
            // depending on the instance of TransitionRetriever given)
            // that need to be tried.
            Iterator transitions = transitionRetriever
                    .retrieveTransitions(state);
            // Reorder transitions so that default transitions are at
            // the end of the list.
            List reOrderedTransitions = new LinkedList();
            List defaultTransitions = new LinkedList();
            while (transitions.hasNext()) {
                Transition transition = (Transition) transitions.next();
                if (transition.getName().equals("default")) {
                    defaultTransitions.add(transition);
                } else {
                    reOrderedTransitions.add(transition);
                }
            }
            reOrderedTransitions.addAll(defaultTransitions);
            transitions = reOrderedTransitions.iterator();

            int transitionCount = 0;
            depth++;

            while (transitions.hasNext()) {
                if (transitionCount == 0) {
                    codeBuffer.append(_getIndentPrefix(depth));
                    codeBuffer.append("if (");
                } else {
                    codeBuffer.append("else if (");
                }
                transitionCount++;

                Transition transition = (Transition) transitions.next();
                // generate code for guard expression
                if (transition.getName().equals("default")) {
                    codeBuffer.append("true");
                } else {
                    String guard = transition.getGuardExpression();
                    PtParser parser = new PtParser();
                    ASTPtRootNode guardParseTree = parser
                            .generateParseTree(guard);
                    ParseTreeCodeGenerator parseTreeCodeGenerator = getParseTreeCodeGenerator();
                    parseTreeCodeGenerator.evaluateParseTree(guardParseTree,
                            _scope);
                    codeBuffer
                            .append(parseTreeCodeGenerator.generateFireCode());
                }
                codeBuffer.append(") {" + _eol);

                depth++;

                // generate code for choice action
                Iterator actions = transition.choiceActionList().iterator();

                while (actions.hasNext()) {
                    AbstractActionsAttribute action = (AbstractActionsAttribute) actions
                            .next();
                    Iterator destinationNameList = action
                            .getDestinationNameList().iterator();
                    Iterator channelNumberList = action.getChannelNumberList()
                            .iterator();
                    Iterator parseTreeList = action.getParseTreeList()
                            .iterator();

                    while (destinationNameList.hasNext()) {
                        String destinationName = (String) destinationNameList
                                .next();
                        Integer channelNumber = (Integer) channelNumberList
                                .next();
                        ASTPtRootNode parseTree = (ASTPtRootNode) parseTreeList
                                .next();
                        NamedObj destination = action
                                .getDestination(destinationName);

                        int channel = -1;
                        if (channelNumber != null) {
                            channel = channelNumber.intValue();
                        }

                        codeBuffer.append(_getIndentPrefix(depth));

                        // Note in choice action only output can be generated
                        // and no parameter be changed.
                        if (channel >= 0) {
                            codeBuffer.append("$ref(" + destinationName + "#"
                                    + channel + ") = ");

                            // During choice action, an output port
                            // receives token sent by itself when it
                            // is also an input port, i.e., when this
                            // FSMActor is used as a modal controller.

                            if (((IOPort) destination).isInput()) {
                                ComponentCodeGenerator containerHelper = _getHelper(((IOPort) destination)
                                        .getContainer().getContainer());
                                StringBuffer containerReference = new StringBuffer();

                                containerReference.append("$ref("
                                        + destination.getName());

                                if (((IOPort) destination).isMultiport()) {
                                    containerReference.append("#" + channel);
                                }

                                containerReference.append(")");

                                codeBuffer
                                        .append(((CodeGeneratorHelper) containerHelper)
                                                .processCode(containerReference
                                                        .toString())
                                                + " = ");
                            }
                        } else { // broadcast

                            int width = ((IOPort) action
                                    .getDestination(destinationName))
                                    .getWidth();

                            for (int i = 0; i < width; i++) {
                                codeBuffer.append("$ref(" + destinationName
                                        + "#" + i + ") = ");

                                // During choice action, an output
                                // port receives token sent by itself
                                // when it is also an input port,
                                // i.e., when this FSMActor is used as
                                // a modal controller.

                                if (((IOPort) destination).isInput()) {
                                    ComponentCodeGenerator containerHelper = _getHelper(((IOPort) destination)
                                            .getContainer().getContainer());
                                    StringBuffer containerReference = new StringBuffer();

                                    containerReference.append("$ref("
                                            + destination.getName());

                                    if (((IOPort) destination).isMultiport()) {
                                        containerReference.append("#" + i);
                                    }

                                    containerReference.append(")");

                                    codeBuffer
                                            .append(((CodeGeneratorHelper) containerHelper)
                                                    .processCode(containerReference
                                                            .toString())
                                                    + " = ");
                                }
                            }
                        }

                        ParseTreeCodeGenerator parseTreeCodeGenerator = getParseTreeCodeGenerator();
                        parseTreeCodeGenerator.evaluateParseTree(parseTree,
                                _scope);
                        codeBuffer.append(parseTreeCodeGenerator
                                .generateFireCode());
                        codeBuffer.append(";" + _eol);
                    }
                }

                boolean inline = ((BooleanToken) _codeGenerator.inline
                        .getToken()).booleanValue();

                // generate code for transition refinement
                Actor[] actors = transition.getRefinement();

                if (actors != null) {
                    for (int i = 0; i < actors.length; i++) {
                        CodeGeneratorHelper helper = (CodeGeneratorHelper) _getHelper((NamedObj) actors[i]);
                        // fire the actor
                        if (inline) {
                            codeBuffer.append(helper.generateFireCode());
                            codeBuffer.append(helper
                                    .generateTypeConvertFireCode());
                        } else {
                            codeBuffer
                                    .append(generateName((NamedObj) actors[i])
                                            + "();" + _eol);
                        }
                    }
                }

                // generate code for commit action
                actions = transition.commitActionList().iterator();

                while (actions.hasNext()) {
                    AbstractActionsAttribute action = (AbstractActionsAttribute) actions
                            .next();
                    Iterator destinationNameList = action
                            .getDestinationNameList().iterator();
                    Iterator channelNumberList = action.getChannelNumberList()
                            .iterator();
                    Iterator parseTreeList = action.getParseTreeList()
                            .iterator();

                    while (destinationNameList.hasNext()) {
                        String destinationName = (String) destinationNameList
                                .next();
                        Integer channelNumber = (Integer) channelNumberList
                                .next();
                        ASTPtRootNode parseTree = (ASTPtRootNode) parseTreeList
                                .next();
                        NamedObj destination = action
                                .getDestination(destinationName);

                        int channel = -1;
                        if (channelNumber != null) {
                            channel = channelNumber.intValue();
                        }

                        codeBuffer.append(_getIndentPrefix(depth));

                        if (destination instanceof IOPort) {
                            if (channel >= 0) {
                                codeBuffer.append("$ref(" + destinationName
                                        + "#" + channel + ") = ");
                            } else { // broadcast

                                int width = ((IOPort) action
                                        .getDestination(destinationName))
                                        .getWidth();

                                for (int i = 0; i < width; i++) {
                                    codeBuffer.append("$ref(" + destinationName
                                            + "#" + i + ") = ");
                                }
                            }
                        } else if (destination instanceof Variable) {
                            codeBuffer
                                    .append(_codeGenerator
                                            .generateVariableName((Variable) destination)
                                            + " = ");
                        }

                        ParseTreeCodeGenerator parseTreeCodeGenerator = getParseTreeCodeGenerator();
                        parseTreeCodeGenerator.evaluateParseTree(parseTree,
                                _scope);
                        codeBuffer.append(parseTreeCodeGenerator
                                .generateFireCode());
                        codeBuffer.append(";" + _eol);
                    }
                }

                // generate code for updating current state
                State destinationState = transition.destinationState();
                _updateCurrentState(codeBuffer, destinationState, depth);

                // generate code for reinitialization if reset is
                // true.  we assume the value of reset itself cannot
                // be changed dynamically

                BooleanToken resetToken = (BooleanToken) transition.reset
                        .getToken();
                if (resetToken.booleanValue()) {
                    actors = destinationState.getRefinement();

                    if (actors != null) {
                        for (int i = 0; i < actors.length; ++i) {
                            ActorCodeGenerator helper = (ActorCodeGenerator) _getHelper((NamedObj) actors[i]);
                            codeBuffer.append(helper.generateInitializeCode());
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.