Package ptolemy.codegen.kernel

Examples of ptolemy.codegen.kernel.ParseTreeCodeGenerator


            // generate code for guard expression
            String guard = transition.getGuardExpression();
            PtParser parser = new PtParser();
            ASTPtRootNode guardParseTree = parser.generateParseTree(guard);
            ParseTreeCodeGenerator parseTreeCodeGenerator = controllerHelper
                    .getParseTreeCodeGenerator();
            parseTreeCodeGenerator.evaluateParseTree(guardParseTree,
                    controllerHelper._scope);
            codeBuffer.append(parseTreeCodeGenerator.generateFireCode());
            codeBuffer.append(") {" + _eol);

            // generate code for commit action
            Iterator actions = transition.commitActionList().iterator();
            while (actions.hasNext()) {
                AbstractActionsAttribute action = (AbstractActionsAttribute) actions
                        .next();
                Iterator destinationNameList = action.getDestinationNameList()
                        .iterator();

                while (destinationNameList.hasNext()) {
                    String destinationName = (String) destinationNameList
                            .next();
                    NamedObj destination = action
                            .getDestination(destinationName);
                    ASTPtRootNode parseTree = action
                            .getParseTree(destinationName);
                    if (destination instanceof Variable) {
                        codeBuffer.append(_codeGenerator
                                .generateVariableName((Variable) destination)
                                + " = ");
                    } else {
                        throw new IllegalActionException("No output can be"
                                + " produced in any action for "
                                + "MultirateFSMDirector.");
                    }
                    parseTreeCodeGenerator = controllerHelper
                            .getParseTreeCodeGenerator();
                    parseTreeCodeGenerator.evaluateParseTree(parseTree,
                            controllerHelper._scope);
                    codeBuffer
                            .append(parseTreeCodeGenerator.generateFireCode());
                    codeBuffer.append(";" + _eol);
                }
            }

            // All contained actors must be initialized before the
View Full Code Here


                } 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);
                    }
                }
View Full Code Here

TOP

Related Classes of ptolemy.codegen.kernel.ParseTreeCodeGenerator

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.