27 #include <SimoxUtility/algorithm/get_map_keys_values.h>
50 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;
156 template <
class DerivedT>
167 std::vector<PredictionResult>
170 const auto& derivedThis = base::detail::derived<DerivedT>(
this);
171 const std::map<MemoryID, std::vector<PredictionEngine>> engines =
172 derivedThis.getAllPredictionEngines();
174 std::vector<PredictionResult> results;
181 armem::findMostSpecificEntryContainingIDAnd<std::vector<PredictionEngine>>(
185 const std::vector<PredictionEngine>& supported) ->
bool
189 if (engine.engineID ==
190 request.predictionSettings.predictionEngineID)
198 if (iter != engines.end())
200 const MemoryID& responsibleID = iter->first;
207 std::stringstream sstream;
208 sstream <<
"Could not find segment offering prediction engine '"
209 << request.predictionSettings.predictionEngineID <<
"' for memory ID "
210 << 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];