knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
DataSourceHandler.cpp
Go to the documentation of this file.
1 #include "knowrob/DataSourceHandler.h"
2 #include "knowrob/integration/python/utils.h"
3 
4 using namespace knowrob;
5 
6 void
7 DataSourceHandler::addDataHandler(const std::string &format, const std::function<bool(const DataSourcePtr &)> &fn) {
8  dataSourceHandler_[format] = fn;
9 }
10 
12  if (dataSource->format().empty()) {
13  return loadDataSourceWithUnknownFormat(dataSource);
14  } else {
15  auto it = dataSourceHandler_.find(dataSource->format());
16  if (it != dataSourceHandler_.end()) {
17  return it->second(dataSource);
18  } else {
19  return false;
20  }
21  }
22 }
23 
24 bool DataSourceHandler::hasDataHandler(const DataSourcePtr &dataSource) const {
25  return dataSourceHandler_.find(dataSource->format()) != dataSourceHandler_.end();
26 }
27 
28 namespace knowrob::py {
29  template<>
31  using namespace boost::python;
32  class_<DataSourceHandler, std::shared_ptr<DataSourceHandler>>("DataSourceHandler", init<>())
33  .def("addDataHandler", +[]
34  (DataSourceHandler &x, const std::string &format, object &fn) {
35  // when KnowRob calls the data handler, it does not hold the GIL
36  // so we need to acquire it before calling fn.
37  x.addDataHandler(format, [fn](const DataSourcePtr &dataSource) {
38  gil_lock lock;
39  return fn(dataSource);
40  });
41  })
42  .def("loadDataSource", &DataSourceHandler::loadDataSource);
43  }
44 }
void addDataHandler(const std::string &format, const DataSourceLoader &fn)
virtual bool loadDataSourceWithUnknownFormat(const DataSourcePtr &)
std::map< std::string, DataSourceLoader > dataSourceHandler_
bool loadDataSource(const DataSourcePtr &dataSource)
bool hasDataHandler(const DataSourcePtr &dataSource) const
FunctionRule & function()
Definition: terms.cpp:140
TermRule & string()
Definition: terms.cpp:63
void createType< DataSourceHandler >()
std::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:107