package avatartranslator.tosysmlv2; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.function.BiConsumer; import java_cup.runtime.ComplexSymbolFactory.Location; 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); } @Override public E get(int index){ if (index >= size()) return null; else return super.get(index); } } 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; 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 STXTRIVIALTRANSITION = 0; public static final byte STXSENDTRANSITION = 1; public static final byte STXRECEIVETRANSITION = 2; public static final byte STXSETTIMERTRANSITION = 3; public static final byte STXRESETTIMERTRANSITION = 4; public static final byte STXEXPIRETIMERTRANSITION = 5; public static final byte STXASSIGNACTION = 0; public static final byte STXMETHODACTION = 1; /** idents collected while parsing an Avatar SysML Model */ public static class Ident extends StxElement { private byte type; private String sysMLName; private String avatarName; public Ident(byte _type, String _smlName) { super(); setTypeName(_type, _smlName); } public Ident(Location _left, Location _right, byte _type, String _smlName) { super(_left, _right); setTypeName(_type, _smlName); }; private void setTypeName(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; } } } 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 StxTerm { public String value ; protected StxTerm(String v) { value = v; } } public static class StxExpr extends StxTerm { public StxExpr(String v) { super(v); } } public static class StxCall extends StxTerm { public StxCall(String v) { super(v); } } public abstract static class StxElement { protected Location defaultLocation = new Location(-1,-1); private Location left; private Location right; public StxElement(Location _left, Location _right){ left = _left; right = _right; } public StxElement(){ left = defaultLocation; right = defaultLocation; } public void setLeft(Location _left) { left = _left; } public void setRight(Location _right) { right = _right; } public Location getLeft() { return left; } public Location getRight() { return right; } } public static class StxStructure extends StxElement { private String name; protected MyArray<String> fieldNames; protected MyArray<String> fieldTypes; public StxStructure(Location _left, Location _right, String _name) { super(_left, _right); name = _name; fieldNames = new MyArray<String>(); fieldTypes = new MyArray<String>(); } public StxStructure(String _name) { super(); name = _name; fieldNames = new MyArray<String>(); fieldTypes = new MyArray<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(Location _left, Location _right, String _name) { super(_left, _right, _name); } public StxDataType(String _name) { super(_name); } } public static class StxRelation extends StxElement { ArrayList<StxChannel> channels; String block1; String block2; boolean blocking; boolean lossy; boolean isprivate; int sizeOfFIFO; boolean asynchronous; public StxRelation(Location _left, Location _right) { super(_left, _right); channels = new ArrayList<StxChannel>(); isprivate = false; lossy = false; String block1 = null; String block2 = null; } public StxRelation() { super(); channels = new ArrayList<StxChannel>(); isprivate = false; lossy = false; 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 setPrivate(Boolean _b) { isprivate = _b; } public void setLossy(Boolean _b) { lossy = _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() { return block1; } public String getBlock2() { return block2; } public Boolean getBlocking() { return blocking; } public Boolean getPrivate() { return isprivate; } public Boolean getLossy() { return lossy; } public int getFifoSize() { return sizeOfFIFO; } public boolean getAsynchronous() { return asynchronous; } public int getSize() { return channels.size(); } public StxChannel getChannel(int i) { return channels.get(i); } } public static class StxMethod extends StxStructure { public StxMethod(Location _left, Location _right, String _name) { super(_left, _right, _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(Location _left, Location _right, String _name, String _type){ super(_left, _right); 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 static class StxTimer extends StxAttribute { public StxTimer(Location _left, Location _right, String _name){ super(_left, _right, _name, ""); } public StxTimer(String _name){ super(_name, ""); } } public static class StxBlock extends StxElement { String name; StxBlock father = null; ArrayList<StxAttribute> attributes; ArrayList<StxAttribute> constants; ArrayList<StxMethod> methods; ArrayList<StxSignal> signals; ArrayList<StxTimer> timers; StxState[] states; public StxBlock(Location _left, Location _right, String _name) { super(_left, _right); 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 StxState[] getStates() { return states; } public void setStates(StxState[] l) { states = l; } 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 addConstant(StxAttribute a){ boolean result = true; for (StxAttribute att : constants) result &= (! att.getName().equals(a.getName())); if (result) constants.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 getNbConstants() { return constants.size(); } public int getNbMethods() { return methods.size(); } public int getNbSignals() { return signals.size(); } public int getNbTimers() { return timers.size(); } public StxAttribute getAttribute(int i) { return attributes.get(i); } public StxAttribute getConstant(int i) { return constants.get(i); } public StxMethod getMethod(int i) { return methods.get(i); } public StxSignal getSignal(int i) { return signals.get(i); } public StxTimer getTimer(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(Location _left, Location _right){ super(_left, _right); 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(Location _left, Location _right, String _name){ super(_left, _right); 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 { protected StxChannel channel; public StxInMessage(Location _left, Location _right) { super(_left, _right, null ); } public StxInMessage() { super(null); } public void setChannel(StxChannel c) {channel = c; } public int indexOf(String s) { return fieldNames.indexOf(s); } } public static class StxOutMessage extends StxInMessage { public StxInMessage inMessage; private HashMap<String,String> fieldMap; public StxOutMessage(Location _left, Location _right, StxInMessage _inMessage) { super(_left, _right); inMessage = _inMessage; fieldMap = new HashMap<String,String>(); } public StxOutMessage(StxInMessage _inMessage) { super(); inMessage = _inMessage; fieldMap = new HashMap<String,String>(); } public boolean addFieldLink(String fieldName, String inFieldName){ String test = fieldMap.get(inFieldName); if (test == null) { fieldMap.put(fieldName, inFieldName); return true; } return false; } private class Complete implements BiConsumer<String, String> { public Complete(){}; public void accept(String o, String i) { int index = inMessage.fieldNames.indexOf(i); fieldNames.set(index,o); fieldTypes.set(index, inMessage.fieldTypes.get(index)); } } public void complete() { fieldMap.forEach(new Complete()); inMessage.channel.setOutProfile(this); } } public static class StxState extends StxElement { 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 List<StxTransition> getTransitions() { return transitions; } 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 extends StxElement { private byte type; private final int index; private String guard; private StxSignal signal; private String timer; private ArrayList<String> sendPayload; private HashMap<String,String> receivePayload; private String minDelay; private String maxDelay; private double probability; private String delayDistributionLaw; private HashMap<String,String> delayExtra; private StxState target; private ArrayList<StxAction> actions; private Location rleft; private Location rright; public StxTransition(int _index){ super(); type = -1; index = _index; guard = null; signal = null; sendPayload = null; receivePayload = null; minDelay = null; maxDelay = null; delayDistributionLaw = null; delayExtra = null; target = null; actions = new ArrayList<StxAction>(); rleft = defaultLocation; rright = defaultLocation; } 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; } public void setProbability(double _p) { probability = _p; } public void setDelayDistributionLaw(String _s) { delayDistributionLaw = _s; } public void setDelayExtra(HashMap<String,String> _m) { delayExtra = _m; } public void setTarget(StxState _s) { target = _s; } public void setActions(ArrayList<StxAction> _a) { actions = _a; } public void setRleft(Location _l) { rleft = _l; } public void setRright(Location _l) { rright = _l; } public byte getType() { return type; } public int getIndex() { return index; } public String getGuard() { return guard; } public String getTimer() { return timer; } 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 double getProbability() { return probability; } public String getDelayDistributionLaw() { return delayDistributionLaw; } public HashMap<String,String> getDelayExtra() { return delayExtra; } public StxState getTarget() { return target; } public ArrayList<StxAction> getActions() { return actions; } public Location setRleft() { return rleft; } public Location setRright() { return rright; } } public static class StxAction extends StxElement { private byte type; private String value; private String target; public StxAction (String _value) { super(); type = STXMETHODACTION; value = _value; } public StxAction (Location _left, Location _right, String _value) { super(_left, _right); type = STXMETHODACTION; value = _value; } public StxAction (String _target, String _value) { super(); type = STXASSIGNACTION; value = _value; target = _target; } public StxAction (Location _left, Location _right, String _target, String _value) { super(_left, _right); type = STXASSIGNACTION; value = _value; target = _target; } public byte getType() { return type; } public String getValue() { return value; } public String getTarget() { return target; } } public static class StxModel{ private ArrayList<String> errors; private HashMap<String,StxDataType> dataTypeMap; private HashMap<String,StxRelation> relationMap; private HashMap<String,StxChannel> channelMap; private HashMap<String,StxBlock> blockMap; private HashMap<String,StxSignal> signalMap; public StxModel( ArrayList<String> _errors, HashMap<String,StxDataType> _dataTypeMap, HashMap<String,StxRelation> _relationMap, HashMap<String,StxChannel> _channelMap, HashMap<String,StxBlock> _blockMap, HashMap<String,StxSignal> _signalMap) { errors = _errors; dataTypeMap = _dataTypeMap; relationMap = _relationMap; channelMap = _channelMap; blockMap = _blockMap; signalMap = _signalMap; } public ArrayList<String> getErrors() { return errors; } public HashMap<String,StxDataType> getDataTypeMap() { return dataTypeMap; } public HashMap<String,StxRelation> getRelationMap() { return relationMap; } public HashMap<String,StxChannel> getChannelMap() { return channelMap; } public HashMap<String,StxBlock> getBlockMap() { return blockMap; } public HashMap<String,StxSignal> getSignalMap() { return signalMap; } } }