knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
knowrob::ImportHierarchy Class Reference

#include <ImportHierarchy.h>

Public Member Functions

 ImportHierarchy ()
 
bool isCurrentGraph (std::string_view graphName) const
 
void clear ()
 
void setDefaultGraph (std::string_view defaultGraph)
 
const auto & defaultGraph () const
 
void addCurrentGraph (std::string_view graphName)
 
void removeCurrentGraph (std::string_view graphName)
 
void addDirectImport (std::string_view importerGraphName, std::string_view importedGraphName)
 
const std::set< CurrentGraph * > & getImports (std::string_view graphName)
 
 ImportHierarchy ()
 
bool isCurrentGraph (std::string_view graphName) const
 
void clear ()
 
void setDefaultGraph (std::string_view defaultGraph)
 
const auto & defaultGraph () const
 
void addCurrentGraph (std::string_view graphName)
 
void removeCurrentGraph (std::string_view graphName)
 
void addDirectImport (std::string_view importerGraphName, std::string_view importedGraphName)
 
const std::set< CurrentGraph * > & getImports (std::string_view graphName)
 

Static Public Member Functions

static bool isReservedOrigin (std::string_view origin)
 
static bool isReservedOrigin (std::string_view origin)
 

Static Public Attributes

static constexpr std::string_view ORIGIN_ANY = "any"
 
static constexpr std::string_view ORIGIN_SYSTEM = "system"
 
static constexpr std::string_view ORIGIN_SESSION = "session"
 
static constexpr std::string_view ORIGIN_USER = "user"
 
static constexpr std::string_view ORIGIN_REASONER = "reasoner"
 
static constexpr std::string_view ORIGIN_TEST = "test"
 

Protected Member Functions

CurrentGraphgetCurrentGraph (std::string_view name)
 
bool isSystemOrigin (CurrentGraph &graph)
 
bool isSessionOrigin (CurrentGraph &graph)
 
CurrentGraphgetCurrentGraph (std::string_view name)
 
bool isSystemOrigin (CurrentGraph &graph)
 
bool isSessionOrigin (CurrentGraph &graph)
 

Protected Attributes

std::map< std::string_view, std::unique_ptr< CurrentGraph > > graphs_
 
std::string defaultGraph_
 

Detailed Description

Manages a hierarchy between named triple origins. One of the purposes of this hierarchy is to distinguish between triples that are dynamically inserted during runtime from those that are more static like files loaded initially.

Definition at line 20 of file ImportHierarchy.h.

Constructor & Destructor Documentation

◆ ImportHierarchy() [1/2]

ImportHierarchy::ImportHierarchy ( )

Definition at line 13 of file ImportHierarchy.cpp.

20 }
void addDirectImport(std::string_view importerGraphName, std::string_view importedGraphName)
static constexpr std::string_view ORIGIN_TEST
static constexpr std::string_view ORIGIN_SESSION
static constexpr std::string_view ORIGIN_USER
static constexpr std::string_view ORIGIN_REASONER
static constexpr std::string_view ORIGIN_SYSTEM
static constexpr std::string_view ORIGIN_ANY

◆ ImportHierarchy() [2/2]

knowrob::ImportHierarchy::ImportHierarchy ( )

Member Function Documentation

◆ addCurrentGraph() [1/2]

void ImportHierarchy::addCurrentGraph ( std::string_view  graphName)

Defines a named graph if it is not defined yet.

Parameters
graphNamea graph name.

Definition at line 41 of file ImportHierarchy.cpp.

41  {
42  auto it = graphs_.find(graphName);
43  if (it == graphs_.end()) {
44  auto newGraph = new CurrentGraph(graphName);
45  graphs_.emplace(newGraph->name(), newGraph);
46  }
47 }
std::map< std::string_view, std::unique_ptr< CurrentGraph > > graphs_

◆ addCurrentGraph() [2/2]

void knowrob::ImportHierarchy::addCurrentGraph ( std::string_view  graphName)

Defines a named graph if it is not defined yet.

Parameters
graphNamea graph name.

◆ addDirectImport() [1/2]

void ImportHierarchy::addDirectImport ( std::string_view  importerGraphName,
std::string_view  importedGraphName 
)

Adds the imports relation between two named graphs.

Parameters
importerGraphNamea graph name.
importedGraphNamea graph name.

Definition at line 69 of file ImportHierarchy.cpp.

69  {
70  auto &g_importer = getCurrentGraph(importerGraphName);
71  auto g_imported = &(getCurrentGraph(importedGraphName));
72 
73  // if a system graph imports a session graph, remove the session graph from the session
74  // before adding to the system. And if a session graph imports a system graph, ignore the import.
75  if (isSystemOrigin(g_importer) && isSessionOrigin(*g_imported)) {
76  removeCurrentGraph(importedGraphName);
77  g_imported = &(getCurrentGraph(importedGraphName));
78  } else if (isSessionOrigin(g_importer) && isSystemOrigin(*g_imported)) {
79  KB_WARN("Ignoring session graph \"{}\" import of system graph \"{}\".", importerGraphName, importedGraphName);
80  return;
81  }
82 
83  // g_importer.directlyImports += [g_imported]
84  auto pair = g_importer.directImports_.insert(g_imported);
85  if (!pair.second) return;
86  KB_DEBUG("Graph \"{}\" imports \"{}\".", importerGraphName, importedGraphName);
87  // g_importer.imports += g_imported.imports + [gb]
88  g_importer.imports_.insert(g_imported->imports_.begin(), g_imported->imports_.end());
89  g_importer.imports_.insert(g_imported);
90  // for every graph gx that imports g_importer:
91  // gx.imports += (g_imported.imports + [g_imported])
92  for (auto &it: graphs_) {
93  if (it.second->imports_.count(&g_importer) > 0) {
94  it.second->imports_.insert(g_imported->imports_.begin(), g_imported->imports_.end());
95  it.second->imports_.insert(g_imported);
96  }
97  }
98 }
#define KB_DEBUG
Definition: Logger.h:25
#define KB_WARN
Definition: Logger.h:27
CurrentGraph & getCurrentGraph(std::string_view name)
bool isSessionOrigin(CurrentGraph &graph)
void removeCurrentGraph(std::string_view graphName)
bool isSystemOrigin(CurrentGraph &graph)

◆ addDirectImport() [2/2]

void knowrob::ImportHierarchy::addDirectImport ( std::string_view  importerGraphName,
std::string_view  importedGraphName 
)

Adds the imports relation between two named graphs.

Parameters
importerGraphNamea graph name.
importedGraphNamea graph name.

◆ clear() [1/2]

void knowrob::ImportHierarchy::clear ( )
inline

Clear the hierarchy.

Definition at line 65 of file ImportHierarchy.h.

65 { graphs_.clear(); }

◆ clear() [2/2]

void knowrob::ImportHierarchy::clear ( )
inline

Clear the hierarchy.

Definition at line 65 of file ImportHierarchy.h.

65 { graphs_.clear(); }

◆ defaultGraph() [1/2]

const auto& knowrob::ImportHierarchy::defaultGraph ( ) const
inline
Returns
the default graph name of this hierarchy.

Definition at line 76 of file ImportHierarchy.h.

76 { return defaultGraph_; }

◆ defaultGraph() [2/2]

const auto& knowrob::ImportHierarchy::defaultGraph ( ) const
inline
Returns
the default graph name of this hierarchy.

Definition at line 76 of file ImportHierarchy.h.

76 { return defaultGraph_; }

◆ getCurrentGraph() [1/2]

CurrentGraph & ImportHierarchy::getCurrentGraph ( std::string_view  name)
protected

Definition at line 59 of file ImportHierarchy.cpp.

59  {
60  auto it1 = graphs_.find(name);
61  if (it1 == graphs_.end()) {
62  auto newGraph = new CurrentGraph(name);
63  auto inserted = graphs_.emplace(newGraph->name(), newGraph);
64  it1 = inserted.first;
65  }
66  return *it1->second;
67 }

◆ getCurrentGraph() [2/2]

CurrentGraph& knowrob::ImportHierarchy::getCurrentGraph ( std::string_view  name)
protected

◆ getImports() [1/2]

const std::set< CurrentGraph * > & ImportHierarchy::getImports ( std::string_view  graphName)
Parameters
graphNamea graph name.
Returns
the transitive closure of the import relation starting from graphName

Definition at line 49 of file ImportHierarchy.cpp.

49  {
50  auto it = graphs_.find(graphName);
51  if (it == graphs_.end()) {
52  static std::set<CurrentGraph *> empty;
53  return empty;
54  } else {
55  return it->second->imports_;
56  }
57 }

◆ getImports() [2/2]

const std::set<CurrentGraph *>& knowrob::ImportHierarchy::getImports ( std::string_view  graphName)
Parameters
graphNamea graph name.
Returns
the transitive closure of the import relation starting from graphName

◆ isCurrentGraph() [1/2]

bool ImportHierarchy::isCurrentGraph ( std::string_view  graphName) const
Parameters
graphNamea graph name
Returns
true if graphName is known in this hierarchy.

Definition at line 27 of file ImportHierarchy.cpp.

27  {
28  return graphs_.count(graphName) > 0;
29 }

◆ isCurrentGraph() [2/2]

bool knowrob::ImportHierarchy::isCurrentGraph ( std::string_view  graphName) const
Parameters
graphNamea graph name
Returns
true if graphName is known in this hierarchy.

◆ isReservedOrigin() [1/2]

bool ImportHierarchy::isReservedOrigin ( std::string_view  origin)
static
Parameters
origina graph name
Returns
true if origin is a reserved origin.

Definition at line 22 of file ImportHierarchy.cpp.

22  {
23  static std::set<std::string_view> reservedOrigins = {ORIGIN_SYSTEM, ORIGIN_SESSION, ORIGIN_USER, ORIGIN_REASONER, ORIGIN_TEST};
24  return reservedOrigins.count(origin) > 0;
25 }

◆ isReservedOrigin() [2/2]

static bool knowrob::ImportHierarchy::isReservedOrigin ( std::string_view  origin)
static
Parameters
origina graph name
Returns
true if origin is a reserved origin.

◆ isSessionOrigin() [1/2]

bool ImportHierarchy::isSessionOrigin ( CurrentGraph graph)
protected

Definition at line 36 of file ImportHierarchy.cpp.

36  {
37  auto originSession = getCurrentGraph(ORIGIN_SESSION);
38  return graph == originSession || originSession.imports_.count(&graph) > 0;
39 }
std::set< CurrentGraph * > imports_
Definition: CurrentGraph.h:48

◆ isSessionOrigin() [2/2]

bool knowrob::ImportHierarchy::isSessionOrigin ( CurrentGraph graph)
protected

◆ isSystemOrigin() [1/2]

bool ImportHierarchy::isSystemOrigin ( CurrentGraph graph)
protected

Definition at line 31 of file ImportHierarchy.cpp.

31  {
32  auto originSystem = getCurrentGraph(ORIGIN_SYSTEM);
33  return graph == originSystem || originSystem.imports_.count(&graph) > 0;
34 }

◆ isSystemOrigin() [2/2]

bool knowrob::ImportHierarchy::isSystemOrigin ( CurrentGraph graph)
protected

◆ removeCurrentGraph() [1/2]

void ImportHierarchy::removeCurrentGraph ( std::string_view  graphName)

Removes any definition of a named graph.

Parameters
graphNamea graph name.

Definition at line 100 of file ImportHierarchy.cpp.

100  {
101  auto it = graphs_.find(graphName);
102  if (it == graphs_.end()) return;
103  auto &ga = *it->second;
104 
105  // for every graph gx that directly imports ga
106  for (auto &jt: graphs_) {
107  auto &gx = *jt.second;
108  if (gx.directImports_.count(&ga) > 0) {
109  // gx.directlyImports -= [ga]
110  gx.directImports_.erase(&ga);
111  gx.imports_.erase(&ga);
112  // gx.directlyImports += [ga.directImports]
113  gx.directImports_.insert(ga.directImports_.begin(), ga.directImports_.end());
114  }
115  }
116  // for every graph gy that imports ga:
117  for (auto &kt: graphs_) {
118  auto &gy = *kt.second;
119  if (gy.imports_.count(&ga) > 0) {
120  // gy.imports -= [ga]
121  gy.imports_.erase(&ga);
122  }
123  }
124 
125  // finally delete the node
126  graphs_.erase(it);
127 }

◆ removeCurrentGraph() [2/2]

void knowrob::ImportHierarchy::removeCurrentGraph ( std::string_view  graphName)

Removes any definition of a named graph.

Parameters
graphNamea graph name.

◆ setDefaultGraph() [1/2]

void knowrob::ImportHierarchy::setDefaultGraph ( std::string_view  defaultGraph)
inline

Set the default graph used for triples in case no graph name is specified.

Parameters
defaultGrapha graph name.

Definition at line 71 of file ImportHierarchy.h.

const auto & defaultGraph() const

◆ setDefaultGraph() [2/2]

void knowrob::ImportHierarchy::setDefaultGraph ( std::string_view  defaultGraph)
inline

Set the default graph used for triples in case no graph name is specified.

Parameters
defaultGrapha graph name.

Definition at line 71 of file ImportHierarchy.h.

Member Data Documentation

◆ defaultGraph_

std::string knowrob::ImportHierarchy::defaultGraph_
protected

Definition at line 105 of file ImportHierarchy.h.

◆ graphs_

std::map< std::string_view, std::unique_ptr< CurrentGraph > > knowrob::ImportHierarchy::graphs_
protected

Definition at line 104 of file ImportHierarchy.h.

◆ ORIGIN_ANY

static constexpr std::string_view knowrob::ImportHierarchy::ORIGIN_ANY = "any"
staticconstexpr

Used to match triples of any origin in a query.

Definition at line 25 of file ImportHierarchy.h.

◆ ORIGIN_REASONER

static constexpr std::string_view knowrob::ImportHierarchy::ORIGIN_REASONER = "reasoner"
staticconstexpr

Used to only match triples from reasoners.

Definition at line 42 of file ImportHierarchy.h.

◆ ORIGIN_SESSION

static constexpr std::string_view knowrob::ImportHierarchy::ORIGIN_SESSION = "session"
staticconstexpr

Used to only match triples from the current session, i.e. those that are inserted by the user, by reasoners or unittests during the current session.

Definition at line 34 of file ImportHierarchy.h.

◆ ORIGIN_SYSTEM

static constexpr std::string_view knowrob::ImportHierarchy::ORIGIN_SYSTEM = "system"
staticconstexpr

Used to only match system triples.

Definition at line 29 of file ImportHierarchy.h.

◆ ORIGIN_TEST

static constexpr std::string_view knowrob::ImportHierarchy::ORIGIN_TEST = "test"
staticconstexpr

Used to only match triples from unittests.

Definition at line 46 of file ImportHierarchy.h.

◆ ORIGIN_USER

static constexpr std::string_view knowrob::ImportHierarchy::ORIGIN_USER = "user"
staticconstexpr

Used to only match triples from the current user.

Definition at line 38 of file ImportHierarchy.h.


The documentation for this class was generated from the following files: