27 #include <SimoxUtility/algorithm/get_map_keys_values.h>
51 template <
class DerivedT>
58 return dynamic_cast<Predictive*
>(&base::detail::derived<DerivedT>(
this));
62 explicit Prediction(
const std::map<PredictionEngine, Predictor>& predictors = {})
70 _predictors.emplace(engine.
engineID, predictor);
72 if (
Predictive* predictive = this->_asPredictive())
74 predictive->addPredictionEngine(engine);
81 this->_predictors.clear();
82 for (
const auto& [engine, predictor] : predictors)
84 _predictors.emplace(engine.engineID, predictor);
87 if (
Predictive* predictive = this->_asPredictive())
89 predictive->setPredictionEngines(simox::alg::get_keys(predictors));
98 std::vector<PredictionResult>
101 const MemoryID ownID = base::detail::derived<DerivedT>(
this).id();
102 std::vector<PredictionResult> results;
103 for (
const auto& request : requests)
119 MemoryID ownID = base::detail::derived<DerivedT>(
this).id();
123 if (it != _predictors.end())
126 result = predictor(request);
131 std::stringstream sstream;
132 sstream <<
"Could not dispatch prediction request for " << request.
snapshotID
134 <<
"' in " << ownID <<
": Engine not registered.";
141 std::stringstream sstream;
142 sstream <<
"Could not dispatch prediction request for " << request.
snapshotID
143 <<
" to " <<
target <<
" from " << ownID;
150 std::map<std::string, Predictor> _predictors;
157 template <
class DerivedT>
168 std::vector<PredictionResult>
171 const auto& derivedThis = base::detail::derived<DerivedT>(
this);
172 const std::map<MemoryID, std::vector<PredictionEngine>> engines =
173 derivedThis.getAllPredictionEngines();
175 std::vector<PredictionResult> results;
182 armem::findMostSpecificEntryContainingIDAnd<std::vector<PredictionEngine>>(
183 engines, request.snapshotID,
185 const std::vector<PredictionEngine>& supported) ->
bool
189 if (engine.engineID == request.predictionSettings.predictionEngineID)
197 if (iter != engines.end())
199 const MemoryID& responsibleID = iter->first;
206 std::stringstream sstream;
207 sstream <<
"Could not find segment offering prediction engine '"
208 << request.predictionSettings.predictionEngineID <<
"' for memory ID "
209 << request.snapshotID <<
".";
227 const auto& derivedThis = base::detail::derived<DerivedT>(
this);
237 std::string childName = _getChildName(ownID,
target);
242 typename DerivedT::ChildT* child =
nullptr;
243 derivedThis.forEachChild(
244 [&child, &childName](
auto& otherChild)
246 if (otherChild.name() == childName)
253 result = child->dispatchTargetedPrediction(request,
target);
258 std::stringstream sstream;
259 sstream <<
"Could not find memory item with ID " <<
target <<
".";
266 std::stringstream sstream;
267 sstream <<
"Could not dispatch prediction request for " << request.
snapshotID
268 <<
" to " <<
target <<
" from " << ownID <<
".";
281 size_t parentLength = parent.
getItems().size();
284 std::vector<std::string> childItems = child.
getItems();
286 int index = parentLength;
289 return childItems[
index];