Skip to content
Snippets Groups Projects
AvatarFromSysML.java 59.8 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.
 */
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;

public class AvatarFromSysML {
    private AvatarSpecification avSpec;
    private StxModel stxSpec;
    private HashMap<StxSignal, AvatarSignal> signalMap;
    private HashMap<StxBlock, AvatarBlock> blockMap;
    private HashMap<StxState, AvatarStateMachineElement> stateMap;
    private HashMap<AvatarStateMachineElement, StateTransitions> transitionMap;
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;
    }
    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; }
    }
Sophie Coudert's avatar
Sophie Coudert committed
    private void addError(AvatarFromSysMLError _err) {
        errors.add(_err);
Sophie Coudert's avatar
Sophie Coudert committed
    public AvatarSpecification sysMLtoSpec(String _fileName) {
        AvatarFromSysMLParser parser;
        try { parser =
                new AvatarFromSysMLParser(new AvatarFromSysMLLexer(new FileReader(_fileName)),
                        new ComplexSymbolFactory()); }
        catch (java.lang.Exception e) {
            e.printStackTrace(out);
            return new AvatarSpecification("DummySpec", null);
        }
        TraceManager.addDev("Parsing Model");
        try { stxSpec = parser.parseModel(); }
        catch (java.lang.Exception e) {
            e.printStackTrace(out);
Sophie Coudert's avatar
Sophie Coudert committed
            return new AvatarSpecification("DummySpec", null);
        if (stxSpec == null) {
            for(AvatarFromSysMLError e : parser.getErrors())
                TraceManager.addDev(e.toString());
            return new AvatarSpecification("DummySpec", null);
        }
        errors = stxSpec.getErrors();
        TraceManager.addDev("Building Specification");
Sophie Coudert's avatar
Sophie Coudert committed
        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();
        for(AvatarFromSysMLError e : errors)
            TraceManager.addDev(e.toString());
Sophie Coudert's avatar
Sophie Coudert committed
        return avSpec;
    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));
        }
    }
    private void addTransition(AvatarTransition t, AvatarStateMachineElement e) {
        transitionMap.get(e).getTransitions().add(t);
    }
    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;
    }
    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;
    }
    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;
    }
    private AvatarAttribute getTimerByName(String _name, AvatarBlock _block) {
        AvatarAttribute res =  getAttributeByName(_name, _block);
        if (res != null && res.getType() == AvatarType.TIMER) return res;
        return null;
    }
    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, _b);
                    break;
                case AvatarFromSysMLSyntax.STXPRESENDSTATE :
                case AvatarFromSysMLSyntax.STXPRERECEIVESTATE :
                    addError(new AvatarFromSysMLError(AvatarFromSysMLError.BUGERROR, 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    private class BuildDataType implements BiConsumer<String, StxDataType> {
        public BuildDataType(){}
        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);
            int nbFields = d.getSize();
            for (int i = 0; i < nbFields; i++) {
                String type =  d.getFieldType(i);
Sophie Coudert's avatar
Sophie Coudert committed
                if(type.equals("Integer")) {
                    AvatarAttribute aa = new AvatarAttribute(d.getFieldName(i),AvatarType.INTEGER,null,null);
                    dataType.addAttribute(aa, null);
                }
                else if(type.equals("Boolean")) {
                    AvatarAttribute aa = new AvatarAttribute(d.getFieldName(i),AvatarType.BOOLEAN,null,null);
                    dataType.addAttribute(aa, null);
                }
                else {
                    AvatarAttribute aa = new AvatarAttribute(d.getFieldName(i),AvatarType.UNDEFINED,null,null);
                    dataType.addAttribute(aa, type);
                }
            }
        }
    }
    private void buildDataTypes(){
        List<AvatarDataType> dtList = avSpec.getDataTypes();
        stxSpec.getDataTypeMap().forEach(new BuildDataType());
        int size = dtList.size();
        for(int i = 0; i < size; i++) {
            AvatarDataType dt = dtList.get(i);
            AvatarDataType.finalize(dt, avSpec);
            if (dt.getFullSize() == -1)
                addError(new AvatarFromSysMLError(AvatarFromSysMLError.HIGHERROR, null, "datatype " + dt.getName() + " is not well defined", null));
        }
    Location location = null;
    // BUILDING BLOCKS (but not statemachines) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    private class BuildBlock implements BiConsumer<String, StxBlock> {
        List<AvatarDataType> dataTypeList;
        public BuildBlock(){}
        public void accept(String n, StxBlock blk) {
Sophie Coudert's avatar
Sophie Coudert committed
            if (!blk.isDeclared()) {
                addError(new AvatarFromSysMLError(AvatarFromSysMLError.HIGHERROR,
                        "block " + blk.getName() +"has been used but has not been declared"));
                return;
Sophie Coudert's avatar
Sophie Coudert committed
            }
            AvatarBlock theBlock = getBlock(blk);
            avSpec.addBlock(theBlock);
            // set father
            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);
                AvatarAttribute aa;
                if (a.getType().equals("Integer")) {
                    aa = new AvatarAttribute(a.getName(), AvatarType.INTEGER, theBlock, null);
                    aa.setInitialValue(a.getInit());
                    ax = new AvatarAttribute(a.getName(), AvatarType.INTEGER, theBlock, null);
                    ax.setInitialValue(a.getInit());
                    theBlock.addAttribute(ax);
                    ax.setAsConstant(false);
                }
                else if (a.getType().equals("Boolean")) {
                    aa = new AvatarAttribute(a.getName(), AvatarType.BOOLEAN, theBlock, null);
                    aa.setInitialValue(a.getInit());
                    ax = new AvatarAttribute(a.getName(), AvatarType.BOOLEAN, theBlock, null);
                    ax.setInitialValue(a.getInit());
                    theBlock.addAttribute(ax);
                    ax.setAsConstant(false);
                    aa = new AvatarAttribute(a.getName(), AvatarType.UNDEFINED, theBlock, null);
                    // aa.setInitialValue(a.getInit());
Sophie Coudert's avatar
Sophie Coudert committed
                    AvatarDataType adt = avSpec.getDataTypeByName(a.getType());
                    aa.setDataType(adt);
                    String aaName =  a.getName() + "__";
                    int dtsize = adt.getFullSize();
                    for (int j = 0; j < dtsize; j++) {
                        ax = new AvatarAttribute(aaName + adt.getFieldString(j),
                                adt.getFieldStringType(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);
                AvatarAttribute aa;
Loading
Loading full blame...