Skip to content
Snippets Groups Projects
AvatarFromSysML.java 50.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.
 */
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 ArrayList<AvatarTransition> transitionList;
    private HashMap<StxSignal, AvatarSignal> signalMap;
    private HashMap<StxBlock, AvatarBlock> blockMap;
    private HashMap<StxState, AvatarStateMachineElement> stateMap;
Sophie Coudert's avatar
Sophie Coudert committed
    private List<AvatarFromSysMLError> errors;
    public AvatarFromSysML(){
        avSpec = null;
        stxSpec = null;
        transitionList = new ArrayList<AvatarTransition>();
        signalMap = new HashMap<StxSignal, AvatarSignal>();
        blockMap = new HashMap<StxBlock, AvatarBlock>();
        stateMap = new HashMap<StxState, AvatarStateMachineElement>();
Sophie Coudert's avatar
Sophie Coudert committed
        errors = null;
    }
    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);
        transitionList.clear();
        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 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;
                if (a.getType().equals("Integer")) {
                    aa = new AvatarAttribute(a.getName(), AvatarType.INTEGER, theBlock, null);
                    aa.setInitialValue(a.getInit());
                    theBlock.addAttribute(aa);
                    aa.setAsConstant(true);
                    ax = new AvatarAttribute(a.getName(), AvatarType.INTEGER, theBlock, null);
                    ax.setInitialValue(a.getInit());
                    theBlock.addAttribute(ax);
                    ax.setAsConstant(true);
                }
                else if (a.getType().equals("Boolean")) {
                    aa = new AvatarAttribute(a.getName(), AvatarType.BOOLEAN, theBlock, null);
                    aa.setInitialValue(a.getInit());
                    theBlock.addAttribute(aa);
                    aa.setAsConstant(true);
                    ax = new AvatarAttribute(a.getName(), AvatarType.INTEGER, theBlock, null);
                    ax.setInitialValue(a.getInit());
                    theBlock.addAttribute(ax);
                    ax.setAsConstant(true);
                else
                    addError(new AvatarFromSysMLError(AvatarFromSysMLError.LOWERROR,
                            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);
                buildProfile(sm, am, theBlock);
                String returnType = sm.getReturnType();
Sophie Coudert's avatar
Sophie Coudert committed
                if (returnType == null || returnType.length() == 0) continue;
                if (returnType.equals("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")) {
                    am.addReturnParameter(new AvatarAttribute("return__0", AvatarType.BOOLEAN, theBlock, null));
                    am.addOriginalReturnParameter(new AvatarAttribute("return__", AvatarType.BOOLEAN, theBlock, null));
                }
               else {
Sophie Coudert's avatar
Sophie Coudert committed
                    AvatarDataType adt = avSpec.getDataTypeByName(returnType);
Sophie Coudert's avatar
Sophie Coudert committed
                    if (adt == null)
                        addError(new AvatarFromSysMLError(AvatarFromSysMLError.HIGHERROR, sm.getLeft(),
                                "unknown type for method return parameter", null));
                    AvatarAttribute ret = new AvatarAttribute("return__", AvatarType.UNDEFINED, theBlock, null);
Sophie Coudert's avatar
Sophie Coudert committed
                    ret.setDataType(adt);
                    am.addOriginalReturnParameter(ret);
                    int dtsize = adt.getFullSize();
                    for (int j = 0; j < dtsize; j++) {
                        ret = new AvatarAttribute("return__" + j,
                                 adt.getFieldStringType(j), theBlock, null);
                        am.addReturnParameter(ret);
                    }
                }
            }
            // add Signals (build profile delayed)
            size = blk.getNbSignals();
            for (int i = 0; i < size; i++) {
                StxSignal ss = blk.getSignal(i);
                AvatarSignal as = getSignal(ss);
                theBlock.addSignal(as);
Sophie Coudert's avatar
Sophie Coudert committed
                StxChannel ch = ss.getChannel();
                if (ch == null) {
                    addError(new AvatarFromSysMLError(AvatarFromSysMLError.LOWERROR, ss.getLeft(),
                            "channel of signal " + ss.getName() + " has not been set"));
                    continue;
                }
                if (!ch.isDeclared()) {
                    addError(new AvatarFromSysMLError(AvatarFromSysMLError.LOWERROR, ss.getLeft(),
                            "channel of signal " + theBlock.getName() + "." + ss.getName() + " has not been declared"));
                    continue;
                }
                if (ss.isInput()) {
                    StxInMessage im = ss.getChannel().getInProfile();
                    if (im == null) {
                        addError(new AvatarFromSysMLError(AvatarFromSysMLError.LOWERROR, ss.getLeft(),
                                "input profile of signal " + ss.getName() + " has not been set"));
                        continue;
                    }
                    buildProfile(im, as, theBlock);
                }
                else {
                    StxOutMessage om = ss.getChannel().getOutProfile();
                    if (om == null) {
                        addError(new AvatarFromSysMLError(AvatarFromSysMLError.LOWERROR, ss.getLeft(),
                                "output profile of signal " + ss.getName() + " has not been set"));
                        continue;
                    }
                    buildProfile(om, as, theBlock);
                }
            // add states
            StxState[] states = blk.getStates();
            AvatarStateMachine asm = theBlock.getStateMachine();
            AvatarStateMachineElement theSourceState;
            size = states.length;
            for (int i = 0; i < size; i++) {
                byte stateType = states[i].getType();
                boolean undeclaredState = false;
Sophie Coudert's avatar
Sophie Coudert committed
                if (! states[i].isDeclared()) {
                    addError(new AvatarFromSysMLError(AvatarFromSysMLError.HIGHERROR,
                            "state " + theBlock.getName() + "." + states[i].getName() + " is used but not declared"));
                    stateType = AvatarFromSysMLSyntax.STXSTANDARDSTATE;
                    undeclaredState = true;
Sophie Coudert's avatar
Sophie Coudert committed
                }
                if (stateType != AvatarFromSysMLSyntax.STXPRESENDSTATE &&
                        stateType != AvatarFromSysMLSyntax.STXPRERECEIVESTATE &&
                        stateType != AvatarFromSysMLSyntax.STXSTOPSTATE ) {
                    if (undeclaredState) {
                        states[i].setType(AvatarFromSysMLSyntax.STXSTANDARDSTATE);
                    }
                    theSourceState = getState(states[i], theBlock);
                    asm.addElement(theSourceState);
                    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)
                        asm.setStartState((AvatarStartState) theSourceState);
                // add transitions
                    List<StxTransition> transitions = states[i].getTransitions();
                    int tsize = transitions.size();
                    for (int j = 0; j < tsize; j++) {
                        StxTransition transition = transitions.get(j);
Sophie Coudert's avatar
Sophie Coudert committed
                        if (transition == null) {
                            addError(new AvatarFromSysMLError(AvatarFromSysMLError.HIGHERROR, states[i].getLeft(),
                                    "something is wrong in state " + states[i].getName() + " : null transition found"));
                            continue;
                        }
                        if (! transition.isDeclared()) {
                            addError(new AvatarFromSysMLError(AvatarFromSysMLError.HIGHERROR, transition.getRleft(),
                                    "transition request in state " + states[i].getName() + " in block" + theBlock.getName() +
                                            " has no associated declaration" ));
Sophie Coudert's avatar
Sophie Coudert committed
                            continue;
                        }
                        AvatarTransition theTransition = new AvatarTransition(theBlock,"", null);
                        transitionList.add(theTransition);
                        asm.addElement(theTransition);
                        theSourceState.addNext(theTransition);
                        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);
Sophie Coudert's avatar
Sophie Coudert committed
                        setDistributionLaw(theTransition, transition.getDelayDistributionLaw(),transition.getDelayExtra(), transition);
                        theTransition.setProbability(transition.getProbability());
                        // add actions
                        List<StxAction> actions = transition.getActions(); // should exist only in some cases...
                        int asize = actions.size();
                        for (int k = 0; k < asize; k++) {
                            StxAction action = actions.get(k);
                            location = action.getLeft();
                            if(action.getType() == AvatarFromSysMLSyntax.STXASSIGNACTION){
                                String originalLeftHandStr = action.getTarget();
                                String leftHandStr = extendIdent(action.getTarget().replaceAll("\\.", "__"), blk);
                                String originalRightHandStr;
                                String rightHandStr;
                                StxTerm value = action.getValue();
                                if (value instanceof StxId) {
                                    originalRightHandStr = ((StxId)value).toString();
                                    rightHandStr = ((StxId)value).toString().replaceAll("\\.", "__");
                                }
                                else if (value instanceof StxExpr){
                                    originalRightHandStr = ((StxExpr)value).toString();
                                    rightHandStr = ((StxExpr)value).toString().replaceAll("\\.", "__");
                                }
                                else {
                                    originalRightHandStr = extendOriginalCall((StxCall)value,blk);
                                    rightHandStr = extendCall((StxCall)value,blk);
                                }
                                theTransition.addAction(leftHandStr + "=" + rightHandStr,
                                        originalLeftHandStr + "=" + originalRightHandStr);
                            }
                            else { // STXMETHODACTION
                                if (action.getValue() instanceof StxCall) { // else ERROR
                                    String originalTermStr = extendOriginalCall((StxCall)action.getValue(),blk);
                                    String termStr = extendCall((StxCall)action.getValue(),blk);
                                    theTransition.addAction(termStr, originalTermStr);
Sophie Coudert's avatar
Sophie Coudert committed
                        if (transition.getTarget() == null) {
                            addError(new AvatarFromSysMLError(AvatarFromSysMLError.HIGHERROR, transition.getLeft(),
                                    "something is wrong in transition : no target defined", transition.getRleft()));
                            continue;
                        }
                        if (transition.getTarget().getType() == AvatarFromSysMLSyntax.STXPRESENDSTATE ||
                           transition.getTarget().getType() == AvatarFromSysMLSyntax.STXPRERECEIVESTATE) {
                            if (transition.getTarget().getTransitions().size() == 0 ||
                                transition.getTarget().getTransition(0) == null) {
                                addError(new AvatarFromSysMLError(AvatarFromSysMLError.HIGHERROR, transition.getTarget().getLeft(),
                                        "something is wrong in precommunication state " + transition.getTarget().getName() +
                                        " : no outgoing transition"));
                                continue;
                            }
                            byte type = transition.getTarget().getType();
                            transition = transition.getTarget().getTransition(0);
Sophie Coudert's avatar
Sophie Coudert committed
                            if (type == AvatarFromSysMLSyntax.STXPRESENDSTATE &
                                    ! ( ( 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.HIGHERROR, 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;
                            }
                            if (type == AvatarFromSysMLSyntax.STXPRERECEIVESTATE &
                                    ! ( ( transition.getTarget().getType() == AvatarFromSysMLSyntax.STXRECEIVESTATE &&
                                            transition.getType() == AvatarFromSysMLSyntax.STXRECEIVETRANSITION ) ||
                                        ( transition.getTarget().getType() == AvatarFromSysMLSyntax.STXEXPIRETIMERSTATE &&
                                                transition.getType() == AvatarFromSysMLSyntax.STXEXPIRETIMERTRANSITION ) )) {
                                addError(new AvatarFromSysMLError(AvatarFromSysMLError.HIGHERROR, transition.getTarget().getLeft(),
                                        "outgoing transitions from pre-reveive state must be sending transitions leading to some " +
                                                "receivinging state (receive/expire timer)", transition.getLeft()));
                                continue;
                            }
                        }
                        AvatarStateMachineElement tgtState;
                        if (transition.getTarget().getType() == AvatarFromSysMLSyntax.STXSTOPSTATE) {
Sophie Coudert's avatar
Sophie Coudert committed
                            if (transition.getTarget().getTransitions().size() != 0) {
                                addError(new AvatarFromSysMLError(AvatarFromSysMLError.LOWWARNING, transition.getTarget().getLeft(),
                                        "stopstate should not have outgoing transitions"));
                            }
                            tgtState = new AvatarStopState("StopState", null, theBlock);
                            asm.addElement(tgtState);
                        }
Sophie Coudert's avatar
Sophie Coudert committed
                        else tgtState = getState(transition.getTarget(), theBlock);
                        theTransition.addNext(tgtState);

                        if (transition.getType() == AvatarFromSysMLSyntax.STXSENDTRANSITION) {
                            if(tgtState instanceof AvatarActionOnSignal) {
                                AvatarActionOnSignal state = (AvatarActionOnSignal)tgtState;
Sophie Coudert's avatar
Sophie Coudert committed
                                if (transition.getSignal() == null) {
                                    addError(new AvatarFromSysMLError(AvatarFromSysMLError.HIGHERROR, transition.getRleft(),
                                            "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()) {
                                    new AvatarFromSysMLError(AvatarFromSysMLError.HIGHERROR, transition.getRleft(),
                                            "signal of sending transition must be an output signal", transition.getSignal().getLeft());
                                    continue;
                                }
                                if (! transition.getSignal().isDeclared()) {
                                    addError(new AvatarFromSysMLError(AvatarFromSysMLError.LOWERROR, transition.getRleft(),
                                            "transition uses a signal " + transition.getSignal().getName() + " that has not been declared"));
                                    continue;
                                }
                                location = transition.getRleft();
                                ArrayList<StxTerm> sendPayload = transition.getSendPayload();
Sophie Coudert's avatar
Sophie Coudert committed
                                if (sendPayload != null) {
                                    int nbParam = sendPayload.size();
                                    for(int u = 0; u < nbParam; u++) {
                                        StxTerm p = sendPayload.get(u);
                                        if (p instanceof StxId) {
                                            state.addOriginalValue(((StxId)p).toString());
                                            String[] ids = extendPath(
                                                    ((StxId)p).toString().replaceAll("\\.", "__"),
                                                    blk).split(",");
                                            int isize = ids.length;
                                            for(int w = 0; w < isize; w++)  state.addValue(ids[w]);
                                        }
                                        else if (p instanceof StxExpr) {
                                            state.addOriginalValue( ((StxExpr)p).toString() );
                                            state.addValue( ((StxExpr)p).toString().replaceAll("\\.", "__"));
                                        }
                                        else {
                                            state.addOriginalValue(extendOriginalCall((StxCall)p,blk));
                                            state.addValue(extendCall((StxCall)p,blk));
                                        }
Sophie Coudert's avatar
Sophie Coudert committed
                            else  {
                                addError(new AvatarFromSysMLError(AvatarFromSysMLError.HIGHERROR, transition.getLeft(),
                                        "sending transition should lead to a send state", transition.getRleft()));
                            }
                        }
                        else if (transition.getType() == AvatarFromSysMLSyntax.STXRECEIVETRANSITION) {
                            if(tgtState instanceof AvatarActionOnSignal) {
                                AvatarActionOnSignal state = (AvatarActionOnSignal)tgtState;
Sophie Coudert's avatar
Sophie Coudert committed
                                if (transition.getSignal() == null) {
                                    addError(new AvatarFromSysMLError(AvatarFromSysMLError.HIGHERROR, transition.getRleft(),
                                            "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()) {
                                    addError(new AvatarFromSysMLError(AvatarFromSysMLError.LOWERROR, transition.getRleft(),
                                        "transition uses a signal " + transition.getSignal().getName() + " that has not been declared"));
                                    continue;
                                }
                                if (state.getSignal().isOut()) {
                                    new AvatarFromSysMLError(AvatarFromSysMLError.HIGHERROR, transition.getRleft(),
                                            "signal of receiving transition must be an input signal", transition.getSignal().getLeft());
                                    continue;
                                }
                                if (transition.getSignal().getChannel() == null) {
                                    addError(new AvatarFromSysMLError(AvatarFromSysMLError.HIGHERROR, transition.getRleft(),
                                            "signal of receiving transition doesn't have an associated channel", transition.getSignal().getLeft()));
                                    continue;
                                }
                                location = transition.getLeft();
                                StxOutMessage profile = transition.getSignal().getChannel().getOutProfile();
Sophie Coudert's avatar
Sophie Coudert committed
                                if (profile != null) {
                                    HashMap<String, String> receivePayload = transition.getReceivePayload();
                                    int nbParams = profile.getSize();
                                    for (int k = 0; k < nbParams; k++) {
                                        String value = receivePayload.get(profile.getFieldName(k));
                                        state.addOriginalValue(value);
                                        String[] ids = extendPath(value.replaceAll("\\.", "__"), blk).split(",");
                                        int isize = ids.length;
                                        for(int w = 0; w < isize; w++)  state.addValue(ids[w]);
Sophie Coudert's avatar
Sophie Coudert committed
                                    }
Sophie Coudert's avatar
Sophie Coudert committed
                            else  {
                                addError(new AvatarFromSysMLError(AvatarFromSysMLError.HIGHERROR, transition.getLeft(),
                                        "receiving transition should lead to a receive state", transition.getRleft()));
                            }
                        }
                        else if (transition.getType() == AvatarFromSysMLSyntax.STXSETTIMERTRANSITION) {
                             if(tgtState instanceof AvatarSetTimer) {
                                 AvatarSetTimer state = (AvatarSetTimer) tgtState;
Sophie Coudert's avatar
Sophie Coudert committed
                                 if (transition.getTimer() == null) {
                                     addError(new AvatarFromSysMLError(AvatarFromSysMLError.HIGHERROR, transition.getLeft(),
                                             "set-timer transition has no associated timer", transition.getRleft()));
                                 }
                                 state.setTimer(getTimerByName(transition.getTimer(), theBlock));
                                 if (transition.getSendPayload() == null || transition.getSendPayload().size() != 1
                                        || !(transition.getSendPayload().get(0) instanceof StxExpr)) {
Sophie Coudert's avatar
Sophie Coudert committed
                                     addError(new AvatarFromSysMLError(AvatarFromSysMLError.HIGHERROR, transition.getLeft(),
                                             "payload of set-timer transition is not well formed", transition.getRleft()));
                                     continue;
                                 }
                                 state.setTimerValue(((StxExpr)transition.getSendPayload().get(0)).toString().replaceAll(".","__"));
                                 state.setTimerOriginalValue(((StxExpr)transition.getSendPayload().get(0)).toString());
Sophie Coudert's avatar
Sophie Coudert committed
                             }
                             else  {
                                 addError(new AvatarFromSysMLError(AvatarFromSysMLError.HIGHERROR, transition.getLeft(),
                                         "set-timer transition should lead to a set-timer state", transition.getRleft()));
                             }
                        }
                        else if (transition.getType() == AvatarFromSysMLSyntax.STXRESETTIMERTRANSITION) {
                            if(tgtState instanceof AvatarResetTimer) {
                                AvatarResetTimer state = (AvatarResetTimer) tgtState;
Sophie Coudert's avatar
Sophie Coudert committed
                                if (transition.getTimer() == null) {
                                    addError(new AvatarFromSysMLError(AvatarFromSysMLError.HIGHERROR, transition.getLeft(),
                                             "reset-timer transition has no associated timer", transition.getRleft()));
                                }
                                state.setTimer(getTimerByName(transition.getTimer(), theBlock));
                            }
Sophie Coudert's avatar
Sophie Coudert committed
                             else  {
                                 addError(new AvatarFromSysMLError(AvatarFromSysMLError.HIGHERROR, transition.getLeft(),
                                         "reset-timer transition should lead to a reset-timer state", transition.getRleft()));
                             }
                        }
                        else if (transition.getType() == AvatarFromSysMLSyntax.STXEXPIRETIMERTRANSITION) {
                            if(tgtState instanceof AvatarExpireTimer) {
                                AvatarExpireTimer state = (AvatarExpireTimer) tgtState;
Sophie Coudert's avatar
Sophie Coudert committed
                                if (transition.getTimer() == null) {
                                    addError(new AvatarFromSysMLError(AvatarFromSysMLError.HIGHERROR, transition.getLeft(),
                                             "expire-timer transition has no associated timer", transition.getRleft()));
                                }
                                state.setTimer(getTimerByName(transition.getTimer(), theBlock));
Sophie Coudert's avatar
Sophie Coudert committed
                             else  {
                                 addError(new AvatarFromSysMLError(AvatarFromSysMLError.HIGHERROR, transition.getLeft(),
                                         "expire-timer transition should lead to a expire-timer state", transition.getRleft()));
                             }
        }
    }
    private void buildBlocks(){
        stxSpec.getBlockMap().forEach(new BuildBlock());
    }
Sophie Coudert's avatar
Sophie Coudert committed
    private void setDistributionLaw(AvatarTransition _t, String distributionLaw, HashMap<String,String> delayExtra, StxTransition _st) {
        int law = -1;
        int size;
        if (distributionLaw == null || distributionLaw.equals("")) law = AvatarTransition.DELAY_UNIFORM_LAW;
        else {
            size = _t.DISTRIBUTION_LAWS.length;
            for (int i = 0; i < size; i++) {
                if (_t.DISTRIBUTION_LAWS[i].equals(distributionLaw)) {
                    law = i;
                    break;
                }
            }
Sophie Coudert's avatar
Sophie Coudert committed
            if (law == -1) {
                addError(new AvatarFromSysMLError(AvatarFromSysMLError.HIGHERROR, _st.getLeft(),
                 "transition with unknown transition law : " + distributionLaw + " (set to uniform)"));
                law = AvatarTransition.DELAY_UNIFORM_LAW;
            }
        }
        String extra1 = _t.LABELS_OF_EXTRA_ATTRIBUTES_1[law];
        String extra2 = _t.LABELS_OF_EXTRA_ATTRIBUTES_2[law];
        size = _t.NB_OF_EXTRA_ATTRIBUTES[law];
Sophie Coudert's avatar
Sophie Coudert committed
        String tmpString;
Sophie Coudert's avatar
Sophie Coudert committed
            tmpString = extra1;
            extra1 = delayExtra.get(extra1);
Sophie Coudert's avatar
Sophie Coudert committed
            if (extra1 == null) {
                addError(new AvatarFromSysMLError(AvatarFromSysMLError.LOWERROR, _st.getLeft(),
                 "transition law : " + distributionLaw + " requires a " + tmpString + " parameter"));
            }
Sophie Coudert's avatar
Sophie Coudert committed
                tmpString = extra2;
                extra2 = delayExtra.get(extra2);
Sophie Coudert's avatar
Sophie Coudert committed
                if (extra2 == null) {
                    addError(new AvatarFromSysMLError(AvatarFromSysMLError.LOWERROR, _st.getLeft(),
                     "transition law : " + distributionLaw + " requires a " + tmpString + " parameter"));
                }
Sophie Coudert's avatar
Sophie Coudert committed
        if (delayExtra != null && delayExtra.size() > size) {
Sophie Coudert's avatar
Sophie Coudert committed
                    addError(new AvatarFromSysMLError(AvatarFromSysMLError.LOWWARNING, _st.getLeft(),
                     "transition law : " + distributionLaw + " has too much parameters"));

        }
        _t.setDistributionLaw(law, extra1, extra2);
    }
    private void buildProfile(StxStructure ss, AvatarMethod am, AvatarBlock b) {
        AvatarAttribute aa;
        int size = ss.getSize();
        for (int i = 0; i < size; i++) {
            String type = ss.getFieldType(i);
            if (type.equals("Integer")){
                aa = new AvatarAttribute(ss.getFieldName(i), AvatarType.INTEGER, b, null);
Sophie Coudert's avatar
Sophie Coudert committed
                am.addOriginalParameter(aa);
                aa.setAsConstant(false);
                aa = new AvatarAttribute(ss.getFieldName(i), AvatarType.INTEGER, b, null);
                am.addParameter(aa);
                aa.setAsConstant(false);
           } else if (type.equals("Boolean")) {
                aa = new AvatarAttribute(ss.getFieldName(i), AvatarType.BOOLEAN, b, null);
Sophie Coudert's avatar
Sophie Coudert committed
                am.addOriginalParameter(aa);
                aa.setAsConstant(false);
                aa = new AvatarAttribute(ss.getFieldName(i), AvatarType.BOOLEAN, b, null);
                am.addParameter(aa);
                aa.setAsConstant(false);
Sophie Coudert's avatar
Sophie Coudert committed
                AvatarDataType adt = avSpec.getDataTypeByName(type);
Sophie Coudert's avatar
Sophie Coudert committed
                if (adt == null) {
                    addError(new AvatarFromSysMLError(AvatarFromSysMLError.LOWWARNING, ss.getLeft(),
                     "message profile uses undefined datatype " + type));
                String aaName = ss.getFieldName(i);
                aa = new AvatarAttribute(aaName, AvatarType.UNDEFINED, b, null);
Sophie Coudert's avatar
Sophie Coudert committed
                aa.setDataType(adt);
                am.addOriginalParameter(aa);
                aa.setAsConstant(false);
                aaName = aaName + "__";
                int dtsize = adt.getFullSize();
                for (int j = 0; j < dtsize; j++) {
                    aa = new AvatarAttribute(aaName + adt.getFieldString(j),
                             adt.getFieldStringType(j), b, null);
                    am.addParameter(aa);
                    aa.setAsConstant(false);
                }
           }
        }
    }
    private String getStxAttributeType(String name, StxBlock blk){
        int size = blk.getNbAttributes();
        for (int i = 0; i < size; i++) {
            if (blk.getAttribute(i).getName().equals(name)) return blk.getAttribute(i).getType();
        }
        return null;
    }
    private String getStxPathType(String s, StxBlock b) {
        String[] path = s.split("__");
        String type = getStxAttributeType(path[0], b);
        int size = path.length;
        for (int i = 1; i < size; i++) {
Sophie Coudert's avatar
Sophie Coudert committed
            AvatarDataType adt = avSpec.getDataTypeByName(type);
            if (adt == null) return null;
Sophie Coudert's avatar
Sophie Coudert committed
            int nbFields = adt.getAttributeNb();
            int j;
            for (j = 0; j < nbFields; j++)
Sophie Coudert's avatar
Sophie Coudert committed
                if (adt.getAttribute(j).getName().equals(path[i])) break;
            if (j == nbFields) return null;
Sophie Coudert's avatar
Sophie Coudert committed
            AvatarType adtType = adt.getAttribute(j).getType();
            if (adtType == AvatarType.INTEGER)
Sophie Coudert's avatar
Sophie Coudert committed
            else if (adtType == AvatarType.BOOLEAN)
                type = "Boolean";
            else
                type = adt.getDataTypeName(j);
        }
        return type;
    }
    private String extendString;
    private AvatarDataType extendType;
    private String extendPath(String path, StxBlock b) {
        extendString = getStxPathType(path, b);
        if (extendString == null) {
            addError(new AvatarFromSysMLError(AvatarFromSysMLError.LOWERROR, location, "attribute " + path.replaceAll("__", "\\.") + " is undefined"));
            extendType = null;
            return path;
        }
        if (extendString.equals("Integer") || extendString.equals("Boolean")) {
            extendType = null;
            return path;
        }
        extendType = avSpec.getDataTypeByName(extendString);
        int size = extendType.getFullSize() - 1;
        if (size == -1) return path;
        StringBuffer res = new StringBuffer();
        int i;
        for (i = 0; i < size; i++)
            res.append(path + "__" + extendType.getFieldString(i) + ", ");
        res.append(path + "__" + extendType.getFieldString(i));
        return res.toString();
    }
    private String extendIdent (String path, StxBlock b) {
        String result = extendPath(path, b);
        return (result.indexOf(',') == -1 ? result : "(" + result + ")");
    }
     private String extendCall (StxCall call, StxBlock b) {
        StringBuffer result = new StringBuffer();
        int size = call.getNbIdents();
        int i;
        for (i =0 ; i < size; i++) {
            result.append(call.getSegment(i));
            result.append(extendPath(call.getIdent(i).replaceAll("\\.", "__"), b));
        }
        result.append(call.getSegment(i));
        return result.toString().replaceAll("\\.", "__");
     }
     private String extendOriginalCall (StxCall call, StxBlock b) {
        StringBuffer result = new StringBuffer();
        int size = call.getNbIdents();
        int i;
        for (i =0 ; i < size; i++) {
            result.append(call.getSegment(i));
Sophie Coudert's avatar
Sophie Coudert committed
            result.append(call.getIdent(i));
        }
        result.append(call.getSegment(i));
        return result.toString();
     }
     // BUILD RELATIONS %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    private void buildRelations(){
        stxSpec.getRelationMap().forEach(new BuildRelation());
    }
    private class BuildRelation implements BiConsumer<String, StxRelation> {
        public BuildRelation() {
        }
        public void accept(String n, StxRelation r) {
            if (!r.isDeclared()) {
                addError(new AvatarFromSysMLError(AvatarFromSysMLError.LOWERROR,
                        "relation " + r.getName() +" has been used but has not been declared"));
                int size = r.getSize();
                if (size == 0) {
                    addError(new AvatarFromSysMLError(AvatarFromSysMLError.LOWWARNING, r.getLeft(),
                     "relation is empty"));
                    return;
                } else {
                    int i = 0;
                    while (i < size && (r.getChannel(i).getBlockA() == null || r.getChannel(i).getBlockB() == null)) i++;
                    if (i == size) {
                        addError(new AvatarFromSysMLError(AvatarFromSysMLError.HIGHERROR,
                     "cannot determine blocs of relation " + r.getName()));
                        return;
                    }
                    r.setBlock1(stxSpec.getBlockMap().get(r.getChannel(i).getBlockA()));
                    r.setBlock2(stxSpec.getBlockMap().get(r.getChannel(i).getBlockB()));
                    if (r.getBlock1() == null || r.getBlock2() == null) {
                        addError(new AvatarFromSysMLError(AvatarFromSysMLError.HIGHERROR,
                     "cannot determine blocs of relation " + r.getName()));
                        return;
                    }
               }
            }
            AvatarBlock blk1 = getBlock(r.getBlock1());
            AvatarBlock blk2 = getBlock(r.getBlock2());
            AvatarRelation theRelation = new AvatarRelation(n, blk1, blk2, null);
            avSpec.addRelation(theRelation);
            theRelation.setAsynchronous(r.getAsynchronous());
            theRelation.setPrivate(r.getPrivate());
            theRelation.setLossy(r.getLossy());
            theRelation.setBlocking(r.getBlocking());
            theRelation.setSizeOfFIFO(r.getFifoSize());
            int size = r.getSize();
Sophie Coudert's avatar
Sophie Coudert committed
            if (size == 0) {
                addError(new AvatarFromSysMLError(AvatarFromSysMLError.LOWWARNING, r.getLeft(),
                 "relation " + r.getName() + " is empty"));
Sophie Coudert's avatar
Sophie Coudert committed
            }
            for (int i = 0; i < size; i++) {
                StxChannel c = r.getChannel(i);
                c.commuteSignals(r.getBlock1().getName());
                if (!blk1.getName().equals(c.getBlockA()) || !blk2.getName().equals(c.getBlockB())) {
                 addError(new AvatarFromSysMLError(AvatarFromSysMLError.HIGHERROR, c.getLeft(),
                 "inconsistency between blocs of channel " + c.getName() + " and relation " + r.getName()));
                 continue;
                }
                if (c.getSignalA() != null && c.getSignalB() != null)
                    theRelation.addSignals(getSignal(c.getSignalA()),getSignal(c.getSignalB()));
                else
                 addError(new AvatarFromSysMLError(AvatarFromSysMLError.LOWERROR, c.getLeft(),
                 "missing signal binding for channel"));
           }