MemoryQueryProcessorBase.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <regex>
4 
5 #include <RobotAPI/interface/armem/query.h>
7 
9 
11 {
12 
13  template <class _MemoryT, class _ResultMemoryT, class _ChildProcessorT>
15  public BaseQueryProcessorBase<_MemoryT, _ResultMemoryT, armem::query::data::MemoryQuery>
16  {
17  protected:
18  using Base =
20 
21  public:
22  using MemoryT = _MemoryT;
24 
25  using ResultMemoryT = _ResultMemoryT;
27 
28  using ChildProcessorT = _ChildProcessorT;
29 
30 
31  public:
33  {
34  }
35 
37  {
38  }
39 
40  using Base::process;
41 
43  process(const armem::query::data::Input& input, const MemoryT& memory) const
44  {
45  return this->process(input.memoryQueries, memory);
46  }
47 
48  virtual void
50  const armem::query::data::MemoryQuery& query,
51  const MemoryT& memory) const override
52  {
53  if (auto q = dynamic_cast<const armem::query::data::memory::All*>(&query))
54  {
55  process(result, *q, memory);
56  }
57  else if (auto q = dynamic_cast<const armem::query::data::memory::Single*>(&query))
58  {
59  process(result, *q, memory);
60  }
61  else if (auto q = dynamic_cast<const armem::query::data::memory::Regex*>(&query))
62  {
63  process(result, *q, memory);
64  }
65  else
66  {
67  throw armem::error::UnknownQueryType(MemoryT::getLevelName(), query);
68  }
69  }
70 
71  virtual void
73  const armem::query::data::memory::All& query,
74  const MemoryT& memory) const
75  {
76  memory.forEachCoreSegment([this, &result, &query](const CoreSegmentT& coreSegment)
77  { this->_processResult(result, coreSegment, query); });
78  }
79 
80  virtual void
82  const armem::query::data::memory::Single& query,
83  const MemoryT& memory) const
84  {
85  if (auto coreSegment = memory.findCoreSegment(query.coreSegmentName))
86  {
87  this->_processResult(result, *coreSegment, query);
88  }
89  }
90 
91  virtual void
93  const armem::query::data::memory::Regex& query,
94  const MemoryT& memory) const
95  {
96  const std::regex regex(query.coreSegmentNameRegex);
97  memory.forEachCoreSegment(
98  [this, &result, &query, &regex](const CoreSegmentT& coreSegment)
99  {
100  if (std::regex_search(coreSegment.name(), regex))
101  {
102  this->_processResult(result, coreSegment, query);
103  }
104  });
105  }
106 
107 
108  protected:
109  virtual bool
110  _processAllowed(const armem::query::data::MemoryQuery& query) const
111  {
112  // always execute query. Override if you want to execute the quey only if a special condition is fulfilled (e.g. querytargets)
113  return true;
114  }
115 
116  void
118  const CoreSegmentT& coreSegment,
119  const armem::query::data::MemoryQuery& query) const
120  {
121  ResultCoreSegmentT* child = result.findCoreSegment(coreSegment.name());
122  if (child == nullptr)
123  {
124  child = &result.addCoreSegment(coreSegment.name(), coreSegment.aronType());
125  }
126  childProcessor.process(*child, query.coreSegmentQueries, coreSegment);
127  }
128 
129 
130  protected:
132  };
133 } // namespace armarx::armem::server::query_proc::base
armarx::armem::server::query_proc::base::MemoryQueryProcessorBase::_processAllowed
virtual bool _processAllowed(const armem::query::data::MemoryQuery &query) const
Definition: MemoryQueryProcessorBase.h:110
armarx::armem::server::query_proc::base::MemoryQueryProcessorBase::process
virtual void process(ResultMemoryT &result, const armem::query::data::memory::Single &query, const MemoryT &memory) const
Definition: MemoryQueryProcessorBase.h:81
armarx::armem::server::query_proc::base::MemoryQueryProcessorBase::process
virtual void process(ResultMemoryT &result, const armem::query::data::memory::Regex &query, const MemoryT &memory) const
Definition: MemoryQueryProcessorBase.h:92
armarx::armem::server::query_proc::base
Definition: BaseQueryProcessorBase.cpp:6
armarx::armem::server::query_proc::base::MemoryQueryProcessorBase< armem::server::ltm::Memory, armem::wm::Memory, CoreSegmentQueryProcessor >::CoreSegmentT
typename MemoryT::CoreSegmentT CoreSegmentT
Definition: MemoryQueryProcessorBase.h:23
armarx::armem::base::MemoryBase::findCoreSegment
CoreSegmentT * findCoreSegment(const std::string &name)
Definition: MemoryBase.h:119
BaseQueryProcessorBase.h
armarx::armem::server::query_proc::base::MemoryQueryProcessorBase::ResultMemoryT
_ResultMemoryT ResultMemoryT
Definition: MemoryQueryProcessorBase.h:25
armarx::armem::server::query_proc::base::BaseQueryProcessorBase
Base class for memory query processors.
Definition: BaseQueryProcessorBase.h:21
armarx::armem::server::query_proc::base::MemoryQueryProcessorBase::MemoryQueryProcessorBase
MemoryQueryProcessorBase()
Definition: MemoryQueryProcessorBase.h:32
armarx::memory
Brief description of class memory.
Definition: memory.h:39
armarx::armem::server::ltm::Memory
A memory storing data on the hard drive and in mongodb (needs 'armarx memory start' to start the mong...
Definition: Memory.h:19
armarx::armem::server::query_proc::base::MemoryQueryProcessorBase::process
virtual void process(ResultMemoryT &result, const armem::query::data::memory::All &query, const MemoryT &memory) const
Definition: MemoryQueryProcessorBase.h:72
armarx::armem::server::query_proc::base::MemoryQueryProcessorBase::process
ResultMemoryT process(const armem::query::data::Input &input, const MemoryT &memory) const
Definition: MemoryQueryProcessorBase.h:43
armarx::armem::server::query_proc::base::MemoryQueryProcessorBase::process
virtual void process(ResultMemoryT &result, const armem::query::data::MemoryQuery &query, const MemoryT &memory) const override
Process the query and populate result.
Definition: MemoryQueryProcessorBase.h:49
armarx::armem::server::query_proc::base::MemoryQueryProcessorBase::MemoryQueryProcessorBase
MemoryQueryProcessorBase(ChildProcessorT &&childProcessor)
Definition: MemoryQueryProcessorBase.h:36
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:19
error.h
armarx::armem::wm::Memory
Client-side working memory.
Definition: memory_definitions.h:133
armarx::armem::base::MemoryBase::addCoreSegment
CoreSegmentT & addCoreSegment(const std::string &name, aron::type::ObjectPtr coreSegmentType=nullptr, const std::vector< PredictionEngine > &predictionEngines={})
Add an empty core segment with the given name, type and prediction engines.
Definition: MemoryBase.h:259
q
#define q
armarx::armem::server::query_proc::base::MemoryQueryProcessorBase::_processResult
void _processResult(ResultMemoryT &result, const CoreSegmentT &coreSegment, const armem::query::data::MemoryQuery &query) const
Definition: MemoryQueryProcessorBase.h:117
armarx::armem::server::query_proc::base::MemoryQueryProcessorBase< armem::server::ltm::Memory, armem::wm::Memory, CoreSegmentQueryProcessor >::ChildProcessorT
CoreSegmentQueryProcessor ChildProcessorT
Definition: MemoryQueryProcessorBase.h:28
armarx::armem::server::ltm::CoreSegment
Definition: CoreSegment.h:13
armarx::armem::server::query_proc::base::MemoryQueryProcessorBase
Definition: MemoryQueryProcessorBase.h:14
armarx::armem::base::MemoryBase< CoreSegment, Memory >::CoreSegmentT
CoreSegment CoreSegmentT
Definition: MemoryBase.h:37
armarx::armem::server::query_proc::base::BaseQueryProcessorBase< _MemoryT, _ResultMemoryT, armem::query::data::MemoryQuery >::process
_ResultMemoryT process(const armem::query::data::MemoryQuery &query, const _MemoryT &data) const
Definition: BaseQueryProcessorBase.h:33
armarx::armem::error::UnknownQueryType
Indicates that an entity's history was queried, but is empty.
Definition: ArMemError.h:173
armarx::armem::server::query_proc::base::MemoryQueryProcessorBase< armem::server::ltm::Memory, armem::wm::Memory, CoreSegmentQueryProcessor >::ResultCoreSegmentT
typename ResultMemoryT::CoreSegmentT ResultCoreSegmentT
Definition: MemoryQueryProcessorBase.h:26
armarx::armem::server::query_proc::base::MemoryQueryProcessorBase::childProcessor
ChildProcessorT childProcessor
Definition: MemoryQueryProcessorBase.h:131