77#ifndef LUMIERA_ADVICE_INDEX_H
78#define LUMIERA_ADVICE_INDEX_H
90#include <unordered_map>
98 using std::placeholders::_1;
99 using std::unordered_map;
145 : pair<Binding::Matcher, POA*>
149 : pair<
Binding::Matcher, POA*> (elm.getMatcher(), &elm)
155 operator string() const
157 return "E-" +
hash_value(this->first) +
"--> "+ this->second ;
163 return a.second == b.second;
169 return a.second == &p;
191 REQUIRE (!
contains (elm),
"Duplicate entry");
194 catch(std::bad_alloc&)
196 throw error::Fatal(
"AdviceSystem failure due to exhausted memory");
204 REQUIRE (pos!=
elms_.end(),
"Attempt to overwrite an entry which isn't there.");
205 REQUIRE_IF (&oldRef != &newElm, !
contains (newElm),
"Duplicate entry");
207 *pos =
Entry(newElm);
209 REQUIRE_IF (&oldRef != &newElm, !
contains (oldRef),
"Duplicate entry");
216 if (pos!=
elms_.end())
219 ENSURE (!
contains (refElm),
"Duplicate entry");
226 if (*i == refElm)
return true;
230 operator string() const
232 string dump{
"elmList("+toString(
elms_.size())+
")\n"};
233 for (
auto const& entry :
elms_)
234 dump +=
"E...:"+entry+
"\n";
241 return eachElm (
elms_);
252 using RIter = EntryList::reverse_iterator;
254 for (RIter ii=this->
elms_.rbegin();
255 ii!=this->
elms_.rend();
257 if (ii->first.matches (pattern))
270 requestElm.setSolution (solution);
272 requestElm.setSolution (
NULL );
285 ii!=this->
elms_.end();
287 if (pattern.matches (ii->first))
290 ii->second->setSolution (&provisionElm);
298 ii!=this->
elms_.end();
300 if (pattern.matches (ii->first))
312 ii!=this->
elms_.end();
314 if (newPattern.
matches (ii->first))
315 ii->second->setSolution (&newProv);
317 if (oldPattern.
matches (ii->first))
326 typedef unordered_map<HashVal, RequestCluster>
RTable;
327 typedef unordered_map<HashVal, ProvisionCluster>
PTable;
417 WARN (library,
"Purging Advice Binding Index...");
498 :
error::Fatal {string(
"Failed test: ")+failure
499 ,LUMIERA_ERROR_INDEX_CORRUPTED}
517 using RTIter = RTable::const_iterator;
518 using PTIter = PTable::const_iterator;
521 for (PTIter ii =provisionEntries_.begin();
522 ii != provisionEntries_.end(); ++ii)
524 HashVal
hash (ii->first);
525 Cluster& clu = unConst(ii->second);
528 for (RTIter ii=requestEntries_.begin();
529 ii != requestEntries_.end(); ++ii)
531 HashVal
hash (ii->first);
532 Cluster& clu = unConst(ii->second);
538 catch(SelfCheckFailure& failure)
541 ERROR (library,
"%s", failure.what());
548#define VERIFY(_CHECK_, DESCRIPTION) \
550 throw SelfCheckFailure ((DESCRIPTION));
558 VERIFY (e.second,
"Invalid Entry: back-link is NULL");
565 verify_Entry (e,
hash);
566 POA& request = *(e.second);
567 const POA* solution (request.getSolution());
568 if (solution and hasProvision (*solution))
570 POA* currentSolution = provisionEntries_[
hash].find_latest_solution (request);
571 VERIFY (e.first.matches (solution->getMatcher()),
"stored advice solution not supported by binding match");
572 VERIFY (
bool(currentSolution),
"unable to reproduce stored solution with the current provisions")
573 VERIFY (solution == currentSolution,
"stored advice solution isn't the topmost solution for this request")
Accessing a STL element range through a Lumiera forward iterator, An instance of this iterator adapte...
Functor object for matching against another Binding.
bool matches(Binding const &obi) const
Conjunction of predicates to be matched against a collaboration partner for establishing an Advice co...
Index datastructure for organising advice solutions.
EntryList::iterator EIter
unordered_map< HashVal, ProvisionCluster > PTable
void verify_Request(Entry const &, HashVal) const
void verify_Entry(Entry const &, HashVal) const
size_t provision_count() const
bool isValid() const
validity self-check
bool hasRequest(POA const &refEntry) const
size_t request_count() const
void modifyRequest(HashVal oKey, POA &entry)
static size_t sumClusters(IT ii)
internal: sum element count over all clusters in the given hashtable
void addRequest(POA &entry)
unordered_map< HashVal, RequestCluster > RTable
void modifyProvision(POA const &oldRef, POA &newEntry)
void removeProvision(POA const &refEntry)
vector< Entry > EntryList
void addProvision(POA &entry)
bool hasProvision(POA const &refEntry) const
void removeRequest(POA const &refEntry)
Derived specific exceptions within Lumiera's exception hierarchy.
A pattern to define and identify a specific attachment to the Advice system.
lumiera_err lumiera_error(void)
Get and clear current error state.
#define LUMIERA_ERROR_DEFINE(err, msg)
Definition and initialisation of an error constant.
Lumiera error handling (C++ interface).
#define VERIFY(_CHECK_, DESCRIPTION)
Preconfigured adapters for some STL container standard usage situations.
This header is for including and configuring NoBug.
_SeqT< CON >::Range eachElm(CON &coll)
_MapT< MAP >::ValIter eachVal(MAP &map)
HashVal hash_value(Binding::Matcher const &bm)
LumieraError< LERR_(FATAL), Logic > Fatal
Lumiera public interface.
constexpr IT find(IT, IT, V const &)
bool contains(MAP &map, typename MAP::key_type const &key)
shortcut for containment test on a map
std::string toString(TY const &val) noexcept
get some string representation of any object, reliably.
OBJ * unConst(const OBJ *)
shortcut to save some typing when having to define const and non-const variants of member functions
disable_if< can_IterForEach< Container >, FUN > for_each(Container const &coll, FUN doIt)
operate on all elements of a STL container.
void remove(POA const &refElm)
bool contains(POA const &refElm)
lib::RangeIter< EIter > allElms()
void overwrite(POA const &oldRef, POA &newElm)
friend bool operator==(Entry const &a, Entry const &b)
POA * find_latest_solution(POA &requestElm)
void publish_latest_solution(POA &requestElm)
void publish_all_solutions(POA &provisionElm)
void retract_all_solutions(POA const &oldProv, ProvisionCluster &possibleReplacementSolutions)
void rewrite_all_solutions(POA const &oldProv, POA &newProv, ProvisionCluster &possibleReplacementSolutions)
SelfCheckFailure(Literal failure)
Marker types to indicate a literal string and a Symbol.
Perform operations "for each element" of a collection.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...