Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal system parameters #2199

Merged
merged 3 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions HopsanCore/include/ComponentSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ namespace hopsan {
bool wasSimulationAborted() const;

// System parameters
bool setOrAddSystemParameter(const HString &rName, const HString &rValue, const HString &rType, const HString &rDescription="", const HString &rUnitOrQuantity="", const bool force=false);
bool setSystemParameter(const HString &rName, const HString &rValue, const HString &rType, const HString &rDescription="", const HString &rUnitOrQuantity="", const bool force=false);
bool setOrAddSystemParameter(const HString &rName, const HString &rValue, const HString &rType, const HString &rDescription="", const HString &rUnitOrQuantity="", const bool internal=false, const bool force=false);
bool setSystemParameter(const HString &rName, const HString &rValue, const HString &rType, const HString &rDescription="", const HString &rUnitOrQuantity="", const bool internal=false, const bool force=false);
void unRegisterParameter(const HString &name);
void addSearchPath(HString searchPath);

Expand Down
10 changes: 6 additions & 4 deletions HopsanCore/include/Parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ class HOPSANCORE_DLLAPI ParameterEvaluator
friend class ParameterEvaluatorHandler;
public:
ParameterEvaluator(const HString &rName, const HString &rValue, const HString &rDescription, const HString &rQuantity, const HString &rUnit,
const HString &rType, void* pDataPtr=0, ParameterEvaluatorHandler* pParameterEvalHandler=0);
const HString &rType, const bool internal=false, void* pDataPtr=0, ParameterEvaluatorHandler* pParameterEvalHandler=0);

bool setParameterValue(const HString &rValue, ParameterEvaluator **ppNeedEvaluation=0, bool force=false);
bool setParameter(const HString &rValue, const HString &rDescription, const HString &rQuantity, const HString &rUnit,
const HString &rType, ParameterEvaluator **pNeedEvaluation=0, bool force=false);
const HString &rType, ParameterEvaluator **pNeedEvaluation=0, bool internal=false, bool force=false);

bool evaluate(HString &rResult);
bool evaluate();
Expand All @@ -67,6 +67,7 @@ class HOPSANCORE_DLLAPI ParameterEvaluator
const HString &getDescription() const;
const HString &getQuantity() const;
const std::vector<HString> &getConditions() const;
bool isInternal() const;

void setTriggersReconfiguration();
bool triggersReconfiguration();
Expand All @@ -85,6 +86,7 @@ class HOPSANCORE_DLLAPI ParameterEvaluator
size_t mDepthCounter;
ParameterEvaluatorHandler* mpParameterEvaluatorHandler;
std::vector<HString> mConditions;
bool mInternal;
bool mTriggersReconfiguration;
};

Expand All @@ -97,15 +99,15 @@ class HOPSANCORE_DLLAPI ParameterEvaluatorHandler

bool addParameter(const HString &rName, const HString &rValue, const HString &rDescription,
const HString &rQuantity, const HString &rUnit, const HString &rType,
void* pData=0, bool force=false, std::vector<HString> conditions = std::vector<HString>());
void* pData=0, bool internal=false, bool force=false, std::vector<HString> conditions = std::vector<HString>());
void deleteParameter(const HString &rName);
bool renameParameter(const HString &rOldName, const HString &rNewName);

const std::vector<ParameterEvaluator*> *getParametersVectorPtr() const;
const ParameterEvaluator* getParameter(const HString &rName) const;
void getParameterNames(std::vector<HString> &rParameterNames);
bool setParameter(const HString &rName, const HString &rValue, const HString &rDescription="", const HString &rQuantity="",
const HString &rUnit="", const HString &rType="", const bool force=false);
const HString &rUnit="", const HString &rType="", const bool internal=false, const bool force=false);

void getParameterValue(const HString &rName, HString &rValue);
bool setParameterValue(const HString &rName, const HString &rValue, bool force=false);
Expand Down
2 changes: 1 addition & 1 deletion HopsanCore/src/Component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ void Component::registerConditionalParameter(const HString &rName, const HString
if(mpParameters->hasParameter(rName))
mpParameters->deleteParameter(rName); //Remove parameter if it is already registered

mpParameters->addParameter(rName, to_hstring(rValue), rDescription, "", "", "conditional", &rValue, false, rConditions);
mpParameters->addParameter(rName, to_hstring(rValue), rDescription, "", "", "conditional", &rValue, false, false, rConditions);
}


Expand Down
10 changes: 5 additions & 5 deletions HopsanCore/src/ComponentSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,14 +290,14 @@ void ComponentSystem::addSearchPath(HString searchPath)


//! @brief Set, add or change a system parameter including all meta data
bool ComponentSystem::setOrAddSystemParameter(const HString &rName, const HString &rValue, const HString &rType, const HString &rDescription, const HString &rUnitOrQuantity, const bool force)
bool ComponentSystem::setOrAddSystemParameter(const HString &rName, const HString &rValue, const HString &rType, const HString &rDescription, const HString &rUnitOrQuantity, const bool internal, const bool force)
{
bool success;
HString quantity, bu;
if(mpParameters->hasParameter(rName))
{
checkIfQuantityOrUnit(rUnitOrQuantity, quantity, bu);
success = mpParameters->setParameter(rName, rValue, rDescription, quantity, bu, rType, force);
success = mpParameters->setParameter(rName, rValue, rDescription, quantity, bu, rType, internal, force);
}
else
{
Expand All @@ -309,7 +309,7 @@ bool ComponentSystem::setOrAddSystemParameter(const HString &rName, const HStrin
else
{
checkIfQuantityOrUnit(rUnitOrQuantity, quantity, bu);
success = mpParameters->addParameter(rName, rValue, rDescription, quantity, bu, rType, 0, force);
success = mpParameters->addParameter(rName, rValue, rDescription, quantity, bu, rType, 0, internal, force);
if (success)
{
reserveUniqueName(rName,UniqueSysparamNameType);
Expand All @@ -322,11 +322,11 @@ bool ComponentSystem::setOrAddSystemParameter(const HString &rName, const HStrin


//! @brief Set or change a system parameter including all meta data
bool ComponentSystem::setSystemParameter(const HString &rName, const HString &rValue, const HString &rType, const HString &rDescription, const HString &rUnitOrQuantity, const bool force)
bool ComponentSystem::setSystemParameter(const HString &rName, const HString &rValue, const HString &rType, const HString &rDescription, const HString &rUnitOrQuantity, const bool internal, const bool force)
{
if(mpParameters->hasParameter(rName))
{
return setOrAddSystemParameter(rName, rValue, rType, rDescription, rUnitOrQuantity, force);
return setOrAddSystemParameter(rName, rValue, rType, rDescription, rUnitOrQuantity, internal, force);
}
addErrorMessage("No such system parameter: "+rName);
return false;
Expand Down
3 changes: 2 additions & 1 deletion HopsanCore/src/CoreUtilities/HmfLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,13 +674,14 @@ void loadSystemParameters(rapidxml::xml_node<> *pSysNode, ComponentSystem* pSyst
string paramName = readStringAttribute(pParameter, "name", "ERROR_NO_PARAM_NAME_GIVEN");
string val = readStringAttribute(pParameter, "value", "ERROR_NO_PARAM_VALUE_GIVEN");
string type = readStringAttribute(pParameter, "type", "ERROR_NO_PARAM_TYPE_GIVEN");
bool internal = readBoolAttribute(pParameter, "internal", false);
//! @todo maybe type should be data type or value type or something
string quantityORunit = readStringAttribute(pParameter, "quantity", readStringAttribute(pParameter, "unit", ""));
string description = readStringAttribute(pParameter, "description", "");

// Here we use force=true to make sure system parameters load even if they do not evaluate
//! @todo if system parameters are loaded in the correct order (top to bottom) they should evaluate, why don't they?
bool ok = pSystem->setOrAddSystemParameter(paramName.c_str(), val.c_str(), type.c_str(), description.c_str(), quantityORunit.c_str(), true);
bool ok = pSystem->setOrAddSystemParameter(paramName.c_str(), val.c_str(), type.c_str(), description.c_str(), quantityORunit.c_str(), internal, true);
if(!ok)
{
pSystem->addErrorMessage(HString("Failed to load parameter: ")+(paramName+"="+val).c_str());
Expand Down
22 changes: 16 additions & 6 deletions HopsanCore/src/Parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ using namespace std;
//! @param [in] pDataPtr Only used by Components, system parameters don't use this, default: 0
//! @param [in] pParentParameters A pointer to the Parameters object that contains the Parameter
ParameterEvaluator::ParameterEvaluator(const HString &rName, const HString &rValue, const HString &rDescription, const HString &rQuantity, const HString &rUnit,
const HString &rType, void* pDataPtr, ParameterEvaluatorHandler* pParameterEvalHandler)
const HString &rType, const bool internal, void* pDataPtr, ParameterEvaluatorHandler* pParameterEvalHandler)
{
mDepthCounter=0;
mParameterName = rName;
Expand All @@ -73,6 +73,7 @@ ParameterEvaluator::ParameterEvaluator(const HString &rName, const HString &rVal
mQuantity = rQuantity;
mUnit = rUnit;
mTriggersReconfiguration = false;
mInternal = internal;

mpData = pDataPtr;
mpParameterEvaluatorHandler = pParameterEvalHandler;
Expand All @@ -87,14 +88,15 @@ void* ParameterEvaluator::getDataPtr()
return mpData;
}

bool ParameterEvaluator::setParameter(const HString &rValue, const HString &rDescription, const HString &rQuantity, const HString &rUnit, const HString &rType, ParameterEvaluator **pNeedEvaluation, bool force)
bool ParameterEvaluator::setParameter(const HString &rValue, const HString &rDescription, const HString &rQuantity, const HString &rUnit, const HString &rType, ParameterEvaluator **pNeedEvaluation, bool internal, bool force)
{
bool success;
HString oldValue = mParameterValue;
HString oldDescription = mDescription;
HString oldUnit = mUnit;
HString oldType = mType;
HString oldQuantity = mQuantity;
bool oldInternal = mInternal;
if(!rDescription.empty())
{
mDescription = rDescription;
Expand All @@ -111,6 +113,7 @@ bool ParameterEvaluator::setParameter(const HString &rValue, const HString &rDes
{
mType = rType;
}
mInternal = internal;
success = setParameterValue(rValue, pNeedEvaluation);
if((force) && !(success))
{
Expand All @@ -124,6 +127,7 @@ bool ParameterEvaluator::setParameter(const HString &rValue, const HString &rDes
mQuantity = oldQuantity;
mUnit = oldUnit;
mType = oldType;
mInternal = oldInternal;
}
return success;
}
Expand Down Expand Up @@ -461,6 +465,11 @@ const std::vector<HString> &ParameterEvaluator::getConditions() const
return mConditions;
}

bool ParameterEvaluator::isInternal() const
{
return mInternal;
}

void ParameterEvaluator::setTriggersReconfiguration()
{
mTriggersReconfiguration = true;
Expand Down Expand Up @@ -548,15 +557,15 @@ ParameterEvaluatorHandler::~ParameterEvaluatorHandler()
//! @param [in] force Should we force to add parameter even if it fails to evaluate
//! @param [in] conditions Conditions for a conditional constant parameter
//! @return true if success, otherwise false
bool ParameterEvaluatorHandler::addParameter(const HString &rName, const HString &rValue, const HString &rDescription, const HString &rQuantity, const HString &rUnit, const HString &rType, void* pData, bool force, std::vector<HString> conditions)
bool ParameterEvaluatorHandler::addParameter(const HString &rName, const HString &rValue, const HString &rDescription, const HString &rQuantity, const HString &rUnit, const HString &rType, void* pData, bool internal, bool force, std::vector<HString> conditions)
{
bool success = false;
if (!rName.empty())
{
if(!hasParameter(rName))
{
//! @todo should make sure that parameter names do not have + - * / . or similar as first character
ParameterEvaluator* newParameter = new ParameterEvaluator(rName, rValue, rDescription, rQuantity, rUnit, rType, pData, this);
ParameterEvaluator* newParameter = new ParameterEvaluator(rName, rValue, rDescription, rQuantity, rUnit, rType, internal, pData, this);
if(rType == "conditional")
{
newParameter->mConditions = conditions;
Expand Down Expand Up @@ -688,9 +697,10 @@ const std::vector<ParameterEvaluator*> *ParameterEvaluatorHandler::getParameters


bool ParameterEvaluatorHandler::setParameter(const HString &rName, const HString &rValue, const HString &rDescription,
const HString &rQuantity, const HString &rUnit, const HString &rType, const bool force)
const HString &rQuantity, const HString &rUnit, const HString &rType, const bool internal, const bool force)
{
bool success = false;
std::cout << "Setting hidden to " << internal << "\n";

// Try to find the parameter among the existing parameters
for(size_t i=0; i<mParameters.size(); ++i)
Expand All @@ -699,7 +709,7 @@ bool ParameterEvaluatorHandler::setParameter(const HString &rName, const HString
if( (rName == mParameters[i]->getName()) )//&& (value != mParameters[i]->getName()) ) //By commenting this a parameter can be set to a systems parameter with same name as component parameter e.g. mass m = m (system parameter) related to issue #783
{
ParameterEvaluator *needEvaluation=0;
success = mParameters[i]->setParameter(rValue, rDescription, rQuantity, rUnit, rType, &needEvaluation, force); //Sets the new value, if the parameter is of the type to need evaluation e.g. if it is a system parameter needEvaluation points to the parameter
success = mParameters[i]->setParameter(rValue, rDescription, rQuantity, rUnit, rType, &needEvaluation, internal, force); //Sets the new value, if the parameter is of the type to need evaluation e.g. if it is a system parameter needEvaluation points to the parameter
if(needEvaluation)
{
if(mParametersNeedEvaluation.end() == find(mParametersNeedEvaluation.begin(), mParametersNeedEvaluation.end(), needEvaluation))
Expand Down
Loading
Loading