Skip to content
Snippets Groups Projects
AvatarFromSysML.java 87 KiB
Newer Older
/* 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.
 */
Sophie Coudert's avatar
Sophie Coudert committed
/**
 * Class AvatarFromSysML
 * Creation: 20/06/2024
 *
 * @author Sophie Coudert
 * @version 0.1 20/06/2024
 */
package avatartranslator.tosysmlv2;

Sophie Coudert's avatar
Sophie Coudert committed
import java.io.FileReader;
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 java.lang.System.out;
import static avatartranslator.AvatarTransition.*;
Sophie Coudert's avatar
Sophie Coudert committed
/** 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 {
Sophie Coudert's avatar
Sophie Coudert committed
    /** the build Avatar specification, returned at the end of the building process */
    private AvatarSpecification avSpec;
Sophie Coudert's avatar
Sophie Coudert committed
    /** the source abstract syntax tree, obtained through a call to the parser */
    private StxModel stxSpec;
Sophie Coudert's avatar
Sophie Coudert committed
    /** maps signal syntactic elements to their associated avatar signal by the building process */
    private HashMap<StxSignal, AvatarSignal> signalMap;
Sophie Coudert's avatar
Sophie Coudert committed
    /** maps block syntactic elements to their associated avatar block by the building process */
    private HashMap<StxBlock, AvatarBlock> blockMap;
Sophie Coudert's avatar
Sophie Coudert committed
    /** 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;
Sophie Coudert's avatar
Sophie Coudert committed
    /** maps Avatar datatypes extracted from the SysML sourse to their sequence of primitive fields */
    private HashMap<AvatarDataType, AvatarDataTypePrimitiveFields> primitiveFieldsMap;
Sophie Coudert's avatar
Sophie Coudert committed
    /** all errors encountered while parsing and building model from parsing-returned abstract syntax tree */
Sophie Coudert's avatar
Sophie Coudert committed
    private List<AvatarFromSysMLError> errors;
    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>();
Sophie Coudert's avatar
Sophie Coudert committed
        errors = null;
    }
Sophie Coudert's avatar
Sophie Coudert committed

    /** 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; }
    
Sophie Coudert's avatar
Sophie Coudert committed
    /** 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
     */
Sophie Coudert's avatar
Sophie Coudert committed
    public AvatarSpecification sysMLtoSpec(String _fileName) {
Sophie Coudert's avatar
Sophie Coudert committed
        errors = new ArrayList<AvatarFromSysMLError>();
Sophie Coudert's avatar
Sophie Coudert committed
        AvatarFromSysMLParser parser;
        try { parser =
                new AvatarFromSysMLParser(new AvatarFromSysMLLexer(new FileReader(_fileName)),
                        new ComplexSymbolFactory()); }
        catch (java.lang.Exception e) {
Sophie Coudert's avatar
Sophie Coudert committed
            //e.printStackTrace(out);
            String errorMsg = "cannot initialize parser from file " + _fileName;
            addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, errorMsg));
            TraceManager.addDev(errorMsg);
            return null;

        // Run parser and get parser errors
        TraceManager.addDev("Parsing Model");
Sophie Coudert's avatar
Sophie Coudert committed
        stxSpec = parser.parseModel();
        if (stxSpec == null) {
            for(AvatarFromSysMLError e : parser.getErrors())
                TraceManager.addDev(e.toString());
Sophie Coudert's avatar
Sophie Coudert committed
            errors = parser.getErrors();
            return null;
        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)
Sophie Coudert's avatar
Sophie Coudert committed
                TraceManager.addDev(e.toString());
Sophie Coudert's avatar
Sophie Coudert committed
            TraceManager.addDev("Building failed with exception " + ex.toString());
            return null;
        // TODO: move error handling
Sophie Coudert's avatar
Sophie Coudert committed
        boolean hasError = false;
        for(AvatarFromSysMLError e : errors){
            if (e.getLevel() > AvatarFromSysMLError.WARNING)
                hasError = true;
            TraceManager.addDev(e.toString());
        if (hasError) return null;
Sophie Coudert's avatar
Sophie Coudert committed
        else return avSpec;
    /** 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;
    }
    private AvatarAttribute getOriginalAttributeByName(String _name, AvatarBlock _block) {
Sophie Coudert's avatar
Sophie Coudert committed
        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 :
Sophie Coudert's avatar
Sophie Coudert committed
                    s = new AvatarActionOnSignal(_s.getName(), null, null, _b);
                    break;
                case AvatarFromSysMLSyntax.STXPRESENDSTATE :
                case AvatarFromSysMLSyntax.STXPRERECEIVESTATE :
Sophie Coudert's avatar
Sophie Coudert committed
                    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) {
Sophie Coudert's avatar
Sophie Coudert committed
            AvatarDataType dataType = new AvatarDataType(d.getName(),null);
Sophie Coudert's avatar
Sophie Coudert committed
            avSpec.addDataType(dataType);
            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);
Sophie Coudert's avatar
Sophie Coudert committed
                else {
                    aa = new AvatarAttribute(f.getName(), AvatarType.UNDEFINED, null, null);
                    attributeMap.put(aa, type);
Sophie Coudert's avatar
Sophie Coudert committed
                }
                String initVal = f.getInitialValue().trim();
                if(f.getDefaultValue() && initVal.equals(aa.getType().getDefaultInitialValue()))
                    aa.setInitialValue("");
                else aa.setInitialValue(initVal);

                dataType.addAttribute(aa);

    /** 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
            if (adt == null) {
Sophie Coudert's avatar
Sophie Coudert committed
                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);
Sophie Coudert's avatar
Sophie Coudert committed
        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) {
Sophie Coudert's avatar
Sophie Coudert committed
            if (!blk.isDeclared()) {
Sophie Coudert's avatar
Sophie Coudert committed
                addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR,
                        "block " + blk.getName() +" has been used but has not been declared"));
                return;
Sophie Coudert's avatar
Sophie Coudert committed
            }
            // 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);
Sophie Coudert's avatar
Sophie Coudert committed
                    AvatarDataType adt = avSpec.getDataTypeByName(a.getType());
                    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;
                if (a.getType().equals("Integer")) {
                    aa = new AvatarAttribute(a.getName(), AvatarType.INTEGER, theBlock, null);
                    theBlock.addOriginalAttribute(aa);
                    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);
                    theBlock.addOriginalAttribute(aa);
                    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);
Sophie Coudert's avatar
Sophie Coudert committed
                    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);
                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
Sophie Coudert's avatar
Sophie Coudert committed
                    AvatarDataType adt = avSpec.getDataTypeByName(returnType);
                    if (adt == null) {
Sophie Coudert's avatar
Sophie Coudert committed
                        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()) {
Sophie Coudert's avatar
Sophie Coudert committed
                    addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, ss.getLeft(),
                            "signal " + theBlock.getName() + "." + ss.getName() + " has not been declared"));
                    continue;
                }
                theBlock.addSignal(as); // add signal
Sophie Coudert's avatar
Sophie Coudert committed
                StxChannel ch = ss.getChannel();
                if (ch == null) {
Sophie Coudert's avatar
Sophie Coudert committed
                    addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, ss.getLeft(),
Sophie Coudert's avatar
Sophie Coudert committed
                            "channel of signal " + ss.getName() + " has not been set"));
                    continue;
                }
                if (!ch.isDeclared()) {
Sophie Coudert's avatar
Sophie Coudert committed
                    addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, ss.getLeft(),
Sophie Coudert's avatar
Sophie Coudert committed
                            "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
Sophie Coudert's avatar
Sophie Coudert committed
                    if (im == null) {
Sophie Coudert's avatar
Sophie Coudert committed
                        addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, ss.getLeft(),
Sophie Coudert's avatar
Sophie Coudert committed
                                "input profile of signal " + ss.getName() + " has not been set"));
                        continue;
                    }
                    buildProfile(im, as, theBlock); // add input signal parameters
Sophie Coudert's avatar
Sophie Coudert committed
                }
                else {
                    StxOutMessage om = ss.getChannel().getOutProfile(); // profile is channel's out profile
Sophie Coudert's avatar
Sophie Coudert committed
                    if (om == null) {
Sophie Coudert's avatar
Sophie Coudert committed
                        addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, ss.getLeft(),
Sophie Coudert's avatar
Sophie Coudert committed
                                "output profile of signal " + ss.getName() + " has not been set"));
                        continue;
                    }
                    buildProfile(om, as, theBlock); // add output signal parameters
Sophie Coudert's avatar
Sophie Coudert committed
                }
    /**
     * <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
            transitionMap.clear();

            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();
            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
Sophie Coudert's avatar
Sophie Coudert committed
                    addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR,
Sophie Coudert's avatar
Sophie Coudert committed
                            "state " + theBlock.getName() + "." + states[i].getName() + " is used but not declared"));
                    stateType = AvatarFromSysMLSyntax.STXSTANDARDSTATE;
                    undeclaredState = true;
Sophie Coudert's avatar
Sophie Coudert committed
                }

                // 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 {
Sophie Coudert's avatar
Sophie Coudert committed
                            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
Sophie Coudert's avatar
Sophie Coudert committed
                        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
Sophie Coudert's avatar
Sophie Coudert committed
                        if (transition == null) {
Sophie Coudert's avatar
Sophie Coudert committed
                            addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, states[i].getLeft(),
Sophie Coudert's avatar
Sophie Coudert committed
                                    "something is wrong in state " + states[i].getName() + " : null transition found"));
                            continue;
                        }
                        if (! transition.isDeclared()) {
Sophie Coudert's avatar
Sophie Coudert committed
                            addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getRleft(),
                                    "transition request in state " + states[i].getName() + " of block " + theBlock.getName() +
                                            " has no associated declaration" ));
Sophie Coudert's avatar
Sophie Coudert committed
                            continue;
                        }
                        if (! transition.isRequested()) {
Sophie Coudert's avatar
Sophie Coudert committed
                            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);
                        if (transition.getGuard() != null) {
                            theTransition.setGuard(
                                    "[" + transition.getGuard().replaceAll("\\.", "__") + "]",
                                    "[" + transition.getGuard() + "]");
                        }
                        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
Sophie Coudert's avatar
Sophie Coudert committed
                        setDistributionLaw(theTransition, transition.getDelayDistributionLaw(),transition.getDelayExtra(), transition);
                        theTransition.setProbability(transition.getProbability());

                        // handle actions: iterate on them and add them
                        List<StxAction> actions = transition.getActions();
                        int asize = actions.size();
                        for (int k = 0; k < asize; k++) { // iterate on transition's actions

                            StxAction action = actions.get(k);

                            // 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);
                                String originalRightHandStr;
                                String rightHandStr; //
                                StxTerm value = action.getValue();
                                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
                                theTransition.addAction(leftHandStr + "=" + rightHandStr,
                                        originalLeftHandStr + "=" + originalRightHandStr);
                            // method call action ...................
                            else { // STXMETHODACTION
                                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);
                                    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
Sophie Coudert's avatar
Sophie Coudert committed
                            addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getRleft(),
                                    "something is wrong in transition : no target defined", transition.getLeft()));
Sophie Coudert's avatar
Sophie Coudert committed
                            continue;
                        }

                        // 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

Sophie Coudert's avatar
Sophie Coudert committed
                        if (transition.getTarget().getType() == AvatarFromSysMLSyntax.STXPRESENDSTATE ||
                                transition.getTarget().getType() == AvatarFromSysMLSyntax.STXPRERECEIVESTATE) { // target must be removed

Sophie Coudert's avatar
Sophie Coudert committed
                            if (transition.getTarget().getTransitions().size() == 0 ||
                                    transition.getTarget().getTransition(0) == null) { // target has no outgoing transition, unrecoverable
Sophie Coudert's avatar
Sophie Coudert committed
                                addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getTarget().getLeft(),
Sophie Coudert's avatar
Sophie Coudert committed
                                        "something is wrong in precommunication state " + transition.getTarget().getName() +
                                                " : no outgoing transition"));
Sophie Coudert's avatar
Sophie Coudert committed
                                continue;
                            }

                            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
Sophie Coudert's avatar
Sophie Coudert committed
                                    ! ( ( 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 ) )) {
Sophie Coudert's avatar
Sophie Coudert committed
                                addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getTarget().getLeft(),
Sophie Coudert's avatar
Sophie Coudert committed
                                        "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
Sophie Coudert's avatar
Sophie Coudert committed
                                    ! ( ( transition.getTarget().getType() == AvatarFromSysMLSyntax.STXRECEIVESTATE &&
                                            transition.getType() == AvatarFromSysMLSyntax.STXRECEIVETRANSITION ) ||
                                            ( transition.getTarget().getType() == AvatarFromSysMLSyntax.STXEXPIRETIMERSTATE &&
                                                    transition.getType() == AvatarFromSysMLSyntax.STXEXPIRETIMERTRANSITION ) )) {
Sophie Coudert's avatar
Sophie Coudert committed
                                addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getTarget().getLeft(),
Sophie Coudert's avatar
Sophie Coudert committed
                                        "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
Sophie Coudert's avatar
Sophie Coudert committed
                            if (transition.getTarget().getTransitions().size() != 0) {
Sophie Coudert's avatar
Sophie Coudert committed
                                addError(new AvatarFromSysMLError(AvatarFromSysMLError.WARNING, transition.getTarget().getLeft(),
Sophie Coudert's avatar
Sophie Coudert committed
                                        "stopstate should not have outgoing transitions"));
                            }
                            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)
Sophie Coudert's avatar
Sophie Coudert committed
                                if (transition.getSignal() == null) {
Sophie Coudert's avatar
Sophie Coudert committed
                                    addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getRleft(),
Sophie Coudert's avatar
Sophie Coudert committed
                                            "sending transition should have an associated signal", transition.getLeft()));
                                    continue;
                                }
                                state.setSignal( getSignal(transition.getSignal()));
Sophie Coudert's avatar
Sophie Coudert committed
                                if (state.getSignal().isIn()) {
Sophie Coudert's avatar
Sophie Coudert committed
                                    addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getRleft(),
                                            "signal of sending transition must be an output signal", transition.getSignal().getLeft()));
Sophie Coudert's avatar
Sophie Coudert committed
                                    continue;
                                }
                                if (! transition.getSignal().isDeclared()) {
Sophie Coudert's avatar
Sophie Coudert committed
                                    addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getRleft(),
Sophie Coudert's avatar
Sophie Coudert committed
                                            "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")
Sophie Coudert's avatar
Sophie Coudert committed
                                    int nbParam = sendPayload.size();
                                    for(int u = 0; u < nbParam; u++) { // iterate on payload parameter list
Sophie Coudert's avatar
Sophie Coudert committed
                                        StxTerm p = sendPayload.get(u);

                                        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
Sophie Coudert's avatar
Sophie Coudert committed
                                                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
Sophie Coudert's avatar
Sophie Coudert committed
                                addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getLeft(),
Sophie Coudert's avatar
Sophie Coudert committed
                                        "sending transition should lead to a send state", transition.getRleft()));
                                continue;
Sophie Coudert's avatar
Sophie Coudert committed
                            }

                        // receiving transitions =====================
                        else if (transition.getType() == AvatarFromSysMLSyntax.STXRECEIVETRANSITION) {
                            if(tgtState instanceof AvatarActionOnSignal) { // expected
                                AvatarActionOnSignal state = (AvatarActionOnSignal)tgtState;

                                // some unrecovered errors (transition skiped)
Sophie Coudert's avatar
Sophie Coudert committed
                                if (transition.getSignal() == null) {
Sophie Coudert's avatar
Sophie Coudert committed
                                    addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getRleft(),
Sophie Coudert's avatar
Sophie Coudert committed
                                            "receiving transition should have an associated signal", transition.getLeft()));
                                    continue;
                                }
                                state.setSignal(getSignal(transition.getSignal()));
Sophie Coudert's avatar
Sophie Coudert committed
                                if (! transition.getSignal().isDeclared()) {
Sophie Coudert's avatar
Sophie Coudert committed
                                    addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getRleft(),
                                            "transition uses a signal " + transition.getSignal().getName() + " that has not been declared"));
Sophie Coudert's avatar
Sophie Coudert committed
                                    continue;
                                }
                                if (state.getSignal().isOut()) {
Sophie Coudert's avatar
Sophie Coudert committed
                                    addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getRleft(),
                                            "signal of receiving transition must be an input signal", transition.getSignal().getLeft()));
Sophie Coudert's avatar
Sophie Coudert committed
                                    continue;
                                }
                                if (transition.getSignal().getChannel() == null) {
Sophie Coudert's avatar
Sophie Coudert committed
                                    addError(new AvatarFromSysMLError(AvatarFromSysMLError.ERROR, transition.getRleft(),
Sophie Coudert's avatar
Sophie Coudert committed
                                            "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
Sophie Coudert's avatar
Sophie Coudert committed
                                    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;
                                    }
                                    int nbParams = profile.size();
                                    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
Sophie Coudert's avatar
Sophie Coudert committed
                                            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