forked from ostis-ai/sc-machine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sc_common_templ.hpp
52 lines (42 loc) · 1.31 KB
/
sc_common_templ.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#pragma once
#include "sc_link.hpp"
#include "sc_memory.hpp"
#include "sc_template.hpp"
namespace sc
{
/* Create construction:
* elAddr <= relAddr: {};;
* If construction exist, then returns exist tuple addr; otherwise create new one.
* Returns ScAddr of tuple
*/
_SC_EXTERN ScAddr ResolveRelationTuple(ScMemoryContext & ctx, ScAddr const & elAddr, ScAddr const & relAddr);
/* Generates construction:
* elAddr => relAddr: [];;
* If link with relation exist, then changes it value; otherwise create new one.
* Returns ScAddr of sc-link
*/
template <typename ValueType>
ScAddr SetRelationValue(ScMemoryContext & ctx, ScAddr const & elAddr, ScAddr const & relAddr, ValueType const & value)
{
ScTemplate templ;
templ.TripleWithRelation(
elAddr, ScType::EdgeDCommonVar, ScType::Link >> "_link", ScType::EdgeAccessVarPosPerm, relAddr);
ScAddr linkAddr;
ScTemplateSearchResult res;
if (ctx.HelperSearchTemplate(templ, res))
linkAddr = res[0]["_link"];
if (!linkAddr.IsValid())
{
ScTemplateGenResult genRes;
if (ctx.HelperGenTemplate(templ, genRes))
linkAddr = genRes["_link"];
if (!linkAddr.IsValid())
{
SC_THROW_EXCEPTION(utils::ExceptionInvalidState, "Can't create value relation");
}
}
ScLink link(ctx, linkAddr);
link.Set(value);
return linkAddr;
}
} // namespace sc