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

#include <Class.h>

Inheritance diagram for knowrob::semweb::Class:
Collaboration diagram for knowrob::semweb::Class:

Classes

struct  ClassComparator
 

Public Member Functions

 Class (std::string_view iri)
 
 Class (const IRIAtomPtr &iri)
 
void addDirectParent (const std::shared_ptr< Class > &directParent, std::optional< std::string_view > graph)
 
void removeDirectParent (const std::shared_ptr< Class > &directParent, std::optional< std::string_view > graph)
 
const auto & directParents () const
 
bool isDirectSubClassOf (const std::shared_ptr< Class > &directParent)
 
bool isSubClassOf (const std::shared_ptr< Class > &parent, bool includeSelf=true)
 
void forallParents (const ClassVisitor &visitor, bool includeSelf=true, bool skipDuplicates=true)
 
void forallChildren (const ClassTupleVisitor &visitor, bool skipDuplicates=true)
 
void detach ()
 
 Class (std::string_view iri)
 
 Class (const IRIAtomPtr &iri)
 
void addDirectParent (const std::shared_ptr< Class > &directParent, std::optional< std::string_view > graph)
 
void removeDirectParent (const std::shared_ptr< Class > &directParent, std::optional< std::string_view > graph)
 
const auto & directParents () const
 
bool isDirectSubClassOf (const std::shared_ptr< Class > &directParent)
 
bool isSubClassOf (const std::shared_ptr< Class > &parent, bool includeSelf=true)
 
void forallParents (const ClassVisitor &visitor, bool includeSelf=true, bool skipDuplicates=true)
 
void forallChildren (const ClassTupleVisitor &visitor, bool skipDuplicates=true)
 
void detach ()
 
- Public Member Functions inherited from knowrob::semweb::Resource
 Resource (std::string_view iri)
 
 Resource (const IRIAtomPtr &iri)
 
auto iri () const
 
auto iriAtom () const
 
std::string_view name () const
 
std::string_view ns (bool includeDelimiter=false) const
 
 Resource (std::string_view iri)
 
 Resource (const IRIAtomPtr &iri)
 
auto iri () const
 
auto iriAtom () const
 
std::string_view name () const
 
std::string_view ns (bool includeDelimiter=false) const
 

Protected Attributes

std::map< std::shared_ptr< Class >, std::set< AtomPtr, AtomComparator >, ClassComparatordirectParents_
 
std::set< std::shared_ptr< Class >, ClassComparatordirectChildren_
 
- Protected Attributes inherited from knowrob::semweb::Resource
knowrob::AtomPtr iri_
 

Additional Inherited Members

- Static Public Member Functions inherited from knowrob::semweb::Resource
static IRIAtomPtr unique_iri (std::string_view ns, std::string_view name)
 
static IRIAtomPtr unique_iri (std::string_view type_iri)
 
static std::string_view iri_name (std::string_view iri)
 
static std::string_view iri_ns (std::string_view iri, bool includeDelimiter=false)
 
static AtomPtr graph_atom (std::optional< std::string_view > graph)
 
static IRIAtomPtr unique_iri (std::string_view ns, std::string_view name)
 
static IRIAtomPtr unique_iri (std::string_view type_iri)
 
static std::string_view iri_name (std::string_view iri)
 
static std::string_view iri_ns (std::string_view iri, bool includeDelimiter=false)
 
static AtomPtr graph_atom (std::optional< std::string_view > graph)
 

Detailed Description

A RDF class.

Definition at line 26 of file Class.h.

Constructor & Destructor Documentation

◆ Class() [1/4]

Class::Class ( std::string_view  iri)
explicit
Parameters
iriA class IRI.

Definition at line 15 of file Class.cpp.

16  : Resource(iri) {}
Resource(std::string_view iri)
Definition: Resource.cpp:23

◆ Class() [2/4]

Class::Class ( const IRIAtomPtr iri)
explicit
Parameters
iriA class IRI.

Definition at line 18 of file Class.cpp.

19  : Resource(iri) {}

◆ Class() [3/4]

knowrob::semweb::Class::Class ( std::string_view  iri)
explicit
Parameters
iriA class IRI.

◆ Class() [4/4]

knowrob::semweb::Class::Class ( const IRIAtomPtr iri)
explicit
Parameters
iriA class IRI.

Member Function Documentation

◆ addDirectParent() [1/2]

void Class::addDirectParent ( const std::shared_ptr< Class > &  directParent,
std::optional< std::string_view >  graph 
)
Parameters
directParenta direct super class.
graphthe graph where the relation is defined.

Definition at line 25 of file Class.cpp.

25  {
26  auto graphAtom = graph_atom(graph);
27  auto pair = directParents_.find(directParent);
28  if (pair != directParents_.end()) {
29  // add origin to list
30  pair->second.insert(graphAtom);
31  } else {
32  // add new entry
33  directParents_[directParent].insert(graphAtom);
34  directParent->directChildren_.insert(shared_from_this());
35  }
36 }
std::map< std::shared_ptr< Class >, std::set< AtomPtr, AtomComparator >, ClassComparator > directParents_
Definition: Class.h:93
static AtomPtr graph_atom(std::optional< std::string_view > graph)
Definition: Resource.cpp:82

◆ addDirectParent() [2/2]

void knowrob::semweb::Class::addDirectParent ( const std::shared_ptr< Class > &  directParent,
std::optional< std::string_view >  graph 
)
Parameters
directParenta direct super class.
graphthe graph where the relation is defined.

◆ detach() [1/2]

void Class::detach ( )

Detach this class from its children and parents.

Definition at line 138 of file Class.cpp.

138  {
139  directParents_.clear();
140  directChildren_.clear();
141 }
std::set< std::shared_ptr< Class >, ClassComparator > directChildren_
Definition: Class.h:94

◆ detach() [2/2]

void knowrob::semweb::Class::detach ( )

Detach this class from its children and parents.

◆ directParents() [1/2]

const auto& knowrob::semweb::Class::directParents ( ) const
inline
Returns
all direct super classes of this class.

Definition at line 53 of file Class.h.

53 { return directParents_; }

◆ directParents() [2/2]

const auto& knowrob::semweb::Class::directParents ( ) const
inline
Returns
all direct super classes of this class.

Definition at line 53 of file Class.h.

53 { return directParents_; }

◆ forallChildren() [1/2]

void Class::forallChildren ( const ClassTupleVisitor visitor,
bool  skipDuplicates = true 
)
Parameters
visitora function that is called for each child in the class hierarchy.
skipDuplicatesif true, the method calls the visitor only once for each class.

Definition at line 113 of file Class.cpp.

113  {
114  std::queue<std::pair<Class *, Class *>> queue_;
115  std::set<std::string_view> visited_;
116 
117  // push initial elements to the queue
118  for (auto &x: directChildren_) queue_.emplace(x.get(), this);
119 
120  // visit each child
121  while (!queue_.empty()) {
122  auto pair = queue_.front();
123  auto front_child = pair.first;
124  auto front_parent = pair.second;
125  queue_.pop();
126  // visit popped property
127  visitor(*front_child, *front_parent);
128  // remember visited nodes
129  if (skipDuplicates) visited_.insert(front_child->iri());
130  // push children of visited property on the queue
131  for (auto &directChild: front_child->directChildren_) {
132  if (skipDuplicates && visited_.count(directChild->iri()) > 0) continue;
133  queue_.emplace(directChild.get(), front_child);
134  }
135  }
136 }

◆ forallChildren() [2/2]

void knowrob::semweb::Class::forallChildren ( const ClassTupleVisitor visitor,
bool  skipDuplicates = true 
)
Parameters
visitora function that is called for each child in the class hierarchy.
skipDuplicatesif true, the method calls the visitor only once for each class.

◆ forallParents() [1/2]

void Class::forallParents ( const ClassVisitor visitor,
bool  includeSelf = true,
bool  skipDuplicates = true 
)
Parameters
visitora function that is called for each parent in the class hierarchy.
includeSelfif true, the method calls the visitor for this class.
skipDuplicatesif true, the method calls the visitor only once for each class.

Definition at line 87 of file Class.cpp.

89  {
90  std::queue<Class *> queue_;
91  std::set<std::string_view> visited_;
92 
93  // push initial elements to the queue
94  if (includeSelf) queue_.push(this);
95  else for (auto &x: directParents_) queue_.push(x.first.get());
96 
97  // visit each parent
98  while (!queue_.empty()) {
99  auto front = queue_.front();
100  queue_.pop();
101  // visit popped property
102  visitor(*front);
103  // remember visited nodes
104  if (skipDuplicates) visited_.insert(front->iri());
105  // push parents of visited property on the queue
106  for (auto &directParent: front->directParents_) {
107  if (skipDuplicates && visited_.count(directParent.first->iri()) > 0) continue;
108  queue_.push(directParent.first.get());
109  }
110  }
111 }

◆ forallParents() [2/2]

void knowrob::semweb::Class::forallParents ( const ClassVisitor visitor,
bool  includeSelf = true,
bool  skipDuplicates = true 
)
Parameters
visitora function that is called for each parent in the class hierarchy.
includeSelfif true, the method calls the visitor for this class.
skipDuplicatesif true, the method calls the visitor only once for each class.

◆ isDirectSubClassOf() [1/2]

bool Class::isDirectSubClassOf ( const std::shared_ptr< Class > &  directParent)
Parameters
directParenta direct super class.
Returns
true if directParent is a direct super class of this class.

Definition at line 57 of file Class.cpp.

57  {
58  return directParents_.count(directParent) > 0;
59 }

◆ isDirectSubClassOf() [2/2]

bool knowrob::semweb::Class::isDirectSubClassOf ( const std::shared_ptr< Class > &  directParent)
Parameters
directParenta direct super class.
Returns
true if directParent is a direct super class of this class.

◆ isSubClassOf() [1/2]

bool Class::isSubClassOf ( const std::shared_ptr< Class > &  parent,
bool  includeSelf = true 
)
Parameters
parenta super class.
includeSelfif true, the method returns true if this class is the same
Returns
true if this class is a sub class of parent.

Definition at line 61 of file Class.cpp.

61  {
62  std::queue<Class *> queue_;
63  std::set<std::string_view> visited_;
64 
65  if (includeSelf && this == parent.get()) return true;
66  queue_.push(this);
67 
68  // visit each parent
69  while (!queue_.empty()) {
70  auto front = queue_.front();
71  queue_.pop();
72 
73  // visit popped property
74  if (front->directParents_.count(parent) > 0) return true;
75  // remember visited nodes
76  visited_.insert(front->iri());
77  // push parents of visited property on the queue
78  for (auto &directParent: front->directParents_) {
79  if (visited_.count(directParent.first->iri()) > 0) continue;
80  queue_.push(directParent.first.get());
81  }
82  }
83 
84  return false;
85 }

◆ isSubClassOf() [2/2]

bool knowrob::semweb::Class::isSubClassOf ( const std::shared_ptr< Class > &  parent,
bool  includeSelf = true 
)
Parameters
parenta super class.
includeSelfif true, the method returns true if this class is the same
Returns
true if this class is a sub class of parent.

◆ removeDirectParent() [1/2]

void Class::removeDirectParent ( const std::shared_ptr< Class > &  directParent,
std::optional< std::string_view >  graph 
)
Parameters
directParenta direct super class.
graphthe graph where the relation is defined.

Definition at line 38 of file Class.cpp.

38  {
39  auto pair = directParents_.find(directParent);
40  if (pair != directParents_.end()) {
41  // remove origin from list
42  std::string_view v_graph = (graph) ? graph.value() : ImportHierarchy::ORIGIN_SESSION;
43  for (auto it = pair->second.begin(); it != pair->second.end(); it++) {
44  if ((*it)->stringForm() == v_graph) {
45  pair->second.erase(it);
46  break;
47  }
48  }
49  // remove if no origin is left
50  if (pair->second.empty()) {
51  directParents_.erase(pair);
52  directParent->directChildren_.erase(shared_from_this());
53  }
54  }
55 }
static constexpr std::string_view ORIGIN_SESSION

◆ removeDirectParent() [2/2]

void knowrob::semweb::Class::removeDirectParent ( const std::shared_ptr< Class > &  directParent,
std::optional< std::string_view >  graph 
)
Parameters
directParenta direct super class.
graphthe graph where the relation is defined.

Member Data Documentation

◆ directChildren_

std::set< std::shared_ptr< Class >, ClassComparator > knowrob::semweb::Class::directChildren_
protected

Definition at line 94 of file Class.h.

◆ directParents_

std::map< std::shared_ptr< Class >, std::set< AtomPtr, AtomComparator >, ClassComparator > knowrob::semweb::Class::directParents_
protected

Definition at line 93 of file Class.h.


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