Skip to content
Snippets Groups Projects
Avatar2SysML.java 48.6 KiB
Newer Older
Sophie Coudert's avatar
Sophie Coudert committed
/* Copyright or (C) or Copr. GET / ENST, Telecom-Paris, Ludovic Apvrille
 *
 * ludovic.apvrille AT enst.fr
 *
 * This software is a computer program whose purpose is to allow the
 * edition of TURTLE analysis, design and deployment diagrams, to
 * allow the generation of RT-LOTOS or Java code from this diagram,
 * and at last to allow the analysis of formal validation traces
 * obtained from external tools, e.g. RTL from LAAS-CNRS and CADP
 * from INRIA Rhone-Alpes.
 *
 * This software is governed by the CeCILL  license under French law and
 * abiding by the rules of distribution of free software.  You can  use,
 * modify and/ or redistribute the software under the terms of the CeCILL
 * license as circulated by CEA, CNRS and INRIA at the following URL
 * "http://www.cecill.info".
 *
 * As a counterpart to the access to the source code and  rights to copy,
 * modify and redistribute granted by the license, users are provided only
 * with a limited warranty  and the software's author,  the holder of the
 * economic rights,  and the successive licensors  have only  limited
 * liability.
 *
 * In this respect, the user's attention is drawn to the risks associated
 * with loading,  using,  modifying and/or developing or reproducing the
 * software by the user in light of its specific status of free software,
 * that may mean  that it is complicated to manipulate,  and  that  also
 * therefore means  that it is reserved for developers  and  experienced
 * professionals having in-depth computer knowledge. Users are therefore
 * encouraged to load and test the software's suitability as regards their
 * requirements in conditions enabling the security of their systems and/or
 * data to be ensured and,  more generally, to use and operate it in the
 * same conditions as regards security.
 *
 * The fact that you are presently reading this means that you have had
 * knowledge of the CeCILL license and that you accept its terms.
 */
package avatartranslator.tosysmlv2;

import avatartranslator.*;

import static avatartranslator.AvatarTransition.*;
Sophie Coudert's avatar
Sophie Coudert committed
import static avatartranslator.tosysmlv2.Avatar2SysMLNames.*;
import java.util.HashMap;
import java.util.HashSet;
Sophie Coudert's avatar
Sophie Coudert committed
import java.util.List;
import java.util.ArrayList;



public class Avatar2SysML {

    /**
     * Memorized while declaring channels, to be reused for generating states
     */
    class SignalInfo {
        private String name;
        private String msgtype;
        private ArrayList<String> profile;
       SignalInfo(String _name, String _msgtype) {
            name = _name;
            msgtype = _msgtype;
            profile = new ArrayList<String>();
        }
        public void addField(String _fieldName){ profile.add(_fieldName); }
        public String getName(){ return name; }
        public String getMessageType(){ return msgtype; }
        public List<String> getProfile(){ return profile; }
    }
Sophie Coudert's avatar
Sophie Coudert committed
   /**
     * Memorized while naming states, to be reused for generating states
     */
    class StateInfo {
Sophie Coudert's avatar
Sophie Coudert committed
       private String name;
       private String prename = "";
        StateInfo(String _name) {
            name = _name;
        }
        StateInfo(String _name, String _prename) {
Sophie Coudert's avatar
Sophie Coudert committed
            name = _name;
Sophie Coudert's avatar
Sophie Coudert committed
            prename = _prename;
Sophie Coudert's avatar
Sophie Coudert committed
        }
        public String getName(){ return name; }
Sophie Coudert's avatar
Sophie Coudert committed
        public String getPreName(){ return prename; }
Sophie Coudert's avatar
Sophie Coudert committed
    }

    public Avatar2SysML(AvatarSpecification _avspec) { avspec = _avspec; }
    private AvatarSpecification avspec;
    StringBuffer avsysml;

    HashSet<AvatarSignal> fifoSet = new HashSet<AvatarSignal>();
    HashMap<AvatarSignal, SignalInfo> signalMap = new HashMap<AvatarSignal, SignalInfo>();
Sophie Coudert's avatar
Sophie Coudert committed
    HashMap<AvatarStateMachineElement, StateInfo> stateMap = new HashMap<AvatarStateMachineElement, StateInfo>();
    ArrayList<AvatarStateMachineElement> stateList = new ArrayList<AvatarStateMachineElement>();
    ArrayList<AvatarAttribute> timerList = new ArrayList<AvatarAttribute>();
Sophie Coudert's avatar
Sophie Coudert committed

    void searcCountSignals() {
        fifoSet.clear();
        for(AvatarBlock block: avspec.getListOfBlocks()) {
            for(AvatarStateMachineElement sme : block.getStateMachine().getListOfElements()) {
                    if(sme instanceof AvatarQueryOnSignal)
                        fifoSet.add(((AvatarQueryOnSignal) sme).getSignal());
            }
        }
    }
Sophie Coudert's avatar
Sophie Coudert committed
    ArrayList<String> blockChain = new ArrayList<String>();
    void chainBlock(String _block) { blockChain.add(_block); }
    void unchainBlock() { if(blockChain.size() > 0) blockChain.remove(blockChain.size() - 1); }
    void blocklink() {
Sophie Coudert's avatar
Sophie Coudert committed
        int size = blockChain.size();
        if (size < 2) return;
        StringBuffer chain = new StringBuffer(indentStep + "part " + blockChain.get(size - 1) + " = ");
Sophie Coudert's avatar
Sophie Coudert committed
        for (int i=0; i < size-1; i++){
            chain.append(blockChain.get(i) + ".");
        }
        chain.append(blockChain.get(size - 1) + ";\n");
        blockLinks.append(chain);
Sophie Coudert's avatar
Sophie Coudert committed
    }
    StringBuffer blockLinks = new StringBuffer();

Sophie Coudert's avatar
Sophie Coudert committed
    String indentStep = "    ";
    int indentStepSize = 4;
    String indent = "";
Sophie Coudert's avatar
Sophie Coudert committed
    String header = "package AvatarInstance {\n" +
        indentStep + "import ScalarValues::String;\n" +
Sophie Coudert's avatar
Sophie Coudert committed
        indentStep + "import ScalarValues::Positive;\n" +
        indentStep + "import ScalarValues::Integer;\n" +
        indentStep + "import ScalarValues::Boolean;\n" +
        indentStep + "import ScalarValues::Real;\n" +
Sophie Coudert's avatar
Sophie Coudert committed
        indentStep + "import AvatarBlockTypes::*;\n" +
        indentStep + "import AvatarCommunication::*;\n";

Sophie Coudert's avatar
Sophie Coudert committed

    StringBuffer avatar2SysML() {
Sophie Coudert's avatar
Sophie Coudert committed
        signalMap.clear();
        stateMap.clear();
        stateList.clear();
Sophie Coudert's avatar
Sophie Coudert committed
        blockChain.clear();
        blockLinks = new StringBuffer(indentStep + "// Block Shortcut Links $$$$$$$$$$$$\n");
Sophie Coudert's avatar
Sophie Coudert committed
        avsysml = new StringBuffer(header);
Sophie Coudert's avatar
Sophie Coudert committed
        indent += indentStep;
Sophie Coudert's avatar
Sophie Coudert committed
        searcCountSignals();;
Sophie Coudert's avatar
Sophie Coudert committed
        communications2SysML();
        blocks2SysML();
Sophie Coudert's avatar
Sophie Coudert committed
        avsysml.append(blockLinks);
Sophie Coudert's avatar
Sophie Coudert committed
        return avsysml;
Sophie Coudert's avatar
Sophie Coudert committed
    }
    void communications2SysML(){
        int nbfiforelNumber = 0;
        int bfiforelNumber = 0;
        int syncrelNumber = 0;

        if (avspec.getRelations() == null || avspec.getRelations().size() == 0) return;
Sophie Coudert's avatar
Sophie Coudert committed

        avsysml.append("\n" + indent + "// COMMUNICATIONS $$$$$$$$$$$$$$$$$$$$$$$$\n");
Sophie Coudert's avatar
Sophie Coudert committed
        for(AvatarRelation ar: avspec.getRelations()) {

            // parameters depending on relation type ==============
            int relationType;
            int relationNumber;
            String relationConstructor;
            String relationParameters = null;
            String channelConstructor;

            // computine parameters depending on relation type ==============
            // if (ar.isAMS()) throw exception

            if (! ar.isAsynchronous()) { // sync relation
                relationType = SYNC;
                relationNumber = syncrelNumber++;
                relationConstructor = "'#Sync_Rel'";
                channelConstructor = "'#Sync'";
                if(ar.isPrivate()) relationParameters = "('private'=true)";
            }
            else { // fifo relation
                if(ar.isPrivate()) // default: false
                    if(ar.isLossy()) // default: false
                        if(ar.getSizeOfFIFO() > 1) // default size: 1
                            relationParameters= "('private'=true, lossy=true, max_size=" + ar.getSizeOfFIFO() + ")";
                        else
                            relationParameters= "('private'=true, lossy=true)";
                    else
                        if(ar.getSizeOfFIFO() > 1) // default size: 1
                            relationParameters= "('private'=true, max_size=" + ar.getSizeOfFIFO() + ")";
                        else
                            relationParameters= "('private'=true)";
                else
                    if(ar.isLossy()) // default: false
                        if(ar.getSizeOfFIFO() > 1) // default size: 1
                            relationParameters= "(lossy=true, max_size=" + ar.getSizeOfFIFO() + ")";
                        else
                            relationParameters= "(lossy=true)";
                    else
                        if(ar.getSizeOfFIFO() > 1) // default size: 1
                            relationParameters= "(max_size=" + ar.getSizeOfFIFO() + ")";

                if (ar.isBlocking()) { // blocking fifo relation
                    relationType = BFIFO;
                    relationNumber = bfiforelNumber++;
                    relationConstructor = "'#Bfifo_Rel'";
                    channelConstructor = "'#Bfifo'";
                }
                else { // non blocking fifo relation
                    relationType = NBFIFO;
                    relationNumber = nbfiforelNumber++;
                    relationConstructor = "'#NBfifo_Rel'";
                    channelConstructor = "'#NBfifo'";
                }
            }

            // general type-independent generation ==============

            String blk1 = ar.getBlock1().getName();
            String blk2 = ar.getBlock2().getName();
            String relationSYSMLname = relationSysMLname(blk1, blk2, relationType, relationNumber);

            // relation declaration --------------------

            avsysml.append("\n" + indent + "// Relation " + relationSYSMLname + "=============\n");
Sophie Coudert's avatar
Sophie Coudert committed
            avsysml.append(indent + "part " +  relationSYSMLname + ": " + relationConstructor);
Sophie Coudert's avatar
Sophie Coudert committed
            if(relationParameters != null)
                 avsysml.append(" = " + relationConstructor + relationParameters);
Sophie Coudert's avatar
Sophie Coudert committed

            // Channels and associated Messages declarations ----------------

            int relationSize = ar.getSignals1().size();
            for(int i=0; i<relationSize; i++) {
                AvatarSignal sig1 = ar.getSignals1().get(i);
                AvatarSignal sig2 = ar.getSignals2().get(i);
                boolean in2out = (sig1.getInOut() == AvatarSignal.OUT);

                // to remove
                String channelName = channelName(blk1, blk2, sig1.getName(), sig2.getName(), in2out);

                String blk1SysMLname = blockSysMLname(blk1);
                String blk2SysMLname = blockSysMLname(blk2);
                String channelSYSMLname = channelSysMLname(channelName(blk1, blk2, sig1.getName(), sig2.getName(), in2out), relationType);
                String sig1SYSMLname = signalSysMLname(sig1.getName());
                String sig2SYSMLname = signalSysMLname(sig2.getName());
                String message1SYSMLname = messageSysMLname(blk1,sig1.getName());
                String message2SYSMLname = messageSysMLname(blk2,sig2.getName());
                SignalInfo sig1Info = new SignalInfo(sig1SYSMLname, message1SYSMLname);
                SignalInfo sig2Info = new SignalInfo(sig2SYSMLname, message2SYSMLname);
Sophie Coudert's avatar
Sophie Coudert committed

                // Channel declaration .........................

                avsysml.append("\n" + indent + "// Channel " + channelName + "-------------\n");
Sophie Coudert's avatar
Sophie Coudert committed
                avsysml.append(indent + "part " + channelSYSMLname + " : " + channelConstructor + " = "
                        + channelConstructor + "(relation = " + relationSYSMLname + ");\n");
                avsysml.append(indent + "bind " + blk1SysMLname + "." + sig1SYSMLname + " = " + channelSYSMLname + ";\n");
                avsysml.append(indent + "bind " + blk2SysMLname + "." + sig2SYSMLname + " = " + channelSYSMLname + ";\n");
Sophie Coudert's avatar
Sophie Coudert committed

                // Message declaration .........................

                StringBuffer msg1Buf = new StringBuffer(indent + "// Message of signal " + blk1SysMLname + "." + sig1SYSMLname + "----------\n");
                StringBuffer msg2Buf = new StringBuffer(indent + "// Message of signal " + blk2SysMLname  + sig2SYSMLname + "----------\n");
                if (in2out) {
                    msg1Buf.append(indent + "part def " + message2SYSMLname + " :> '#DataMessage' {\n");
                    msg2Buf.append(indent + "part def " + message1SYSMLname + " :> " + message2SYSMLname + "{\n");
                } else {
                    msg1Buf.append(indent + "part def " + message1SYSMLname + " :> '#DataMessage' {\n");
                    msg2Buf.append(indent + "part def " + message2SYSMLname + " :> " + message1SYSMLname + "{\n");
                }
Sophie Coudert's avatar
Sophie Coudert committed
                indent += indentStep;
Sophie Coudert's avatar
Sophie Coudert committed
                // message fields. SysML names preserve Avatar sender and receiver names
                int channelSize = sig1.getListOfAttributes().size();
Sophie Coudert's avatar
Sophie Coudert committed
                for (int j=0; j<channelSize; j++) { // browse channels' parameters
                    String attributeType = (sig1.getListOfAttributes().get(j).isInt() ? "Integer;\n" : "Boolean;\n");
                    String fieldName1 = fieldSysMLname(sig1.getListOfAttributes().get(j).getName());
                    String fieldName2 = fieldSysMLname(sig2.getListOfAttributes().get(j).getName());
               if (in2out) {
                    msg1Buf.append(indent + "attribute " + fieldName2 + " : " + attributeType);
                    msg2Buf.append(indent + "attribute " + fieldName1 + " redefines " + fieldName2 + ";\n");
                } else {
                    msg1Buf.append(indent + "attribute " + fieldName1 + " : " + attributeType);
                    msg2Buf.append(indent + "attribute " + fieldName2 + " redefines " + fieldName1 + ";\n");
                }
                    sig1Info.addField(fieldName1);
                    sig2Info.addField(fieldName2);
Sophie Coudert's avatar
Sophie Coudert committed
                }
Sophie Coudert's avatar
Sophie Coudert committed
                indent = indent.substring(indentStepSize);
                msg1Buf.append(indent + "}\n");
                msg2Buf.append(indent + "}\n");

                avsysml.append(msg1Buf);
                avsysml.append(msg2Buf);
Sophie Coudert's avatar
Sophie Coudert committed

                signalMap.put(sig1,sig1Info);
                signalMap.put(sig2,sig2Info);
    void attribute2SysML(AvatarAttribute aa) {
        if (aa.isTimer())
            timerList.add(aa);
        else {
            avsysml.append(indent + "attribute " + attributeSysMLname(aa.getName()) + " : ");
            if (aa.isInt()) avsysml.append("Integer");
            else // if (aa.isBool())
                avsysml.append("Boolean");
            if (aa.hasInitialValue()) avsysml.append(" := " + aa.getInitialValue().trim());
            avsysml.append(";\n");
        }
    }
    void field2SysML(AvatarAttribute aa, String prefix) {
        avsysml.append(indent + prefix + "attribute " + fieldSysMLname(aa.getName()) + " : ");
        if (aa.isInt()) avsysml.append("Integer;\n");
        else
            avsysml.append("Boolean;\n");
Sophie Coudert's avatar
Sophie Coudert committed
    }

    void method2SysML(AvatarMethod am){
        // "return tuple" not handled
        String returnType = null;
        if(am.getListOfReturnAttributes().size() == 1){
            String avatarType = am.getListOfReturnAttributes().get(0).getType().getStringType();
            if (avatarType.equals("int"))
                returnType = "Integer";
            else
                returnType = "Boolean";
        }
Sophie Coudert's avatar
Sophie Coudert committed
        if (returnType == null) {
            avsysml.append(indent + "action " + methodSysMLname(am.getName()) +  ": '#AvatarVoidMethod' {\n");
            indent += indentStep;
            for (AvatarAttribute aa : am.getListOfAttributes()) field2SysML(aa, "in ");
Sophie Coudert's avatar
Sophie Coudert committed
            indent = indent.substring(indentStepSize);
            avsysml.append(indent + "}\n");
        } else {
            avsysml.append(indent + "calc " + methodSysMLname(am.getName()) +  ": '#AvatarCalcMethod' {\n");
            indent += indentStep;
            for (AvatarAttribute aa : am.getListOfAttributes()) field2SysML(aa, "in ");
Sophie Coudert's avatar
Sophie Coudert committed
            avsysml.append(indent + "return : " + returnType + ";\n");
            indent = indent.substring(indentStepSize);
            avsysml.append(indent + "}\n");
        }
    }

Sophie Coudert's avatar
Sophie Coudert committed
    void signal2SysML(AvatarSignal as){

        avsysml.append(indent + "part " + signalMap.get(as).getName() + " : " +
                (fifoSet.contains(as) ? "'#Fifo'" : "'#Channel'") + ";\n");
    }
Sophie Coudert's avatar
Sophie Coudert committed
    // handling one block
    void block2SysML(AvatarBlock block){
        String blockSYSMLname = blockSysMLname(block.getName());
        timerList.clear();
        chainBlock(blockSYSMLname);
        blocklink();
Sophie Coudert's avatar
Sophie Coudert committed

        // block header --------------------
        avsysml.append("\n" + indent + "// Block " + blockSYSMLname + "=============\n");
Sophie Coudert's avatar
Sophie Coudert committed
        avsysml.append(indent + "part " + blockSYSMLname + " : '#AvatarBlock' {\n");
        indent += indentStep;

        // attributes --------------------
        if (block.getAttributes() != null && block.getAttributes().size() != 0) {
            avsysml.append("\n" + indent + "// Attributes ---------------------\n");
            for (AvatarAttribute aa : block.getAttributes()) attribute2SysML(aa);
        }
Sophie Coudert's avatar
Sophie Coudert committed
        // methods --------------------
        if (block.getMethods() != null && block.getMethods().size() != 0) {
            avsysml.append("\n" + indent + "// Methods ---------------------\n");
            for (AvatarMethod am : block.getMethods()) method2SysML(am);
        }
        if (block.getSignals() != null && block.getSignals().size() != 0) {
            avsysml.append("\n" + indent + "// Signals ---------------------\n");
            for (AvatarSignal as : block.getSignals()) signal2SysML(as);
        }
Sophie Coudert's avatar
Sophie Coudert committed
        // state-machine --------------------
        avsysml.append("\n" + indent + "// state-machine -------------------\n");
Sophie Coudert's avatar
Sophie Coudert committed
        statemachine2SysML(block.getStateMachine());
        // timers -------------------
        if (timerList != null && timerList.size() != 0) {
            avsysml.append("\n" + indent + "// Timers ---------------------\n");
            for (AvatarAttribute timer : timerList) timer2SysML(timer);
        }
Sophie Coudert's avatar
Sophie Coudert committed
        // sub-blocks -------------------
        List<AvatarBlock> subBlockList = new ArrayList<AvatarBlock>();
        for(AvatarBlock blk: avspec.getListOfBlocks()) { if (blk.getFather() == block) subBlockList.add(blk); }
        if (subBlockList != null && subBlockList.size() != 0) {
            avsysml.append("\n" + indent + "// Sub-Blocks øøøøøøøøøøøøøøøøøøøøøøø\n");
            for (AvatarBlock blk : subBlockList) if (blk.getFather() == block) block2SysML(blk);
        }
Sophie Coudert's avatar
Sophie Coudert committed
        indent = indent.substring(indentStepSize);
Sophie Coudert's avatar
Sophie Coudert committed
        avsysml.append(indent + "}\n");
        unchainBlock();
Sophie Coudert's avatar
Sophie Coudert committed
    }

    // handling all blocks
    void blocks2SysML(){
        avsysml.append("\n" + indent + "// BLOCKS $$$$$$$$$$$$$$$$$$$$$$$$\n");
Sophie Coudert's avatar
Sophie Coudert committed
        for(AvatarBlock block: avspec.getListOfBlocks())
            if (block.getFather() == null) block2SysML(block);
    }
    void timer2SysML(AvatarAttribute timer) {
        String relationname = timerRelationSysMLname(timer.getName());
        String settimername = setTimerChannelSysMLname(timer.getName());
        String resettimername = resetTimerChannelSysMLname(timer.getName());
        String expiretimername = expireTimerChannelSysMLname(timer.getName());
        String timerblockname = timerBlockSysMLname(timer.getName());
        avsysml.append(indent + "part " + relationname + ": '#Timer_Rel';\n");
        avsysml.append(indent + "part " + settimername + ": '#Sync' = '#Sync'(relation = " + relationname + ");\n");
        avsysml.append(indent + "part " + resettimername + ": '#Sync' = '#Sync'(relation = " + relationname + ");\n");
        avsysml.append(indent + "part " + expiretimername + ": '#Sync' = '#Sync'(relation = " + relationname + ");\n");
        avsysml.append(indent + "part " + timerblockname + ": '#AvatarTimer';\n");
        avsysml.append(indent + "bind " + timerblockname + ".set = " + settimername + ";\n");
        avsysml.append(indent + "bind " + timerblockname + ".reset = " + resettimername + ";\n");
        avsysml.append(indent + "bind " + timerblockname + ".expire = " + expiretimername + ";\n");
Sophie Coudert's avatar
Sophie Coudert committed
    void statemachine2SysML(AvatarStateMachine asm) {
Sophie Coudert's avatar
Sophie Coudert committed
        stateList.clear();
Sophie Coudert's avatar
Sophie Coudert committed
        // associate names to future SysML states
        int randomNumber = 0;
        int countNumber = 0;
        int sendNumber = 0;
        int receiveNumber = 0;
Sophie Coudert's avatar
Sophie Coudert committed
        int setNumber = 0;
        int resetNumber = 0;
        int expireNumber = 0;
Sophie Coudert's avatar
Sophie Coudert committed
        for (AvatarStateMachineElement asme : asm.getListOfElements()) {
            StateInfo stateinfo;
            if      (asme instanceof AvatarStartState)
Sophie Coudert's avatar
Sophie Coudert committed
                stateinfo = new StateInfo(startStateSysMLname());
Sophie Coudert's avatar
Sophie Coudert committed
            else if (asme instanceof AvatarStopState)
Sophie Coudert's avatar
Sophie Coudert committed
                stateinfo = new StateInfo(stopStateSysMLname());
Sophie Coudert's avatar
Sophie Coudert committed
            else if (asme instanceof AvatarState)
Sophie Coudert's avatar
Sophie Coudert committed
                stateinfo = new StateInfo(standardStateSysMLname(asme.getName()));
Sophie Coudert's avatar
Sophie Coudert committed
            else if (asme instanceof AvatarActionOnSignal) {
                if(((AvatarActionOnSignal)asme).isSending())
Sophie Coudert's avatar
Sophie Coudert committed
                    stateinfo = new StateInfo(
                            sendStateSysMLname(((AvatarActionOnSignal)asme).getSignal().getName(), sendNumber),
                            presendStateSysMLname(((AvatarActionOnSignal)asme).getSignal().getName(), sendNumber++));
Sophie Coudert's avatar
Sophie Coudert committed
                else
Sophie Coudert's avatar
Sophie Coudert committed
                    stateinfo = new StateInfo(receiveStateSysMLname(((AvatarActionOnSignal)asme).getSignal().getName(), receiveNumber++),
                            prereceiveStateSysMLname(((AvatarActionOnSignal)asme).getSignal().getName(), receiveNumber++));
Sophie Coudert's avatar
Sophie Coudert committed
            }
Sophie Coudert's avatar
Sophie Coudert committed
            else if (asme instanceof AvatarSetTimer) {
                stateinfo = new StateInfo(setTimerStateSysMLname(((AvatarTimerOperator) asme).getTimer().getName(),setNumber),
                        presetTimerStateSysMLname(((AvatarTimerOperator) asme).getTimer().getName(), setNumber++));
            }
            else if (asme instanceof AvatarResetTimer) {
                stateinfo = new StateInfo(resetTimerStateSysMLname(((AvatarTimerOperator) asme).getTimer().getName(),resetNumber),
                        preresetTimerStateSysMLname(((AvatarTimerOperator) asme).getTimer().getName(), resetNumber++));
            }
            else if (asme instanceof AvatarExpireTimer) {
                stateinfo = new StateInfo(expireTimerStateSysMLname(((AvatarTimerOperator) asme).getTimer().getName(),expireNumber),
                        preexpireTimerStateSysMLname(((AvatarTimerOperator) asme).getTimer().getName(), expireNumber++));

            }
Sophie Coudert's avatar
Sophie Coudert committed
            else if (asme instanceof AvatarQueryOnSignal)
Sophie Coudert's avatar
Sophie Coudert committed
                stateinfo = new StateInfo(countStateSysMLname(((AvatarQueryOnSignal)asme).getSignal().getName(), countNumber++));
Sophie Coudert's avatar
Sophie Coudert committed
            else if (asme instanceof AvatarRandom)
Sophie Coudert's avatar
Sophie Coudert committed
                stateinfo = new StateInfo(randomStateSysMLname(randomNumber++));
Sophie Coudert's avatar
Sophie Coudert committed
            else continue;
            stateList.add(asme);
            stateMap.put(asme, stateinfo);
        }
        avsysml.append(indent + "exhibit state '@statemachine' : '#AvatarStateMachine' {\n");
Sophie Coudert's avatar
Sophie Coudert committed
        indent += indentStep;
        // generate SysML states with associated transitions
Sophie Coudert's avatar
Sophie Coudert committed
        for (AvatarStateMachineElement asme : stateList) state2SysML(asme);
Sophie Coudert's avatar
Sophie Coudert committed
        indent = indent.substring(indentStepSize);
Sophie Coudert's avatar
Sophie Coudert committed
        avsysml.append(indent + "}\n");
    }
Sophie Coudert's avatar
Sophie Coudert committed
    StringBuffer sysMLtransitions = new StringBuffer();
    StringBuffer sysMLrequests = new StringBuffer();
Sophie Coudert's avatar
Sophie Coudert committed
    List<AvatarStateMachineElement> requirePreCom = new ArrayList<AvatarStateMachineElement>();
Sophie Coudert's avatar
Sophie Coudert committed

    void state2SysML(AvatarStateMachineElement asme){
        indent += indentStep;
        transitionsAndRequests(stateMap.get(asme).getName(), asme.getNexts(), ((asme instanceof AvatarState) ? "pool" : "request"));
        indent = indent.substring(indentStepSize);

        String requests = sysMLrequests.toString();

        // State Description, depending on state type

        if(asme instanceof AvatarState){
            avsysml.append("\n" + indent + "state " + stateMap.get(asme).getName() + " : '#AvatarStandardState'");
Sophie Coudert's avatar
Sophie Coudert committed
            if (requests.equals(""))
Sophie Coudert's avatar
Sophie Coudert committed
            else
                avsysml.append(" = '#AvatarStandardState'(\n" + requests + indent + ");\n");
Sophie Coudert's avatar
Sophie Coudert committed
        } else

        if(asme instanceof AvatarRandom){
            avsysml.append("\n" + indent + "state " + stateMap.get(asme).getName() + " : '#AvatarRandomState' = '#AvatarRandomState'(\n");
Sophie Coudert's avatar
Sophie Coudert committed
            if (!requests.equals("")) avsysml.append(requests);
Sophie Coudert's avatar
Sophie Coudert committed
            indent += indentStep;
Sophie Coudert's avatar
Sophie Coudert committed
            avsysml.append(indent + "state_action = '#Assignment'(\n");
            indent += indentStep;
            avsysml.append(indent + "target = " + attributeSysMLname(((AvatarRandom)asme).getVariable()) + ",\n");
            avsysml.append(indent + "value = '#bound_random'(" + expr2SysML(((AvatarRandom)asme).getMinValue()) + ", " + expr2SysML(((AvatarRandom)asme).getMaxValue()) +")\n");
            indent = indent.substring(indentStepSize);
            avsysml.append(indent + ")\n");
            indent = indent.substring(indentStepSize);
            avsysml.append(indent + ");\n");
Sophie Coudert's avatar
Sophie Coudert committed
        } else

        if(asme instanceof AvatarQueryOnSignal){
            avsysml.append("\n" + indent + "state " + stateMap.get(asme).getName() + " : '#AvatarCountState' = '#AvatarCountState'(\n");
Sophie Coudert's avatar
Sophie Coudert committed
            if (!requests.equals("")) avsysml.append(requests);
Sophie Coudert's avatar
Sophie Coudert committed
            indent += indentStep;
Sophie Coudert's avatar
Sophie Coudert committed
            avsysml.append(indent + "state_action = '#Assignment'(\n");
            indent += indentStep;
            avsysml.append(indent + "target = " + attributeSysMLname(((AvatarQueryOnSignal)asme).getAttribute().getName()) + ",\n");
            avsysml.append(indent + "value = " + signalMap.get(((AvatarQueryOnSignal)asme).getSignal()).getName() + ".amount()\n");
            indent = indent.substring(indentStepSize);
            avsysml.append(indent + ")\n");
            indent = indent.substring(indentStepSize);
            avsysml.append(indent + ");\n");
Sophie Coudert's avatar
Sophie Coudert committed
       } else

        if(asme instanceof AvatarActionOnSignal){
            if(((AvatarActionOnSignal)asme).isSending()) {
                avsysml.append("\n" + indent + "state " + stateMap.get(asme).getName() + " : '#AvatarSendState'");
Sophie Coudert's avatar
Sophie Coudert committed
                if (requests.equals(""))
Sophie Coudert's avatar
Sophie Coudert committed
                else
                    avsysml.append(" = '#AvatarSendState'(\n" + requests + indent + ");\n");
Sophie Coudert's avatar
Sophie Coudert committed
            } else {
                avsysml.append("\n" + indent + "state " + stateMap.get(asme).getName() + " : '#AvatarReceiveState'");
Sophie Coudert's avatar
Sophie Coudert committed
                if (requests.equals(""))
Sophie Coudert's avatar
Sophie Coudert committed
                else
                    avsysml.append(" = '#AvatarReceiveState'(\n" + requests + indent + ");\n");
Sophie Coudert's avatar
Sophie Coudert committed
            }
        } else

        if(asme instanceof AvatarStartState){
            avsysml.append("\n" + indent + "entry action " + stateMap.get(asme).getName() + " :'#AvatarStartState'");
Sophie Coudert's avatar
Sophie Coudert committed
            if (requests.equals(""))
Sophie Coudert's avatar
Sophie Coudert committed
            else
                avsysml.append(" = '#AvatarStartState'(\n" + requests + indent + ");\n");
Sophie Coudert's avatar
Sophie Coudert committed
        } else

        if(asme instanceof AvatarStopState){
                avsysml.append("\n" + indent + "exit action " + stateMap.get(asme).getName() + " :'#AvatarStopState';\n");
        } else

        if(asme instanceof AvatarSetTimer){
            avsysml.append("\n" + indent + "state " + stateMap.get(asme).getName() + " : '#AvatarSetTimerState'");
            if (requests.equals(""))
                avsysml.append(" = '#AvatarSetTimerState'(\n" + requests + indent + ");\n");
        } else

        if(asme instanceof AvatarResetTimer){
            avsysml.append("\n" + indent + "state " + stateMap.get(asme).getName() + " : '#AvatarResetTimerState'");
            if (requests.equals(""))
                avsysml.append(" = '#AvatarResetTimerState'(\n" + requests + indent + ");\n");
        } else

        if(asme instanceof AvatarExpireTimer){
            avsysml.append("\n" + indent + "state " + stateMap.get(asme).getName() + " : '#AvatarExpireTimerState'");
            if (requests.equals(""))
                avsysml.append(" = '#AvatarExpireTimerState'(\n" + requests + indent + ");\n");
Sophie Coudert's avatar
Sophie Coudert committed
        }

        // Transition descriptions
        avsysml.append(sysMLtransitions);

        // PreComm States and their outgoing transition
Sophie Coudert's avatar
Sophie Coudert committed
        for (AvatarStateMachineElement aos: requirePreCom) {
Sophie Coudert's avatar
Sophie Coudert committed
            StateInfo stateinfo = stateMap.get(aos);
Sophie Coudert's avatar
Sophie Coudert committed
            if (aos instanceof AvatarActionOnSignal) {
                SignalInfo signalinfo = signalMap.get(((AvatarActionOnSignal)aos).getSignal());

                if (((AvatarActionOnSignal)aos).isSending()) {
                    // preComm State
                    avsysml.append("\n" + indent + "state " + stateinfo.getPreName() + " : '#AvatarPreSendState' = '#AvatarPreSendState' (\n");
Sophie Coudert's avatar
Sophie Coudert committed
                    indent += indentStep;
                    avsysml.append(indent + "request =\n");
                    indent += indentStep;
                    avsysml.append(sendRequest2SysML(1, "0", "0", signalinfo, ((AvatarActionOnSignal)aos).getValues()) + "\n");
                    indent = indent.substring(2 * indentStepSize);
                    avsysml.append(indent + ");\n");
Sophie Coudert's avatar
Sophie Coudert committed
                    // its transition
                    avsysml.append("\n" + indent + "transition : '#AvatarTransition' first " + stateinfo.getPreName() +
                            " then " + stateinfo.getName() +
                            endTransition(0,"","", 1));
Sophie Coudert's avatar
Sophie Coudert committed
                } else {
                    avsysml.append("\n" + indent + "state " + stateinfo.getPreName() + " : '#AvatarPreReceiveState' = '#AvatarPreReceiveState' (\n");
Sophie Coudert's avatar
Sophie Coudert committed
                    indent += indentStep;
                    avsysml.append(indent + "request =\n");
                    indent += indentStep;
                    avsysml.append(receiveRequest2SysML(1, "0", "0", signalinfo) + "\n");
                    indent = indent.substring(2 * indentStepSize);
                    avsysml.append(indent + ");\n");
Sophie Coudert's avatar
Sophie Coudert committed
                    // its transition
                    avsysml.append("\n" + indent + "transition : '#AvatarTransition' first " + stateinfo.getPreName() + "\n");
Sophie Coudert's avatar
Sophie Coudert committed
                    indent += indentStep;
                    String doAction = receiveActions2SysML(signalinfo, ((AvatarActionOnSignal)aos).getValues());
                    if (doAction.length() == 0)
                        avsysml.append(indent + "then " + stateinfo.getName() +
                                endTransition(0,"","", 1));
Sophie Coudert's avatar
Sophie Coudert committed
                    else {
                        avsysml.append(doAction);
                        avsysml.append(" then " + stateinfo.getName() +
                                endTransition(0,"","", 1));
Sophie Coudert's avatar
Sophie Coudert committed
                    }
                    indent = indent.substring(indentStepSize);
Sophie Coudert's avatar
Sophie Coudert committed
                }
            } else if (aos instanceof AvatarSetTimer) {
                    // preComm State
                    avsysml.append("\n" + indent + "state " + stateinfo.getPreName() + " : '#AvatarPreSendState' = '#AvatarPreSendState' (\n");
                    indent += indentStep;
                    avsysml.append(indent + "request =\n");
                    indent += indentStep;
                    avsysml.append(setTimerRequest2SysML(1, "0", "0",
                            setTimerChannelSysMLname(((AvatarSetTimer) aos).getTimer().getName()),
                            ((AvatarSetTimer)aos).getTimerValue()) + "\n");
                    indent = indent.substring(2 * indentStepSize);
                    avsysml.append(indent + ");\n");
                    // its transition
                    avsysml.append("\n" + indent + "transition : '#AvatarTransition' first " + stateinfo.getPreName() +
                            " then " + stateinfo.getName() +
                            endTransition(0,"","", 1));

            } else if (aos instanceof AvatarResetTimer) {
                    // preComm State
                    avsysml.append("\n" + indent + "state " + stateinfo.getPreName() + " : '#AvatarPreSendState' = '#AvatarPreSendState' (\n");
                    indent += indentStep;
                    avsysml.append(indent + "request =\n");
                    indent += indentStep;
                    avsysml.append(resetTimerRequest2SysML(1, "0", "0",
                            resetTimerChannelSysMLname(((AvatarResetTimer) aos).getTimer().getName())) + "\n");
                    indent = indent.substring(2 * indentStepSize);
                    avsysml.append(indent + ");\n");
                    // its transition
                    avsysml.append("\n" + indent + "transition : '#AvatarTransition' first " + stateinfo.getPreName() +
                            " then " + stateinfo.getName() +
                            endTransition(0,"","", 1));

            } else if (aos instanceof AvatarExpireTimer) {
                    avsysml.append("\n" + indent + "state " + stateinfo.getPreName() + " : '#AvatarPreReceiveState' = '#AvatarPreReceiveState' (\n");
                    indent += indentStep;
                    avsysml.append(indent + "request =\n");
                    indent += indentStep;
                    avsysml.append(expireTimerRequest2SysML(1, "0", "0",
                            expireTimerChannelSysMLname(((AvatarExpireTimer) aos).getTimer().getName())) + "\n");
                    indent = indent.substring(2 * indentStepSize);
                    avsysml.append(indent + ");\n");
                    // its transition
                    avsysml.append("\n" + indent + "transition : '#AvatarTransition' first " + stateinfo.getPreName() +
                            endTransition(0,"","", 1));
                    indent += indentStep;
                    avsysml.append(indent + "then " + stateinfo.getName() + ";\n");
                    indent = indent.substring(indentStepSize);
Sophie Coudert's avatar
Sophie Coudert committed
            }
        }
    }

    String endTransition(int delayDistributionLaw, String delayExtra1, String delayExtra2, double probability){
        if (delayDistributionLaw == DELAY_UNIFORM_LAW && probability == 1) return ";\n";
        StringBuffer result = new StringBuffer(" {\n");
        indent += indentStep;
        result.append(indent + "attribute delayDistributionLaw : String = \"" + DISTRIBUTION_LAWS[delayDistributionLaw] + "\";\n");
        if (probability != 1)
            result.append(indent + "attribute weight : Real = " + probability + ";\n");
        if (NB_OF_EXTRA_ATTRIBUTES[delayDistributionLaw] > 0)
            result.append(indent + "attribute '" + LABELS_OF_EXTRA_ATTRIBUTES_1[delayDistributionLaw] + "' : String = \""
                + delayExtra1 + "\";\n");
        if (NB_OF_EXTRA_ATTRIBUTES[delayDistributionLaw] > 1)
            result.append(indent + "attribute '" + LABELS_OF_EXTRA_ATTRIBUTES_2[delayDistributionLaw] + "' : String = \""
                + delayExtra2 + "\";\n");
        indent = indent.substring(indentStepSize);
        result.append(indent + "}\n");
        return result.toString();
    }
Sophie Coudert's avatar
Sophie Coudert committed
    void transitionsAndRequests(String srcName, List<AvatarStateMachineElement> nexts, String poolName) {
Sophie Coudert's avatar
Sophie Coudert committed
        requirePreCom.clear();
Sophie Coudert's avatar
Sophie Coudert committed
        sysMLtransitions.delete(0, sysMLtransitions.length());
        sysMLrequests.delete(0, sysMLrequests.length());
        int nb = nexts.size();
        if (nb == 0) {
Sophie Coudert's avatar
Sophie Coudert committed
            sysMLrequests.append(indent + poolName + " = null\n");
Sophie Coudert's avatar
Sophie Coudert committed
            return;
        }
        if (nb == 1) {
            indent += indentStep;
            transitionAndRequest(srcName, (AvatarTransition)nexts.get(0), 0);
            indent = indent.substring(indentStepSize);

Sophie Coudert's avatar
Sophie Coudert committed
            if(sysMLrequests.toString().trim().equals("'#immediate_request'")) {
Sophie Coudert's avatar
Sophie Coudert committed
                sysMLrequests.delete(0, sysMLrequests.length());
                return;
            }
            sysMLrequests.insert(0, indent + poolName + " =\n");
            sysMLrequests.append("\n");
            return;
        }
        indent += indentStep;
        for(int i=0; i<nb; i++){
            transitionAndRequest(srcName, (AvatarTransition)nexts.get(i), i+1);
            if(i != nb-1)
                sysMLrequests.append(",\n");
            else
                 sysMLrequests.append("\n");
        }
        indent = indent.substring(indentStepSize);
        sysMLrequests.insert(0, indent + poolName + " = (\n");
Sophie Coudert's avatar
Sophie Coudert committed
        sysMLrequests.append(indent + ")\n");
Sophie Coudert's avatar
Sophie Coudert committed
   }
    // index is 0 if transition is alone
    void transitionAndRequest(String srcName, AvatarTransition at, int index){
        int transindex = ((index == 0) ? 1 : index);

        // identifying cases
        boolean guarded = !at.hasNonDeterministicGuard();
        AvatarStateMachineElement target = at.getNext(0);
        String tgtName;
Sophie Coudert's avatar
Sophie Coudert committed
        int requestType = 0; // 0:trivial, 1:Send, 2:Receive, 3:SetTimer, 4: ResetTimer, 5:ExpireTimer
Sophie Coudert's avatar
Sophie Coudert committed

Sophie Coudert's avatar
Sophie Coudert committed
        if((at.getActions()!=null && at.getActions().size()!=0) &&
                (target instanceof AvatarActionOnSignal || target instanceof AvatarTimerOperator)) { // preCommunication Required
            requirePreCom.add(target);
Sophie Coudert's avatar
Sophie Coudert committed
            tgtName = stateMap.get(target).getPreName();
Sophie Coudert's avatar
Sophie Coudert committed
        }
        else {
            tgtName = stateMap.get(target).getName();
            if(target instanceof AvatarActionOnSignal){
                if (((AvatarActionOnSignal)target).isSending())
                    requestType = 1;
                else
                    requestType = 2;
            }
Sophie Coudert's avatar
Sophie Coudert committed
            else if (target instanceof AvatarSetTimer)
                requestType = 3;
            else if (target instanceof AvatarResetTimer)
                requestType = 4;
            else if (target instanceof AvatarExpireTimer)
                requestType = 5;
Sophie Coudert's avatar
Sophie Coudert committed
        }

        // computing request
        if (guarded) {
            sysMLrequests.append(indent + "if " + expr2SysML(((AvatarTransition)at).getGuard().toString()) + " ?\n");
            indent += indentStep;
        }
        String minDelay = ( at.getMinDelay().length()==0 ? "0" : at.getMinDelay().toString() );
        String maxDelay = ( at.getMaxDelay().length()==0 ? "0" : at.getMaxDelay().toString() );
Sophie Coudert's avatar
Sophie Coudert committed
        if(requestType == 0) // Trivial
            sysMLrequests.append(trivialRequest2SysML(transindex,
                    expr2SysML(minDelay),
                    expr2SysML(maxDelay)));
Sophie Coudert's avatar
Sophie Coudert committed
        else if (requestType == 1) // Send
            sysMLrequests.append(sendRequest2SysML(transindex,
                    expr2SysML(minDelay),
                    expr2SysML(maxDelay),
Sophie Coudert's avatar
Sophie Coudert committed
                     signalMap.get(((AvatarActionOnSignal)target).getSignal()),
                    ((AvatarActionOnSignal)target).getValues()));
        else if (requestType == 2) // Receive
Sophie Coudert's avatar
Sophie Coudert committed
            sysMLrequests.append(receiveRequest2SysML(transindex,
                    expr2SysML(minDelay),
                    expr2SysML(maxDelay),
Sophie Coudert's avatar
Sophie Coudert committed
                    signalMap.get(((AvatarActionOnSignal)target).getSignal())));
        else if (requestType == 3) // Set
            sysMLrequests.append(setTimerRequest2SysML(transindex,
                    expr2SysML(minDelay),
                    expr2SysML(maxDelay),
                    setTimerChannelSysMLname(((AvatarTimerOperator) target).getTimer().getName()),
                    ((AvatarSetTimer) target).getTimerValue()));
        else  if (requestType == 4) // Reset
            sysMLrequests.append(resetTimerRequest2SysML(transindex,
                    expr2SysML(minDelay),
                    expr2SysML(maxDelay),
                    resetTimerChannelSysMLname(((AvatarTimerOperator) target).getTimer().getName())));
        else // Expire
            sysMLrequests.append(expireTimerRequest2SysML(transindex,
                    expr2SysML(minDelay),
                    expr2SysML(maxDelay),
                    expireTimerChannelSysMLname(((AvatarTimerOperator) target).getTimer().getName())));
Sophie Coudert's avatar
Sophie Coudert committed
        if(guarded) {
            indent = indent.substring(indentStepSize);
Sophie Coudert's avatar
Sophie Coudert committed
            sysMLrequests.append("\n" + indent + "else '#nok_request'(" + transindex + ")");
Sophie Coudert's avatar
Sophie Coudert committed
        }

        // computing transition
Sophie Coudert's avatar
Sophie Coudert committed
        indent = indent.substring(2 * indentStepSize);
Sophie Coudert's avatar
Sophie Coudert committed
        String doAction;
        indent += indentStep;
        if(requestType == 2)
            doAction = receiveActions2SysML(signalMap.get(((AvatarActionOnSignal)target).getSignal()), ((AvatarActionOnSignal)target).getValues());
Sophie Coudert's avatar
Sophie Coudert committed
        else
            doAction = transitionActions2SysM(at.getActions());
        indent = indent.substring(indentStepSize);

        sysMLtransitions.append("\n" + indent + "transition : '#AvatarTransition' first " + srcName);
Sophie Coudert's avatar
Sophie Coudert committed
        if(index > 0)
            sysMLtransitions.append(" if response.transition_index == " + index + "\n");
        else
            sysMLtransitions.append("\n");
        indent += indentStep;
        if (doAction.length() == 0)
            sysMLtransitions.append(indent + "then " + tgtName +
                    endTransition(at.getDelayDistributionLaw(), at.getDelayExtra1(), at.getDelayExtra2(), at.getProbability()));
Sophie Coudert's avatar
Sophie Coudert committed
        else {
            sysMLtransitions.append(doAction);
            sysMLtransitions.append(" then " + tgtName +
                    endTransition(at.getDelayDistributionLaw(),at.getDelayExtra1(),at.getDelayExtra2(), at.getProbability()));
Sophie Coudert's avatar
Sophie Coudert committed
        }
Sophie Coudert's avatar
Sophie Coudert committed
        indent += indentStep;
Sophie Coudert's avatar
Sophie Coudert committed
    }
    String trivialRequest2SysML(int index, String min, String max) {
        if (max.equals("0"))
            if (min.equals("0"))
                if (index == 1)
                    return indent + "'#immediate_request'";
                else
                    return indent + "'#TrivialRequest'(transition_index= " + index + ")";
            else
                if (index == 1)
                    return indent + "'#TrivialRequest'(delay= " + min + ")";
                else
                    return indent + "'#TrivialRequest'(transition_index= " + index + ", delay= " + min + ")";
Sophie Coudert's avatar
Sophie Coudert committed
        else if (max.trim().equals(min.trim()))
            if(index == 1)
                return indent + "'#TrivialRequest'(delay= " + min + ")";
            else
                return indent + "'#TrivialRequest'(transition_index= " + index + ", delay= " + min + ")";
Sophie Coudert's avatar
Sophie Coudert committed
        else
            if(index == 1)
                return indent + "'#TrivialRequest'(delay= '#bound_random'(" + min + ", " + max + "))";
            else
                return indent + "'#TrivialRequest'(transition_index= " + index + ", delay= '#bound_random'(" + min + ", " + max + "))";
    }

    String sendRequest2SysML(int index, String min, String max, SignalInfo chinfo, List<String> values) {
        StringBuffer result = new StringBuffer(indent + "'#SendRequest'(\n");
Sophie Coudert's avatar
Sophie Coudert committed
        indent += indentStep;
        if (index != 1)
            result.append(indent + "transition_index= " + index + ",\n");
        result.append(indent + "channel= " + chinfo.getName() + ",\n");
Sophie Coudert's avatar
Sophie Coudert committed
        if (max.equals("0")) {
            if (!min.equals("0"))
                result.append(indent + "delay= " + min + ",\n");
        }
Sophie Coudert's avatar
Sophie Coudert committed
        else if (max.trim().equals(min.trim()))
            result.append(indent + "delay= " + min + ",\n");
Sophie Coudert's avatar
Sophie Coudert committed
        else
            result.append(indent + "delay= '#bound_random'(" + min + ", " + max + "),\n");
Sophie Coudert's avatar
Sophie Coudert committed
        if (values.size() == 0)
            result.append(indent + "payload= " + chinfo.getMessageType() + "()\n");
        else {
            result.append(indent + "payload= " + chinfo.getMessageType() + "(\n");
            indent += indentStep;
            for(String vl : values)
                result.append(indent + expr2SysML(vl) + ",\n");
            result.replace(result.length()-2, result.length(), " )\n");
            indent = indent.substring(indentStepSize);
        }
        indent = indent.substring(indentStepSize);
        result.append(indent + ")");
Sophie Coudert's avatar
Sophie Coudert committed
        return result.toString();
    }
Sophie Coudert's avatar
Sophie Coudert committed
    String setTimerRequest2SysML(int index, String min, String max, String chname, String value) {
        StringBuffer result = new StringBuffer(indent + "'#AvatarSetTimerRequest'(\n");
Sophie Coudert's avatar
Sophie Coudert committed
        indent += indentStep;
        if (index != 1)
            result.append(indent + "transition_index= " + index + ",\n");
        result.append(indent + "channel= " + chname + ",\n");
Sophie Coudert's avatar
Sophie Coudert committed
        if (max.equals("0")) {
            if (!min.equals("0"))
                result.append(indent + "delay= " + min + ",\n");
        }
        else if (max.trim().equals(min.trim()))
            result.append(indent + "delay= " + min + ",\n");
        else
            result.append(indent + "delay= '#bound_random'(" + min + ", " + max + "),\n");

        result.append(indent + "payload= '#TimerSetMsg'(" + value + ")\n");

        indent = indent.substring(indentStepSize);
        result.append(indent + ")");
        return result.toString();
    }
    String resetTimerRequest2SysML(int index, String min, String max, String chname) {
        StringBuffer result = new StringBuffer(indent + "'#AvatarResetTimerRequest'(\n");
Sophie Coudert's avatar
Sophie Coudert committed
        indent += indentStep;
        if (index != 1)
            result.append(indent + "transition_index= " + index + ",\n");
        result.append(indent + "channel= " + chname + ",\n");
Sophie Coudert's avatar
Sophie Coudert committed
        if (max.equals("0")) {
            if (!min.equals("0"))
                result.append(indent + "delay= " + min + ",\n");
        }
        else if (max.trim().equals(min.trim()))
            result.append(indent + "delay= " + min + ",\n");
        else
            result.append(indent + "delay= '#bound_random'(" + min + ", " + max + "),\n");

        result.append(indent + "payload= '#TimerResetMsg'()\n");

        indent = indent.substring(indentStepSize);
        result.append(indent + ")");
        return result.toString();
    }
Sophie Coudert's avatar
Sophie Coudert committed

    String receiveRequest2SysML(int index, String min, String max, SignalInfo chinfo) {
        StringBuffer result = new StringBuffer(indent + "'#ReceiveRequest'(\n");
Sophie Coudert's avatar
Sophie Coudert committed
        indent += indentStep;
        if (index != 1)
            result.append(indent + "transition_index= " + index + ",\n");
        result.append(indent + "channel= " + chinfo.getName());
Sophie Coudert's avatar
Sophie Coudert committed
        if (max.equals("0"))
            if (!min.equals("0"))
                result.append(",\n" + indent + "delay= " + min + "\n");
            else
                result.append("\n");
        else if (max.trim().equals(min.trim()))
            result.append(indent + "delay= " + min + ",\n");
        else
            result.append(",\n" + indent + "delay= '#bound_random'(" + min + ", " + max + ")\n");
        indent = indent.substring(indentStepSize);
        result.append(indent + ")");
        return result.toString();
    }
    String expireTimerRequest2SysML(int index, String min, String max, String chname) {
        StringBuffer result = new StringBuffer(indent + "'#AvatarExpireTimerRequest'(\n");
Sophie Coudert's avatar
Sophie Coudert committed
        indent += indentStep;
        if (index != 1)
            result.append(indent + "transition_index= " + index + ",\n");
        result.append(indent + "channel= " + chname);
Sophie Coudert's avatar
Sophie Coudert committed
        if (max.equals("0"))
            if (!min.equals("0"))
                result.append(",\n" + indent + "delay= " + min + "\n");
            else
                result.append("\n");
Sophie Coudert's avatar
Sophie Coudert committed
        else if (max.trim().equals(min.trim()))
            result.append(indent + "delay= " + min + ",\n");
Sophie Coudert's avatar
Sophie Coudert committed
        else
            result.append(",\n" + indent + "delay= '#bound_random'(" + min + ", " + max + ")\n");
        indent = indent.substring(indentStepSize);
        result.append(indent + ")");
Sophie Coudert's avatar
Sophie Coudert committed
        return result.toString();
    }
    String transitionActions2SysM(List<AvatarAction> aas) {
        StringBuffer result;
        if (aas == null || aas.size() == 0) return "";
        result = new StringBuffer(indent + "do action {\n" + indent + indentStep + "first start;\n");
Sophie Coudert's avatar
Sophie Coudert committed
        indent += indentStep;
        for(AvatarAction aa : aas) {
            if(aa instanceof AvatarActionAssignment) {
                AvatarLeftHand lh = ((AvatarActionAssignment)aa).getLeftHand();
                if(lh instanceof AvatarAttribute) { // tuples are not yet handled
Sophie Coudert's avatar
Sophie Coudert committed
                    result.append(indent + "then assign " + attributeSysMLname(lh.getName()) + ":= ");
Sophie Coudert's avatar
Sophie Coudert committed
                     result.append(expr2SysML(((AvatarActionAssignment)aa).getRightHand().getName()) + ";\n");
                }
            }
            else {
                result.append(indent + "then action = " + expr2SysML(aa.getName()) + ";\n");
            }
        }
        result.append(indent + "then done;\n");
Sophie Coudert's avatar
Sophie Coudert committed
        indent = indent.substring(indentStepSize);
        result.append(indent + "}");
Sophie Coudert's avatar
Sophie Coudert committed
        return result.toString();
    }

    String receiveActions2SysML(SignalInfo chinfo, List<String> values) {
        if (values == null || values.size() == 0) return "";
        StringBuffer result = new StringBuffer(indent + "do action {\n");
Sophie Coudert's avatar
Sophie Coudert committed
        indent += indentStep;
        result.append(indent + "item msg : " + chinfo.getMessageType() + " = response.payload as " + chinfo.getMessageType() +
                ";\n" + indent + "first start;\n");
        List<String> fields = chinfo.getProfile();
        int length = values.size(); // must be equal to fields.size()
        for (int i = 0; i<length; i++) {
            result.append(indent + "then assign " + attributeSysMLname(values.get(i)) + " := msg." + fields.get(i) + ";\n");
Sophie Coudert's avatar
Sophie Coudert committed
        }
        result.append(indent + "then done;\n");
Sophie Coudert's avatar
Sophie Coudert committed
        indent = indent.substring(indentStepSize);
        result.append(indent + "}");
Sophie Coudert's avatar
Sophie Coudert committed
        return result.toString();
    }
}