diff --git a/src/main/java/avatartranslator/tosysmlv2/Avatar2SysML.java b/src/main/java/avatartranslator/tosysmlv2/Avatar2SysML.java index eeec4f2e6f74fbe005aa2ba4481ee9d0538e9a51..4b58996bad24ccadb7d2ade9210a529070fbf204 100644 --- a/src/main/java/avatartranslator/tosysmlv2/Avatar2SysML.java +++ b/src/main/java/avatartranslator/tosysmlv2/Avatar2SysML.java @@ -191,6 +191,10 @@ public class Avatar2SysML { else return null; } + String removeFieldName(AvatarAttribute aa) { + if (aa.getDataType() == null) return null; + return removeFieldName(aa.getName(), aa.getDataType()); + } void dataTypes2SysML(){ if (avspec.getDataTypes() == null || avspec.getDataTypes().size() == 0) return; avsysml.append("\n" + indent + "// DATATYPES $$$$$$$$$$$$$$$$$$$$$$$$\n"); @@ -352,8 +356,8 @@ public class Avatar2SysML { } else { attributeType = dataTypeSysMLname(dataType.getName()) + ";\n"; type = AvatarDataType.DATATYPE; - fieldName1 = fieldSysMLname(removeFieldName(sig1.getListOfAttributes().get(j).getName(),dataType)); - fieldName2 = fieldSysMLname(removeFieldName(sig2.getListOfAttributes().get(j).getName(),dataType)); + fieldName1 = fieldSysMLname(removeFieldName(sig1.getListOfAttributes().get(j))); + fieldName2 = fieldSysMLname(removeFieldName(sig2.getListOfAttributes().get(j))); j += dataType.getFullSize(); } if (in2out) { @@ -427,7 +431,7 @@ public class Avatar2SysML { } j++; } else { - avsysml.append(indent + "attribute " + fieldSysMLname(removeFieldName(aa.getName(), fieldDataType)) + avsysml.append(indent + "attribute " + fieldSysMLname(removeFieldName(aa)) + " : " + dataTypeSysMLname(fieldDataType.getName())+ "\n"); methodInfo.addField(fieldSysMLname(aa.getName()), AvatarDataType.DATATYPE, fieldDataType); j += fieldDataType.getFullSize(); @@ -478,7 +482,7 @@ public class Avatar2SysML { avsysml.append(";\n"); j++; } else { - avsysml.append(indent + "attribute " + attributeSysMLname(removeFieldName(aa.getName(), dataType)) + + avsysml.append(indent + "attribute " + attributeSysMLname(removeFieldName(aa)) + " : " + dataTypeSysMLname(dataType.getName()) + ";\n"); j += dataType.getFullSize(); } @@ -985,19 +989,18 @@ public class Avatar2SysML { else { result.append(indent + "payload= " + signalInfo.getMessageType() + "(\n"); indent += indentStep; - int j = 0; + int k = 0; while (j < nbFields) { //for(String vl : values) if (signalInfo.getFieldType(j) != AvatarDataType.DATATYPE) { - result.append(indent + expr2SysML(values.get(j)) + ",\n"); - j++; + result.append(indent + expr2SysML(values.get(k)) + ",\n"); + k++; j++; } else { AvatarDataType dt = signalInfo.getDataType(j); - result.append(indent + expr2SysML(removeFieldName(values.get(j), dt)) + ",\n"); - j += dt.getFullSize(); + result.append(indent + expr2SysML(removeFieldName(values.get(k), dt)) + ",\n"); + k += dt.getFullSize(); j++; } } - result.replace(result.length()-2, result.length(), " )\n"); indent = indent.substring(indentStepSize); } @@ -1086,6 +1089,32 @@ public class Avatar2SysML { result.append(indent + ")"); return result.toString(); } + + String methodCall2SysML(AvatarTermFunction m) { + MethodInfo methodInfo = methodMap.get(m.getMethod()); + List<AvatarTerm> parameters = m.getArgs().getComponents(); + StringBuffer result = new StringBuffer(methodInfo.getName() + "("); + int nbFields = parameters.size(); + int j = 0; // index in method profile; + int k = 0; // index in method parameters; + while (j < nbFields) { + if (methodInfo.getFieldType(j) != AvatarDataType.DATATYPE) { + if (parameters.get(k) instanceof AvatarTermFunction) + result.append(methodCall2SysML((AvatarTermFunction) parameters.get(k)) + ","); + else + result.append(indent + expr2SysML(parameters.get(k).getName()) + ","); + k++; + j++; + } else { + AvatarDataType dt = methodInfo.getDataType(j); + result.append(expr2SysML(removeFieldName(((AvatarAttribute) parameters.get(k)).getName(), dt)) + ","); + k += dt.getFullSize(); + j++; + } + } + result.replace(result.length() - 1, result.length(), ")"); + return result.toString(); + } String transitionActions2SysM(List<AvatarAction> aas) { StringBuffer result; if (aas == null || aas.size() == 0) return ""; @@ -1094,13 +1123,23 @@ public class Avatar2SysML { for(AvatarAction aa : aas) { if(aa instanceof AvatarActionAssignment) { AvatarLeftHand lh = ((AvatarActionAssignment)aa).getLeftHand(); - if(lh instanceof AvatarAttribute) { // tuples are not yet handled + if(lh instanceof AvatarAttribute) result.append(indent + "then assign " + attributeSysMLname(lh.getName()) + ":= "); - result.append(expr2SysML(((AvatarActionAssignment)aa).getRightHand().getName()) + ";\n"); + else { // AvatarTuple of field-attributes + result.append(indent + "then assign " + + attributeSysMLname(removeFieldName((AvatarAttribute)((AvatarTuple) lh).getComponents().get(0))) + ":= "); } + AvatarTerm rh = ((AvatarActionAssignment)aa).getRightHand(); + if (rh instanceof AvatarTuple) + result.append( + attributeSysMLname(removeFieldName((AvatarAttribute)((AvatarTuple) rh).getComponents().get(0))) + ";\n"); + else if (rh instanceof AvatarTermFunction) + result.append(methodCall2SysML((AvatarTermFunction)rh) + ";\n"); + else + result.append(expr2SysML(rh.getName()) + ";\n"); } else { - result.append(indent + "then action = " + expr2SysML(aa.getName()) + ";\n"); + result.append(indent + "then action = " + methodCall2SysML((AvatarTermFunction)aa) + ";\n"); } } result.append(indent + "then done;\n"); @@ -1109,16 +1148,26 @@ public class Avatar2SysML { return result.toString(); } - String receiveActions2SysML(MethodInfo chinfo, List<String> values) { + String receiveActions2SysML(MethodInfo signalInfo, List<String> values) { if (values == null || values.size() == 0) return ""; StringBuffer result = new StringBuffer(indent + "do action {\n"); indent += indentStep; - result.append(indent + "item msg : " + chinfo.getMessageType() + " = response.payload as " + chinfo.getMessageType() + + result.append(indent + "item msg : " + signalInfo.getMessageType() + " = response.payload as " + signalInfo.getMessageType() + ";\n" + indent + "first start;\n"); - List<String> fields = null; //chinfo.getFieldName(); - 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"); + int nbFields = signalInfo.getArity(); + int j = 0; + int k = 0; + while (j < nbFields) { + if (signalInfo.getFieldType(j) != AvatarDataType.DATATYPE) { + result.append(indent + "then assign " + attributeSysMLname(values.get(k)) + + " := msg." + signalInfo.getFieldName(j) + ";\n"); + k++; j++; + } else { + AvatarDataType dt = signalInfo.getDataType(j); + result.append(indent + "then assign " + attributeSysMLname(removeFieldName(values.get(k), dt)) + + " := msg." + signalInfo.getFieldName(j) + ";\n"); + k += dt.getFullSize(); j++; + } } result.append(indent + "then done;\n"); indent = indent.substring(indentStepSize); diff --git a/src/main/java/avatartranslator/tosysmlv2/Avatar2SysMLLexer.java b/src/main/java/avatartranslator/tosysmlv2/Avatar2SysMLLexer.java index 5bca1fd9d0d26527be2a73d057b6a6b36f51a306..6e78fff8ad7b567b7783a514aa0409f12a0a6afa 100644 --- a/src/main/java/avatartranslator/tosysmlv2/Avatar2SysMLLexer.java +++ b/src/main/java/avatartranslator/tosysmlv2/Avatar2SysMLLexer.java @@ -1,4 +1,4 @@ -/* The following code was generated by JFlex 1.4.3 on 09/04/2024 14:20 */ +/* The following code was generated by JFlex 1.4.3 on 24/04/2024 15:37 */ /* Copyright or (C) or Copr. GET / ENST, Telecom-Paris, Ludovic Apvrille * @@ -45,7 +45,7 @@ import java_cup.runtime.*; /** * This class is a scanner generated by * <a href="http://www.jflex.de/">JFlex</a> 1.4.3 - * on 09/04/2024 14:20 from the specification file + * on 24/04/2024 15:37 from the specification file * <tt>__Avatar2SysMLLexer__.jflex</tt> */ public class Avatar2SysMLLexer implements java_cup.runtime.Scanner { @@ -623,65 +623,65 @@ public class Avatar2SysMLLexer implements java_cup.runtime.Scanner { { return new Symbol(Avatar2SysMLSymbols.COMMA); } case 30: break; - case 4: - { return new Symbol(Avatar2SysMLSymbols.LEAVE, "'$" + yytext() + "'"); - } - case 31: break; case 23: { return new Symbol(Avatar2SysMLSymbols.LEAVE, "false"); } - case 32: break; + case 31: break; case 5: { return new Symbol(Avatar2SysMLSymbols.PLUS); } - case 33: break; + case 32: break; case 12: { return new Symbol(Avatar2SysMLSymbols.GT); } - case 34: break; + case 33: break; case 21: { return new Symbol(Avatar2SysMLSymbols.GEQ); } - case 35: break; + case 34: break; case 9: { return new Symbol(Avatar2SysMLSymbols.MOD); } - case 36: break; + case 35: break; case 19: { return new Symbol(Avatar2SysMLSymbols.EQ); } - case 37: break; + case 36: break; case 14: { return new Symbol(Avatar2SysMLSymbols.RPAR); } - case 38: break; + case 37: break; case 17: { return new Symbol(Avatar2SysMLSymbols.OR); } - case 39: break; + case 38: break; case 20: { return new Symbol(Avatar2SysMLSymbols.LEQ); } - case 40: break; + case 39: break; case 1: { throw new Exception ("Unknown character in expression"); } - case 41: break; + case 40: break; case 6: { return new Symbol(Avatar2SysMLSymbols.MINUS); } - case 42: break; + case 41: break; case 7: { return new Symbol(Avatar2SysMLSymbols.MULT); } - case 43: break; + case 42: break; case 22: { return new Symbol(Avatar2SysMLSymbols.LEAVE, "true"); } - case 44: break; + case 43: break; case 13: { return new Symbol(Avatar2SysMLSymbols.LPAR); } + case 44: break; + case 4: + { return new Symbol(Avatar2SysMLSymbols.LEAVE, yytext()); + } case 45: break; case 2: { diff --git a/src/main/java/avatartranslator/tosysmlv2/Avatar2SysMLNames.java b/src/main/java/avatartranslator/tosysmlv2/Avatar2SysMLNames.java index fdb472d5715a260512169bf8ca73caf43ddd175c..64ba45acd05ea4a66cce931b61b8cf82ef467418 100644 --- a/src/main/java/avatartranslator/tosysmlv2/Avatar2SysMLNames.java +++ b/src/main/java/avatartranslator/tosysmlv2/Avatar2SysMLNames.java @@ -37,57 +37,68 @@ */ package avatartranslator.tosysmlv2; import java.io.StringReader; +import java.util.List; + public class Avatar2SysMLNames { public final static int SYNC = 0; public final static int BFIFO = 1; public final static int NBFIFO = 2; - public static String dataTypeSysMLname(String name){ return "'@dt:" + name + "'"; } - public static String blockSysMLname(String name){ return "'@blk:" + name + "'"; } - public static String attributeSysMLname(String name){ return "'$" + name + "'"; } - public static String methodSysMLname(String name){ return "'$" + name + "'"; } - public static String fieldSysMLname(String name){ return "'" + name + "'"; } + public static String dataTypeSysMLname(String name){ return "'@dt:" + name.trim() + "'"; } + public static String blockSysMLname(String name){ return "'@blk:" + name.trim() + "'"; } + public static String attributeSysMLname(String _name){ + if (_name == null) return null; + String name = _name.trim(); + String[] list = name.split("__"); + StringBuffer result = new StringBuffer("'$" + list[0] + "'"); + int length = list.length; + for(int i = 1; i < length; i++) + result.append(".'" + list[i] + "'"); + return result.toString(); + } + public static String methodSysMLname(String name){ return "'$" + name.trim() + "'"; } + public static String fieldSysMLname(String name){ return "'" + name.trim() + "'"; } public static String relationSysMLname(String b1, String b2, int type, int n){ - if (type == NBFIFO) return "'@NBF" + n + ":" + b1 + "-" + b2 + "'"; - if (type == BFIFO) return "'@BF" + n + ":" + b1 + "-" + b2 + "'"; - if (type == SYNC) return "'@SYN" + n + ":" + b1 + "-" + b2 + "'"; + if (type == NBFIFO) return "'@NBF" + n + ":" + b1.trim() + "-" + b2.trim() + "'"; + if (type == BFIFO) return "'@BF" + n + ":" + b1.trim() + "-" + b2.trim() + "'"; + if (type == SYNC) return "'@SYN" + n + ":" + b1.trim() + "-" + b2.trim() + "'"; return ""; } public static String channelName(String b1, String b2, String s1, String s2, boolean in2out){ - if(in2out) return b1 + "." + s1 + ">" + b2 + "." + s2; - else return b1 + "." + s1 + "<" + b2 + "." + s2; + if(in2out) return b1.trim() + "." + s1.trim() + ">" + b2.trim() + "." + s2.trim(); + else return b1.trim() + "." + s1.trim() + "<" + b2.trim() + "." + s2.trim(); } public static String signalSysMLname(String s){ return "'@sig:" + s + "'"; } public static String channelSysMLname(String _signalname, int type){ - if (type == NBFIFO) return "'@nbf:" + _signalname + "'"; - if (type == BFIFO) return "'@bf:" + _signalname + "'"; - if (type == SYNC) return "'@syn:" + _signalname + "'"; + if (type == NBFIFO) return "'@nbf:" + _signalname.trim() + "'"; + if (type == BFIFO) return "'@bf:" + _signalname.trim() + "'"; + if (type == SYNC) return "'@syn:" + _signalname.trim() + "'"; return ""; } public static String messageSysMLname(String _blockname, String _signalsname){ - return "'@MSG:" + _blockname + "." + _signalsname + "'"; + return "'@MSG:" + _blockname.trim() + "." + _signalsname.trim() + "'"; } public static String startStateSysMLname(){ return "'@st:start'"; } public static String stopStateSysMLname(){ return "'@st:stop'"; } - public static String standardStateSysMLname(String name){ return "'@st:standard." + name + "'"; } + public static String standardStateSysMLname(String name){ return "'@st:standard." + name.trim() + "'"; } public static String randomStateSysMLname(int number){ return "'@st:random." + number + "'"; } - public static String countStateSysMLname(String sigName, int number){ return "'@st:count." + sigName + "." + number + "'"; } - public static String sendStateSysMLname(String sigName, int number){ return "'@st:send." + sigName + "." + number + "'"; } - public static String receiveStateSysMLname(String sigName, int number){ return "'@st:receive." + sigName + "." + number + "'"; } - public static String presendStateSysMLname(String sigName, int number){ return "'@st:presend." + sigName + "." + number + "'"; } - public static String prereceiveStateSysMLname(String sigName, int number){ return "'@st:prereceive." + sigName + "." + number + "'"; } - public static String setTimerStateSysMLname(String timerName, int number){ return "'@st:set." + timerName + "." + number + "'"; } - public static String resetTimerStateSysMLname(String timerName, int number){ return "'@st:reset." + timerName + "." + number + "'"; } - public static String expireTimerStateSysMLname(String timerName, int number){ return "'@st:expire." + timerName + "." + number + "'"; } - public static String presetTimerStateSysMLname(String timerName, int number){ return "'@st:preset." + timerName + "." + number + "'"; } - public static String preresetTimerStateSysMLname(String timerName, int number){ return "'@st:prereset." + timerName + "." + number + "'"; } - public static String preexpireTimerStateSysMLname(String timerName, int number){ return "'@st:preexpire." + timerName + "." + number + "'"; } - public static String timerBlockSysMLname(String timerName){ return "'@@tmr:" + timerName + "'"; } - public static String timerRelationSysMLname(String timerName){ return "'TMRRel:" + timerName + "'"; } - public static String setTimerChannelSysMLname(String timerName){ return "'@ch:set." + timerName + "'"; } - public static String resetTimerChannelSysMLname(String timerName){ return "'@ch:reset." + timerName + "'"; } - public static String expireTimerChannelSysMLname(String timerName){ return "'@ch:expire." + timerName + "'"; } + public static String countStateSysMLname(String sigName, int number){ return "'@st:count." + sigName.trim() + "." + number + "'"; } + public static String sendStateSysMLname(String sigName, int number){ return "'@st:send." + sigName.trim() + "." + number + "'"; } + public static String receiveStateSysMLname(String sigName, int number){ return "'@st:receive." + sigName.trim() + "." + number + "'"; } + public static String presendStateSysMLname(String sigName, int number){ return "'@st:presend." + sigName.trim() + "." + number + "'"; } + public static String prereceiveStateSysMLname(String sigName, int number){ return "'@st:prereceive." + sigName.trim() + "." + number + "'"; } + public static String setTimerStateSysMLname(String timerName, int number){ return "'@st:set." + timerName.trim() + "." + number + "'"; } + public static String resetTimerStateSysMLname(String timerName, int number){ return "'@st:reset." + timerName.trim() + "." + number + "'"; } + public static String expireTimerStateSysMLname(String timerName, int number){ return "'@st:expire." + timerName.trim() + "." + number + "'"; } + public static String presetTimerStateSysMLname(String timerName, int number){ return "'@st:preset." + timerName.trim() + "." + number + "'"; } + public static String preresetTimerStateSysMLname(String timerName, int number){ return "'@st:prereset." + timerName.trim() + "." + number + "'"; } + public static String preexpireTimerStateSysMLname(String timerName, int number){ return "'@st:preexpire." + timerName.trim() + "." + number + "'"; } + public static String timerBlockSysMLname(String timerName){ return "'@@tmr:" + timerName.trim() + "'"; } + public static String timerRelationSysMLname(String timerName){ return "'TMRRel:" + timerName.trim() + "'"; } + public static String setTimerChannelSysMLname(String timerName){ return "'@ch:set." + timerName.trim() + "'"; } + public static String resetTimerChannelSysMLname(String timerName){ return "'@ch:reset." + timerName.trim() + "'"; } + public static String expireTimerChannelSysMLname(String timerName){ return "'@ch:expire." + timerName.trim() + "'"; } public static String expr2SysML(String _expr) { Avatar2SysMLParser parser = new Avatar2SysMLParser(new Avatar2SysMLLexer(new StringReader (_expr))); diff --git a/src/main/java/avatartranslator/tosysmlv2/Avatar2SysMLParser.java b/src/main/java/avatartranslator/tosysmlv2/Avatar2SysMLParser.java index 4a90e273f2fcea8b3ff9307d63f18b186b74f288..f2dd737c468bfefd87de48c23811bc34822cea0f 100644 --- a/src/main/java/avatartranslator/tosysmlv2/Avatar2SysMLParser.java +++ b/src/main/java/avatartranslator/tosysmlv2/Avatar2SysMLParser.java @@ -6,6 +6,7 @@ package avatartranslator.tosysmlv2; import java_cup.runtime.*; +import static avatartranslator.tosysmlv2.Avatar2SysMLNames.*; import java_cup.runtime.XMLElement; /** CUP v0.11b 20210814 (GIT d69c832) generated parser. @@ -487,7 +488,7 @@ class CUP$Avatar2SysMLParser$actions { int lleft = (CUP$Avatar2SysMLParser$stack.elementAt(CUP$Avatar2SysMLParser$top-1)).left; int lright = (CUP$Avatar2SysMLParser$stack.elementAt(CUP$Avatar2SysMLParser$top-1)).right; String l = CUP$Avatar2SysMLParser$stack.elementAt(CUP$Avatar2SysMLParser$top-1).<String>value(); - RESULT = i + "(" + l + ")"; + RESULT = methodSysMLname(i) + "(" + l + ")"; CUP$Avatar2SysMLParser$result = parser.getSymbolFactory().newSymbol("expr",0, CUP$Avatar2SysMLParser$stack.elementAt(CUP$Avatar2SysMLParser$top-3), CUP$Avatar2SysMLParser$stack.peek(), RESULT); } return CUP$Avatar2SysMLParser$result; @@ -499,7 +500,7 @@ class CUP$Avatar2SysMLParser$actions { int eleft = (CUP$Avatar2SysMLParser$stack.peek()).left; int eright = (CUP$Avatar2SysMLParser$stack.peek()).right; String e = CUP$Avatar2SysMLParser$stack.peek().<String>value(); - RESULT = e; + RESULT = attributeSysMLname(e); CUP$Avatar2SysMLParser$result = parser.getSymbolFactory().newSymbol("expr",0, CUP$Avatar2SysMLParser$stack.peek(), CUP$Avatar2SysMLParser$stack.peek(), RESULT); } return CUP$Avatar2SysMLParser$result; diff --git a/src/main/java/avatartranslator/tosysmlv2/javacup/Avatar2SysMLLexer.jflex b/src/main/java/avatartranslator/tosysmlv2/javacup/Avatar2SysMLLexer.jflex index 245d3c662b8b8d0e1973596d868a3c0be6966147..f31df2fca74eebe67e3d1fd81550be6c5b761eaf 100644 --- a/src/main/java/avatartranslator/tosysmlv2/javacup/Avatar2SysMLLexer.jflex +++ b/src/main/java/avatartranslator/tosysmlv2/javacup/Avatar2SysMLLexer.jflex @@ -86,6 +86,6 @@ Identifier = [a-zA-Z_][a-zA-Z0-9_\.]* "(" { return new Symbol(#Symb.LPAR); } ")" { return new Symbol(#Symb.RPAR); } "," { return new Symbol(#Symb.COMMA); } - {Identifier} { return new Symbol(#Symb.LEAVE, "'$" + yytext() + "'"); } + {Identifier} { return new Symbol(#Symb.LEAVE, yytext()); } . { throw new Exception ("Unknown character in expression"); } } diff --git a/src/main/java/avatartranslator/tosysmlv2/javacup/Avatar2SysMLParser.jcup b/src/main/java/avatartranslator/tosysmlv2/javacup/Avatar2SysMLParser.jcup index 94d2023683e7b11fbbe843f06d73de24b4593467..1baa19a91767dd7829aa70247084a2a7fbe7ffeb 100644 --- a/src/main/java/avatartranslator/tosysmlv2/javacup/Avatar2SysMLParser.jcup +++ b/src/main/java/avatartranslator/tosysmlv2/javacup/Avatar2SysMLParser.jcup @@ -40,6 +40,7 @@ package avatartranslator.tosysmlv2; import java_cup.runtime.*; +import static avatartranslator.tosysmlv2.Avatar2SysMLNames.*; parser code {: :} @@ -84,8 +85,8 @@ expr ::= | expr:l DIF expr:r {: RESULT = l + " != " + r; :} | LPAR expr:e RPAR {: RESULT = "(" + e + ")"; :} | LEAVE:i LPAR RPAR {: RESULT = i + "()"; :} - | LEAVE:i LPAR exprlist:l RPAR {: RESULT = i + "(" + l + ")"; :} - | LEAVE:e {: RESULT = e; :} + | LEAVE:i LPAR exprlist:l RPAR {: RESULT = methodSysMLname(i) + "(" + l + ")"; :} + | LEAVE:e {: RESULT = attributeSysMLname(e); :} ; exprlist ::= expr:e {: RESULT = e; :}