From a9600387ba59fea08b5313be9a5b1aa71191618c Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sat, 22 Dec 2012 00:39:23 +0100 Subject: [PATCH] refactor defaults-manager to use the reworked query interface --- src/common/query.hpp | 31 +++++++++++++++++---- src/common/query/defs-manager-impl.hpp | 2 +- src/common/query/defs-registry.hpp | 38 ++++++++++---------------- src/lib/query-text.hpp | 21 +++++++++++++- 4 files changed, 61 insertions(+), 31 deletions(-) diff --git a/src/common/query.hpp b/src/common/query.hpp index 46ef4ff01..e47f93019 100644 --- a/src/common/query.hpp +++ b/src/common/query.hpp @@ -307,13 +307,13 @@ namespace lumiera { * Implicitly convertible to and from Query instances. */ class QueryKey - : boost::totally_ordered< QueryKey> + : boost::totally_ordered { Goal::QueryID id_; lib::QueryText def_; public: - QueryKey (Goal::QueryID id, lib::QueryText def) + QueryKey (Goal::QueryID id, lib::QueryText q) : id_(id) , def_(q) { } @@ -323,20 +323,32 @@ namespace lumiera { template operator Query() const { - return Query::build().withConditions(def_); + REQUIRE (getResultTypeID() == id_.type); + return Query::build(id_.kind).withConditions(def_); } + operator string() const + { + return "kind=" + lexical_cast(id_.kind) + +",type=" + lexical_cast(id_.type) + +",def=" + string(def_); + } + + uint degree() const { - return def_.degree(); + return def_.degree_of_constriction(); } friend bool operator< (QueryKey const& q1, QueryKey const& q2) { - return q1.degree() < q2.degree(); + uint d1 = q1.degree(); + uint d2 = q2.degree(); + return d1 < d2 + ||(d1 == d2 && q1.def_ < q2.def_); } friend size_t @@ -424,6 +436,14 @@ namespace lumiera { + template + inline typename Query::Builder + Query::build (Kind queryType) + { + return Builder(defineQueryTypeID (queryType)); + } + + template inline typename Query::Builder Query::rebuild() const @@ -452,6 +472,7 @@ namespace lumiera { * ordered storage in sets, maps and for generation of metrics. */ template + inline Query::operator QueryKey() const { return QueryKey (this->id_, this->def_); diff --git a/src/common/query/defs-manager-impl.hpp b/src/common/query/defs-manager-impl.hpp index 005fc8774..2db01c395 100644 --- a/src/common/query/defs-manager-impl.hpp +++ b/src/common/query/defs-manager-impl.hpp @@ -52,7 +52,7 @@ using util::_Fmt; using proc::ConfigResolver; -using lumiera::query::QueryHandler; +using lumiera::query::QueryHandler; ///////TODO preliminary interface defined in config-rules.hpp using lumiera::query::LUMIERA_ERROR_CAPABILITY_QUERY; diff --git a/src/common/query/defs-registry.hpp b/src/common/query/defs-registry.hpp index 28f9af8a0..8ee58d40d 100644 --- a/src/common/query/defs-registry.hpp +++ b/src/common/query/defs-registry.hpp @@ -71,7 +71,6 @@ namespace query { using std::tr1::weak_ptr; using std::string; - using boost::format; using boost::lambda::_1; using boost::lambda::var; @@ -101,17 +100,15 @@ namespace query { template struct Record { - uint degree; - Query query; + QueryKey queryKey; weak_ptr objRef; + uint degree; Record (Query const& q, P const& obj) - : degree (lib::query::countPred ("TODO")),//q)),////////////////////////////////////////////////////////////////////////////////////////////TODO - query (q), - objRef (obj) - { - UNIMPLEMENTED("Query remolding");////////////////////////////////////////////////////////////////////////////////////////////TODO - } + : queryKey (q) + , objRef (obj) + , degree(queryKey.degree()) + { } struct Search ///< Functor searching for a specific object @@ -129,19 +126,13 @@ namespace query { } }; - struct OrderRelation - { - inline bool - operator() (Record one, Record two) ///< @note doesn't touch the objRef - { - UNIMPLEMENTED ("arbitrary total ordering of queries"); - return ( one.degree < two.degree - ||(one.degree == two.degree && false)//one.query < two.query)////////////////////////////////////////////////////////////////////////////////////////////TODO - ); - } - }; + friend bool + operator< (Record one, Record two) ///< @note doesn't touch the objRef + { + return one.queryKey < two.queryKey; + } - operator string () const { return dumpRecord % degree % query % dumpObj(); } + operator string () const { return dumpRecord % degree % queryKey % dumpObj(); } string dumpObj () const { P o (objRef.lock()); return o? string(*o):"dead"; } }; @@ -154,8 +145,7 @@ namespace query { struct Slot : public TableEntry { - typedef typename Record::OrderRelation Ordering; - typedef std::set, Ordering> Registry; + typedef std::set > Registry; Registry registry; static size_t index; ///< where to find this Slot in every Table @@ -306,7 +296,7 @@ namespace query { Registry& registry = Slot::access(table_); RIter pos = registry.lower_bound (entry); if ( pos!=registry.end() - && pos->query == query) + && pos->queryKey == query) { P storedObj (pos->objRef.lock()); if (storedObj) diff --git a/src/lib/query-text.hpp b/src/lib/query-text.hpp index 42441a388..6052f30af 100644 --- a/src/lib/query-text.hpp +++ b/src/lib/query-text.hpp @@ -46,6 +46,7 @@ #include "lib/error.hpp" #include "lib/hash-value.h" +#include "lib/query-util.hpp" #include "lib/util.hpp" #include @@ -93,11 +94,24 @@ namespace lib { } bool - hasAtom(string const& predSymbol) + hasAtom (string const& predSymbol) { return util::contains (definition_, predSymbol); } + /** synthetic total order to classify query definitions. + * Queries with more specific conditions should yield larger values. + * @warning this is rather a design idea and it's not clear + * if this metric can be made to work in practice + * @todo using a rather deaf placeholder implementation + * based just on counting the top level predicates. + */ + uint + degree_of_constriction() const + { + return query::countPred (definition_); + } + private: string normalise (string const& rawDefinition); @@ -107,6 +121,11 @@ namespace lib { { return q1.definition_ == q2.definition_; } + friend bool + operator< (QueryText const& q1, QueryText const& q2) + { + return q1.definition_ < q2.definition_; + } friend HashVal hash_value (QueryText const& entry); };