Skip to content

Commit

Permalink
[memory][template][search] Use Fiver instead of TripleWithRelation
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaZotov committed Apr 14, 2023
1 parent 9df46b4 commit 0d01d50
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 21 deletions.
25 changes: 10 additions & 15 deletions sc-memory/sc-memory/sc_memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ class ScMemoryContext
ScTemplateSearchResult & result) noexcept(false);

/*!
* Searches constructions by isomorphic search template and pass search result construction to `callback`
* Searches sc-constructions by isomorphic search template and pass search result construction to `callback`
* lambda-function. If `filterCallback` passed, then all found constructions triples are filtered by `filterCallback`
* condition.
* @param templ A sc-template object to find constructions by it
Expand Down Expand Up @@ -389,7 +389,7 @@ class ScMemoryContext
* return ctx->HelperCheckEdge(modelAddr, addr, ScType::EdgeAccessConstPosPerm);
* });
* \endcode
* @throws utils::ExceptionInvalidState
* @throws utils::ExceptionInvalidState if sc-template is not valid
*/
_SC_EXTERN void HelperSearchTemplate(
ScTemplate const & templ,
Expand Down Expand Up @@ -432,21 +432,16 @@ class ScMemoryContext
* );
* m_ctx->HelperSmartSearchTemplate(templ, [&ctx](ScTemplateSearchResultItem const & item) -> ScTemplateSearchRequest
* {
* if (!ctx->HelperCheckEdge(structureAddr, edgeAddr, ScType::EdgeAccessConstPosPerm))
* {
* if (ctx.CreateEdge(ScType::EdgeAccessConstPosTemp, setAddr, item["_addr2"]))
* {
* return ScTemplateSearchRequest::STOP;
* }
* else
* {
* return ScTemplateSearchRequest::ERROR;
* }
* }
* return ScTemplateSearchRequest::CONTINUE;
* if (ctx->HelperCheckEdge(structureAddr, edgeAddr, ScType::EdgeAccessConstPosPerm))
* return ScTemplateSearchRequest::CONTINUE;
*
* if (ctx.CreateEdge(ScType::EdgeAccessConstPosTemp, setAddr, item["_addr2"]))
* return ScTemplateSearchRequest::STOP;
*
* return ScTemplateSearchRequest::ERROR;
* });
* \endcode
* @throws utils::ExceptionInvalidState
* @throws utils::ExceptionInvalidState if sc-template is not valid
*/
_SC_EXTERN void HelperSmartSearchTemplate(
ScTemplate const & templ,
Expand Down
21 changes: 18 additions & 3 deletions sc-memory/sc-memory/sc_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,20 @@ ScTemplateItem operator>>(ScType const & value, std::string const & replName)

// --------------------------------

ScTemplate::ScTemplate(bool forceOrder /* = false */)
ScTemplate::ScTemplate()
{
SC_UNUSED(forceOrder);
m_templateTriples.reserve(16);

auto const tripleTypeCount = (size_t)ScTemplateTripleType::ScConstr3TypeCount;
m_priorityOrderedTemplateTriples.resize(tripleTypeCount);
}

ScTemplate::ScTemplate(bool forceOrder)
: ScTemplate()
{
SC_UNUSED(forceOrder);
}

ScTemplate & ScTemplate::operator()(
ScTemplateItem const & param1,
ScTemplateItem const & param2,
Expand Down Expand Up @@ -145,7 +150,7 @@ ScTemplate & ScTemplate::Triple(
return *this;
}

ScTemplate & ScTemplate::TripleWithRelation(
ScTemplate & ScTemplate::Fiver(
ScTemplateItem const & param1,
ScTemplateItem const & param2,
ScTemplateItem const & param3,
Expand All @@ -170,6 +175,16 @@ ScTemplate & ScTemplate::TripleWithRelation(
return *this;
}

ScTemplate & ScTemplate::TripleWithRelation(
ScTemplateItem const & param1,
ScTemplateItem const & param2,
ScTemplateItem const & param3,
ScTemplateItem const & param4,
ScTemplateItem const & param5)
{
return Fiver(param1, param2, param3, param4, param5);
}

inline ScTemplateTripleType ScTemplate::GetPriority(ScTemplateTriple * triple)
{
ScTemplateItem const & item1 = triple->m_values[0];
Expand Down
16 changes: 15 additions & 1 deletion sc-memory/sc-memory/sc_template.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,10 @@ class ScTemplate final
using ScTemplateItemsToReplacementsItemsPositions = std::unordered_map<std::string, size_t>;
using ScTemplateTriplesVector = std::vector<ScTemplateTriple *>;

_SC_EXTERN explicit ScTemplate();

SC_DEPRECATED(0.8.0, "Now ScTemplate sorts itself effectively")
_SC_EXTERN explicit ScTemplate(bool forceOrder = false);
_SC_EXTERN explicit ScTemplate(bool forceOrder);

_SC_EXTERN ~ScTemplate()
{
Expand Down Expand Up @@ -345,6 +347,18 @@ class ScTemplate final
* Equal to two calls of triple, so this template add 6 items to result (NOT 5), to minimize
* possible abuse, use result name mapping, and get result by names
*/
_SC_EXTERN ScTemplate & Fiver(
ScTemplateItem const & param1,
ScTemplateItem const & param2,
ScTemplateItem const & param3,
ScTemplateItem const & param4,
ScTemplateItem const & param5) noexcept(false);

SC_DEPRECATED(
0.4.0,
"Use ScTemplate::Fiver(ScTemplateItem const & param1, ScTemplateItem const & param2, "
"ScTemplateItem const & param3, ScTemplateItem const & param4, ScTemplateItem const & param5) "
"noexcept(false) instead.")
_SC_EXTERN ScTemplate & TripleWithRelation(
ScTemplateItem const & param1,
ScTemplateItem const & param2,
Expand Down
13 changes: 13 additions & 0 deletions sc-memory/tests/sc-memory/templates/sc-templates-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,16 @@ TEST_F(ScTemplateApiTest, TripleWithInvalidTargetAddr)
ScType::EdgeAccessVarPosPerm >> "_edge",
ScAddr::Empty >> "_addr2"), utils::ExceptionInvalidParams);
}

TEST_F(ScTemplateApiTest, FiverWithConstSourceType)
{
ScTemplate templ;
EXPECT_THROW(
templ.Fiver(
ScType::NodeConst >> "_addr1",
ScType::EdgeAccessVarPosPerm >> "_edge",
ScType::Unknown >> "_addr2",
ScType::EdgeAccessVarPosPerm,
ScType::NodeVarNoRole >> "_relation"),
utils::ExceptionInvalidParams);
}
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ TEST_F(ScTemplateCommonTest, CycledTemplateSmoke)
TEST_F(ScTemplateCommonTest, MultipleConnectivitiesTemplateSmoke)
{
size_t const constrCount = 20;
std::unordered_map<ScAddr, ScAddr, ScAddrHashFunc<size_t>, ScAddLessFunc> cache;
std::map<ScAddr, ScAddr, ScAddLessFunc> cache;
for (size_t i = 0; i < constrCount; ++i)
{
ScAddr const & sourceNodeAddr = m_ctx->CreateNode(ScType::NodeConst);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ TEST_F(ScTemplateSearchTest, SimpleSearch2)

// now check search
ScTemplate templ;
templ.TripleWithRelation(
templ.Fiver(
addr >> "_addr",
ScType::EdgeDCommonVar >> "_edgeCommon",
ScType::Link >> "_link",
Expand Down

0 comments on commit 0d01d50

Please sign in to comment.