Skip to content

Commit

Permalink
add external resources from filesystem to ssp
Browse files Browse the repository at this point in the history
  • Loading branch information
arun3688 committed Aug 27, 2021
1 parent 1f48df8 commit de1817e
Show file tree
Hide file tree
Showing 22 changed files with 215 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/OMSimulatorLib/Component.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ namespace oms
virtual oms_status_enu_t setReal(const ComRef& cref, double value) { return logError_NotImplemented; }
virtual oms_status_enu_t setRealInputDerivative(const ComRef& cref, const SignalDerivative& der) { return logError_NotImplemented; }
virtual oms_status_enu_t stepUntil(double stopTime) { return oms_status_ok; }
virtual oms_status_enu_t newResources(std::string& filename) { return logError_NotImplemented; }
virtual oms_status_enu_t newResources(const std::string& filename) { return logError_NotImplemented; }
virtual oms_status_enu_t addResources(std::string& filename) { return logError_NotImplemented; }
virtual oms_status_enu_t deleteReferencesInSSD(const std::string& filename) {return oms_status_ok;}
virtual oms_status_enu_t deleteResourcesInSSP(const std::string& filename) {return oms_status_ok;}
Expand Down
2 changes: 1 addition & 1 deletion src/OMSimulatorLib/ComponentFMUCS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ oms::ComRef oms::ComponentFMUCS::getValidCref(ComRef cref)
return tail;
}

oms_status_enu_t oms::ComponentFMUCS::newResources(std::string& filename)
oms_status_enu_t oms::ComponentFMUCS::newResources(const std::string& filename)
{
Values resources;
if (!values.hasResources())
Expand Down
2 changes: 1 addition & 1 deletion src/OMSimulatorLib/ComponentFMUCS.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ namespace oms

void getFilteredSignals(std::vector<Connector>& filteredSignals) const;

oms_status_enu_t newResources(std::string& filename);
oms_status_enu_t newResources(const std::string& filename);
oms_status_enu_t setResourcesHelper1(Values value);
oms_status_enu_t setResourcesHelper2(Values value);

Expand Down
2 changes: 1 addition & 1 deletion src/OMSimulatorLib/ComponentFMUME.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ oms_status_enu_t oms::ComponentFMUME::doEventIteration()
return oms_status_ok;
}

oms_status_enu_t oms::ComponentFMUME::newResources(std::string& filename)
oms_status_enu_t oms::ComponentFMUME::newResources(const std::string& filename)
{
Values resources;
if (!values.hasResources())
Expand Down
2 changes: 1 addition & 1 deletion src/OMSimulatorLib/ComponentFMUME.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ namespace oms

void getFilteredSignals(std::vector<Connector>& filteredSignals) const;

oms_status_enu_t newResources(std::string& filename);
oms_status_enu_t newResources(const std::string& filename);
oms_status_enu_t setResourcesHelper1(Values value);
oms_status_enu_t setResourcesHelper2(Values value);

Expand Down
39 changes: 38 additions & 1 deletion src/OMSimulatorLib/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,14 +498,42 @@ oms_status_enu_t oms::Model::updateParameterBindingsToSSD(pugi::xml_node& node,
oms_status_enu_t oms::Model::newResources(const oms::ComRef& cref)
{
ComRef subCref(cref);
std::string fileName = "resources/" + subCref.pop_suffix();
const std::string fileName = "resources/" + subCref.pop_suffix();

if (system)
return system->newResources(subCref, fileName);

return oms_status_ok;
}

oms_status_enu_t oms::Model::addResources(const oms::ComRef& cref, const std::string& path)
{
filesystem::path path_ = oms_canonical(path);
if (!filesystem::exists(path_))
return logError("file does not exist: \"" + path + "\"");

ComRef subCref(cref);
std::string fileName = subCref.pop_suffix();

// get the filename from path, if no name provided
if (fileName.empty())
{
filesystem::path path_(path);
fileName = path_.filename().generic_string();
}

// copy the file to temp directory
filesystem::path temp_root(getTempDirectory());
filesystem::path temp_temp = temp_root / "temp";
filesystem::path relFMUPath = filesystem::path("resources/" + fileName);
filesystem::path absFMUPath = temp_root / relFMUPath;
oms_copy_file(filesystem::path(path), absFMUPath);
// push to external resources
externalResources.push_back("resources/"+ fileName);

return oms_status_ok;
}

oms_status_enu_t oms::Model::deleteReferencesInSSD(const oms::ComRef& cref)
{
ComRef subCref(cref);
Expand Down Expand Up @@ -797,6 +825,15 @@ void oms::Model::writeAllResourcesToFilesystem(std::vector<std::string>& resourc

if (system)
system->getAllResources(resources);

// check for external resources added at model level
if (!externalResources.empty())
{
for (const auto &file : externalResources)
{
resources.push_back(file);
}
}
}

oms_status_enu_t oms::Model::setStartTime(double value)
Expand Down
4 changes: 3 additions & 1 deletion src/OMSimulatorLib/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ namespace oms
oms_status_enu_t list(const ComRef& cref, char** contents);
oms_status_enu_t addSystem(const ComRef& cref, oms_system_enu_t type);
oms_status_enu_t newResources(const ComRef& cref);
oms_status_enu_t addResources(const ComRef& cref);
oms_status_enu_t addResources(const ComRef& cref, const std::string& path);
oms_status_enu_t deleteReferencesInSSD(const ComRef& cref);
oms_status_enu_t deleteResourcesInSSP(const std::string& filename);
oms_status_enu_t exportToSSD(Snapshot& snapshot) const;
Expand Down Expand Up @@ -161,6 +161,8 @@ namespace oms
std::string resultFilename; ///< default <name>_res.mat
std::string signalFilterFilename = "resources/signalFilter.xml";

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

bool isolatedFMU = false;

ctpl::thread_pool* pool = nullptr;
Expand Down
15 changes: 15 additions & 0 deletions src/OMSimulatorLib/OMSimulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,21 @@ oms_status_enu_t oms_newResources(const char* cref_)
return model->newResources(tail);
}

oms_status_enu_t oms_addResources(const char* cref_, const char* path)
{
oms::ComRef tail(cref_);
oms::ComRef front = tail.pop_front();

oms::ComRef modelCref(front);
modelCref.pop_suffix();

oms::Model* model = oms::Scope::GetInstance().getModel(modelCref);
if (!model)
return logError_ModelNotInScope(front);

return model->addResources(front, path);
}

oms_status_enu_t oms_addSystem(const char* cref_, oms_system_enu_t type)
{
oms::ComRef cref(cref_);
Expand Down
1 change: 1 addition & 0 deletions src/OMSimulatorLib/OMSimulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ OMSAPI oms_status_enu_t OMSCALL oms_addConnectorToTLMBus(const char* busCref, co
OMSAPI oms_status_enu_t OMSCALL oms_addDynamicValueIndicator(const char* signal, const char* lower, const char* upper, double stepSize);
OMSAPI oms_status_enu_t OMSCALL oms_addEventIndicator(const char* signal);
OMSAPI oms_status_enu_t OMSCALL oms_addExternalModel(const char* cref, const char* path, const char* startscript);
OMSAPI oms_status_enu_t OMSCALL oms_addResources(const char* cref, const char* path);
OMSAPI oms_status_enu_t OMSCALL oms_addSignalsToResults(const char* cref, const char* regex);
OMSAPI oms_status_enu_t OMSCALL oms_addStaticValueIndicator(const char* signal, double lower, double upper, double stepSize);
OMSAPI oms_status_enu_t OMSCALL oms_addSubModel(const char* cref, const char* fmuPath);
Expand Down
4 changes: 3 additions & 1 deletion src/OMSimulatorLib/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ oms_status_enu_t oms::System::addSubModel(const oms::ComRef& cref, const std::st
return system->addSubModel(tail, path);
}

oms_status_enu_t oms::System::newResources(const ComRef& cref, std::string& filename)
oms_status_enu_t oms::System::newResources(const ComRef& cref, const std::string& filename, bool isExternalresources)
{
ComRef tail(cref);
ComRef front = tail.pop_front();
Expand All @@ -337,11 +337,13 @@ oms_status_enu_t oms::System::newResources(const ComRef& cref, std::string& file
if (!values.hasResources())
{
resources.allresources[filename] = resources;
resources.isExternalSSV = isExternalresources;
values.parameterResources.push_back(resources);
}
else
{
// generate empty ssv file, if more resources are added to same level
resources.isExternalSSV = isExternalresources;
values.parameterResources[0].allresources[filename] = resources;
}
return oms_status_ok;
Expand Down
2 changes: 1 addition & 1 deletion src/OMSimulatorLib/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ namespace oms
oms_status_enu_t setConnectorGeometry(const ComRef& cref, const oms::ssd::ConnectorGeometry* geometry);
oms_status_enu_t setConnectionGeometry(const ComRef &crefA, const ComRef &crefB, const oms::ssd::ConnectionGeometry* geometry);
oms_status_enu_t addBus(const ComRef& cref);
oms_status_enu_t newResources(const ComRef& cref, std::string& filename);
oms_status_enu_t newResources(const ComRef& cref, const std::string& filename, bool isExternalResources = false);
oms_status_enu_t addConnectorToBus(const ComRef& busCref, const ComRef& connectorCref);
oms_status_enu_t deleteConnectorFromBus(const ComRef& busCref, const ComRef& connectorCref);
oms_status_enu_t setBusGeometry(const ComRef& cref, const oms::ssd::ConnectorGeometry* geometry);
Expand Down
12 changes: 10 additions & 2 deletions src/OMSimulatorLib/Values.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,8 +638,16 @@ void oms::Values::exportParameterBindings(pugi::xml_node &node, Snapshot &snapsh
node_parameter_binding = node_parameters_bindings.append_child(oms::ssp::Version1_0::ssd::parameter_binding);
node_parameter_binding.append_attribute("source") = res.first.c_str();
}
pugi::xml_node ssvNode = snapshot.getTemplateResourceNodeSSV(res.first, "parameters");
res.second.exportToSSV(ssvNode);
//std::cout << "\n export To SSV file :" << res.first.c_str() << "=" << res.second.isExternalSSV;
/*
export ssv file only for newResources and not external ssv files,
as they will be copied directly to resources folder and only the references must be updated in ssd
*/
if (!res.second.isExternalSSV)
{
pugi::xml_node ssvNode = snapshot.getTemplateResourceNodeSSV(res.first, "parameters");
res.second.exportToSSV(ssvNode);
}
// export SSM file if exist
if (!res.second.ssmFile.empty())
{
Expand Down
1 change: 1 addition & 0 deletions src/OMSimulatorLib/Values.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ namespace oms
std::map<std::string, Values> allresources; ///< mapped resources either inline or ssv
std::string ssmFile = ""; ///< mapped ssm files associated with ssv files;
bool linkResources = true;
bool isExternalSSV = false;
};
}

Expand Down
17 changes: 17 additions & 0 deletions src/OMSimulatorLua/OMSimulatorLua.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,22 @@ static int OMSimulatorLua_oms_newResources(lua_State *L)
return 1;
}

//oms_status_enu_t oms_addResources(const char* cref, const char* path);
static int OMSimulatorLua_oms_addResources(lua_State *L)
{
if (lua_gettop(L) != 2)
return luaL_error(L, "expecting exactly 2 argument");
luaL_checktype(L, 1, LUA_TSTRING);
luaL_checktype(L, 2, LUA_TSTRING);

const char* cref = lua_tostring(L, 1);
const char* path = lua_tostring(L, 2);
oms_status_enu_t status = oms_addResources(cref, path);

lua_pushinteger(L, status);
return 1;
}

//oms_status_enu_t oms_addSystem(const char* cref, oms_system_enu_t type);
static int OMSimulatorLua_oms_addSystem(lua_State *L)
{
Expand Down Expand Up @@ -1286,6 +1302,7 @@ DLLEXPORT int luaopen_OMSimulatorLua(lua_State *L)
REGISTER_LUA_CALL(oms_addDynamicValueIndicator);
REGISTER_LUA_CALL(oms_addEventIndicator);
REGISTER_LUA_CALL(oms_addExternalModel);
REGISTER_LUA_CALL(oms_addResources);
REGISTER_LUA_CALL(oms_addSignalsToResults);
REGISTER_LUA_CALL(oms_addStaticValueIndicator);
REGISTER_LUA_CALL(oms_addSubModel);
Expand Down
3 changes: 3 additions & 0 deletions testsuite/api/Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
TEST = ../rtest -v

TESTFILES = \
addExternalResources1.lua \
buses.lua \
buses.py \
connections.lua \
connections.py \
deleteReferencesInSSD.lua \
deleteResourcesInSSP.lua \
test_omsExport.lua \
test_omsExport.py \
test01.lua \
Expand Down
65 changes: 65 additions & 0 deletions testsuite/api/addExternalResources1.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
-- status: correct
-- teardown_command: rm -rf addExternalResources1_lua/
-- linux: yes
-- mingw32: yes
-- mingw64: yes
-- win: yes
-- mac: no

oms_setCommandLineOption("--suppressPath=true")
oms_setTempDirectory("./addExternalResources1_lua/")
oms_setWorkingDirectory("./addExternalResources1_lua/")

oms_newModel("addExternalResources")

oms_addSystem("addExternalResources.root", oms_system_wc)
oms_addConnector("addExternalResources.root.Input1", oms_causality_input, oms_signal_type_real)
oms_addConnector("addExternalResources.root.Input2", oms_causality_input, oms_signal_type_real)

-- add Top level resources
oms_newResources("addExternalResources.root:root.ssv")

oms_setReal("addExternalResources.root.Input1", 10)
oms_setReal("addExternalResources.root.Input2", 50)

oms_addSystem("addExternalResources.root.system1", oms_system_sc)
oms_addConnector("addExternalResources.root.system1.C1", oms_causality_input, oms_signal_type_real)
oms_addConnector("addExternalResources.root.system1.C2", oms_causality_input, oms_signal_type_real)

-- add resources to subsystem
oms_newResources("addExternalResources.root.system1:system1.ssv")
oms_setReal("addExternalResources.root.system1.C1", -10)

oms_addSubModel("addExternalResources.root.Gain", "../../resources/Modelica.Blocks.Math.Gain.fmu")

-- add resources to submodule
oms_newResources("addExternalResources.root.Gain:gain.ssv")

oms_setReal("addExternalResources.root.Gain.k", 27)

oms_setResultFile("addExternalResources", "addExternalResources1.mat", 10)

oms_export("addExternalResources", "addExternalResources1.ssp")

oms_terminate("addExternalResources")
oms_delete("addExternalResources")

oms_importFile("addExternalResources1.ssp")

-- add list of external resources from filesystem to ssp
oms_addResources("addExternalResources", "../../resources/externalRoot.ssv")
oms_addResources("addExternalResources:externalSystem", "../../resources/externalSystem1.ssv")
oms_addResources("addExternalResources", "../../resources/externalGain.ssv")

-- switch with new resouces
-- oms_referenceResources("addExternalResources.root:root.ssv")

oms_export("addExternalResources", "addExternalResources1.ssp")

oms_terminate("addExternalResources")
oms_delete("addExternalResources")



-- Result:
-- endResult
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ oms_addConnector("deleteResources.root.Input1", oms_causality_input, oms_signal_
oms_addConnector("deleteResources.root.Input2", oms_causality_input, oms_signal_type_real)

-- add Top level resources
oms_addResources("deleteResources.root:root.ssv")
oms_newResources("deleteResources.root:root.ssv")

oms_setReal("deleteResources.root.Input1", 10)
oms_setReal("deleteResources.root.Input2", 50)
Expand All @@ -27,13 +27,13 @@ oms_addConnector("deleteResources.root.system1.C1", oms_causality_input, oms_sig
oms_addConnector("deleteResources.root.system1.C2", oms_causality_input, oms_signal_type_real)

-- add resources to subsystem
oms_addResources("deleteResources.root.system1:system1.ssv")
oms_newResources("deleteResources.root.system1:system1.ssv")
oms_setReal("deleteResources.root.system1.C1", -10)

oms_addSubModel("deleteResources.root.Gain", "../../resources/Modelica.Blocks.Math.Gain.fmu")

-- add resources to submodule
oms_addResources("deleteResources.root.Gain:gain.ssv")
oms_newResources("deleteResources.root.Gain:gain.ssv")

oms_setReal("deleteResources.root.Gain.k", 27)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ oms_addConnector("deleteResources.root.Input1", oms_causality_input, oms_signal_
oms_addConnector("deleteResources.root.Input2", oms_causality_input, oms_signal_type_real)

-- add Top level resources
oms_addResources("deleteResources.root:root.ssv")
oms_newResources("deleteResources.root:root.ssv")

oms_setReal("deleteResources.root.Input1", 10)
oms_setReal("deleteResources.root.Input2", 50)
Expand All @@ -27,13 +27,13 @@ oms_addConnector("deleteResources.root.system1.C1", oms_causality_input, oms_sig
oms_addConnector("deleteResources.root.system1.C2", oms_causality_input, oms_signal_type_real)

-- add resources to subsystem
oms_addResources("deleteResources.root.system1:system1.ssv")
oms_newResources("deleteResources.root.system1:system1.ssv")
oms_setReal("deleteResources.root.system1.C1", -10)

oms_addSubModel("deleteResources.root.Gain", "../../resources/Modelica.Blocks.Math.Gain.fmu")

-- add resources to submodule
oms_addResources("deleteResources.root.Gain:gain.ssv")
oms_newResources("deleteResources.root.Gain:gain.ssv")

oms_setReal("deleteResources.root.Gain.k", 27)

Expand All @@ -57,12 +57,6 @@ oms_deleteResources("deleteResources:system1.ssv")
-- delete references and resources
oms_deleteResources("deleteResources:gain.ssv")

-- adding new resources to exisiting API, -- skip it as of now
-- oms_deleteResources2("deleteResources", "newResources.ssv")
-- oms_newResources("deleteResources.root:root.ssv") --- empty file , renaming the API

-- oms_addResources("deleteResources.root:root.ssv", "path to exissting file") -- :root.ssv, renaming the file with new name from file system

-- snapshot after deleting references and resources
src = oms_exportSnapshot("deleteResources")
print(src)
Expand Down
Loading

0 comments on commit de1817e

Please sign in to comment.