Skip to content
Snippets Groups Projects
AvatarFromSysMLSyntax.java 22.8 KiB
Newer Older
package avatartranslator.tosysmlv2;

import java.util.ArrayList;
import java.util.HashMap;

public class AvatarFromSysMLSyntax {
    public static class MyArray<E> extends ArrayList<E> {
        @Override
        public E set(int index, E element){
            int max = size();
            for(int i = max; i <= index; i++)
                add(null);
            return super.set(index, element);
        }
Sophie Coudert's avatar
Sophie Coudert committed
       @Override
       public E get(int index){
            if (index >= size()) return null;
            else return super.get(index);
       }
    }
    /** idents collected while parsing an Avatar SysML Model */
Sophie Coudert's avatar
Sophie Coudert committed
    public class Ident {
        public static final byte DATATYPENAME = 0;
        public static final byte BLOCKNAME = 1;
        public static final byte ATTRIBUTENAME = 2;
        public static final byte RELATIONNAME = 3;
        public static final byte CHANNELPRENAME = 4;
        public static final byte SIGNALNAME = 5;
        public static final byte CHANNELNAME = 6;
        public static final byte MESSAGENAME = 7;
        public static final byte STANDARDSTATENAME = 8;
        public static final byte STARTSTATENAME = 9;
        public static final byte STOPSTATENAME = 10;
        public static final byte RANDOMSTATENAME = 11;
        public static final byte COUNTSTATENAME = 12;
        public static final byte SENDSTATENAME = 13;
        public static final byte RECEIVESTATENAME = 14;
        public static final byte PRESENDSTATENAME = 15;
        public static final byte PRERECEIVESTATENAME = 16;
        public static final byte SETTIMERSTATENAME = 17;
        public static final byte RESETTIMERSTATENAME = 18;
        public static final byte EXPIRETIMERSTATENAME = 19;
        public static final byte PRESETTIMERSTATENAME = 20;
        public static final byte PRERESETTIMERSTATENAME = 21;
        public static final byte PREEXPIRETIMERSTATENAME = 22;
        public static final byte TIMERBLOCKNAME = 23;
        public static final byte QUOTEDNAME = 24;
        public static final byte AVATARNAME = 25;

        private byte type;
        private String sysMLName;
        private String avatarName;
Sophie Coudert's avatar
Sophie Coudert committed

        public Ident(byte _type, String _smlName) {
            type = _type;
            if (type == AVATARNAME) {
                sysMLName = _smlName;
                avatarName = _smlName;
            } else {
                sysMLName = _smlName.substring(1, _smlName.length() - 2);
                if (type == DATATYPENAME) {
                    avatarName = _smlName.substring(5,_smlName.length() - 2);
                } else if (type == ATTRIBUTENAME) {
                    avatarName = _smlName.substring(2,_smlName.length() - 2);
                } else if (type == SIGNALNAME) {
                    avatarName = _smlName.substring(6,_smlName.length() - 2);
                } else if (type == STANDARDSTATENAME) {
                    avatarName = _smlName.substring(14,_smlName.length() - 2);
                } else if (type == BLOCKNAME) {
                    avatarName = _smlName.substring(6,_smlName.length() - 2);
                } else {
                    avatarName = sysMLName;
                    if (type == QUOTEDNAME) type = AVATARNAME;
                }

            }

Sophie Coudert's avatar
Sophie Coudert committed
        public byte getType() { return type; }
        public String getSysMLName() { return sysMLName; }
        public String getAvatarName() { return avatarName; }
        public boolean is_DATATYPENAME() { return type == DATATYPENAME || type == AVATARNAME; }
        public boolean is_BLOCKNAME() { return type == BLOCKNAME || type == AVATARNAME; }
        public boolean is_ATTRIBUTENAME() { return type == ATTRIBUTENAME || type == AVATARNAME; }
        public boolean is_RELATIONNAME() { return type == RELATIONNAME || type == AVATARNAME; }
        public boolean is_CHANNELPRENAME() { return type == CHANNELPRENAME || type == AVATARNAME; }
        public boolean is_SIGNALNAME() { return type == SIGNALNAME || type == AVATARNAME; }
        public boolean is_CHANNELNAME() { return type == CHANNELNAME || type == AVATARNAME; }
        public boolean is_MESSAGENAME() { return type == MESSAGENAME || type == AVATARNAME; }
        public boolean is_STANDARDSTATENAME() { return type == STANDARDSTATENAME || type == AVATARNAME; }
        public boolean is_STARTSTATENAME() { return type == STARTSTATENAME || type == AVATARNAME; }
        public boolean is_STOPSTATENAME() { return type == STOPSTATENAME || type == AVATARNAME; }
        public boolean is_RANDOMSTATENAME() { return type == RANDOMSTATENAME || type == AVATARNAME; }
        public boolean is_COUNTSTATENAME() { return type == COUNTSTATENAME || type == AVATARNAME; }
        public boolean is_SENDSTATENAME() { return type == SENDSTATENAME || type == AVATARNAME; }
        public boolean is_RECEIVESTATENAME() { return type == RECEIVESTATENAME || type == AVATARNAME; }
        public boolean is_PRESENDSTATENAME() { return type == PRESENDSTATENAME || type == AVATARNAME; }
        public boolean is_PRERECEIVESTATENAME() { return type == PRERECEIVESTATENAME || type == AVATARNAME; }
        public boolean is_SETTIMERSTATENAME() { return type == SETTIMERSTATENAME || type == AVATARNAME; }
        public boolean is_RESETTIMERSTATENAME() { return type == RESETTIMERSTATENAME || type == AVATARNAME; }
        public boolean is_EXPIRETIMERSTATENAME() { return type == EXPIRETIMERSTATENAME || type == AVATARNAME; }
        public boolean is_PRESETTIMERSTATENAME() { return type == PRESETTIMERSTATENAME || type == AVATARNAME; }
        public boolean is_PRERESETTIMERSTATENAME() { return type == PRERESETTIMERSTATENAME || type == AVATARNAME; }
        public boolean is_PREEXPIRETIMERSTATENAME() { return type == PREEXPIRETIMERSTATENAME || type == AVATARNAME; }
        public boolean is_TIMERBLOCKNAME() { return type == TIMERBLOCKNAME || type == AVATARNAME; }
    public abstract static class StxElement {
        private int row;
        private int column;
        public StxElement(int _row, int _column){ row = _row; column = _column; }
        public StxElement(){ row = -1; column = -1; }
        public void setRowColumn(int _row, int _column) { row = _row; column = _column; }

        public int getRow() { return row; }
        public int getColumn() { return column; }
    }
    public static class StxStructure extends StxElement {
        private String name;
        private ArrayList<String> fieldNames;
        private ArrayList<String> fieldTypes;
        public StxStructure(int _row, int _column, String _name) {
            super(_row, _column);
            name = _name;
            fieldNames = new ArrayList<String>();
            fieldTypes = new ArrayList<String>();
        }
        public StxStructure(String _name) {
            super();
            name = _name;
            fieldNames = new ArrayList<String>();
            fieldTypes = new ArrayList<String>();
        }
        public boolean addField(String fieldName, String fieldType){
            boolean result = true;
            for (String fld : fieldNames) result &= (! fld.equals(fieldName));
            if (result) {
                fieldNames.add(fieldName);
                fieldTypes.add(fieldType);
            }
            return result;
        }
        public String getName() { return name; }
        public int getSize() { return fieldNames.size(); }
        public String getFieldName(int i) { return fieldNames.get(i);}
        public String getFieldType(int i) { return fieldTypes.get(i);}
    }
    public static class StxDataType extends StxStructure {
        public StxDataType(int _row, int _column, String _name) { super(_row, _column, _name); }
        public StxDataType(String _name) { super(_name); }
    }
    public static class StxRelation extends StxElement {
        ArrayList<StxChannel> channels;
        String block1;
        String block2;
        boolean blocking;
        int sizeOfFIFO;
        boolean asynchronous;
        public StxRelation(int _row, int _column) {
            super(_row, _column);
            channels = new ArrayList<StxChannel>();
            String block1 = null;
            String block2 = null;
        }
        public StxRelation() {
            super();
            channels = new ArrayList<StxChannel>();
            String block1 = null;
            String block2 = null;
        }
        public void setBlock1(String _name) { block1 = _name; }
        public void setBlock2(String _name) { block2 = _name; }
        public void setBlocking(Boolean _b) { blocking = _b; }
        public void setFifoSize(int _size) { sizeOfFIFO = _size; }
        public void setAsynchronous(boolean _b) { asynchronous = _b; }
        public void addChannel(StxChannel ch) { channels.add(ch); }
        public String getBlock1(String _name) { return block1; }
        public String getBlock2(String _name) { return block2; }
        public Boolean getBlocking(boolean _b) { return blocking; }
        public int getFifoSize(int _size) { return sizeOfFIFO; }
        public boolean getAsynchronous(boolean _b) { return asynchronous = _b; }
        public int getSize() { return channels.size(); }
        public StxChannel getChannel(int i) { return channels.get(i); }
    }
    public static class StxMethod extends StxStructure {
        public StxMethod(int _row, int _column, String _name) { super(_row, _column, _name); returnType = null; }
        public StxMethod(String _name) { super(_name); returnType = null; }
        private String returnType;
        public String getReturnType() { return returnType; }
        public void setReturnType(String _s) { returnType = _s; }
    public static class StxAttribute extends StxElement {
        String name;
        String type;
        String init;
        public StxAttribute(int _row, int _column, String _name, String _type){
            super(_row, _column);
            name = _name;
            type = _type;
            init = null;
        }
        public StxAttribute(String _name, String _type){
            super();
            name = _name;
            type = _type;
            init = null;
        }
        public String getName() { return name; }
        public String getType() { return type; }
        public void setInit(String _s) { init = _s; }
        public String getInit() { return init; }
    public class StxTimer extends StxElement {
        String name;
        public StxTimer(int _row, int _column, String _name){
            super(_row, _column);
            name = _name;
        }
        public StxTimer(String _name){
            super();
            name = _name;
        }
        public String getName() { return name; }
    }
    public static class StxBlock extends StxElement {
        String name;
        StxBlock father = null;
        ArrayList<StxAttribute> attributes;
        ArrayList<StxMethod> methods;
        ArrayList<StxSignal> signals;
        ArrayList<StxTimer> timers;
        HashMap<String, StxState> states;
        public StxBlock(int _row, int _column, String _name) {
            super(_row, _column);
            name = _name;
            father = null;
            attributes = new ArrayList<StxAttribute>();
            methods = new ArrayList<StxMethod>();
            signals = new ArrayList<StxSignal>();
            timers = new ArrayList<StxTimer>();
        }
        public StxBlock(String _name) {
            super();
            name = _name;
            father = null;
            attributes = new ArrayList<StxAttribute>();
            methods = new ArrayList<StxMethod>();
            signals = new ArrayList<StxSignal>();
            timers = new ArrayList<StxTimer>();
        }
        public void setFather(StxBlock _father) { father = _father; }
        public HashMap<String, StxState> getStates() { return states; }
        public void setStates(HashMap<String, StxState> m) {states = m; }
        public boolean addAttribute(StxAttribute a){
            boolean result = true;
            for (StxAttribute att : attributes) result &= (! att.getName().equals(a.getName()));
            if (result) attributes.add(a);
            return result;
        }
        public boolean addMethod(StxMethod m){
            boolean result = true;
            for (StxMethod mth : methods) result &= (! mth.getName().equals(m.getName()));
            if (result) methods.add(m);
            return result;
        }
        public boolean addSignal(StxSignal s){
            boolean result = true;
            for (StxSignal sg : signals) result &= (! sg.getName().equals(s.getName()));
            if (result) signals.add(s);
            return result;
        }
        public boolean addTimer(StxTimer t){
            boolean result = true;
            for (StxTimer tm : timers) result &= (! tm.getName().equals(t.getName()));
            if (result) timers.add(t);
            return result;
        }
        public String getName() { return name; }
        public StxBlock getFather() { return father; }
        public int getNbAttributes() { return attributes.size(); }
        public int getNbMethods() { return methods.size(); }
        public int getNbSignals() { return signals.size(); }
        public int getNbTimerss() { return timers.size(); }
Sophie Coudert's avatar
Sophie Coudert committed
        public StxAttribute getAttribute(int i) { return attributes.get(i); }
        public StxMethod getMethod(int i) { return methods.get(i); }
        public StxSignal getSignal(int i) { return signals.get(i); }
        public StxTimer getTimers(int i) { return timers.get(i); }
    public static class StxChannel extends StxElement {
        private StxInMessage inProfile;
        private StxOutMessage outProfile;
        private String blockA;
        private StxSignal signalA;
        private String blockB;
        private StxSignal signalB;
        public StxChannel(int _row, int _column){
            super(_row, _column);
            inProfile = null;
            outProfile = null;
            blockA = null;
            blockB = null;
            signalA = null;
            signalB = null;
        }
        public StxChannel(){
            super();
            inProfile = null;
            outProfile = null;
            blockA = null;
            blockB = null;
            signalA = null;
            signalB = null;
        }
        public void setInProfile(StxInMessage m) { inProfile = m; }
        public void setOutProfile(StxOutMessage m) { outProfile = m; }
        public void setSignal(String _block, StxSignal s) {
            if (signalA == null) { blockA = _block; signalA = s; }
            else if (signalB == null) { blockB = _block; signalB = s; }
        }
        public void commuteSignals(String _block1) {
            boolean permut =
                    (blockB != null && blockB.equals(_block1)) ||(blockA != null && ! blockB.equals(_block1));
            if (permut) {
                String auxStr = blockA;
                StxSignal auxSig = signalA;
                blockA = blockB;
                signalA = signalB;
                blockB = auxStr;
                signalB = auxSig;
            }
        }
        public void setSignalA(StxSignal s) { signalA = s; }
        public void setSignalB(StxSignal s) { signalB = s; }
        public StxInMessage getInProfile() { return inProfile; }
        public StxOutMessage getOutProfile() { return outProfile; }
        public StxSignal getSignalA() { return signalA; }
        public StxSignal getSignalB() { return signalB; }
    }
    public static class StxSignal extends StxElement {
        private String name;
        private boolean input;
        public StxSignal(int _row, int _column, String _name){
            super(_row, _column);
            name = _name;
        }
        public StxSignal(String _name){
            super();
            name = _name;
        }
        public void setInput(boolean _b) { input = _b; }
        public boolean isInput() { return input; }
        public String getName() { return name; }
    }
    public static class StxInMessage extends StxStructure {
        private StxChannel channel;
        public StxInMessage(int _row, int _column, String _name) { super(_row, _column, _name ); }
        public StxInMessage(String _name) { super(_name); }
        public void setChannel(StxChannel _c) { channel = _c; }
        public StxChannel getChannel() { return channel; }
    public static class StxOutMessage extends StxElement {
        private StxInMessage inMessage;
        private HashMap<String,String> fieldMap;
        public StxOutMessage(int _row, int _column, StxInMessage _inMessage) {
            super(_row, _column);
            inMessage = _inMessage;
            fieldMap = new HashMap<String,String>();
        public StxOutMessage(StxInMessage _inMessage) {
            super();
            inMessage = _inMessage;
            fieldMap = new HashMap<String,String>();
        }
        public boolean addField(String fieldName, String inFieldName){
            String test = fieldMap.get(inFieldName);
            if (test == null) {
                fieldMap.put(fieldName, inFieldName);
                return true;
            return false;
        public StxInMessage getInMessage() { return inMessage; }
        public HashMap<String,String> getFieldMap() { return fieldMap; }
Sophie Coudert's avatar
Sophie Coudert committed
    public static class StxState {
        private byte type;
        private String name;
        private StxSignal signal;
        private String variable;
        private String minValue;
        private String maxValue;
        private MyArray<StxTransition>  transitions;
        public StxState() {
            type = -1;
            name = null;
            signal = null;
            variable = null;
            minValue = null;
            maxValue = null;
            transitions = new MyArray<StxTransition>();
        }
        public byte getType() { return type; };
        public void setType(byte _b) { type = _b; };
        public String getName() { return name; };
        public void setName(String _s) { name = _s; };
        public StxSignal getSignal() { return signal; };
        public void setSignal(StxSignal _s) { signal = _s; };
        public String getVariable() { return variable; };
        public void setVariable(String _s) { variable = _s; };
        public String getMinValue() { return minValue; };
        public void setMinValue(String _s) { minValue = _s; };
        public String getMaxValue() { return maxValue; };
        public void setMaxValue(String _s) { maxValue = _s; };
        public StxTransition getTransition(int i) {
            if (i<0) return null;
            StxTransition result = transitions.get(i);
            if (result == null) {
                result = new StxTransition(i);
                transitions.set(i,result);
            }
            return result;
        }
    }
    public static class StxTransition {
Sophie Coudert's avatar
Sophie Coudert committed
        private byte type;
        private final int index;
        private String guard;
        private StxSignal signal;
        private String timer;
Sophie Coudert's avatar
Sophie Coudert committed
        private ArrayList<String> sendPayload;
        private HashMap<String,String> receivePayload;
        private String minDelay;
        private String maxDelay;
        private String delayDistributionLaw;
        private String delayExtraNameA;
        private String delayExtraNameB;
        private String delayExtraValA;
        private String delayExtraValB;
        private StxState target;
        private ArrayList<StxAction> actions;
        public StxTransition(int _index){
            type = -1;
            index = _index;
            guard = null;
            signal = null;
            sendPayload = new ArrayList<String>();
            receivePayload = new HashMap<String,String>();
            minDelay = null;
            maxDelay = null;
            delayDistributionLaw = null;
            delayExtraNameA = null;
            delayExtraNameB = null;
            delayExtraValA = null;
            delayExtraValB = null;
            target = null;
            actions = new ArrayList<StxAction>();
        }
        public void setType(byte _t) { type = _t; };
        public void setGuard(String _s) { guard = _s; }
        public void setSignal(StxSignal _s) { signal = _s; }
        public void setSendPayload(ArrayList<String> _p) { sendPayload = _p; }
        public void setReceivePayload(HashMap<String,String> _p) { receivePayload = _p; }
        public void setTimer(String _s) { timer = _s; }
         public void setMinDelay(String _s) { minDelay = _s; }
       public void setMaxDelay(String _s) { maxDelay = _s; }
Sophie Coudert's avatar
Sophie Coudert committed
        public void setDelayDistributionLaw(String _s) { delayDistributionLaw = _s; }
        public void setDelayExtraNameA(String _s) { delayExtraNameA = _s; }
        public void setDelayExtraNameB(String _s) { delayExtraNameB = _s; }
        public void setDelayExtraValA(String _s) { delayExtraValA = _s; }
        public void setDelayExtraValB(String _s) { delayExtraValB = _s; }
        public void setTarget(StxState _s) { target = _s; }
        public void setActions(ArrayList<StxAction> _a) { actions = _a; }
        public byte getType() { return type; }
        public int getIndex() { return index; }
        public String getGuard() { return guard; }
        public String getTimer() { return timer; }
Sophie Coudert's avatar
Sophie Coudert committed
        public StxSignal getSignal() { return signal; }
        public ArrayList<String> getSendPayload() { return sendPayload; }
        public HashMap<String, String> getReceivePayload() { return receivePayload; }
        public String getMinDelay() { return minDelay; }
        public String getMaxDelay() { return maxDelay; }
        public String getDelayDistributionLaw() { return delayDistributionLaw; }
        public String getDelayExtraNameA() { return delayExtraNameA; }
        public String getDelayExtraNameB() { return delayExtraNameB; }
        public String getDelayExtraValA() { return delayExtraValA; }
        public String getDelayExtraValB() { return delayExtraValB; }
        public StxState getTarget() { return target; }
        public ArrayList<StxAction> getActions() { return actions; }
    }
    public class StxAction{}
    public static final byte STXSTARTSTATE = 0;
    public static final byte STXSTOPSTATE = 1;
    public static final byte STXSTANDARDSTATE = 2;
    public static final byte STXRANDOMSTATE = 3;
    public static final byte STXCOUNTSTATE = 4;
    public static final byte STXSENDSTATE = 5;
    public static final byte STXRECEIVESTATE = 6;
    public static final byte STXPRESENDSTATE = 7;
    public static final byte STXPRERECEIVESTATE = 8;
    public static final byte STXSETTIMERSTATE = 9;
    public static final byte STXRESETTIMERSTATE = 10;
    public static final byte STXEXPIRETIMERSTATE = 11;
    public static final byte STXPRESETTIMERSTATE = 12;
    public static final byte STXPRERESETTIMERSTATE = 13;
    public static final byte STXPREEXPIRETIMERSTATE = 14;
    public static final byte STXTRIVIALTRANSITION = 0;
    public static final byte STXSENDTRANSITION = 1;
    public static final byte STXRECEIVETRANSITION = 2;
Sophie Coudert's avatar
Sophie Coudert committed
    public static final byte STXSETTIMERTRANSITION = 3;
    public static final byte STXRESETTIMERTRANSITION = 4;
    public static final byte STXEXPIRETIMERTRANSITION = 5;