knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
XSDAtomic.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 "map"
7 #include "knowrob/terms/XSDAtomic.h"
8 #include "knowrob/terms/String.h"
9 #include "knowrob/terms/Numeric.h"
10 #include "knowrob/semweb/xsd.h"
11 #include "knowrob/Logger.h"
12 #include <boost/python.hpp>
13 #include "knowrob/integration/python/utils.h"
14 
15 using namespace knowrob;
16 
17 std::shared_ptr<XSDAtomic> XSDAtomic::create(std::string_view lexicalForm, std::string_view xsdTypeIRI) {
19  switch (type) {
20  case XSDType::STRING:
21  return std::make_shared<String>(lexicalForm);
22  case XSDType::BOOLEAN:
23  return std::make_shared<Boolean>(lexicalForm);
24  case XSDType::DOUBLE:
25  return std::make_shared<Double>(lexicalForm);
26  case XSDType::FLOAT:
27  return std::make_shared<Float>(lexicalForm);
29  case XSDType::INTEGER:
30  return std::make_shared<Integer>(lexicalForm);
31  case XSDType::LONG:
32  return std::make_shared<Long>(lexicalForm);
33  case XSDType::SHORT:
34  return std::make_shared<Short>(lexicalForm);
36  return std::make_shared<UnsignedLong>(lexicalForm);
38  return std::make_shared<UnsignedInt>(lexicalForm);
40  return std::make_shared<UnsignedShort>(lexicalForm);
41  default:
42  KB_ERROR("Unknown XSD type {} treated as string.", static_cast<int>(type));
43  return std::make_shared<String>(lexicalForm);
44  }
45 }
46 
47 namespace knowrob {
48  XSDType xsdTypeFromIRI(std::string_view iri) {
49  static std::map<std::string_view, XSDType> typeIRIs = {
50  {xsd::IRI_string->stringForm(), XSDType::STRING},
51  {xsd::IRI_anyURI->stringForm(), XSDType::STRING},
52  {xsd::IRI_boolean->stringForm(), XSDType::BOOLEAN},
53  {xsd::IRI_double->stringForm(), XSDType::DOUBLE},
54  {xsd::IRI_float->stringForm(), XSDType::FLOAT},
55  {xsd::IRI_integer->stringForm(), XSDType::INTEGER},
56  {xsd::IRI_int->stringForm(), XSDType::INTEGER},
58  {xsd::IRI_long->stringForm(), XSDType::LONG},
59  {xsd::IRI_short->stringForm(), XSDType::SHORT},
63  };
64  auto it = typeIRIs.find(iri);
65  if (it != typeIRIs.end()) return it->second;
66  KB_WARN("Unknown XSD type IRI {} treated as string.", iri);
67  return XSDType::STRING;
68  }
69 
70  std::string_view xsdTypeToIRI(XSDType type) {
71  static std::map<XSDType, std::string_view> typeIRIs = {
72  {XSDType::STRING, xsd::IRI_string->stringForm()},
73  {XSDType::BOOLEAN, xsd::IRI_boolean->stringForm()},
74  {XSDType::DOUBLE, xsd::IRI_double->stringForm()},
75  {XSDType::FLOAT, xsd::IRI_float->stringForm()},
76  {XSDType::INTEGER, xsd::IRI_integer->stringForm()},
78  {XSDType::LONG, xsd::IRI_long->stringForm()},
79  {XSDType::SHORT, xsd::IRI_short->stringForm()},
83  };
84  auto it = typeIRIs.find(type);
85  if (it != typeIRIs.end()) return it->second;
86  KB_WARN("Unknown XSD type {} treated as string.", static_cast<int>(type));
87  return xsd::IRI_string->stringForm();
88  }
89 
90  std::string_view XSDAtomic::xsdTypeIRI() const {
91  return xsdTypeToIRI(xsdType());
92  }
93 }
94 
95 namespace knowrob::py {
96  template<>
98  using namespace boost::python;
99  enum_<XSDType>("XSDType")
100  .value("STRING", XSDType::STRING)
101  .value("BOOLEAN", XSDType::BOOLEAN)
102  .value("DOUBLE", XSDType::DOUBLE)
103  .value("FLOAT", XSDType::FLOAT)
104  .value("INTEGER", XSDType::INTEGER)
105  .value("LONG", XSDType::LONG)
106  .value("SHORT", XSDType::SHORT)
107  .value("UNSIGNED_LONG", XSDType::UNSIGNED_LONG)
108  .value("UNSIGNED_INT", XSDType::UNSIGNED_INT)
109  .value("UNSIGNED_SHORT", XSDType::UNSIGNED_SHORT)
110  .export_values();
111  class_<XSDAtomic, std::shared_ptr<XSDAtomic>, bases<Atomic, RDFNode>, boost::noncopyable>
112  ("XSDAtomic", no_init)
113  .def("xsdTypeIRI", &XSDAtomic::xsdTypeIRI)
114  .def("xsdType", &XSDAtomic::xsdType)
115  .def("create", &XSDAtomic::create);
116  }
117 }
#define KB_ERROR
Definition: Logger.h:28
#define KB_WARN
Definition: Logger.h:27
static std::shared_ptr< XSDAtomic > create(std::string_view lexicalForm, std::string_view xsdTypeIRI)
Definition: XSDAtomic.cpp:17
std::string_view xsdTypeIRI() const
Definition: XSDAtomic.cpp:90
XSDType xsdType() const
Definition: XSDAtomic.h:43
void createType< XSDAtomic >()
Definition: XSDAtomic.cpp:97
const IRIAtomPtr type
Definition: rdf.h:15
const IRIAtomPtr IRI_integer
Definition: xsd.h:22
const IRIAtomPtr IRI_string
Definition: xsd.h:16
const IRIAtomPtr IRI_short
Definition: xsd.h:30
const IRIAtomPtr IRI_boolean
Definition: xsd.h:34
const IRIAtomPtr IRI_long
Definition: xsd.h:28
const IRIAtomPtr IRI_int
Definition: xsd.h:21
const IRIAtomPtr IRI_unsignedShort
Definition: xsd.h:31
const IRIAtomPtr IRI_unsignedLong
Definition: xsd.h:29
const IRIAtomPtr IRI_unsignedInt
Definition: xsd.h:27
const IRIAtomPtr IRI_float
Definition: xsd.h:38
const IRIAtomPtr IRI_anyURI
Definition: xsd.h:15
const IRIAtomPtr IRI_nonNegativeInteger
Definition: xsd.h:25
const IRIAtomPtr IRI_double
Definition: xsd.h:37
std::string_view xsdTypeToIRI(XSDType type)
Definition: XSDAtomic.cpp:70
XSDType xsdTypeFromIRI(std::string_view iri)
Definition: XSDAtomic.cpp:48
IRIAtomPtr iri(std::string_view ns, std::string_view name)
Definition: IRIAtom.cpp:62
XSDType
The XSDType enum Enumeration of the XSD types.
Definition: XSDType.h:16