<commit_before>/*-----------------------------------------------------------------------------
 This source file is part of Hopsan NG

 Copyright (c) 2011 
    <NAME>, <NAME>, <NAME>, <NAME>,
    <NAME>, <NAME>, <NAME>, <NAME>

 This file is provided "as is", with no guarantee or warranty for the
 functionality or reliability of the contents. All contents in this file is
 the original work of the copyright holders at the Division of Fluid and
 Mechatronic Systems (Flumes) at Linköping University. Modifying, using or
 redistributing any part of this file is prohibited without explicit
 permission from the copyright holders.
-----------------------------------------------------------------------------*/

//!
//! @file   Component.h
//! @author FluMeS
//! @date   2009-12-20
//!
//! @brief Contains Component base classes as well as Component Parameter class
//!
//$Id$

#ifndef COMPONENT_H_INCLUDED
#define COMPONENT_H_INCLUDED

#include "Node.h"
#include "Port.h"
#include "win32dll.h"
#include <string>
#include <map>

namespace hopsan {

#pragma pack(push, 1)
typedef struct HopsanExternalLibInfo
{
    char* hopsanCoreVersion;
    char* libCompiledDebugRelease;

}HopsanExternalLibInfoT;
#pragma pack(pop)

class Component;
class Parameters;

class Parameter
{
public:
    Parameter(std::string parameterName, std::string parameterValue, std::string description, std::string unit, std::string type, void* pDataPtr=0, Parameters* parentParameters=0);
    bool setParameterValue(const std::string value, Parameter **pNeedEvaluation=0);
    bool setParameter(std::string parameterValue, std::string description, std::string unit, std::string type, Parameter **pNeedEvaluation=0, bool force=0);
    void getParameter(std::string &rParameterName, std::string &rParameterValue, std::string &rDescription, std::string &rUnit, std::string &rType);
    std::string getType();
    bool evaluate(std::string &rResult, Parameter *ignoreMe=0);
    bool evaluate();

protected:
    std::string mParameterName;
    std::string mParameterValue;
    std::string mDescription;
    std::string mUnit;
    std::string mType;
    void* mpData;
    Parameters* mpParentParameters;
};


class DLLIMPORTEXPORT Parameters
{
public:
    Parameters(Component* parentComponent);
    bool addParameter(std::string parameterName, std::string parameterValue, std::string description="", std::string unit="", std::string type="", void* dataPtr=0, bool force=false);
    void deleteParameter(std::string parameterName);
    void getParameters(std::vector<std::string> &rParameterNames, std::vector<std::string> &rParameterValues, std::vector<std::string> &rDescriptions, std::vector<std::string> &rUnits, std::vector<std::string> &rTypes);
    bool setParameter(std::string name, std::string value, std::string description="", std::string unit="", std::string type="", bool force=0);
    bool setParameterValue(const std::string name, const std::string value, bool force=0);
    bool evaluateParameter(const std::string parameterName, std::string &rEvaluatedParameterValue, const std::string type, Parameter *ignoreMe=0);
    bool evaluateParameters();
    bool exist(std::string parameterName);
    bool checkParameters(std::string &errParName);

protected:
    std::vector<Parameter*> mParameters;
    Component* mParentComponent;
    std::vector<Parameter*> mParametersNeedEvaluation; //! @todo Use this vector to ensure parameters are valid at simulation time e.g. if a used system parameter is deleted before simulation
};


class ComponentSystem; //Forward declaration

    class HopsanEssentials;

    class DLLIMPORTEXPORT Component
    {
        friend class ComponentSystem;
        friend class HopsanEssentials; //Need to be able to set typename

    public:
        virtual ~Component();

        enum typeCQS {C, Q, S, UNDEFINEDCQSTYPE};
        //==========Public functions==========
        //Virtual functions
        virtual void loadStartValues();
        virtual void loadStartValuesFromSimulation();
        virtual bool initialize(const double startT, const double stopT, const size_t nSamples);
        virtual void simulate(const double startT, const double Ts);
        virtual void finalize(const double startT, const double Ts);
        virtual void setDesiredTimestep(const double timestep);
        virtual void setInheritTimestep(const bool inherit=true);
        virtual bool doesInheritTimestep();
        virtual bool isSimulationOk();

        //Name and type
        void setName(std::string name, bool doOnlyLocalRename=false);
        const std::string &getName();
        const std::string &getTypeName();
        typeCQS getTypeCQS();
        std::string getTypeCQSString();

        //Parameters
        void registerParameter(const std::string name, const std::string description, const std::string unit, double &rValue);
        void registerParameter(const std::string name, const std::string description, const std::string unit, std::string &rValue);
        void registerParameter(const std::string name, const std::string description, const std::string unit, bool &rValue);
        void unRegisterParameter(const std::string name);
        void getParameters(std::vector<std::string> &parameterNames, std::vector<std::string> &parameterValues,
                           std::vector<std::string> &descriptions, std::vector<std::string> &units, std::vector<std::string> &types);
        bool setParameterValue(const std::string name, const std::string value, bool force=0);
        void updateParameters();
        bool checkParameters(std::string &errParName);

        //Start values
        double getStartValue(Port* pPort, const size_t idx);
        void setStartValue(Port* pPort, const size_t idx, const double value);
        void disableStartValue(Port* pPort, const size_t idx);

        //Ports
        std::vector<Port*> getPortPtrVector();
        Port *getPort(const std::string portname);

        //System parent
        ComponentSystem *getSystemParent();
        size_t getModelHierarchyDepth();

        // Component type identification
        bool isComponentC();
        bool isComponentQ();
        bool isComponentSystem();
        bool isComponentSignal();

        //! @todo Should it be possible to set timestep of a component? Should only be possible for a Systemcomponent
        //void setTimestep(const double timestep);
        //double getTimestep();
        double *getTimePtr();

        void setMeasuredTime(double time);
        double getMeasuredTime();

        void addDebugMessage(std::string message);
        void addWarningMessage(std::string message);
        void addErrorMessage(std::string message);
        void addInfoMessage(std::string message);

        //Temporarily made public for RT-simulation
        virtual void initialize(); //! @todo Default values are hard set

    protected:
        //==========Protected member functions==========
        //Constructor - Destructor
        Component();<commit_msg>minor change<commit_after>/*-----------------------------------------------------------------------------
 This source file is part of Hopsan NG

 Copyright (c) 2011 
    <NAME>, <NAME>, <NAME>, <NAME>,
    <NAME>, <NAME>, <NAME>, <NAME>

 This file is provided "as is", with no guarantee or warranty for the
 functionality or reliability of the contents. All contents in this file is
 the original work of the copyright holders at the Division of Fluid and
 Mechatronic Systems (Flumes) at Linköping University. Modifying, using or
 redistributing any part of this file is prohibited without explicit
 permission from the copyright holders.
-----------------------------------------------------------------------------*/

//!
//! @file   Component.h
//! @author FluMeS
//! @date   2009-12-20
//!
//! @brief Contains Component base classes as well as Component Parameter class
//!
//$Id$

#ifndef COMPONENT_H_INCLUDED
#define COMPONENT_H_INCLUDED

#include "Node.h"
#include "Port.h"
#include "win32dll.h"
#include <string>
#include <map>

namespace hopsan {

#pragma pack(push, 1)
typedef struct HopsanExternalLibInfo
{
    char* hopsanCoreVersion;
    char* libCompiledDebugRelease;

}HopsanExternalLibInfoT;
#pragma pack(pop)

class Component;
class Parameters;

class Parameter
{
public:
    Parameter(std::string parameterName, std::string parameterValue, std::string description, std::string unit, std::string type, void* pDataPtr=0, Parameters* parentParameters=0);
    bool setParameterValue(const std::string value, Parameter **pNeedEvaluation=0);
    bool setParameter(std::string parameterValue, std::string description, std::string unit, std::string type, Parameter **pNeedEvaluation=0, bool force=0);
    void getParameter(std::string &rParameterName, std::string &rParameterValue, std::string &rDescription, std::string &rUnit, std::string &rType);
    std::string getType();
    bool evaluate(std::string &rResult, Parameter *ignoreMe=0);
    bool evaluate();

protected:
    std::string mParameterName;
    std::string mParameterValue;
    std::string mDescription;
    std::string mUnit;
    std::string mType;
    void* mpData;
    Parameters* mpParentParameters;
};


class DLLIMPORTEXPORT Parameters
{
public:
    Parameters(Component* parentComponent);
    bool addParameter(std::string parameterName, std::string parameterValue, std::string description="", std::string unit="", std::string type="", void* dataPtr=0, bool force=false);
    void deleteParameter(std::string parameterName);
    void getParameters(std::vector<std::string> &rParameterNames, std::vector<std::string> &rParameterValues, std::vector<std::string> &rDescriptions, std::vector<std::string> &rUnits, std::vector<std::string> &rTypes);
    bool setParameter(std::string name, std::string value, std::string description="", std::string unit="", std::string type="", bool force=0);
    bool setParameterValue(const std::string name, const std::string value, bool force=0);
    bool evaluateParameter(const std::string parameterName, std::string &rEvaluatedParameterValue, const std::string type, Parameter *ignoreMe=0);
    bool evaluateParameters();
    bool exist(std::string parameterName);
    bool checkParameters(std::string &errParName);

protected:
    std::vector<Parameter*> mParameters;
    Component* mParentComponent;
    std::vector<Parameter*> mParametersNeedEvaluation; //! @todo Use this vector to ensure parameters are valid at simulation time e.g. if a used system parameter is deleted before simulation
};


class ComponentSystem; //Forward declaration

    class HopsanEssentials;

    class DLLIMPORTEXPORT Component
    {
        friend class ComponentSystem;
        friend class HopsanEssentials; //Need to be able to set typename

    public:
        virtual ~Component();

        enum typeCQS {C, Q, S, UNDEFINEDCQSTYPE};
        //==========Public functions==========
        //Virtual functions
        virtual void loadStartValues();
        virtual void loadStartValuesFromSimulation();
        virtual void initialize(); //!< @todo We should really be able to return sucess true or false from components
        virtual bool initialize(const double startT, const double stopT, const size_t nSamples);
        virtual void simulate(const double startT, const double Ts);
        virtual void finalize(const double startT, const double Ts);
        virtual void setDesiredTimestep(const double timestep);
        virtual void setInheritTimestep(const bool inherit=true);
        virtual bool doesInheritTimestep();
        virtual bool isSimulationOk();

        //Name and type
        void setName(std::string name, bool doOnlyLocalRename=false);
        const std::string &getName();
        const std::string &getTypeName();
        typeCQS getTypeCQS();
        std::string getTypeCQSString();

        //Parameters
        void registerParameter(const std::string name, const std::string description, const std::string unit, double &rValue);
        void registerParameter(const std::string name, const std::string description, const std::string unit, std::string &rValue);
        void registerParameter(const std::string name, const std::string description, const std::string unit, bool &rValue);
        void unRegisterParameter(const std::string name);
        void getParameters(std::vector<std::string> &parameterNames, std::vector<std::string> &parameterValues,
                           std::vector<std::string> &descriptions, std::vector<std::string> &units, std::vector<std::string> &types);
        bool setParameterValue(const std::string name, const std::string value, bool force=0);
        void updateParameters();
        bool checkParameters(std::string &errParName);

        //Start values
        double getStartValue(Port* pPort, const size_t idx);
        void setStartValue(Port* pPort, const size_t idx, const double value);
        void disableStartValue(Port* pPort, const size_t idx);

        //Ports
        std::vector<Port*> getPortPtrVector();
        Port *getPort(const std::string portname);

        //System parent
        ComponentSystem *getSystemParent();
        size_t getModelHierarchyDepth();

        // Component type identification
        bool isComponentC();
        bool isComponentQ();
        bool isComponentSystem();
        bool isComponentSignal();

        //! @todo Should it be possible to set timestep of a component? Should only be possible for a Systemcomponent
        //void setTimestep(const double timestep);
        //double getTimestep();
        double *getTimePtr();

        void setMeasuredTime(double time);
        double getMeasuredTime();

        void addDebugMessage(std::string message);
        void addWarningMessage(std::string message);
        void addErrorMessage(std::string message);
        void addInfoMessage(std::string message);

    protected:
        //==========Protected member functions==========
        //Constructor - Destructor
        Component();
{'fraction_non_alphanumeric': 0.12987756855444976, 'fraction_numerical': 0.0037230615021121216, 'mean_word_length': 3.596248766041461, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1339676', 'n_tokens_mistral': 3695, 'n_tokens_neox': 3554, 'n_words': 1179}