Skip to content

Commit

Permalink
C api aliases (#2454)
Browse files Browse the repository at this point in the history
* add C api for addAlias

* add test cases for C alias methods

* add alias operations to C++98 API

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Apply suggestions from code review

Co-authored-by: Ryan Mast <[email protected]>

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Ryan Mast <[email protected]>
  • Loading branch information
3 people authored Oct 21, 2022
1 parent 9b340a2 commit 7a300fb
Show file tree
Hide file tree
Showing 10 changed files with 266 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/helics/application_api/Federate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,10 @@ class HELICS_CXX_EXPORT Federate {
@param value the value of the global
*/
void setGlobal(std::string_view valueName, std::string_view value);
/** add a global alias for an interface */
/** add a global alias for an interface
@param interfaceName the given name of the interface
@param alias the new name by which the interface can be referenced
*/
void addAlias(std::string_view interfaceName, std::string_view alias);
/** send a command to another core or federate
@param target the target of the command can be "federation", "federate", "broker", "core", or a
Expand Down
8 changes: 8 additions & 0 deletions src/helics/cpp98/Broker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ class Broker {
{
helicsBrokerSetGlobal(broker, valueName.c_str(), value.c_str(), hThrowOnError());
}
/** add a global alias for an interface
@param interfaceName the given name of the interface
@param alias the new name by which the interface can be referenced
*/
void addAlias(const std::string& interfaceName, const std::string& alias)
{
helicsBrokerAddAlias(broker, interfaceName.c_str(), alias.c_str(), hThrowOnError());
}
/** create a data link between a named publication and a named input
@param source the name of the publication
@param target the name of the input*/
Expand Down
9 changes: 9 additions & 0 deletions src/helics/cpp98/Core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ class Core {
helicsCoreSetGlobal(core, valueName.c_str(), value.c_str(), hThrowOnError());
}

/** add a global alias for an interface
@param interfaceName the given name of the interface
@param alias the new name by which the interface can be referenced
*/
void addAlias(const std::string& interfaceName, const std::string& alias)
{
helicsCoreAddAlias(core, interfaceName.c_str(), alias.c_str(), hThrowOnError());
}

void globalError(int errorCode, const std::string& errorString)
{
helicsCoreGlobalError(core, errorCode, errorString.c_str(), HELICS_IGNORE_ERROR);
Expand Down
9 changes: 9 additions & 0 deletions src/helics/cpp98/Federate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,15 @@ class Federate {
helicsFederateSetGlobal(fed, valueName.c_str(), value.c_str(), hThrowOnError());
}

/** add a global alias for an interface
@param interfaceName the given name of the interface
@param alias the new name by which the interface can be referenced
*/
void addAlias(const std::string& interfaceName, const std::string& alias)
{
helicsFederateAddAlias(fed, interfaceName.c_str(), alias.c_str(), hThrowOnError());
}

/** set a tag (key-value pair) for a federate
@details the tag is an arbitrary user defined string and value; the tags for a federate are
queryable through a "tags" query or "tag/<tagname>"
Expand Down
27 changes: 27 additions & 0 deletions src/helics/shared_api_library/FederateExport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,33 @@ void helicsFederateSetGlobal(HelicsFederate fed, const char* valueName, const ch
// LCOV_EXCL_STOP
}

static constexpr char invalidInterfaceName[] = "Interface name cannot be empty";
static constexpr char invalidAliasName[] = "Alias cannot be empty";

void helicsFederateAddAlias(HelicsFederate fed, const char* interfaceName, const char* alias, HelicsError* err)
{
auto* fedObj = getFed(fed, err);
if (fedObj == nullptr) {
return;
}
if (interfaceName == nullptr || strlen(interfaceName) == 0) {
assignError(err, HELICS_ERROR_INVALID_ARGUMENT, invalidInterfaceName);
return;
}
if (alias == nullptr || strlen(alias) == 0) {
assignError(err, HELICS_ERROR_INVALID_ARGUMENT, invalidAliasName);
return;
}
try {
fedObj->addAlias(interfaceName, alias);
}
// LCOV_EXCL_START
catch (...) {
helicsErrorHandler(err);
}
// LCOV_EXCL_STOP
}

static constexpr char invalidTagString[] = "Tag name cannot be null";
void helicsFederateSetTag(HelicsFederate fed, const char* tagName, const char* value, HelicsError* err)
{
Expand Down
45 changes: 39 additions & 6 deletions src/helics/shared_api_library/backup/helics/helics.h
Original file line number Diff line number Diff line change
Expand Up @@ -1733,10 +1733,10 @@ HELICS_EXPORT void helicsFederateEnterInitializingModeAsync(HelicsFederate fed,
HELICS_EXPORT void helicsFederateEnterInitializingModeComplete(HelicsFederate fed, HelicsError* err);

/**
* Trigger a blocking call and return to created state after all federates have either triggered an iteration or waiting to enter
* initializing mode
* Trigger a blocking call and return to created state after all federates have either triggered an iteration or are waiting to enter
* initializing mode.
*
* @details this call will return the federate to the created state to allow additional setup to occur with federates either iterating in
* @details This call will return the federate to the created state to allow additional setup to occur with federates either iterating in
* the mode or waiting.
*
* @param fed The federate to operate on.
Expand All @@ -1757,10 +1757,10 @@ HELICS_EXPORT void helicsFederateEnterInitializingModeIterative(HelicsFederate f
HELICS_EXPORT void helicsFederateEnterInitializingModeIterativeAsync(HelicsFederate fed, HelicsError* err);

/**
* Complete the call to initialize mode that was initiated with /ref heliceEnterInitializingModeIterativeAsync. The federate will be in
* startup or error mode on return
* Complete the call to enter initializing mode Iterative that was initiated with /ref heliceEnterInitializingModeIterativeAsync. The
* federate will be in created or error mode on return
*
* @param fed The federate desiring to iterate to the startup mode again
* @param fed The federate used in the corresponding async call
*
* @param[in,out] err An error object that will contain an error code and string if any error occurred during the execution of the function.
*/
Expand Down Expand Up @@ -2097,6 +2097,17 @@ HELICS_EXPORT int helicsFederateGetIntegerProperty(HelicsFederate fed, int intPr
*/
HELICS_EXPORT HelicsTime helicsFederateGetCurrentTime(HelicsFederate fed, HelicsError* err);

/**
* create an alias for an interface
*
* @param fed The federate to use to set the alias
* @param interfaceName The current name of an interface
* @param alias the additional name to use for the given interface
*
* @param[in,out] err A pointer to an error object for catching errors.
*/
HELICS_EXPORT void helicsFederateAddAlias(HelicsFederate fed, const char* interfaceName, const char* alias, HelicsError* err);

/**
* Set a federation global value through a federate.
*
Expand Down Expand Up @@ -2271,6 +2282,28 @@ HELICS_EXPORT void helicsCoreSetGlobal(HelicsCore core, const char* valueName, c
*/
HELICS_EXPORT void helicsBrokerSetGlobal(HelicsBroker broker, const char* valueName, const char* value, HelicsError* err);

/**
* create an alias for an interface
*
* @param core The core to use to set the alias
* @param interfaceName The current name of an interface
* @param alias the additional name to use for the given interface
*
* @param[in,out] err A pointer to an error object for catching errors.
*/
HELICS_EXPORT void helicsCoreAddAlias(HelicsCore core, const char* interfaceName, const char* alias, HelicsError* err);

/**
* create an alias for an interface
*
* @param broker The broker to use to set the alias
* @param interfaceName The current name of an interface
* @param alias the additional name to use for the given interface
*
* @param[in,out] err A pointer to an error object for catching errors.
*/
HELICS_EXPORT void helicsBrokerAddAlias(HelicsBroker broker, const char* interfaceName, const char* alias, HelicsError* err);

/**
* Send a command to another helics object though a core using asynchronous(fast) operations.
*
Expand Down
3 changes: 3 additions & 0 deletions src/helics/shared_api_library/backup/helics/helics_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ HelicsTime helicsFederateGetTimeProperty(HelicsFederate fed, int timeProperty, H
HelicsBool helicsFederateGetFlagOption(HelicsFederate fed, int flag, HelicsError* err);
int helicsFederateGetIntegerProperty(HelicsFederate fed, int intProperty, HelicsError* err);
HelicsTime helicsFederateGetCurrentTime(HelicsFederate fed, HelicsError* err);
void helicsFederateAddAlias(HelicsFederate fed, const char* interfaceName, const char* alias, HelicsError* err);
void helicsFederateSetGlobal(HelicsFederate fed, const char* valueName, const char* value, HelicsError* err);
void helicsFederateSetTag(HelicsFederate fed, const char* tagName, const char* value, HelicsError* err);
const char* helicsFederateGetTag(HelicsFederate fed, const char* tagName, HelicsError* err);
Expand All @@ -475,6 +476,8 @@ const char* helicsFederateGetCommandSource(HelicsFederate fed, HelicsError* err)
const char* helicsFederateWaitCommand(HelicsFederate fed, HelicsError* err);
void helicsCoreSetGlobal(HelicsCore core, const char* valueName, const char* value, HelicsError* err);
void helicsBrokerSetGlobal(HelicsBroker broker, const char* valueName, const char* value, HelicsError* err);
void helicsCoreAddAlias(HelicsCore core, const char* interfaceName, const char* alias, HelicsError* err);
void helicsBrokerAddAlias(HelicsBroker broker, const char* interfaceName, const char* alias, HelicsError* err);
void helicsCoreSendCommand(HelicsCore core, const char* target, const char* command, HelicsError* err);
void helicsCoreSendOrderedCommand(HelicsCore core, const char* target, const char* command, HelicsError* err);
void helicsBrokerSendCommand(HelicsBroker broker, const char* target, const char* command, HelicsError* err);
Expand Down
33 changes: 33 additions & 0 deletions src/helics/shared_api_library/helicsCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,17 @@ HELICS_EXPORT int helicsFederateGetIntegerProperty(HelicsFederate fed, int intPr
*/
HELICS_EXPORT HelicsTime helicsFederateGetCurrentTime(HelicsFederate fed, HelicsError* err);

/**
* Create an alias for an interface.
*
* @param fed The federate to use to set the alias.
* @param interfaceName The current name of an interface.
* @param alias The additional name to use for the given interface.
*
* @param[in,out] err A pointer to an error object for catching errors.
*/
HELICS_EXPORT void helicsFederateAddAlias(HelicsFederate fed, const char* interfaceName, const char* alias, HelicsError* err);

/**
* Set a federation global value through a federate.
*
Expand Down Expand Up @@ -1554,6 +1565,28 @@ HELICS_EXPORT void helicsCoreSetGlobal(HelicsCore core, const char* valueName, c
*/
HELICS_EXPORT void helicsBrokerSetGlobal(HelicsBroker broker, const char* valueName, const char* value, HelicsError* err);

/**
* Create an alias for an interface.
*
* @param core The core to use to set the alias.
* @param interfaceName The current name of an interface.
* @param alias The additional name to use for the given interface.
*
* @param[in,out] err A pointer to an error object for catching errors.
*/
HELICS_EXPORT void helicsCoreAddAlias(HelicsCore core, const char* interfaceName, const char* alias, HelicsError* err);

/**
* Create an alias for an interface.
*
* @param broker The broker to use to set the alias.
* @param interfaceName The current name of an interface.
* @param alias The additional name to use for the given interface.
*
* @param[in,out] err A pointer to an error object for catching errors.
*/
HELICS_EXPORT void helicsBrokerAddAlias(HelicsBroker broker, const char* interfaceName, const char* alias, HelicsError* err);

/**
* Send a command to another helics object though a core using asynchronous(fast) operations.
*
Expand Down
51 changes: 51 additions & 0 deletions src/helics/shared_api_library/helicsExport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,57 @@ void helicsBrokerSetGlobal(HelicsBroker broker, const char* valueName, const cha
brk->setGlobal(valueName, AS_STRING_VIEW(value));
}

static constexpr char invalidInterfaceName[] = "Interface name cannot be empty";
static constexpr char invalidAliasName[] = "Alias cannot be empty";

void helicsCoreAddAlias(HelicsCore core, const char* interfaceName, const char* alias, HelicsError* err)
{
auto* cr = getCore(core, err);
if (cr == nullptr) {
return;
}
if (interfaceName == nullptr || strlen(interfaceName) == 0) {
assignError(err, HELICS_ERROR_INVALID_ARGUMENT, invalidInterfaceName);
return;
}
if (alias == nullptr || strlen(alias) == 0) {
assignError(err, HELICS_ERROR_INVALID_ARGUMENT, invalidAliasName);
return;
}
try {
cr->addAlias(interfaceName, alias);
}
// LCOV_EXCL_START
catch (...) {
helicsErrorHandler(err);
}
// LCOV_EXCL_STOP
}

void helicsBrokerAddAlias(HelicsBroker broker, const char* interfaceName, const char* alias, HelicsError* err)
{
auto* brk = getBroker(broker, err);
if (brk == nullptr) {
return;
}
if (interfaceName == nullptr || strlen(interfaceName) == 0) {
assignError(err, HELICS_ERROR_INVALID_ARGUMENT, invalidInterfaceName);
return;
}
if (alias == nullptr || strlen(alias) == 0) {
assignError(err, HELICS_ERROR_INVALID_ARGUMENT, invalidAliasName);
return;
}
try {
brk->addAlias(interfaceName, alias);
}
// LCOV_EXCL_START
catch (...) {
helicsErrorHandler(err);
}
// LCOV_EXCL_STOP
}

void helicsBrokerSendCommand(HelicsBroker broker, const char* target, const char* command, HelicsError* err)
{
auto* brk = getBroker(broker, err);
Expand Down
83 changes: 83 additions & 0 deletions tests/helics/shared_library/test-value-federate2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,89 @@ TEST_F(vfed2_tests, file_load)
helicsFederateFree(vFed);
}

TEST(valuefederate, fedAlias)
{
auto fi = helicsCreateFederateInfo();
helicsFederateInfoSetCoreType(fi, HELICS_CORE_TYPE_TEST, nullptr);
helicsFederateInfoSetCoreName(fi, "core_alias", nullptr);
helicsFederateInfoSetCoreInitString(fi, "-f 1 --autobroker", nullptr);
helicsFederateInfoSetFlagOption(fi,
HELICS_HANDLE_OPTION_CONNECTION_REQUIRED,
HELICS_TRUE,
nullptr);
auto Fed1 = helicsCreateValueFederate("vfed1", fi, nullptr);
helicsFederateInfoFree(fi);
helicsFederateRegisterGlobalPublication(
Fed1, "pub1", HELICS_DATA_TYPE_DOUBLE, "parsecs", nullptr);

helicsFederateAddAlias(Fed1, "pub1", "theBigPub", nullptr);

helicsFederateRegisterSubscription(Fed1, "theBigPub", nullptr, nullptr);

auto err = helicsErrorInitialize();
helicsFederateEnterExecutingMode(Fed1, &err);
EXPECT_EQ(err.error_code, 0);
helicsFederateDestroy(Fed1);
}

TEST(valuefederate, coreAlias)
{
auto fi = helicsCreateFederateInfo();
helicsFederateInfoSetCoreType(fi, HELICS_CORE_TYPE_TEST, nullptr);
helicsFederateInfoSetCoreName(fi, "core_alias", nullptr);
helicsFederateInfoSetCoreInitString(fi, "-f 1 --autobroker", nullptr);
helicsFederateInfoSetFlagOption(fi,
HELICS_HANDLE_OPTION_CONNECTION_REQUIRED,
HELICS_TRUE,
nullptr);
auto Fed1 = helicsCreateValueFederate("vfed1", fi, nullptr);
auto cr = helicsFederateGetCore(Fed1, nullptr);
helicsFederateInfoFree(fi);
helicsFederateRegisterGlobalPublication(
Fed1, "pub1", HELICS_DATA_TYPE_DOUBLE, "parsecs", nullptr);

helicsCoreAddAlias(cr, "pub1", "theBigPub", nullptr);

helicsFederateRegisterSubscription(Fed1, "theBigPub", nullptr, nullptr);

auto err = helicsErrorInitialize();
helicsFederateEnterExecutingMode(Fed1, &err);
EXPECT_EQ(err.error_code, 0);
helicsFederateDestroy(Fed1);
helicsCoreDestroy(cr);
}

TEST(valuefederate, brokerAlias)
{
auto brk = helicsCreateBroker("test", "alias_broker", "-f1", nullptr);
auto fi = helicsCreateFederateInfo();
helicsFederateInfoSetCoreType(fi, HELICS_CORE_TYPE_TEST, nullptr);
helicsFederateInfoSetCoreName(fi, "core_alias", nullptr);
helicsFederateInfoSetCoreInitString(fi, "-f 1 --broker=alias_broker", nullptr);
helicsFederateInfoSetFlagOption(fi,
HELICS_HANDLE_OPTION_CONNECTION_REQUIRED,
HELICS_TRUE,
nullptr);
auto Fed1 = helicsCreateValueFederate("vfed1", fi, nullptr);

helicsFederateInfoFree(fi);
helicsFederateRegisterGlobalPublication(
Fed1, "pub1", HELICS_DATA_TYPE_DOUBLE, "parsecs", nullptr);

helicsBrokerAddAlias(brk, "pub1", "theBigPub", nullptr);

helicsFederateRegisterSubscription(Fed1, "theBigPub", nullptr, nullptr);

auto err = helicsErrorInitialize();
helicsFederateEnterExecutingMode(Fed1, &err);
EXPECT_EQ(err.error_code, 0);
helicsFederateDestroy(Fed1);
auto res = helicsBrokerWaitForDisconnect(brk, 1000, &err);
EXPECT_EQ(err.error_code, 0);
EXPECT_EQ(res, HELICS_TRUE);
helicsBrokerDestroy(brk);
}

static void stateChangeCallback(HelicsFederateState newState,
HelicsFederateState /*oldState*/,
void* userData)
Expand Down

0 comments on commit 7a300fb

Please sign in to comment.