Skip to content
Open
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
1 change: 1 addition & 0 deletions include/OMSimulator/OMSimulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ extern "C"
#endif

OMSAPI oms_status_enu_t OMSCALL oms_activateVariant(const char* crefA, const char* crefB);
OMSAPI oms_status_enu_t OMSCALL oms_addAlias(const char* cref, const char* rhs);
OMSAPI oms_status_enu_t OMSCALL oms_addBus(const char* cref);
OMSAPI oms_status_enu_t OMSCALL oms_addConnection(const char* crefA, const char* crefB, bool suppressUnitConversion);
OMSAPI oms_status_enu_t OMSCALL oms_addConnector(const char* cref, oms_causality_enu_t causality, oms_signal_type_enu_t type);
Expand Down
81 changes: 81 additions & 0 deletions src/OMSimulatorLib/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1392,6 +1392,47 @@ oms_status_enu_t oms::Model::registerSignalsForResultFile()
if (system)
if (oms_status_ok != system->registerSignalsForResultFile(*resultFile))
return oms_status_error;

for (auto &it : aliases)
{
const ComRef& alias = it.first;
ComRef rhs;
oms_signal_type_enu_t varType = oms_signal_type_real;
int result_file_id = -1;
std::tie(rhs, varType, result_file_id) = it.second;

std::string description = std::string("Alias target: ") + std::string(rhs);
oms::SignalType_t sigType;
switch (std::get<1>(it.second))
{
case oms_signal_type_real:
sigType = oms::SignalType_t::SignalType_REAL;
break;
case oms_signal_type_integer:
sigType = oms::SignalType_t::SignalType_INT;
break;
case oms_signal_type_boolean:
sigType = oms::SignalType_t::SignalType_BOOL;
break;
default:
return logError("Unsupported signal type for alias \"" + std::string(alias) + "\" in model \"" + std::string(getCref()) + "\"");
}

result_file_id = resultFile->addSignal(alias, description, sigType);
it.second = std::make_tuple(rhs, varType, result_file_id);
}

return oms_status_ok;
}

oms_status_enu_t oms::Model::addAlias(const ComRef& alias, const ComRef& rhs, oms_signal_type_enu_t type)
{
// check that alias does not already exist
if (aliases.find(alias) != aliases.end())
return logError("Alias \"" + std::string(alias) + "\" already exists in model \"" + std::string(getCref()) + "\"");

// insert tuple (rhs, type, result_file_id) with result_file_id = -1 (not yet registered)
aliases[alias] = std::make_tuple(rhs, type, -1);
return oms_status_ok;
}

Expand All @@ -1415,6 +1456,46 @@ oms_status_enu_t oms::Model::emit(double time, bool force, bool* emitted)
if (oms_status_ok != system->updateSignals(*resultFile))
return oms_status_error;

// emit alias variables
for (const auto &it : aliases)
{
const ComRef& alias = it.first;
ComRef rhs;
oms_signal_type_enu_t varType;
int result_file_id = -1;
std::tie(rhs, varType, result_file_id) = it.second;

if (result_file_id < 0)
continue; // not registered in result file

SignalValue_t value;
oms_status_enu_t status = oms_status_ok;
if (varType == oms_signal_type_real)
{
double v;
status = system->getReal(rhs, v);
if (status == oms_status_ok)
value.realValue = v;
}
else if (varType == oms_signal_type_integer || varType == oms_signal_type_enum)
{
int v;
status = system->getInteger(rhs, v);
if (status == oms_status_ok)
value.intValue = v;
}
else if (varType == oms_signal_type_boolean)
{
bool v;
status = system->getBoolean(rhs, v);
if (status == oms_status_ok)
value.boolValue = v;
}

if (status == oms_status_ok)
resultFile->updateSignal(static_cast<unsigned int>(result_file_id), value);
}

resultFile->emit(time);
lastEmit = time;
if (emitted)
Expand Down
4 changes: 4 additions & 0 deletions src/OMSimulatorLib/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <pugixml.hpp>

#include <ctpl_stl.h>
#include <tuple>

namespace oms
{
Expand Down Expand Up @@ -123,6 +124,7 @@ namespace oms
oms_status_enu_t addSignalsToResults(const char* regex);
oms_status_enu_t removeSignalsFromResults(const char* regex);
std::string escapeSpecialCharacters(const std::string& regex);
oms_status_enu_t addAlias(const ComRef& alias, const ComRef& rhs, oms_signal_type_enu_t type);

bool validState(int validStates) const {return (modelState & validStates);}

Expand Down Expand Up @@ -178,6 +180,8 @@ namespace oms

std::vector<std::string> externalResources; ///< list of external ssv or ssm resources from filesystem

std::map<ComRef, std::tuple<ComRef, oms_signal_type_enu_t, int>> aliases; ///< list of aliases: (rhs, type, result_file_id)

bool isolatedFMU = false;

ctpl::thread_pool* pool = nullptr;
Expand Down
21 changes: 21 additions & 0 deletions src/OMSimulatorLib/OMSimulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,27 @@ oms_status_enu_t oms_getConnections(const char *cref, oms_connection_t ***connec
return oms_status_ok;
}

oms_status_enu_t oms_addAlias(const char* cref, const char* rhs_)
{
oms::ComRef tail(cref);
oms::ComRef modelCref = tail.pop_front();
oms::ComRef aliasCref = tail.pop_front();
oms::Model* model = oms::Scope::GetInstance().getModel(modelCref);
if (!model) {
return logError_ModelNotInScope(modelCref);
}

if (!rhs_)
return logError("Invalid argument: rhs=NULL");

oms::ComRef rhs(rhs_);
oms_signal_type_enu_t type;
if (oms_status_ok != oms_getVariableType(rhs_, &type))
return logError("The rhs \"" + std::string(rhs_) + "\" does not exist.");

return model->addAlias(aliasCref, rhs, type);
}

oms_status_enu_t oms_addBus(const char *cref)
{
oms::ComRef tail(cref);
Expand Down