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

#include <Atom.h>

Inheritance diagram for knowrob::Atom:
Collaboration diagram for knowrob::Atom:

Public Member Functions

 Atom (std::string_view stringForm, AtomType atomType=AtomType::REGULAR)
 
bool isSameAtom (const Atom &other) const
 
AtomType atomType () const
 
std::string_view stringForm () const final
 
void write (std::ostream &os) const override
 
 Atom (std::string_view stringForm, AtomType atomType=AtomType::REGULAR)
 
bool isSameAtom (const Atom &other) const
 
AtomType atomType () const
 
std::string_view stringForm () const final
 
void write (std::ostream &os) const override
 
- Public Member Functions inherited from knowrob::Atomic
 Atomic (AtomicType atomicType)
 
 ~Atomic () override=default
 
bool isSameAtomic (const Atomic &other) const
 
AtomicType atomicType () const
 
size_t hashOfAtomic () const
 
const std::set< std::string_view > & variables () const final
 
 Atomic (AtomicType atomicType)
 
 ~Atomic () override=default
 
bool isSameAtomic (const Atomic &other) const
 
AtomicType atomicType () const
 
size_t hashOfAtomic () const
 
const std::set< std::string_view > & variables () const final
 
void write (std::ostream &os) const override
 
- Public Member Functions inherited from knowrob::Term
 Term (TermType termType)
 
 ~Term () override=default
 
bool operator== (const Term &other) const
 
bool operator!= (const Term &other) const
 
TermType termType () const
 
bool isGround () const
 
bool isAtomic () const
 
bool isAtom () const
 
bool isVariable () const
 
bool isFunction () const
 
bool isNumeric () const
 
bool isString () const
 
bool isIRI () const
 
bool isBlank () const
 
size_t hash () const
 
 Term (TermType termType)
 
 ~Term () override=default
 
bool operator== (const Term &other) const
 
bool operator!= (const Term &other) const
 
TermType termType () const
 
bool isGround () const
 
bool isAtomic () const
 
bool isAtom () const
 
bool isVariable () const
 
bool isFunction () const
 
bool isNumeric () const
 
bool isString () const
 
bool isIRI () const
 
bool isBlank () const
 
size_t hash () const
 
- Public Member Functions inherited from knowrob::Printable
virtual ~Printable ()=default
 
virtual std::string format () const
 
virtual ~Printable ()=default
 
virtual std::string format () const
 

Static Public Member Functions

static std::shared_ptr< knowrob::AtomTabled (std::string_view stringForm)
 
static std::shared_ptr< knowrob::AtomTabled (std::string_view stringForm)
 
- Static Public Member Functions inherited from knowrob::Atomic
static std::shared_ptr< AtomicmakeTripleValue (const Triple &triple)
 
static std::shared_ptr< AtomicmakeTripleValue (const Triple &triple)
 

Protected Types

using AtomTable = std::map< std::string, std::optional< std::weak_ptr< Atom > >, std::less<> >
 
using AtomTable = std::map< std::string, std::optional< std::weak_ptr< Atom > >, std::less<> >
 

Static Protected Member Functions

static AtomTabletable ()
 
static AtomTabletable ()
 

Protected Attributes

std::string_view stringForm_
 
const AtomType atomType_
 
- Protected Attributes inherited from knowrob::Atomic
const AtomicType atomicType_
 
- Protected Attributes inherited from knowrob::Term
const TermType termType_
 
bool isBlank_ = false
 
bool isIRI_ = false
 

Additional Inherited Members

- Static Protected Attributes inherited from knowrob::Term
static const std::set< std::string_view > noVariables_ = {}
 

Detailed Description

An atom is an atomic term that represents a constant.

Definition at line 28 of file Atom.h.

Member Typedef Documentation

◆ AtomTable [1/2]

using knowrob::Atom::AtomTable = std::map<std::string, std::optional<std::weak_ptr<Atom> >, std::less<> >
protected

Definition at line 64 of file Atom.h.

◆ AtomTable [2/2]

using knowrob::Atom::AtomTable = std::map<std::string, std::optional<std::weak_ptr<Atom> >, std::less<> >
protected

Definition at line 64 of file Atom.h.

Constructor & Destructor Documentation

◆ Atom() [1/2]

Atom::Atom ( std::string_view  stringForm,
AtomType  atomType = AtomType::REGULAR 
)
explicit

Constructs an atom from a string.

Parameters
stringFormthe string form of the atom
atomTypethe type of the atom

Definition at line 16 of file Atom.cpp.

20 }
const AtomType atomType_
Definition: Atom.h:62
AtomType atomType() const
Definition: Atom.h:52
std::string_view stringForm_
Definition: Atom.h:61
std::string_view stringForm() const final
Definition: Atom.h:55
Atomic(AtomicType atomicType)
Definition: Atomic.h:34

◆ Atom() [2/2]

knowrob::Atom::Atom ( std::string_view  stringForm,
AtomType  atomType = AtomType::REGULAR 
)
explicit

Constructs an atom from a string.

Parameters
stringFormthe string form of the atom
atomTypethe type of the atom

Member Function Documentation

◆ atomType() [1/2]

AtomType knowrob::Atom::atomType ( ) const
inline
Returns
the type of this atom.

Definition at line 52 of file Atom.h.

52 { return atomType_; }

◆ atomType() [2/2]

AtomType knowrob::Atom::atomType ( ) const
inline
Returns
the type of this atom.

Definition at line 52 of file Atom.h.

52 { return atomType_; }

◆ isSameAtom() [1/2]

bool Atom::isSameAtom ( const Atom other) const
Parameters
otheranother atom
Returns
true if both atoms are equal

Definition at line 22 of file Atom.cpp.

22  {
23  // Atoms can also be constructed without adding them to the table,
24  // so we need to compare the string forms.
25  return stringForm_ == other.stringForm_;
26 }

◆ isSameAtom() [2/2]

bool knowrob::Atom::isSameAtom ( const Atom other) const
Parameters
otheranother atom
Returns
true if both atoms are equal

◆ stringForm() [1/2]

std::string_view knowrob::Atom::stringForm ( ) const
inlinefinalvirtual

Get the lexical form of this atomic term.

Returns
the lexical form of this atomic term.

Implements knowrob::Atomic.

Definition at line 55 of file Atom.h.

55 { return stringForm_; }

◆ stringForm() [2/2]

std::string_view knowrob::Atom::stringForm ( ) const
inlinefinalvirtual

Get the lexical form of this atomic term.

Returns
the lexical form of this atomic term.

Implements knowrob::Atomic.

Definition at line 55 of file Atom.h.

55 { return stringForm_; }

◆ table() [1/2]

Atom::AtomTable & Atom::table ( )
staticprotected

Definition at line 11 of file Atom.cpp.

11  {
12  static Atom::AtomTable theTable;
13  return theTable;
14 }
std::map< std::string, std::optional< std::weak_ptr< Atom > >, std::less<> > AtomTable
Definition: Atom.h:64

◆ table() [2/2]

static AtomTable& knowrob::Atom::table ( )
staticprotected

◆ Tabled() [1/2]

std::shared_ptr< knowrob::Atom > Atom::Tabled ( std::string_view  stringForm)
static
Parameters
stringFormthe string form of the atom
Returns
a shared pointer to an atom

Definition at line 40 of file Atom.cpp.

40  {
41  auto it = table().find(name);
42  if (it != table().end()) {
43  if (auto atomPtr = it->second.value().lock()) {
44  // Atom still exists, return it
45  return atomPtr;
46  }
47  table().erase(it);
48  }
49  // Atom does not exist or was destroyed, create a new one
50  auto inserted = table().emplace(name, std::nullopt);
51  auto &jt = inserted.first;
52  auto atom = std::make_shared<knowrob::Atom>(jt->first);
53  jt->second = atom;
54  auto locked = jt->second.value().lock();
55  if (!locked) {
56  throw std::runtime_error("Failed to lock Atom");
57  }
58  return locked;
59 }
static AtomTable & table()
Definition: Atom.cpp:11
StringRule & atom()
Definition: strings.cpp:54

◆ Tabled() [2/2]

static std::shared_ptr<knowrob::Atom> knowrob::Atom::Tabled ( std::string_view  stringForm)
static
Parameters
stringFormthe string form of the atom
Returns
a shared pointer to an atom

◆ write() [1/2]

void Atom::write ( std::ostream &  os) const
overridevirtual

Print this object to a stream.

Parameters
osthe stream to print to.

Implements knowrob::Printable.

Reimplemented in knowrob::IRIAtom, and knowrob::IRIAtom.

Definition at line 28 of file Atom.cpp.

28  {
29  if (stringForm_.empty()) {
30  os << "''";
31  } else if (std::islower(stringForm_[0]) &&
32  std::all_of(stringForm_.begin(), stringForm_.end(), ::isalnum)) {
33  // avoid single quotes
34  os << stringForm_;
35  } else {
36  os << '\'' << stringForm_ << '\'';
37  }
38 }

◆ write() [2/2]

void knowrob::Atom::write ( std::ostream &  os) const
overridevirtual

Print this object to a stream.

Parameters
osthe stream to print to.

Reimplemented from knowrob::Atomic.

Reimplemented in knowrob::IRIAtom.

Member Data Documentation

◆ atomType_

const AtomType knowrob::Atom::atomType_
protected

Definition at line 62 of file Atom.h.

◆ stringForm_

std::string_view knowrob::Atom::stringForm_
protected

Definition at line 61 of file Atom.h.


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