refactor defaults-manager to use the reworked query interface

This commit is contained in:
Fischlurch 2012-12-22 00:39:23 +01:00
parent 5cddc57932
commit a9600387ba
4 changed files with 61 additions and 31 deletions

View file

@ -307,13 +307,13 @@ namespace lumiera {
* Implicitly convertible to and from Query instances.
*/
class QueryKey
: boost::totally_ordered< QueryKey>
: boost::totally_ordered<QueryKey>
{
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<class RES>
operator Query<RES>() const
{
return Query<RES>::build().withConditions(def_);
REQUIRE (getResultTypeID<RES>() == id_.type);
return Query<RES>::build(id_.kind).withConditions(def_);
}
operator string() const
{
return "kind=" + lexical_cast<string>(id_.kind)
+",type=" + lexical_cast<string>(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<class RES>
inline typename Query<RES>::Builder
Query<RES>::build (Kind queryType)
{
return Builder(defineQueryTypeID (queryType));
}
template<class RES>
inline typename Query<RES>::Builder
Query<RES>::rebuild() const
@ -452,6 +472,7 @@ namespace lumiera {
* ordered storage in sets, maps and for generation of metrics.
*/
template<class RES>
inline
Query<RES>::operator QueryKey() const
{
return QueryKey (this->id_, this->def_);

View file

@ -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;

View file

@ -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<class TAR>
struct Record
{
uint degree;
Query<TAR> query;
QueryKey queryKey;
weak_ptr<TAR> objRef;
uint degree;
Record (Query<TAR> const& q, P<TAR> 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<TAR> o (objRef.lock()); return o? string(*o):"dead"; }
};
@ -154,8 +145,7 @@ namespace query {
struct Slot
: public TableEntry
{
typedef typename Record<TAR>::OrderRelation Ordering;
typedef std::set<Record<TAR>, Ordering> Registry;
typedef std::set<Record<TAR> > Registry;
Registry registry;
static size_t index; ///< where to find this Slot in every Table
@ -306,7 +296,7 @@ namespace query {
Registry& registry = Slot<TAR>::access(table_);
RIter pos = registry.lower_bound (entry);
if ( pos!=registry.end()
&& pos->query == query)
&& pos->queryKey == query)
{
P<TAR> storedObj (pos->objRef.lock());
if (storedObj)

View file

@ -46,6 +46,7 @@
#include "lib/error.hpp"
#include "lib/hash-value.h"
#include "lib/query-util.hpp"
#include "lib/util.hpp"
#include <string>
@ -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);
};