// /home/siemens/bip/examples/sync_lec/learn.bip:119:1

#include <learn/AT__learn__SpeedSensor.hpp>

#include <iostream>
#include <sstream>

#include <CycleInAtomPrioritiesError.hpp>
#include <NonDeterministicPetriNetError.hpp>
#include <NonOneSafePetriNetError.hpp>
#include <AtomInvariantViolationError.hpp>
#include <Variables.hpp>


bool AT__learn__SpeedSensor::atIdle () const {
    return __statesbv[ 0/(8*sizeof(int))] & 1<< (0%(8*sizeof(int)));
}

bool AT__learn__SpeedSensor::toIdle () {
    if (atIdle()) return false;
    __statesbv[ 0/(8*sizeof(int))] |= 1<< (0%(8*sizeof(int)));
    return true;
}

bool AT__learn__SpeedSensor::fromIdle () {
    if (!atIdle()) return false;
    __statesbv[ 0/(8*sizeof(int))] &= ~(1<< (0%(8*sizeof(int))));
    return true;
}

AT__learn__SpeedSensor::AT__learn__SpeedSensor(const string &name , AtomIPort__learn__intPort &_iport_decl__getSpeed, AtomIPort__learn__intPort &_iport_decl__changeSpeed
                         
                         , AtomEPort__learn__intPort &_eport_decl__getSpeed, AtomEPort__learn__intPort &_eport_decl__changeSpeed
                         
                         ) :  ComponentItf(name, ATOM), Atom(name) , _iport_decl__getSpeed(_iport_decl__getSpeed), _iport_decl_pv__getSpeed(_id__speed, this->fullName()+"._id__speed"), _iport_decl__changeSpeed(_iport_decl__changeSpeed), _iport_decl_pv__changeSpeed(_id__deltaSpeed, this->fullName()+"._id__deltaSpeed"), _eport_decl__getSpeed(_eport_decl__getSpeed), _eport_decl__changeSpeed(_eport_decl__changeSpeed), _id__speed(100), _transguard__1(false), _transguard__2(false), __previous(-1) {
    this->addInternalPort(_iport_decl__getSpeed);

    // link data internal to internal port
    this->addInternalPort(_iport_decl__changeSpeed);
    // export port
    this->addPort(_eport_decl__getSpeed);
    this->addPort(_eport_decl__changeSpeed);
}

BipError& AT__learn__SpeedSensor::execute(PortValue &portValue) {
    BipError *ret = &BipError::NoError;

#ifndef NDEBUG
    bool something_happened = false;
#endif

    // find the right transitions to execute
    if ((&_iport_decl_pv__getSpeed == &portValue) && _transguard__1) {
        // check consistency (enabledness, port values, ...)
        assert(_iport_decl__getSpeed.hasPortValue());
        assert(&_iport_decl__getSpeed.portValue() == &portValue);

        // check at most one transitions is executed
        assert(!something_happened);

        // the transition should be enabled by places
        assert(atIdle());

        // update input places
        fromIdle();

        // check one-safetyness
        if (atIdle()) {
            NonOneSafePetriNetError *r = new NonOneSafePetriNetError(*this);
            r->setPort(_iport_decl__getSpeed);
            return *r;
        }

        // update output places
        toIdle();

        // record the index of the latest executed transition
        __previous = 1;
// /home/siemens/bip/examples/sync_lec/learn.bip:127:2



        
#ifndef NDEBUG
        something_happened = true;
#endif
    } else if ((&_iport_decl_pv__changeSpeed == &portValue) && _transguard__2) {
        // check consistency (enabledness, port values, ...)
        assert(_iport_decl__changeSpeed.hasPortValue());
        assert(&_iport_decl__changeSpeed.portValue() == &portValue);

        // check at most one transitions is executed
        assert(!something_happened);

        // the transition should be enabled by places
        assert(atIdle());

        // update input places
        fromIdle();

        // check one-safetyness
        if (atIdle()) {
            NonOneSafePetriNetError *r = new NonOneSafePetriNetError(*this);
            r->setPort(_iport_decl__changeSpeed);
            return *r;
        }

        // update output places
        toIdle();

        // record the index of the latest executed transition
        __previous = 2;

        // execute transition actions
// /home/siemens/bip/examples/sync_lec/learn.bip:128:46
        int_variables[this->fullName()+"._id__speed"] = (int_variables[this->fullName()+"._id__speed"])  +  (int_variables[this->fullName()+"._id__deltaSpeed"]);

        // end of execute transition actions
// /home/siemens/bip/examples/sync_lec/learn.bip:128:2



        
#ifndef NDEBUG
        something_happened = true;
#endif
    }
    if (ret->type() != NO_ERROR) return *ret;

    // check that at least one transition has been executed
    assert(something_happened);

    // execute internal transitions
    ret = &executeInternalTransitions();

    if (ret->type() != NO_ERROR) return *ret;

    return update();
}

BipError& AT__learn__SpeedSensor::execute(AtomExternalPort &port) {
   BipError *ret = &BipError::NoError;

#ifndef NDEBUG
    bool something_happened = false;
#endif

    if (ret->type() != NO_ERROR) return *ret;

    // check that at least one transition has been executed
    assert(something_happened);

    // execute internal transitions
    ret = &executeInternalTransitions();

    if (ret->type() != NO_ERROR) return *ret;

    return update();
}

BipError& AT__learn__SpeedSensor::initialize() {
    BipError *ret = &BipError::NoError;
    
    // reset status of ports
    _iport_decl__getSpeed.clearPortValue();
    _iport_decl__changeSpeed.clearPortValue();

    // reset status of transitions
    _transguard__1 = false;
    _transguard__2 = false;

    // initialize to empty marking
    for (unsigned int idx = 0; idx < bvector_size; idx++){
        __statesbv[idx] = 0;
    }

    // marking must be initialized to empty
    assert((!atIdle()));

    // update initial places
    toIdle();

    // record the index of the latest executed transition
    __previous = 0;




    if (ret->type() != NO_ERROR) return *ret;

    // execute (initial) internal transitions
    ret = &executeInternalTransitions();

    if (ret->type() != NO_ERROR) return *ret;

    return update();
}

string AT__learn__SpeedSensor::toString() const {
    ostringstream oss;
    bool first=true;
    if (atIdle()) {
      if (!first) oss << ", ";
      else {
        first = false;
        oss << "at ";
      }
      oss << "Idle";
    }

    if (first) first = false;
    else oss << std::endl;
    oss << "speed=" << _id__speed;
    if (first) first = false;
    else oss << std::endl;
    oss << "deltaSpeed=" << _id__deltaSpeed;

    return oss.str(); 
}

BipError& AT__learn__SpeedSensor::update() {
    BipError *ret = &BipError::NoError;

    // update status of internal/external ports w.r.t. transitions
    // reset status of port changeSpeed
    this->_iport_decl__changeSpeed.setIsEnabled(false);
    
    _transguard__2 = (atIdle()
    
                );
    
    if (_transguard__2) {
        if (_iport_decl__changeSpeed.isEnabled()) {
            NonDeterministicPetriNetError *r = new NonDeterministicPetriNetError(*this);
            r->setPort(_iport_decl__changeSpeed);
        
            return *r;
        }
        
        _iport_decl__changeSpeed.setIsEnabled(true);
    }
    
    // reset status of port getSpeed
    this->_iport_decl__getSpeed.setIsEnabled(false);
    
    _transguard__1 = (atIdle()
    
                );
    
    if (_transguard__1) {
        if (_iport_decl__getSpeed.isEnabled()) {
            NonDeterministicPetriNetError *r = new NonDeterministicPetriNetError(*this);
            r->setPort(_iport_decl__getSpeed);
        
            return *r;
        }
        
        _iport_decl__getSpeed.setIsEnabled(true);
    }
    // recompute modified priority paths
    
    // applies priorities to port getSpeed
    
    // recompute modified priority paths
    
    // applies priorities to port changeSpeed
    
    // update port value of getSpeed
    if (this->_iport_decl__getSpeed.isEnabled() && !this->_iport_decl__getSpeed.isDisabledByPriorities()) {
        this->_iport_decl__getSpeed.setPortValue(_iport_decl_pv__getSpeed);
    }
    else {
        this->_iport_decl__getSpeed.clearPortValue();
    }
    
    // update port value of changeSpeed
    if (this->_iport_decl__changeSpeed.isEnabled() && !this->_iport_decl__changeSpeed.isDisabledByPriorities()) {
        this->_iport_decl__changeSpeed.setPortValue(_iport_decl_pv__changeSpeed);
    }
    else {
        this->_iport_decl__changeSpeed.clearPortValue();
    }
    
    // force recomputation of connectors involving getSpeed
    _eport_decl__getSpeed.setIsReset(true);
    
    // recompute port values of getSpeed from scratch
    this->_eport_decl__getSpeed.portValues().clear();
    
    // check port value of getSpeed exported by getSpeed
    if (this->_iport_decl__getSpeed.hasPortValue()) {
        this->_eport_decl__getSpeed.addPortValue(_iport_decl_pv__getSpeed);
    }
    
    // force recomputation of connectors involving changeSpeed
    _eport_decl__changeSpeed.setIsReset(true);
    
    // recompute port values of changeSpeed from scratch
    this->_eport_decl__changeSpeed.portValues().clear();
    
    // check port value of changeSpeed exported by changeSpeed
    if (this->_iport_decl__changeSpeed.hasPortValue()) {
        this->_eport_decl__changeSpeed.addPortValue(_iport_decl_pv__changeSpeed);
    }

    return *ret;
}

BipError &AT__learn__SpeedSensor::executeInternalTransitions() {
    BipError &ret = BipError::NoError;

    // used to record the enabled internal transition
    int __enabled_internal = 0;

    do {
        // recompute enabled internal transitions
        __enabled_internal = 0;


        // execute the enabled internal transition if exist
        if (__enabled_internal != 0) {
            switch (__enabled_internal) {

              default:
                assert(false);
            }
        }
    } while (__enabled_internal != 0);

    return ret;
}

AT__learn__SpeedSensor::~AT__learn__SpeedSensor() {
}
