⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smcobjcgenerator.java

📁 SMC takes a state machine stored in a .sm file and generates a State pattern in twelve programming l
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                // If -g was specified, then set the transition                // name so it can be printed out.                if (Smc.isDebug() == true)                {                    _source.print(_indent);                    _source.print("    [self setTransition:@\"");                    _source.print(trans.getName());                    _source.println("\"];");                }                _source.print(_indent);                _source.print("    [[self state] ");                _source.print(trans.getName());                _source.print(":self");                for (pit = params.iterator();                     pit.hasNext() == true;                    )                {                    _source.print(" :");                    _source.print((pit.next()).getName());                }                _source.println("];");                if (Smc.isDebug() == true)                {                    _source.print(_indent);                    _source.println(                        "    [self setTransition:nil];");                }                _source.print(_indent);                _source.println("}");            }        }        // End the class implementation        _source.print(_indent);        _source.println("@end");        return;    } // end of visit(SmcFSM)    public void visit(SmcMap map)    {        // Print out the default state class        _source.println();        _source.print(_indent);        _source.print("@implementation ");        _source.print(map.getName());        _source.println("_Default");        // Declare the user-defined default transitions first.        if (map.hasDefaultState() == true)        {            SmcState defaultState = map.getDefaultState();            for (SmcTransition transition:                     defaultState.getTransitions())            {                transition.accept(this);            }        }        _source.println("@end");        // Have each state now generate its code.        for (SmcState state: map.getStates())        {            state.accept(this);        }        return;    } // end of visit(SmcMap)    public void visit(SmcState state)    {        SmcMap map = state.getMap();        String context = map.getFSM().getContext();        String mapName = map.getName();        String className = state.getClassName();        String indent2;        List<SmcAction> actions;        _source.print(_indent);        _source.print("@implementation ");        _source.print(mapName);        _source.print("_");        _source.println(className);        // Add the Entry() and Exit() methods if this state        // defines them.        actions = state.getEntryActions();        if (actions != null && actions.size() > 0)        {            _source.print(_indent);            _source.print("- (void)Entry:(");            _source.print(context);            _source.println("Context*)context;");            _source.println();            _source.println("{");            // Declare the "ctxt" local variable.            _source.print(_indent);            _source.print("    ");            _source.print(context);            _source.println(" *ctxt = [context owner];");            _source.println();            // Generate the actions associated with this code.            indent2 = _indent;            _indent = _indent + "    ";            for (SmcAction action: actions)            {                action.accept(this);            }            _indent = indent2;            // End the Entry() method with a return.            _source.print(_indent);            _source.println("}");        }        actions = state.getExitActions();        if (actions != null && actions.size() > 0)        {            _source.print(_indent);            _source.print("- (void)Exit:(");            _source.print(context);            _source.println("Context*)context;");            _source.println();            _source.println("{");            // Declare the "ctxt" local variable.            _source.print(_indent);            _source.print("    ");            _source.print(context);            _source.println(" *ctxt = [context owner];");            _source.println();            // Generate the actions associated with this code.            indent2 = _indent;            _indent = _indent + "    ";            for (SmcAction action: actions)            {                action.accept(this);            }            _indent = indent2;            _source.print(_indent);            _source.println("}");        }        // Have the transitions generate their code.        for (SmcTransition transition: state.getTransitions())        {            transition.accept(this);        }        _source.print(_indent);        _source.println("@end");        _source.println();        return;    } // end of visit(SmcState)    public void visit(SmcTransition transition)    {        SmcState state = transition.getState();        SmcMap map = state.getMap();        String context = map.getFSM().getContext();        String mapName = map.getName();        String stateName = state.getClassName();        String transName = transition.getName();        boolean defaultFlag = false;        boolean nullCondition = false;        List<SmcGuard> guards = transition.getGuards();        Iterator<SmcGuard> git;        SmcGuard guard;        String fqStateName;        // Set a flag to denote if this is a Default state        // transition.        if (stateName.equals("Default") == true)        {            defaultFlag = true;        }        // Qualify the state name as well.        if (stateName.indexOf("::") < 0)        {            fqStateName = "[" + mapName + " " + stateName + "]";        }        else        {            fqStateName = stateName;        }        _source.println();        _source.print(_indent);        _source.print("- (void)");        _source.print(transName);        _source.print(":(");        _source.print(context);        _source.print("Context*)context");        // Add user-defined parameters.        for (SmcParameter param: transition.getParameters())        {            _source.print(" :");            param.accept(this);        }        _source.println(";");        _source.print(_indent);        _source.println("{");        // All transitions have a "ctxt" local variable.        // 8/14/2003:        // Do this only if there are any transition actions or        // guard conditions which reference it.        if (transition.hasCtxtReference() == true)        {            _source.print(_indent);            _source.print("    ");            _source.print(context);            _source.println(" *ctxt = [context owner];");        }        if (defaultFlag == true)        {            _source.print(_indent);            _source.println("    BOOL loopbackFlag = NO;");        }        // Print the transition to the verbose log.        if (Smc.isDebug() == true)        {            Iterator<SmcParameter> pit;            String sep;            _source.print(_indent);            _source.println("    if ( [context debugFlag] )");            _source.print(_indent);            _source.println("    {");            _source.print(_indent);            _source.print("        TRACE(@\"TRANSITION   : ");            _source.print(mapName);            _source.print(" ");            _source.print(transName);            _source.print("(");            for (pit = transition.getParameters().iterator(),                     sep = "";                 pit.hasNext() == true;                 sep = " ")            {                _source.print(sep);                (pit.next()).accept(this);            }            _source.println(")\\n\\r\");");            _source.print(_indent);            _source.println("    }");        }        // Loop through the guards and print each one.        for (git = guards.iterator(),                     _guardIndex = 0,                     _guardCount = guards.size();             git.hasNext() == true;             ++_guardIndex)        {            guard = git.next();            // Count up the number of guards with no condition.            if (guard.getCondition().length() == 0)            {                nullCondition = true;            }            guard.accept(this);        }        // If all guards have a condition, then create a final        // "else" clause which passes control to the default        // transition.        if (_guardIndex > 0 && nullCondition == false)        {            // If there is only one transition definition, then            // close off the guard.            if (_guardCount == 1)            {                _source.print(_indent);                _source.println("    }");            }            _source.print(_indent);            _source.println("    else");            _source.print(_indent);            _source.println("    {");            _source.print(_indent);            _source.print("         ");            _source.print(mapName);            _source.print("_Default::");            _source.print(transName);            _source.print("(context");            // Output user-defined parameters.            for (SmcParameter param: transition.getParameters())            {                _source.print(", ");                _source.print(param.getName());            }            _source.println(");");            _source.print(_indent);            _source.println("    }");        }        else if (_guardCount > 1)        {            _source.println();        }        _source.print(_indent);        _source.println("}");        return;    } // end of visit(SmcTransition)    public void visit(SmcGuard guard)    {        SmcTransition transition = guard.getTransition();        SmcState state = transition.getState();        SmcMap map = state.getMap();        String context = map.getFSM().getContext();        String mapName = map.getName();        String stateName = state.getClassName();        int transType = guard.getTransType();        boolean defaultFlag =            stateName.equalsIgnoreCase("Default");        boolean loopbackFlag = false;        String indent2;        String indent3;        String indent4 = "";        String endStateName = guard.getEndState();        String fqEndStateName = "";        String pushStateName = guard.getPushState();        String condition = guard.getCondition();        List<SmcAction> actions = guard.getActions();        // If this guard's end state is not of the form        // "map::state", then prepend the map name to the state        // name.        // DON'T DO THIS IF THIS IS A POP TRANSITION!        // The "state" is actually a transition name.        if (transType != Smc.TRANS_POP &&            endStateName.length () > 0 &&            endStateName.equals("nil") == false &&            endStateName.indexOf("::") < 0)        {            endStateName = mapName + "::" + endStateName;        }        // Convert the SMC scope syntex to the Objective-C        // syntax.        if (endStateName.indexOf("::") >= 0)        {            endStateName = convertScope(endStateName);        }        // Qualify the state name as well.        if (stateName.indexOf("::") >= 0)        {            stateName = convertScope(stateName);        }        loopbackFlag =            isLoopback(transType, stateName, endStateName);        // v. 2.0.2: If the push state is not fully-qualified,        // then prepend the current map's name and make if        // fully-qualified.        if (pushStateName != null &&            pushStateName.length() > 0 &&            pushStateName.indexOf("::") < 0)        {            pushStateName =                "[" + mapName + " " + pushStateName + "]";        }        if (pushStateName.indexOf("::") >= 0)        {            pushStateName = convertScope(pushStateName);        }        // The guard code generation is a bit tricky. The first        // question is how many guards are there? If there are        // more than one, then we will need to generate the        // proper "if-then-else" code.        if (_guardCount > 1)        {            indent2 = _indent + "        ";            // More than one guard. Is this the first guard?            if (_guardIndex == 0 && condition.length() > 0)            {                // Yes, this is the first. This means an                // "if" should be used for this condition.                _source.print(_indent);                _source.print("    if (");                _source.print(condition);                _source.println(")");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -