knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
Atomic.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of KnowRob, please consult
3  * https://github.com/knowrob/knowrob for license details.
4  */
5 
6 #include "knowrob/terms/Atomic.h"
7 #include "knowrob/terms/Atom.h"
8 #include "knowrob/terms/Numeric.h"
9 #include "knowrob/terms/String.h"
10 #include "knowrob/terms/IRIAtom.h"
11 #include "knowrob/semweb/Triple.h"
12 #include "knowrob/integration/python/utils.h"
13 
14 using namespace knowrob;
15 
16 bool Atomic::isSameAtomic(const Atomic &other) const {
17  if (atomicType() != other.atomicType()) {
18  return false;
19  }
20  switch (atomicType()) {
21  case AtomicType::ATOM:
22  return ((Atom *) this)->isSameAtom(*((Atom *) &other));
24  return ((Numeric *) this)->isSameNumeric(*((Numeric *) &other));
25  case AtomicType::STRING:
26  return ((String *) this)->isSameString(*((String *) &other));
27  }
28  return false;
29 }
30 
31 std::shared_ptr<Atomic> Atomic::makeTripleValue(const Triple &triple) {
32  if (triple.xsdType()) {
33  switch (triple.xsdType().value()) {
34  case XSDType::STRING:
35  return std::make_shared<String>(triple.valueAsString());
36  case XSDType::BOOLEAN:
37  return std::make_shared<Boolean>(triple.valueAsBoolean());
38  case XSDType::DOUBLE:
39  return std::make_shared<Double>(triple.valueAsDouble());
40  case XSDType::FLOAT:
41  return std::make_shared<Float>(triple.valueAsFloat());
43  case XSDType::INTEGER:
44  return std::make_shared<Integer>(triple.valueAsInt());
45  case XSDType::LONG:
46  return std::make_shared<Long>(triple.valueAsLong());
47  case XSDType::SHORT:
48  return std::make_shared<Short>(triple.valueAsShort());
50  return std::make_shared<UnsignedLong>(triple.valueAsUnsignedLong());
52  return std::make_shared<UnsignedInt>(triple.valueAsUnsignedInt());
54  return std::make_shared<UnsignedShort>(triple.valueAsUnsignedShort());
55  case XSDType::LAST:
56  break;
57  }
58  }
59  return IRIAtom::Tabled(triple.valueAsString());
60 }
61 
62 size_t Atomic::hashOfAtomic() const {
63  return std::hash<std::string_view>{}(stringForm());
64 }
65 
66 namespace knowrob::py {
67  template<>
69  using namespace boost::python;
70  enum_<AtomicType>("AtomicType")
71  .value("ATOM", AtomicType::ATOM)
72  .value("STRING", AtomicType::STRING)
73  .value("NUMERIC", AtomicType::NUMERIC)
74  .export_values();
75  class_<Atomic, std::shared_ptr<Atomic>, bases<Term>, boost::noncopyable>
76  ("Atomic", no_init)
77  .def("atomicType", &Atomic::atomicType)
78  .def("isSameAtomic", &Atomic::isSameAtomic);
79  }
80 }
static std::shared_ptr< Atomic > makeTripleValue(const Triple &triple)
Definition: Atomic.cpp:31
size_t hashOfAtomic() const
Definition: Atomic.cpp:62
bool isSameAtomic(const Atomic &other) const
Definition: Atomic.cpp:16
virtual std::string_view stringForm() const =0
AtomicType atomicType() const
Definition: Atomic.h:48
static std::shared_ptr< IRIAtom > Tabled(std::string_view stringForm)
Definition: IRIAtom.cpp:25
virtual std::string_view valueAsString() const =0
auto xsdType() const
Definition: Triple.h:64
virtual short valueAsShort() const =0
virtual unsigned int valueAsUnsignedInt() const =0
virtual double valueAsDouble() const =0
virtual float valueAsFloat() const =0
virtual long valueAsLong() const =0
virtual unsigned short valueAsUnsignedShort() const =0
virtual unsigned long valueAsUnsignedLong() const =0
virtual int valueAsInt() const =0
virtual bool valueAsBoolean() const =0
void createType< Atomic >()
Definition: Atomic.cpp:68