Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/* 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.
*/
/**
* Class AvatarFromSysML
* Creation: 20/06/2024
*
* @author Sophie Coudert
* @version 0.1 20/06/2024
*/
package avatartranslator.tosysmlv2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.function.BiConsumer;
import avatartranslator.*;
import avatartranslator.tosysmlv2.AvatarFromSysMLSyntax.*;
import static java_cup.runtime.ComplexSymbolFactory.*;
import java_cup.runtime.ComplexSymbolFactory;
import myutil.TraceManager;
import static avatartranslator.AvatarTransition.*;
/** Class AvatarFromSysML
* Creation: 20/06/2024
*
* @author Sophie Coudert
* @version 0.1 20/06/2024
*
* Building Avatar Models from Their SysML V2 description. This Class exports the main function that does this. Technically,
* the class implements the last step of this process, i.e. it builds Avatar models from abstract syntax trees build by AvatarSysMLParser.
*/
public class AvatarFromSysML {
/** the build Avatar specification, returned at the end of the building process */
private AvatarSpecification avSpec;
/** the source abstract syntax tree, obtained through a call to the parser */
private StxModel stxSpec;
/** maps signal syntactic elements to their associated avatar signal by the building process */
private HashMap<StxSignal, AvatarSignal> signalMap;
/** maps block syntactic elements to their associated avatar block by the building process */
private HashMap<StxBlock, AvatarBlock> blockMap;
/** maps state syntactic elements to their associated avatar state-machine element by the building process (when exists) */
private HashMap<StxState, AvatarStateMachineElement> stateMap;
/** maps Avatar state-machine elements to their associated set of incoming transitions and their block.
* <p>filled while handling blocks. Used thereafter to check some well-formedness constraints such as reachability... </p> */
private HashMap<AvatarStateMachineElement, StateTransitions> transitionMap;
/** maps Avatar datatypes extracted from the SysML sourse to their sequence of primitive fields */
private HashMap<AvatarDataType, AvatarDataTypePrimitiveFields> primitiveFieldsMap;
/** all errors encountered while parsing and building model from parsing-returned abstract syntax tree */
public AvatarFromSysML(){
avSpec = null;
stxSpec = null;
signalMap = new HashMap<StxSignal, AvatarSignal>();
blockMap = new HashMap<StxBlock, AvatarBlock>();
stateMap = new HashMap<StxState, AvatarStateMachineElement>();
transitionMap = new HashMap<AvatarStateMachineElement, StateTransitions>();
/** register an error encountered while parsing and building model from parsing-returned abstract syntax tree */
private void addError(AvatarFromSysMLError _err) { errors.add(_err); }
/** get the lis of errors encountered while parsing and building a model */
public List<AvatarFromSysMLError> getErrors() { return errors; }
/** to memorize the set of transitions of a state and memorize its block with them */
private class StateTransitions {
private AvatarBlock block;
private List<AvatarTransition> transitions;
public StateTransitions (AvatarBlock _block, List<AvatarTransition> _transitions) {
block = _block;
transitions = _transitions;
}
public AvatarBlock getBlock() { return block; }
public List<AvatarTransition> getTransitions() { return transitions; }
}
/** Builds an Avatar Specification from an Avatar SysML V2 description provided in a file.
* If errors are found, they are printed using TraceManager.addDev before returning
*/
public AvatarSpecification sysMLtoSpec(String _fileName) {
errors = new ArrayList<AvatarFromSysMLError>();
// Initialize parser
AvatarFromSysMLParser parser;
try { parser =
new AvatarFromSysMLParser(new AvatarFromSysMLLexer(new FileReader(_fileName)),
new ComplexSymbolFactory()); }
catch (java.lang.Exception e) {
//e.printStackTrace(out);
String errorMsg = "cannot initialize parser from file " + _fileName;
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, errorMsg));
TraceManager.addDev(errorMsg);
// Run parser and get parser errors
TraceManager.addDev("Parsing Model");
if (stxSpec == null) {
for(AvatarFromSysMLError e : parser.getErrors())
TraceManager.addDev(e.toString());
errors = stxSpec.getErrors();
// Build Specification from parser-returned abstract syntax tree.
try {
TraceManager.addDev("Building Specification");
avSpec = new AvatarSpecification("FromSysMLV2_EXAMPLE_SPECIFICATION", null);
signalMap.clear();
blockMap.clear();
stateMap.clear();
TraceManager.addDev("Building DataTypes");
buildDataTypes();
TraceManager.addDev("Building Blocks");
buildBlocks();
TraceManager.addDev("Building Relations");
buildRelations();
}
catch (Exception ex) {
for(AvatarFromSysMLError e : errors)
ex.printStackTrace();
TraceManager.addDev("Building failed with exception " + ex.toString());
// TODO: move error handling
boolean hasError = false;
for(AvatarFromSysMLError e : errors){
if (e.getLevel() > AvatarFromSysMLError.WARNING)
hasError = true;
TraceManager.addDev(e.toString());
/** initialize a state entry in the map that associates outgoing transitions to state-machine elements */
private void addState(AvatarStateMachineElement e, AvatarBlock b) {
if (transitionMap.get(e) == null){
ArrayList<AvatarTransition> l = new ArrayList<AvatarTransition>();
transitionMap.put(e, new StateTransitions(b, l));
}
/** add an outgoing transition to a state in the map that associates outgoing transitions to state-machine elements */
private void addTransition(AvatarTransition t, AvatarStateMachineElement e) {
transitionMap.get(e).getTransitions().add(t);
}
/** get the Avatar block associated to a block syntactic element. Create it if necessary (and create entry in the map) */
private AvatarBlock getBlock(StxBlock _b) {
AvatarBlock b = blockMap.get(_b);
if (b == null) {
b = new AvatarBlock(_b.getName(), avSpec, null);
blockMap.put(_b, b);
}
return b;
}
/** get the Avatar signal associated to a signal syntactic element. Create it if necessary (and create entry in the map) */
private AvatarSignal getSignal(StxSignal _b) {
AvatarSignal b = signalMap.get(_b);
if (b == null) {
if (_b.isInput())
b = new AvatarSignal(_b.getName(), AvatarSignal.IN, null);
else
b = new AvatarSignal(_b.getName(), AvatarSignal.OUT, null);
signalMap.put(_b, b);
}
return b;
}
/* not used...........
private AvatarAttribute getOriginalAttributeByName(String _name, AvatarBlock _block) {
List<AvatarAttribute> l = _block.getOriginalAttributes();
int size = l.size();
for (int i = 0; i < size; i++)
if(l.get(i).getName().equals(_name)) return l.get(i);
return null;
}
*/
/** get the basic attribute associated to a name (possibly with "__") in a block */
private AvatarAttribute getAttributeByName(String _name, AvatarBlock _block) {
List<AvatarAttribute> l = _block.getAttributes();
int size = l.size();
for (int i = 0; i < size; i++)
if(l.get(i).getName().equals(_name)) return l.get(i);
return null;
}
/** get the basic attribute associated to a timer in a block */
private AvatarAttribute getTimerByName(String _name, AvatarBlock _block) {
AvatarAttribute res = getAttributeByName(_name, _block);
if (res != null && res.getType() == AvatarType.TIMER) return res;
return null;
}
/** get the Avatar state-machine element associated to a state syntactic element. Create it if necessary (and create entry in the map)
*
* <p> The creation process may use different state-machine element constructors (sub-classes) depending on the type of
* the syntactic state.</p>
* <p> States that have been technically added (STXPRESENDSTATEs and STXPRERECEIVESTATEs) are not handled as they have no correspondant
* state-machine element in the Avatar Model.</p>
* :*/
private AvatarStateMachineElement getState(StxState _s, AvatarBlock _b) {
AvatarStateMachineElement s = stateMap.get(_s);
if (s == null) {
switch(_s.getType()) {
case AvatarFromSysMLSyntax.STXSTARTSTATE :
s = new AvatarStartState("StartState", null, _b);
break;
case AvatarFromSysMLSyntax.STXSTANDARDSTATE :
s = new AvatarState(_s.getName(), null, _b);
break;
case AvatarFromSysMLSyntax.STXRANDOMSTATE :
s = new AvatarRandom(_s.getName(), null, _b);
break;
case AvatarFromSysMLSyntax.STXCOUNTSTATE :
s = new AvatarQueryOnSignal(_s.getName(), getSignal(_s.getSignal()),
getAttributeByName(_s.getVariable().replaceAll("\\.", "__"),_b), null, _b);
case AvatarFromSysMLSyntax.STXSTOPSTATE :
case AvatarFromSysMLSyntax.STXSENDSTATE :
case AvatarFromSysMLSyntax.STXRECEIVESTATE :
s = new AvatarActionOnSignal(_s.getName(), null, null, _b);
break;
case AvatarFromSysMLSyntax.STXPRESENDSTATE :
case AvatarFromSysMLSyntax.STXPRERECEIVESTATE :
addError(new AvatarFromSysMLError(AvatarFromSysMLError.BUG, null,
"adding precom state (AvatarFromSysML.getState)", null));
break;
case AvatarFromSysMLSyntax.STXSETTIMERSTATE :
s = new AvatarSetTimer(_s.getName(), null, _b);
break;
case AvatarFromSysMLSyntax.STXRESETTIMERSTATE :
s = new AvatarResetTimer(_s.getName(), null, _b);
break;
case AvatarFromSysMLSyntax.STXEXPIRETIMERSTATE :
s = new AvatarExpireTimer(_s.getName(), null, _b);
}
if (s != null) stateMap.put(_s, s);
}
return s;
}
// BUILDING DATATYPES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/** To be run on the (filled by parsing) maps that associates syntactic datatypes to their names
*
* <p> This creates-and-adds-to-avSpec the AvatarDataType corresponding to the syntactic datatype. </p>
* <p> datatypes associated to datatype-typed fields cannot be set as here, the complete list
* of datatypes (which is required for this) is not
* known. Thus this setting is delayed.</p>
* <p> To make this delayed setting easier, two informations are collected while iterating on entries:</p>
* <ul>
* <li> a mapping of the attribute to be later completed to the name of their expected datatype </li>
* <li> a mapping of the datatype names to the build AvatarDataTypes </li>
* </ul>
* */
private class BuildDataType implements BiConsumer<String, StxDataType> {
private HashMap<String,AvatarDataType> dataTypeMap;
private HashMap<AvatarAttribute,String> attributeMap;
/** parameters of the constructor are the maps to fill while iterating on entries */
public BuildDataType(HashMap<String,AvatarDataType> _dataTypeMap, HashMap<AvatarAttribute,String> _attributeMap) {
dataTypeMap = _dataTypeMap;
attributeMap = _attributeMap;
}
/** create and add the datatype to avSpec. updates dataTypeMap, and attributeMap for each encountered datatype-typed field. */
public void accept(String n, StxDataType d) {
AvatarDataType dataType = new AvatarDataType(d.getName(),null);
dataTypeMap.put(d.getName(), dataType);
int nbFields = d.getSize();
for (int i = 0; i < nbFields; i++) {
StxField f = d.getField(i);
String type = f.getType();
AvatarAttribute aa;
if(type.equals("Integer"))
aa = new AvatarAttribute(f.getName(),AvatarType.INTEGER,null,null);
else if(type.equals("Boolean"))
aa = new AvatarAttribute(f.getName(),AvatarType.BOOLEAN,null,null);
aa = new AvatarAttribute(f.getName(), AvatarType.UNDEFINED, null, null);
String initVal = f.getInitialValue().trim();
if(f.getDefaultValue() && initVal.equals(aa.getType().getDefaultInitialValue()))
aa.setInitialValue("");
else aa.setInitialValue(initVal);
/** to be run on the map that maps datatype-typed datatype fields to the name of their typing datatype. Using the (to-constructor)
* provided map from names to Avatar datatypes, accept find the associated datatype and update the field with it. */
private class CompleteDataTypeAttribute implements BiConsumer<AvatarAttribute,String> {
HashMap<String,AvatarDataType> dataTypeMap;
public CompleteDataTypeAttribute(HashMap<String,AvatarDataType> _dataTypeMap) { dataTypeMap = _dataTypeMap;}
public void accept(AvatarAttribute aa, String dt) {
AvatarDataType adt = dataTypeMap.get(dt); // find the associated datatype
errors.add(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, "datatype " + dt + " in datatype field " +
aa.getName() + " is undefined"));
aa.setType(AvatarType.INTEGER); // should not be used...
else aa.setDataType(adt);} // update the field with it
/** fills avSpec's list of Avatar datatypes from stxSpec's list of syntactic datatypes. Then, computes and updates primitiveFieldsMap.
*
* <p> Process with two steps:</p>
* <ul>
* <li> invoke BuildDataType to build and add the datatypes, but not set the datatype attribute of datatype-typed fields. Memorize
* information to do this later </li>
* <li> invoke CompleteDataTypeAttribute to do this delayed job with provided information: a mapping of these fields to the name of their
* datatype type, and a mapping of these datatype names to their associated AvatarDataType structure.
* </li>
* </ul>
* <p> A mapping of all datatypes to their extended list of basic fields is then computed and assigned to primitiveFieldsMap </p>
* <p> During this process, errors may be collected </p>
*/
private void buildDataTypes(){
List<AvatarDataType> dtList = avSpec.getDataTypes(); // the list to fill
HashMap<String,AvatarDataType> dataTypeMap = new HashMap<String,AvatarDataType>();
HashMap<AvatarAttribute,String> attributeMap = new HashMap<AvatarAttribute,String>();
stxSpec.getDataTypeMap().forEach(new BuildDataType(dataTypeMap, attributeMap));
attributeMap.forEach(new CompleteDataTypeAttribute(dataTypeMap));
List<String> err = new ArrayList<String>();
primitiveFieldsMap = AvatarDataTypePrimitiveFields.buildMap(dtList, err);
for(String e : err) errors.add(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR,e));
// BUILDING BLOCKS (but not statemachines) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/** To be run on the (filled by parsing) maps that associates block names to syntactic blocks.
*
* <p> this create-and-add-to-avSpec all the corresponding AvatarBlocks and fills their static declarative part (attributes, signals, methods)
* but not their state-machine. Building state-machines requires the static declarations of all hierarchically containing blocks to be up to date
* (to find used signals) and thus is delayed after the iteration of BuildBlock.
* </p>
*/
private class BuildBlock implements BiConsumer<String, StxBlock> {
public BuildBlock(){}
public void accept(String n, StxBlock blk) {
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR,
"block " + blk.getName() +" has been used but has not been declared"));
// create and add the block...................................... !!! TO ADAPT FOR LIBRARIES
AvatarBlock theBlock = getBlock(blk); // create
avSpec.addBlock(theBlock); // add
// set father...................................... !!! TO suppress FOR LIBRARIES
StxBlock fth = blk.getFather();
if (fth != null) theBlock.setFather(getBlock(fth));
// add Attributes......................................
int size = blk.getNbAttributes();
for (int i = 0; i < size; i++) {
StxAttribute a = blk.getAttribute(i);
String initVal = a.getInit().trim();
AvatarAttribute aa; // for original attributes
AvatarAttribute ax; // for extended basic attributes
if (a.getType().equals("Integer")) {
aa = new AvatarAttribute(a.getName(), AvatarType.INTEGER, theBlock, null);
ax = new AvatarAttribute(a.getName(), AvatarType.INTEGER, theBlock, null);
if(a.getDefaultValue() && initVal.equals(AvatarType.INTEGER.getDefaultInitialValue()))
initVal = "";
aa.setInitialValue(initVal);
ax.setInitialValue(initVal);
theBlock.addAttribute(ax);
ax.setAsConstant(false);
}
else if (a.getType().equals("Boolean")) {
aa = new AvatarAttribute(a.getName(), AvatarType.BOOLEAN, theBlock, null);
ax = new AvatarAttribute(a.getName(), AvatarType.BOOLEAN, theBlock, null);
if(a.getDefaultValue() && initVal.equals(AvatarType.BOOLEAN.getDefaultInitialValue()))
initVal = "";
aa.setInitialValue(initVal);
ax.setInitialValue(initVal);
theBlock.addAttribute(ax);
ax.setAsConstant(false);
aa = new AvatarAttribute(a.getName(), AvatarType.UNDEFINED, theBlock, null);
if(adt == null) {
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, a.getLeft(),
"datatype " + a.getType() + " of attribute " + a.getName() + " does not exist"));
}
else {
aa.setDataType(adt);
String aaName = a.getName() + "__";
AvatarDataTypePrimitiveFields primitiveFields = primitiveFieldsMap.get(adt);
// add basic attributes corresponding of the extension of the datatype
if (primitiveFields == null) {
addError(new AvatarFromSysMLError(AvatarFromSysMLError.BUG, a.getLeft(),
"datatype " + a.getType() + " of attribute " + a.getName() + "cannot be extended"));
} else {
int dtsize = primitiveFields.size();
for (int j = 0; j < dtsize; j++) {
ax = new AvatarAttribute(aaName + primitiveFields.getName(j),
primitiveFields.getType(j), theBlock, null);
theBlock.addAttribute(ax);
ax.setAsConstant(false);
}
}
theBlock.addOriginalAttribute(aa);
aa.setAsConstant(false);
// add Constants......................................
size = blk.getNbConstants();
for (int i = 0; i < size; i++) {
StxAttribute a = blk.getConstant(i);
String initVal = a.getInit().trim();
AvatarAttribute aa = null;
AvatarAttribute ax;
if (a.getType().equals("Integer")) {
aa = new AvatarAttribute(a.getName(), AvatarType.INTEGER, theBlock, null);
aa.setAsConstant(true);
ax = new AvatarAttribute(a.getName(), AvatarType.INTEGER, theBlock, null);
theBlock.addAttribute(ax);
ax.setAsConstant(true);
if(a.getDefaultValue() && initVal.equals(AvatarType.INTEGER.getDefaultInitialValue()))
initVal = "";
aa.setInitialValue(initVal);
ax.setInitialValue(initVal);
}
else if (a.getType().equals("Boolean")) {
aa = new AvatarAttribute(a.getName(), AvatarType.BOOLEAN, theBlock, null);
aa.setAsConstant(true);
ax = new AvatarAttribute(a.getName(), AvatarType.BOOLEAN, theBlock, null);
theBlock.addAttribute(ax);
ax.setAsConstant(true);
if(a.getDefaultValue() && initVal.equals(AvatarType.BOOLEAN.getDefaultInitialValue()))
initVal = "";
aa.setInitialValue(initVal);
ax.setInitialValue(initVal);
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR,
a.getLeft(), "datatype for constants is not supported", null));
// add Timers......................................
size = blk.getNbTimers();
for (int i = 0; i < size; i++) {
AvatarAttribute aa = new AvatarAttribute(blk.getTimer(i).getName(), AvatarType.TIMER, theBlock, null);
theBlock.addOriginalAttribute(aa);
aa.setAsConstant(false);
aa = new AvatarAttribute(blk.getTimer(i).getName(), AvatarType.TIMER, theBlock, null);
theBlock.addAttribute(aa);
aa.setAsConstant(false);
}
// add Methods......................................
size = blk.getNbMethods();
for (int i = 0; i < size; i++) {
StxMethod sm = blk.getMethod(i);
AvatarMethod am = new AvatarMethod(sm.getName(),null);
theBlock.addMethod(am);
// add input parameters to method
buildProfile(sm, am, theBlock);
// handle return type
String returnType = sm.getReturnType();
if (returnType == null || returnType.length() == 0) continue; // no return type, void method
if (returnType.equals("Integer")) { // return integer
am.addReturnParameter(new AvatarAttribute("return__0", AvatarType.INTEGER, theBlock, null));
am.addOriginalReturnParameter(new AvatarAttribute("return__", AvatarType.INTEGER, theBlock, null));
}
else if (returnType.equals("Boolean")) { // return boolean
am.addReturnParameter(new AvatarAttribute("return__0", AvatarType.BOOLEAN, theBlock, null));
am.addOriginalReturnParameter(new AvatarAttribute("return__", AvatarType.BOOLEAN, theBlock, null));
}
else { // return datatype
// find datatype
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, sm.getLeft(),
"unknown datatype " + returnType + "for method return parameter", null));
AvatarAttribute ret = new AvatarAttribute("return__", AvatarType.INTEGER, theBlock, null);
am.addOriginalReturnParameter(ret);
am.addReturnParameter(ret);
}
else {
AvatarAttribute ret = new AvatarAttribute("return__", AvatarType.UNDEFINED, theBlock, null);
// set datatype
ret.setDataType(adt);
am.addOriginalReturnParameter(ret);
// extend datatype to a list of primitive basic fields
AvatarDataTypePrimitiveFields primitiveFields = primitiveFieldsMap.get(adt);
if (primitiveFields == null)
addError(new AvatarFromSysMLError(AvatarFromSysMLError.BUG, sm.getLeft(),
"datatype " + returnType + "for method return parameter cannot be extended", null));
else {
int dtsize = primitiveFields.size();
for (int j = 0; j < dtsize; j++) {
ret = new AvatarAttribute("return__" + j,
primitiveFields.getType(j), theBlock, null);
am.addReturnParameter(ret);
}
}
}
// add Signals ..............................
size = blk.getNbSignals();
for (int i = 0; i < size; i++) {
StxSignal ss = blk.getSignal(i);
AvatarSignal as = getSignal(ss); // create signal
if (!ss.isDeclared()) {
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, ss.getLeft(),
"signal " + theBlock.getName() + "." + ss.getName() + " has not been declared"));
continue;
}
theBlock.addSignal(as); // add signal
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, ss.getLeft(),
"channel of signal " + ss.getName() + " has not been set"));
continue;
}
if (!ch.isDeclared()) {
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, ss.getLeft(),
"channel of signal " + theBlock.getName() + "." + ss.getName() + " has not been declared"));
continue;
}
if (ss.isInput()) {
StxInMessage im = ss.getChannel().getInProfile(); // profile is channel's in profile
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, ss.getLeft(),
"input profile of signal " + ss.getName() + " has not been set"));
continue;
}
buildProfile(im, as, theBlock); // add input signal parameters
StxOutMessage om = ss.getChannel().getOutProfile(); // profile is channel's out profile
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, ss.getLeft(),
"output profile of signal " + ss.getName() + " has not been set"));
continue;
}
buildProfile(om, as, theBlock); // add output signal parameters
/**
* <p></p> */
/** To be run on the maps that associates block names to syntactic blocks, after the iteration of BuildBlock (which creates AvatarBlocks
* and fills their static part: attributes, methods, timers, signals).
*
* <p> accept updates the AvatarBlock associated to the handled syntactic block by computing and setting its state-machine. </p>
*/
private class BuildStateMachine implements BiConsumer<String, StxBlock> {
public BuildStateMachine(){}
/** updates the AvatarBlock associated to the handled StxBlock by computing and setting its state-machine */
public void accept(String n, StxBlock blk) {
AvatarBlock theBlock = getBlock(blk); // get relevant block
AvatarStateMachine asm = theBlock.getStateMachine(); // get state-machine to fill
// initialize map that maps states to their sequence of incomming transitions, and block
// filled while handling blocks. Used thereafter to check some well-formedness constraints
AvatarStateMachineElement theSourceState; // technical variable
AvatarStartState startState = null;
Location startStateLocation = null;
// iterate on syntactic states of the syntactic block
// - build statemachine
// - fills transitionMap for a final check.
StxState[] states = blk.getStates();
Sophie Coudert
committed
int size = (states == null ? 0 : states.length);
for (int i = 0; i < size; i++) {
// identify current state type
byte stateType = states[i].getType();
boolean undeclaredState = false;
if (! states[i].isDeclared()) { // in this case, stateType may be null
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR,
"state " + theBlock.getName() + "." + states[i].getName() + " is used but not declared"));
stateType = AvatarFromSysMLSyntax.STXSTANDARDSTATE;
undeclaredState = true;
// handle all states that have a unique corresponding avatar state-machine element (others are handled indirectly)
if (stateType != AvatarFromSysMLSyntax.STXPRESENDSTATE && // STXPRESENDSTATEs disappear in avatar final model
stateType != AvatarFromSysMLSyntax.STXPRERECEIVESTATE && // STXPRERECEIVESTATEs disappear in avatar final model
stateType != AvatarFromSysMLSyntax.STXSTOPSTATE ) { // STXSTOPSTATE: special handling: several associated avatar elements
if (undeclaredState) { // try to recover and continue
states[i].setType(AvatarFromSysMLSyntax.STXSTANDARDSTATE);
}
// create the handled state, which is the source state of outgoing transitions that will be handled
theSourceState = getState(states[i], theBlock);
// two state types that require some specific transition-independent handling
if (stateType == AvatarFromSysMLSyntax.STXRANDOMSTATE) {
((AvatarRandom) theSourceState).setOriginalValues(states[i].getMinValue(), states[i].getMaxValue());
((AvatarRandom) theSourceState).setValues(
states[i].getMinValue().replaceAll("\\.", "__"),
states[i].getMaxValue().replaceAll("\\.", "__"));
((AvatarRandom) theSourceState).setVariable(states[i].getVariable().replaceAll("\\.", "__"));
} else if (stateType == AvatarFromSysMLSyntax.STXSTARTSTATE)
if (startState == null) {
startState = (AvatarStartState) theSourceState;
startStateLocation = states[i].getLeft();
asm.setStartState(startState);
} else {
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, states[i].getLeft(),
"several start-states declared, only one is kept", startStateLocation));
continue;
}
// add the handled state.................
addState(theSourceState, theBlock);
asm.addElement(theSourceState);
// build and add transitions.........................................
// get outgoing transitions list
List<StxTransition> transitions = states[i].getTransitions();
int tsize = transitions.size();
if (tsize > 1 && stateType != AvatarFromSysMLSyntax.STXSTANDARDSTATE) { // Avatar well-formedness constraint broken
tsize = 1; // recover to continue
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, states[i].getTransitions().get(1).getLeft(),
"state " + states[i].getName() + " only accept one single outgoing transition",
states[i].getTransitions().get(1).getRleft()));
}
// iterate on outgoing transitions
for (int j = 0; j < tsize; j++) {
StxTransition transition = transitions.get(j);
// skip some transition that have not been fully defined during parsing
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, states[i].getLeft(),
"something is wrong in state " + states[i].getName() + " : null transition found"));
continue;
}
if (! transition.isDeclared()) {
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getRleft(),
"transition request in state " + states[i].getName() + " of block " + theBlock.getName() +
" has no associated declaration" ));
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getLeft(),
"transition has no request in state " + states[i].getName()));
continue;
}
// create and fills the transition (only added at the end if no critical error occurs)
AvatarTransition theTransition = new AvatarTransition(theBlock,"", null);
// handle guard
if (transition.getGuard() != null) {
theTransition.setGuard(
"[" + transition.getGuard().replaceAll("\\.", "__") + "]",
"[" + transition.getGuard() + "]");
}
// handle delay
String minDelay = transition.getMinDelay().trim();
String maxDelay = transition.getMaxDelay().trim();
if (minDelay.equals(maxDelay)) maxDelay = "";
theTransition.setDelays(
minDelay.replaceAll("\\.", "__"),
maxDelay.replaceAll("\\.", "__"),
minDelay,
maxDelay);
// handle distribution law and priority
setDistributionLaw(theTransition, transition.getDelayDistributionLaw(),transition.getDelayExtra(), transition);
theTransition.setProbability(transition.getProbability());
// handle actions: iterate on them and add them
List<StxAction> actions = transition.getActions();
for (int k = 0; k < asize; k++) { // iterate on transition's actions
// Assignment action ..............
if(action.getType() == AvatarFromSysMLSyntax.STXASSIGNACTION){
// compute left hand
String originalLeftHandStr = action.getTarget(); // get assigned variable
// compute tuple string "(x__field1, ..., x__fieldN)" if variable has a datatype type
extendLocation = action.getLeft();
String leftHandStr = extendIdent(action.getTarget().replaceAll("\\.", "__"), blk);
// compute right hand
String originalRightHandStr;
String rightHandStr; //
if (value instanceof StxId) { // value is a variable (potentially field) ident
originalRightHandStr = ((StxId)value).toString();
rightHandStr = ((StxId)value).toString().replaceAll("\\.", "__");
else if (value instanceof StxExpr){ // value is an int/bool expression
originalRightHandStr = ((StxExpr)value).toString();
rightHandStr = ((StxExpr)value).toString().replaceAll("\\.", "__");
else { // value is a method call
extendLocation = value.getLeft();
originalRightHandStr = extendOriginalCall((StxCall)value,blk); // rebuilds original string
rightHandStr = extendCall((StxCall)value,blk); // rebuilds while extending datatype variables
// add the action
theTransition.addAction(leftHandStr + "=" + rightHandStr,
originalLeftHandStr + "=" + originalRightHandStr);
// method call action ...................
if (action.getValue() instanceof StxCall) { // else ERROR, action ignored
// rebuilds original string
String originalTermStr = extendOriginalCall((StxCall)action.getValue(),blk);
// rebuilds while extending datatype variables
extendLocation = action.getValue().getLeft();
String termStr = extendCall((StxCall)action.getValue(),blk);
// add the action
theTransition.addAction(termStr, originalTermStr);
// Handles transition target.
// Depends on target type.
// Also checks compatibility constraints between transition and target types.
if (transition.getTarget() == null) { // unrecoverable, transition skiped
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getRleft(),
"something is wrong in transition : no target defined", transition.getLeft()));
// handle transition to an added state as target that disappear in avatar
// produced transition must then lead to the target of the single outgoing transition of the removed state
if (transition.getTarget().getType() == AvatarFromSysMLSyntax.STXPRESENDSTATE ||
transition.getTarget().getType() == AvatarFromSysMLSyntax.STXPRERECEIVESTATE) { // target must be removed
if (transition.getTarget().getTransitions().size() == 0 ||
transition.getTarget().getTransition(0) == null) { // target has no outgoing transition, unrecoverable
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getTarget().getLeft(),
"something is wrong in precommunication state " + transition.getTarget().getName() +
" : no outgoing transition"));
byte type = transition.getTarget().getType(); // type of the state to remove
// get second transition (outgoing from removed target) to complete the single produced avatar transition
transition = transition.getTarget().getTransition(0);
// TODO: check errors: this transition must not have actions, delays,... not verified. if exist, these data are skipped
// check type compatibility between this transition and its source (the removed state). error -> unrecoverable
if (type == AvatarFromSysMLSyntax.STXPRESENDSTATE & // expects some sending state
! ( ( transition.getTarget().getType() == AvatarFromSysMLSyntax.STXSENDSTATE &&
transition.getType() == AvatarFromSysMLSyntax.STXSENDTRANSITION ) ||
( transition.getTarget().getType() == AvatarFromSysMLSyntax.STXSETTIMERSTATE &&
transition.getType() == AvatarFromSysMLSyntax.STXSETTIMERTRANSITION ) ||
( transition.getTarget().getType() == AvatarFromSysMLSyntax.STXRESETTIMERSTATE &&
transition.getType() == AvatarFromSysMLSyntax.STXRESETTIMERTRANSITION ) )) {
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getTarget().getLeft(),
"outgoing transitions from pre-send state must be sending transitions leading to some sending state " +
"(send/set timer /reset timer)", transition.getLeft()));
continue;
}
else if (type == AvatarFromSysMLSyntax.STXPRERECEIVESTATE & // expects some receiving state
! ( ( transition.getTarget().getType() == AvatarFromSysMLSyntax.STXRECEIVESTATE &&
transition.getType() == AvatarFromSysMLSyntax.STXRECEIVETRANSITION ) ||
( transition.getTarget().getType() == AvatarFromSysMLSyntax.STXEXPIRETIMERSTATE &&
transition.getType() == AvatarFromSysMLSyntax.STXEXPIRETIMERTRANSITION ) )) {
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getTarget().getLeft(),
"outgoing transitions from pre-reveive state must be sending transitions leading to some " +
"receivinging state (receive/expire timer)", transition.getLeft()));
continue;
}
}
// from here, the target of avatar transition under building is the state-machine element associated
// to the target of variable transition
// handle transition target.......
AvatarStateMachineElement tgtState; // target of Avatar Transition
if (transition.getTarget().getType() == AvatarFromSysMLSyntax.STXSTOPSTATE) {
// special case: a different Avatar stopstate target for each transition to the unique syntactic stopstate
if (transition.getTarget().getTransitions().size() != 0) {
addError(new AvatarFromSysMLError(AvatarFromSysMLError.WARNING, transition.getTarget().getLeft(),
tgtState = new AvatarStopState("StopState", null, theBlock); // create the stopstate
else // standard case
tgtState = getState(transition.getTarget(), theBlock);
addState(tgtState, theBlock); // initialize the map-to-incomming-transitions for the target state
// type dependent transition handling (transition and target types) + error detections
byte tgtType = transition.getTarget().getType();
// sending transitions =====================
if (transition.getType() == AvatarFromSysMLSyntax.STXSENDTRANSITION) {
if(tgtState instanceof AvatarActionOnSignal) { // required
AvatarActionOnSignal state = (AvatarActionOnSignal)tgtState;
// some unrecovered errors (transition skiped)
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getRleft(),
"sending transition should have an associated signal", transition.getLeft()));
continue;
}
state.setSignal( getSignal(transition.getSignal()));
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getRleft(),
"signal of sending transition must be an output signal", transition.getSignal().getLeft()));
continue;
}
if (! transition.getSignal().isDeclared()) {
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getRleft(),
"transition uses a signal " + transition.getSignal().getName() + " that has not been declared"));
continue;
}
ArrayList<StxTerm> sendPayload = transition.getSendPayload();
if (sendPayload != null) { // add payload info to target state (renamed "state")
for(int u = 0; u < nbParam; u++) { // iterate on payload parameter list
if (p instanceof StxId) { // parameter is an attribute (field) identifier
state.addOriginalValue(p.toString());
String[] ids;
// extends parameter if datatpe: "x__field1, ..., x__fieldN" (if well typed)
extendLocation = p.getLeft();
String extendedPath = extendPath(p.toString().replaceAll("\\.", "__"), blk);
if (extendedPath == null) { // parameter cannot be typed
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, p.getLeft(), "attribute \"" +
p.toString() + "\" cannot be typed"));
// recover w.r.t. expected type to continue, while providing information
AvatarDataType adt = state.getSignal().getListOfOriginalAttributes().get(u).getDataType();
if (adt == null) { // basic type, keep parameter unchanged
ids = new String[1];
ids[0] = p.toString();
} else { // datatype, extend parameter with fields (although not well typed)
ids = extendName(p.toString().replaceAll("\\.", "__"),adt).split(",");
}
}
else // parameter has been typed (not verified that it is the expected type)
ids = extendedPath.split(",");
// add parameter to ActionOnSignal state
int isize = ids.length;
for (int w = 0; w < isize; w++) state.addValue(ids[w]);
else if (p instanceof StxExpr) { // parameter is an int/bool expression, basic type
state.addOriginalValue( p.toString() );
state.addValue( p.toString().replaceAll("\\.", "__"));
else { // parameter is a method call
extendLocation = p.getLeft();
state.addOriginalValue(extendOriginalCall((StxCall)p,blk)); // rebuild original string
state.addValue(extendCall((StxCall)p,blk)); // rebuild while extending datatype-typed variable
else { // unrecovered error, skip transition
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getLeft(),
"sending transition should lead to a send state", transition.getRleft()));
// receiving transitions =====================
else if (transition.getType() == AvatarFromSysMLSyntax.STXRECEIVETRANSITION) {
if(tgtState instanceof AvatarActionOnSignal) { // expected
AvatarActionOnSignal state = (AvatarActionOnSignal)tgtState;
// some unrecovered errors (transition skiped)
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getRleft(),
"receiving transition should have an associated signal", transition.getLeft()));
continue;
}
state.setSignal(getSignal(transition.getSignal()));
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getRleft(),
"transition uses a signal " + transition.getSignal().getName() + " that has not been declared"));
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getRleft(),
"signal of receiving transition must be an input signal", transition.getSignal().getLeft()));
continue;
}
if (transition.getSignal().getChannel() == null) {
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getRleft(),
"signal of receiving transition doesn't have an associated channel", transition.getSignal().getLeft()));
continue;
}
// update target state (renamed "state") with assigned attributes (fields)
List<AvatarAttribute> profile = state.getSignal().getListOfOriginalAttributes();
if (profile != null && !profile.isEmpty()) { // signal conveys values (otherwise, nothing to do)
// map that maps parameter names to string denoting the attributes (fields) to assign
HashMap<String, String> receivePayload = transition.getReceivePayload();
if (receivePayload == null) {
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getLeft(),
"receive payload of transition has not been set"));
continue;
}
for (int k = 0; k < nbParams; k++) { // iterate on receiving profile parameters
AvatarAttribute aa = profile.get(k); // parameter name and type
String value; // to receive name of attribute (field) to assign with parameter
if (aa == null) { // parameter has not been correctly defined while parsing
addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getLeft(),
"parameter " + k + " of signal " + state.getSignal().getName() +
" is not well defined"));
value = ""; // default, because no way to identify target variable
} else { // parameter well defined