Skip to content
Snippets Groups Projects
AvatarFromSysML.java 82.6 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); }

    /** 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
        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);
        }

        // Run parser and get parser errors
        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();

        // 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)
            TraceManager.addDev(e.toString());
            throw ex;
        }
        // TODO: move error handling
        for(AvatarFromSysMLError e : errors)
            TraceManager.addDev(e.toString());
Sophie Coudert's avatar
Sophie Coudert committed
        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);
Loading
Loading full blame...