Skip to content

Commit

Permalink
adding initial tag operations
Browse files Browse the repository at this point in the history
  • Loading branch information
phlptp committed Jan 19, 2024
1 parent fdb5cf0 commit 9931148
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 33 deletions.
62 changes: 33 additions & 29 deletions src/helics/apps/Connector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,11 @@ Connector::Connector(std::string_view appName, const std::string& configString):
Connector::loadJsonFile(configString);
}

std::string_view Connector::addTag(std::string_view tagName)
std::size_t Connector::addTag(std::string_view tagName)
{
auto tagIterator = tags.insert(std::string(tagName));
return {*(tagIterator.first)};
std::size_t hash=std::hash<std::string_view>()(tagName);
tags.emplace(hash,tagName);
return hash;
}

std::string_view Connector::addInterface(std::string_view interfaceName)
Expand Down Expand Up @@ -333,7 +334,7 @@ void Connector::addConnection(std::string_view interface1,
InterfaceDirection direction,
const std::vector<std::string>& connectionTags)
{
std::vector<std::string_view> svtags;
std::vector<std::size_t> svtags;
svtags.reserve(connectionTags.size());
for (const auto& tag : connectionTags) {
svtags.push_back(addTag(tag));
Expand Down Expand Up @@ -508,7 +509,7 @@ void Connector::loadJsonFile(const std::string& jsonString)
}

std::vector<Connection>
Connector::buildPossibleConnectionList(std::string_view startingInterface) const
Connector::buildPossibleConnectionList(std::string_view startingInterface, const std::vector<std::size_t> &tags) const
{
std::vector<Connection> matches;
auto [first, last] = connections.equal_range(startingInterface);
Expand Down Expand Up @@ -593,11 +594,11 @@ static std::set<std::string_view>
}

bool Connector::makePotentialConnection(
std::string_view interface,
std::string_view interface, const std::vector<std::size_t> &tags,
std::unordered_map<std::string_view, PotentialConnections>& potentials,
const std::unordered_multimap<std::string_view, std::string_view>& aliases)
{
auto connectionOptions = buildPossibleConnectionList(interface);
auto connectionOptions = buildPossibleConnectionList(interface,tags);
for (const auto& option : connectionOptions) {
auto located = potentials.find(option.interface2);
if (located != potentials.end()) {
Expand All @@ -619,21 +620,21 @@ bool Connector::makePotentialConnection(
}

bool Connector::checkPotentialConnection(
std::string_view interfaceName,
std::string_view interfaceName, const std::vector<std::size_t> &tags,
std::unordered_set<std::string_view>& possibleConnections,
std::unordered_map<std::string_view, PotentialConnections>& potentials,
const std::unordered_multimap<std::string_view, std::string_view>& aliases)
{
static auto nullConnector = [this](std::string_view, std::string_view) {};
/** potential inputs*/
auto matched = makeTargetConnection(interfaceName,
auto matched = makeTargetConnection(interfaceName,tags,
possibleConnections,
aliases,
nullConnector);
if (matched > 0) {
return true;
}
if (makePotentialConnection(interfaceName,potentials,aliases)) {
if (makePotentialConnection(interfaceName,tags,potentials,aliases)) {
return true;
}
if (!aliases.empty()) {
Expand All @@ -642,7 +643,7 @@ bool Connector::checkPotentialConnection(
if (alias == interfaceName) {
continue;
}
if (makePotentialConnection(alias,potentials,aliases)) {
if (makePotentialConnection(alias,tags,potentials,aliases)) {
return true;
}
}
Expand All @@ -651,13 +652,13 @@ bool Connector::checkPotentialConnection(
}

int Connector::makeTargetConnection(
std::string_view origin,
std::string_view origin, const std::vector<std::size_t> &tags,
std::unordered_set<std::string_view>& possibleConnections,
const std::unordered_multimap<std::string_view, std::string_view>& aliases,
const std::function<void(std::string_view, std::string_view)>& callback)
{
int matched{0};
auto connectionOptions = buildPossibleConnectionList(origin);
auto connectionOptions = buildPossibleConnectionList(origin,tags);
for (const auto& option : connectionOptions) {
auto located = possibleConnections.find(option.interface2);
if (located != possibleConnections.end()) {
Expand Down Expand Up @@ -694,7 +695,7 @@ int Connector::makeTargetConnection(
if (alias == origin) {
continue;
}
auto aliasOptions = buildPossibleConnectionList(alias);
auto aliasOptions = buildPossibleConnectionList(alias,tags);
for (const auto& option : aliasOptions) {
auto located = possibleConnections.find(option.interface2);
if (located != possibleConnections.end()) {
Expand Down Expand Up @@ -744,24 +745,26 @@ void Connector::makeConnections(ConnectionsList& possibleConnections)
auto targetEndpointConnector = [this](std::string_view origin, std::string_view source) {
core.linkEndpoints(source, origin);
};

std::vector<std::size_t> tagList;
/** unconnected inputs*/
for (const auto& uInp : possibleConnections.unconnectedInputs) {
matchCount += makeTargetConnection(uInp,
matchCount += makeTargetConnection(uInp,tagList,
possibleConnections.pubs,
possibleConnections.aliases,
inputConnector);
}
/** unconnected publications*/
for (const auto& uPub : possibleConnections.unconnectedPubs) {
matchCount += makeTargetConnection(uPub,
matchCount += makeTargetConnection(uPub,tagList,
possibleConnections.inputs,
possibleConnections.aliases,
pubConnector);
}

/** unconnected source endpoints*/
for (const auto& uEnd : possibleConnections.unconnectedSourceEndpoints) {
matchCount += makeTargetConnection(uEnd,
matchCount += makeTargetConnection(uEnd,tagList,
possibleConnections.endpoints,
possibleConnections.aliases,
sourceEndpointConnector);
Expand All @@ -770,7 +773,7 @@ void Connector::makeConnections(ConnectionsList& possibleConnections)
if (matchTargetEndpoints) {
/** unconnected target endpoints*/
for (const auto& uEnd : possibleConnections.unconnectedTargetEndpoints) {
matchCount += makeTargetConnection(uEnd,
matchCount += makeTargetConnection(uEnd,tagList,
possibleConnections.endpoints,
possibleConnections.aliases,
targetEndpointConnector);
Expand All @@ -781,9 +784,10 @@ void Connector::makeConnections(ConnectionsList& possibleConnections)
void Connector::establishPotentialInterfaces(ConnectionsList& possibleConnections)
{
auto nullConnector = [this](std::string_view, std::string_view) {};
std::vector<std::size_t> tagList;
/** potential inputs*/
for (auto& pInp : possibleConnections.potentialInputs) {
if (checkPotentialConnection(pInp.first, possibleConnections.pubs, possibleConnections.potentialPubs, possibleConnections.aliases))
if (checkPotentialConnection(pInp.first,tagList, possibleConnections.pubs, possibleConnections.potentialPubs, possibleConnections.aliases))
{
pInp.second.used=true;
}
Expand All @@ -793,7 +797,7 @@ void Connector::establishPotentialInterfaces(ConnectionsList& possibleConnection
if (pPub.second.used) {
continue;
}
if (checkPotentialConnection(pPub.first, possibleConnections.inputs, possibleConnections.potentialInputs, possibleConnections.aliases))
if (checkPotentialConnection(pPub.first,tagList, possibleConnections.inputs, possibleConnections.potentialInputs, possibleConnections.aliases))
{
pPub.second.used=true;
}
Expand All @@ -804,15 +808,15 @@ void Connector::establishPotentialInterfaces(ConnectionsList& possibleConnection
if (pEnd.second.used) {
continue;
}
if (checkPotentialConnection(pEnd.first, possibleConnections.endpoints, possibleConnections.potentialEndpoints, possibleConnections.aliases))
if (checkPotentialConnection(pEnd.first,tagList, possibleConnections.endpoints, possibleConnections.potentialEndpoints, possibleConnections.aliases))
{
pEnd.second.used=true;
}
}
/** now try to match unconnected interfaces to some of the potential ones*/
/** unconnected inputs*/
for (const auto& uInp : possibleConnections.unconnectedInputs) {
if (makePotentialConnection(uInp,
if (makePotentialConnection(uInp,tagList,
possibleConnections.potentialPubs,
possibleConnections.aliases)) {
continue;
Expand All @@ -823,7 +827,7 @@ void Connector::establishPotentialInterfaces(ConnectionsList& possibleConnection
if (alias == uInp) {
continue;
}
if (makePotentialConnection(alias,
if (makePotentialConnection(alias,tagList,
possibleConnections.potentialPubs,
possibleConnections.aliases)) {
break;
Expand All @@ -834,7 +838,7 @@ void Connector::establishPotentialInterfaces(ConnectionsList& possibleConnection

/** unconnected publications*/
for (const auto& uPub : possibleConnections.unconnectedPubs) {
if (makePotentialConnection(uPub,
if (makePotentialConnection(uPub,tagList,
possibleConnections.potentialInputs,
possibleConnections.aliases)) {
continue;
Expand All @@ -845,7 +849,7 @@ void Connector::establishPotentialInterfaces(ConnectionsList& possibleConnection
if (alias == uPub) {
continue;
}
if (makePotentialConnection(alias,
if (makePotentialConnection(alias,tagList,
possibleConnections.potentialInputs,
possibleConnections.aliases)) {
break;
Expand All @@ -856,7 +860,7 @@ void Connector::establishPotentialInterfaces(ConnectionsList& possibleConnection

/** unconnected source endpoints*/
for (const auto& uEnd : possibleConnections.unconnectedSourceEndpoints) {
if (makePotentialConnection(uEnd,
if (makePotentialConnection(uEnd,tagList,
possibleConnections.potentialEndpoints,
possibleConnections.aliases)) {
continue;
Expand All @@ -867,7 +871,7 @@ void Connector::establishPotentialInterfaces(ConnectionsList& possibleConnection
if (alias == uEnd) {
continue;
}
if (makePotentialConnection(alias,
if (makePotentialConnection(alias,tagList,
possibleConnections.potentialEndpoints,
possibleConnections.aliases)) {
break;
Expand All @@ -878,7 +882,7 @@ void Connector::establishPotentialInterfaces(ConnectionsList& possibleConnection

/** unconnected source endpoints*/
for (const auto& uEnd : possibleConnections.unconnectedTargetEndpoints) {
if (makePotentialConnection(uEnd,
if (makePotentialConnection(uEnd,tagList,
possibleConnections.potentialEndpoints,
possibleConnections.aliases)) {
continue;
Expand All @@ -889,7 +893,7 @@ void Connector::establishPotentialInterfaces(ConnectionsList& possibleConnection
if (alias == uEnd) {
continue;
}
if (makePotentialConnection(alias,
if (makePotentialConnection(alias,tagList,
possibleConnections.potentialEndpoints,
possibleConnections.aliases)) {
break;
Expand Down
11 changes: 7 additions & 4 deletions src/helics/apps/Connector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct Connection {
std::string_view interface1;
std::string_view interface2;
InterfaceDirection direction;
std::vector<std::string_view> tags;
std::vector<std::size_t> tags;
std::shared_ptr<std::string> stringBuffer;
};

Expand Down Expand Up @@ -103,7 +103,7 @@ necessary
const std::vector<std::string>& tags = {});

/** add a tag for later reference return a string_view reference for the tag*/
std::string_view addTag(std::string_view tagName);
std::size_t addTag(std::string_view tagName);

/** add a interface name for later reference return a string_view reference for the interface
* name*/
Expand Down Expand Up @@ -135,21 +135,24 @@ necessary
/** try to make a connection for an input*/
int makeTargetConnection(
std::string_view origin,
const std::vector<std::size_t> &tags,
std::unordered_set<std::string_view>& possibleConnections,
const std::unordered_multimap<std::string_view, std::string_view>& aliases,
const std::function<void(std::string_view origin, std::string_view target)>& callback);
bool makePotentialConnection(
std::string_view interfaceName,
const std::vector<std::size_t> &tags,
std::unordered_map<std::string_view, PotentialConnections>& potentials,
const std::unordered_multimap<std::string_view, std::string_view>& aliases);

bool checkPotentialConnection(
std::string_view interface,
const std::vector<std::size_t> &tags,
std::unordered_set<std::string_view>& possibleConnections,
std::unordered_map<std::string_view, PotentialConnections>& potentials,
const std::unordered_multimap<std::string_view, std::string_view>& aliases);
/** get a list of the possible connections to based on the database*/
std::vector<Connection> buildPossibleConnectionList(std::string_view startingInterface) const;
std::vector<Connection> buildPossibleConnectionList(std::string_view startingInterface,const std::vector<std::size_t> &tags) const;
/** load the regex matchers */
void generateRegexMatchers();

Expand All @@ -159,7 +162,7 @@ necessary
std::unordered_multimap<std::string_view, Connection> connections;
std::vector<Connection> matchers;
std::vector<std::shared_ptr<RegexMatcher>> regexMatchers;
std::unordered_set<std::string> tags;
std::unordered_map<std::size_t,std::string> tags;
std::unordered_set<std::string> interfaces;
std::uint64_t matchCount{0};
/// indicator to match unconnected target endpoints default{false}
Expand Down
1 change: 1 addition & 0 deletions tests/helics/apps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ set(helics_apps_test_sources
ConnectorTests2.cpp
ConnectorFileTests.cpp
Connector2StageTests.cpp
ConnectorTagTests.cpp
exeTestHelper.h
)

Expand Down
49 changes: 49 additions & 0 deletions tests/helics/apps/ConnectorTagTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
Copyright (c) 2017-2023,
Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Sustainable
Energy, LLC. See the top-level NOTICE for additional details. All rights reserved.
SPDX-License-Identifier: BSD-3-Clause
*/
#include "gtest/gtest.h"
#include <cstdio>
#ifndef DISABLE_SYSTEM_CALL_TESTS
# include "exeTestHelper.h"
#endif

#include "helics/apps/BrokerApp.hpp"
#include "helics/apps/Connector.hpp"
#include "helics/apps/CoreApp.hpp"

#include <future>
#include <thread>

static constexpr std::string_view testdir = TEST_DIR "/connector/";

TEST(connector_tags, no_match_tag)
{
helics::FederateInfo fedInfo(helics::CoreType::TEST);
using helics::apps::InterfaceDirection;

fedInfo.coreName = "ccore1";
fedInfo.coreInitString = "-f2 --autobroker";
fedInfo.setProperty(HELICS_PROPERTY_TIME_PERIOD, 1.0);
helics::apps::Connector conn1("connector1", fedInfo);
conn1.addConnection("inp1", "pub1", InterfaceDirection::FROM_TO,{"tag1"});

helics::ValueFederate vfed("c1", fedInfo);
auto& pub1 = vfed.registerGlobalPublication<double>("pub1");
auto& inp1 = vfed.registerGlobalInput<double>("inp1");

auto fut = std::async(std::launch::async, [&conn1]() { conn1.run(); });
vfed.enterExecutingMode();
const double testValue = 3452.562;
pub1.publish(testValue);
auto retTime = vfed.requestTime(5);
EXPECT_EQ(retTime, 1.0);
auto val = inp1.getDouble();
EXPECT_EQ(val, testValue);

vfed.finalize();
fut.get();
EXPECT_EQ(conn1.madeConnections(), 0);
}

0 comments on commit 9931148

Please sign in to comment.