2009-10-14 05:48:24 +02:00
|
|
|
/*
|
|
|
|
|
QUERY-RESOLVER.hpp - interface for discovering contents of a scope
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-10-14 05:48:24 +02:00
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2009, Hermann Vosseler <Ichthyostega@web.de>
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-10-14 05:48:24 +02:00
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License as
|
2010-12-17 23:28:49 +01:00
|
|
|
published by the Free Software Foundation; either version 2 of
|
|
|
|
|
the License, or (at your option) any later version.
|
|
|
|
|
|
2009-10-14 05:48:24 +02:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-10-14 05:48:24 +02:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
2009-10-14 05:48:24 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2011-12-02 17:50:44 +01:00
|
|
|
#ifndef PROC_MOBJECT_SESSION_QUERY_RESOLVER_H
|
|
|
|
|
#define PROC_MOBJECT_SESSION_QUERY_RESOLVER_H
|
2009-10-14 05:48:24 +02:00
|
|
|
|
|
|
|
|
//#include "proc/mobject/mobject.hpp"
|
|
|
|
|
//#include "proc/mobject/placement.hpp"
|
2009-10-25 16:18:53 +01:00
|
|
|
#include "lib/bool-checkable.hpp"
|
2009-10-24 00:23:22 +02:00
|
|
|
#include "lib/typed-counter.hpp"
|
2009-10-17 21:31:03 +02:00
|
|
|
#include "lib/iter-adapter.hpp"
|
2012-07-12 03:00:12 +02:00
|
|
|
#include "lib/nocopy.hpp"
|
2009-10-24 00:23:22 +02:00
|
|
|
#include "lib/util.hpp"
|
2009-10-14 05:48:24 +02:00
|
|
|
|
2012-11-25 02:04:19 +01:00
|
|
|
#include <boost/lexical_cast.hpp>
|
2009-10-30 18:37:08 +01:00
|
|
|
#include <boost/noncopyable.hpp>
|
2009-10-26 01:39:25 +01:00
|
|
|
#include <boost/scoped_ptr.hpp>
|
2009-10-30 20:33:44 +01:00
|
|
|
#include <tr1/functional>
|
2009-10-25 16:18:53 +01:00
|
|
|
#include <tr1/memory>
|
2009-10-14 05:48:24 +02:00
|
|
|
//#include <vector>
|
2009-11-04 04:56:25 +01:00
|
|
|
#include <string>
|
2009-10-14 05:48:24 +02:00
|
|
|
|
|
|
|
|
//using std::vector;
|
|
|
|
|
//using std::string;
|
|
|
|
|
|
2012-11-25 02:29:52 +01:00
|
|
|
namespace lib {
|
2009-10-14 05:48:24 +02:00
|
|
|
|
2011-12-02 17:50:44 +01:00
|
|
|
using lib::IxID;
|
2009-10-24 00:23:22 +02:00
|
|
|
using util::unConst;
|
2009-10-30 18:37:08 +01:00
|
|
|
using boost::noncopyable;
|
2012-11-25 02:29:52 +01:00
|
|
|
using boost::lexical_cast;
|
2009-10-30 20:33:44 +01:00
|
|
|
using boost::scoped_ptr;
|
|
|
|
|
using std::tr1::function;
|
2009-11-04 04:56:25 +01:00
|
|
|
using std::string;
|
2009-10-24 00:23:22 +02:00
|
|
|
|
|
|
|
|
|
2009-10-25 16:18:53 +01:00
|
|
|
class Goal;
|
2009-10-29 04:32:00 +01:00
|
|
|
class Resolution;
|
2009-10-24 00:23:22 +02:00
|
|
|
class QueryResolver;
|
2009-10-26 01:39:25 +01:00
|
|
|
class QueryDispatcher;
|
2009-10-24 00:23:22 +02:00
|
|
|
|
2009-10-25 16:18:53 +01:00
|
|
|
/** Allow for taking ownership of a result set */
|
2009-10-29 04:32:00 +01:00
|
|
|
typedef std::tr1::shared_ptr<Resolution> PReso;
|
2009-11-15 16:28:42 +01:00
|
|
|
|
|
|
|
|
|
2009-10-17 21:31:03 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* TODO type comment
|
2009-10-24 00:23:22 +02:00
|
|
|
* Query ABC
|
2009-10-17 21:31:03 +02:00
|
|
|
*/
|
2009-10-24 00:23:22 +02:00
|
|
|
class Goal
|
2012-07-12 03:00:12 +02:00
|
|
|
: util::no_copy_by_client
|
2009-10-17 21:31:03 +02:00
|
|
|
{
|
|
|
|
|
public:
|
2009-10-25 21:39:02 +01:00
|
|
|
virtual ~Goal() ;
|
2009-10-17 21:31:03 +02:00
|
|
|
|
2009-10-24 00:23:22 +02:00
|
|
|
enum Kind
|
|
|
|
|
{ GENERIC = 0
|
|
|
|
|
, DISCOVERY
|
|
|
|
|
};
|
2009-10-17 21:31:03 +02:00
|
|
|
|
2009-10-24 00:23:22 +02:00
|
|
|
struct QueryID
|
|
|
|
|
{
|
|
|
|
|
Kind kind;
|
2011-12-02 17:50:44 +01:00
|
|
|
IxID type;
|
2009-10-24 00:23:22 +02:00
|
|
|
};
|
2009-10-17 21:31:03 +02:00
|
|
|
|
2009-11-01 03:47:35 +01:00
|
|
|
QueryID const&
|
|
|
|
|
getQID() const
|
|
|
|
|
{
|
|
|
|
|
return id_;
|
|
|
|
|
}
|
2009-10-17 21:31:03 +02:00
|
|
|
|
|
|
|
|
|
2009-10-30 18:37:08 +01:00
|
|
|
/**
|
|
|
|
|
* Single Solution, possibly part of a result set.
|
|
|
|
|
* A pointer-like object, usually to be down-casted
|
|
|
|
|
* to a specifically typed Query::Cursor
|
|
|
|
|
* @see Resolution
|
|
|
|
|
*/
|
2009-10-24 00:23:22 +02:00
|
|
|
class Result
|
2009-10-25 16:18:53 +01:00
|
|
|
: public lib::BoolCheckable<Result>
|
2009-10-24 00:23:22 +02:00
|
|
|
{
|
2009-11-13 16:32:22 +01:00
|
|
|
void* cur_;
|
2009-10-24 00:23:22 +02:00
|
|
|
|
|
|
|
|
protected:
|
2009-10-26 01:39:25 +01:00
|
|
|
void point_at(void* p) { cur_ = p; }
|
2009-10-25 16:18:53 +01:00
|
|
|
|
2009-10-24 00:23:22 +02:00
|
|
|
template<typename RES>
|
2009-10-25 16:18:53 +01:00
|
|
|
RES&
|
|
|
|
|
access()
|
2009-10-24 00:23:22 +02:00
|
|
|
{
|
|
|
|
|
REQUIRE (cur_);
|
|
|
|
|
return *reinterpret_cast<RES*> (cur_);
|
|
|
|
|
}
|
2009-10-25 16:18:53 +01:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
bool isValid() const { return bool(cur_); }
|
|
|
|
|
|
|
|
|
|
Result() : cur_(0) { } ///< create an NIL result
|
2009-10-24 00:23:22 +02:00
|
|
|
};
|
2009-10-17 21:31:03 +02:00
|
|
|
|
2009-10-25 16:18:53 +01:00
|
|
|
|
2009-10-17 21:31:03 +02:00
|
|
|
|
|
|
|
|
protected:
|
2009-10-24 00:23:22 +02:00
|
|
|
QueryID id_;
|
|
|
|
|
|
|
|
|
|
Goal (QueryID qid)
|
2009-10-29 04:32:00 +01:00
|
|
|
: id_(qid)
|
2009-10-24 00:23:22 +02:00
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2009-11-17 03:01:18 +01:00
|
|
|
inline bool
|
|
|
|
|
operator== (Goal::QueryID const& id1, Goal::QueryID const& id2)
|
|
|
|
|
{
|
|
|
|
|
return id1.kind == id2.kind
|
|
|
|
|
&& id1.type == id2.type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
|
operator!= (Goal::QueryID const& id1, Goal::QueryID const& id2)
|
|
|
|
|
{
|
|
|
|
|
return ! (id1 == id2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-10-24 00:23:22 +02:00
|
|
|
|
|
|
|
|
/** Context used for generating type-IDs to denote
|
|
|
|
|
* the specific result types of issued queries */
|
|
|
|
|
typedef lib::TypedContext<Goal::Result> ResultType;
|
|
|
|
|
|
2009-10-30 18:37:08 +01:00
|
|
|
template<typename RES>
|
2011-12-02 17:50:44 +01:00
|
|
|
inline IxID
|
2009-10-30 18:37:08 +01:00
|
|
|
getResultTypeID() ///< @return unique ID denoting result type RES
|
|
|
|
|
{
|
|
|
|
|
return ResultType::ID<RES>::get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-24 00:23:22 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* TODO type comment
|
|
|
|
|
* Concrete query to yield specifically typed result elements
|
|
|
|
|
*/
|
|
|
|
|
template<class RES>
|
|
|
|
|
class Query
|
|
|
|
|
: public Goal
|
|
|
|
|
{
|
2009-11-01 03:47:35 +01:00
|
|
|
protected:
|
2009-10-24 00:23:22 +02:00
|
|
|
static QueryID
|
2009-11-01 03:47:35 +01:00
|
|
|
defineQueryTypeID (Kind queryType = Goal::GENERIC)
|
2009-10-24 00:23:22 +02:00
|
|
|
{
|
2009-11-01 03:47:35 +01:00
|
|
|
QueryID id = {queryType, getResultTypeID<RES>() };
|
2009-10-24 00:23:22 +02:00
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-25 02:04:19 +01:00
|
|
|
class Builder;
|
|
|
|
|
|
2009-10-24 00:23:22 +02:00
|
|
|
public:
|
|
|
|
|
Query()
|
|
|
|
|
: Goal (defineQueryTypeID())
|
|
|
|
|
{ }
|
|
|
|
|
|
2012-11-25 02:29:52 +01:00
|
|
|
static Builder
|
2012-11-25 02:04:19 +01:00
|
|
|
build (Kind queryType = Goal::GENERIC);
|
|
|
|
|
|
2009-10-30 18:37:08 +01:00
|
|
|
|
2009-10-24 00:23:22 +02:00
|
|
|
/* results retrieval */
|
2009-10-25 16:18:53 +01:00
|
|
|
class Cursor
|
|
|
|
|
: public Goal::Result
|
2009-10-24 00:23:22 +02:00
|
|
|
{
|
|
|
|
|
public:
|
2009-10-29 21:59:02 +01:00
|
|
|
typedef RES value_type;
|
|
|
|
|
typedef RES& reference;
|
|
|
|
|
typedef RES* pointer;
|
|
|
|
|
|
2009-10-26 01:39:25 +01:00
|
|
|
RES& operator* () { return access<RES>(); }
|
|
|
|
|
RES* operator->() { return & access<RES>(); }
|
2009-10-25 16:18:53 +01:00
|
|
|
|
2009-10-29 04:32:00 +01:00
|
|
|
void point_at(RES* r){ Goal::Result::point_at(r);}
|
2009-11-15 01:08:29 +01:00
|
|
|
void point_at(RES& r){ Goal::Result::point_at(&r);}
|
2009-10-24 00:23:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2009-10-29 04:32:00 +01:00
|
|
|
typedef lib::IterAdapter<Cursor,PReso> iterator;
|
2009-10-25 16:18:53 +01:00
|
|
|
|
2009-11-01 03:47:35 +01:00
|
|
|
iterator operator() (QueryResolver const& resolver) const;
|
|
|
|
|
iterator resolveBy (QueryResolver const& resolver) const;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
Query (QueryID qID)
|
|
|
|
|
: Goal (qID)
|
|
|
|
|
{ }
|
2009-10-24 00:23:22 +02:00
|
|
|
|
2012-11-25 02:04:19 +01:00
|
|
|
friend class Builder;
|
|
|
|
|
|
2009-10-17 21:31:03 +02:00
|
|
|
};
|
2009-10-14 05:48:24 +02:00
|
|
|
|
|
|
|
|
|
2009-10-30 20:33:44 +01:00
|
|
|
|
|
|
|
|
|
2012-11-25 02:04:19 +01:00
|
|
|
/**
|
|
|
|
|
* Helper for establishing,
|
|
|
|
|
* reworking and remolding queries.
|
|
|
|
|
*/
|
|
|
|
|
template<class RES>
|
|
|
|
|
class Query<RES>::Builder
|
|
|
|
|
{
|
|
|
|
|
string predicateForm_;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
const string
|
|
|
|
|
asKey() const
|
|
|
|
|
{
|
|
|
|
|
return "type("
|
2012-11-25 02:29:52 +01:00
|
|
|
+ lexical_cast<string> (getResultTypeID<RES>())
|
2012-11-25 02:04:19 +01:00
|
|
|
+ "), "+predicateForm_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
2009-10-30 20:33:44 +01:00
|
|
|
|
2009-10-29 04:32:00 +01:00
|
|
|
/**
|
|
|
|
|
* ABC denoting the result set
|
|
|
|
|
* of an individual query resolution
|
|
|
|
|
*/
|
|
|
|
|
class Resolution
|
2009-10-30 18:37:08 +01:00
|
|
|
: noncopyable
|
2009-10-29 04:32:00 +01:00
|
|
|
{
|
2009-10-29 21:59:02 +01:00
|
|
|
public:
|
2009-10-29 04:32:00 +01:00
|
|
|
typedef Goal::Result Result;
|
|
|
|
|
|
|
|
|
|
virtual ~Resolution();
|
|
|
|
|
|
2009-10-29 21:59:02 +01:00
|
|
|
|
|
|
|
|
friend bool
|
2012-05-19 02:13:18 +02:00
|
|
|
checkPoint (PReso const&, Result const& pos)
|
2009-10-29 04:32:00 +01:00
|
|
|
{
|
|
|
|
|
return bool(pos);
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-29 21:59:02 +01:00
|
|
|
friend void
|
2009-10-29 04:32:00 +01:00
|
|
|
iterNext (PReso& resultSet, Result& pos)
|
|
|
|
|
{
|
|
|
|
|
resultSet->nextResult(pos);
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-29 21:59:02 +01:00
|
|
|
|
2009-10-29 04:32:00 +01:00
|
|
|
virtual Result prepareResolution() =0;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
|
|
virtual void nextResult(Result& pos) =0;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2009-10-14 05:48:24 +02:00
|
|
|
/**
|
2010-06-19 03:36:46 +02:00
|
|
|
* Interface: a facility for resolving (some kind of) queries
|
|
|
|
|
* A concrete subclass has the ability to create Resolution instances
|
|
|
|
|
* in response to specific queries of some kind, \link #canHandle if applicable \endlink.
|
|
|
|
|
* Every resolution mechanism is expected to enrol by calling #installResolutionCase.
|
|
|
|
|
* Such a registration is considered permanent; a factory function gets stored,
|
|
|
|
|
* assuming that the entity implementing this function remains available
|
|
|
|
|
* up to the end of Lumiera main(). The kind of query and a suitable
|
|
|
|
|
* resolver is determined by the QueryID, which includes a type-ID.
|
|
|
|
|
* Thus the implementation might downcast query and resultset.
|
2009-10-14 05:48:24 +02:00
|
|
|
*/
|
|
|
|
|
class QueryResolver
|
2009-10-30 18:37:08 +01:00
|
|
|
: noncopyable
|
2009-10-14 05:48:24 +02:00
|
|
|
{
|
2009-10-30 20:33:44 +01:00
|
|
|
scoped_ptr<QueryDispatcher> dispatcher_;
|
2009-10-14 05:48:24 +02:00
|
|
|
|
|
|
|
|
|
2009-10-30 20:33:44 +01:00
|
|
|
public:
|
2009-10-24 00:23:22 +02:00
|
|
|
virtual ~QueryResolver() ;
|
2009-10-14 05:48:24 +02:00
|
|
|
|
2009-11-06 18:42:15 +01:00
|
|
|
virtual operator string () const =0; ///< short characterisation of the actual facility
|
2009-11-04 04:56:25 +01:00
|
|
|
|
2009-10-25 16:18:53 +01:00
|
|
|
|
2009-10-17 21:31:03 +02:00
|
|
|
/** issue a query to retrieve contents
|
2009-10-24 00:23:22 +02:00
|
|
|
* The query is handed over internally to a suitable resolver implementation.
|
2009-10-25 21:39:02 +01:00
|
|
|
* @return concrete Resolution of the query (ResultSet), \em managed by smart-pointer.
|
2009-10-24 00:23:22 +02:00
|
|
|
* @throw lumiera::Error subclass if query evaluation flounders.
|
|
|
|
|
* This might be broken logic, invalid input, misconfiguration
|
|
|
|
|
* or failure of an external facility used for resolution.
|
|
|
|
|
* @note a query may yield no results, in which case the iterator is empty.
|
2009-10-17 21:31:03 +02:00
|
|
|
*/
|
2009-11-01 03:47:35 +01:00
|
|
|
PReso issue (Goal const& query) const;
|
2009-10-17 21:31:03 +02:00
|
|
|
|
2009-11-12 02:15:02 +01:00
|
|
|
bool canHandle (Goal const&) const;
|
|
|
|
|
|
2009-10-17 21:31:03 +02:00
|
|
|
|
2009-10-30 20:33:44 +01:00
|
|
|
|
2009-11-01 03:47:35 +01:00
|
|
|
protected: /* ===== API for concrete query resolvers ===== */
|
2009-10-30 20:33:44 +01:00
|
|
|
|
2009-11-01 03:47:35 +01:00
|
|
|
virtual bool canHandleQuery (Goal::QueryID const&) const =0;
|
2009-10-30 20:33:44 +01:00
|
|
|
|
|
|
|
|
void installResolutionCase (Goal::QueryID const&,
|
2009-11-01 03:47:35 +01:00
|
|
|
function<Resolution*(Goal const&)>);
|
2009-10-24 00:23:22 +02:00
|
|
|
|
2009-10-29 04:32:00 +01:00
|
|
|
QueryResolver();
|
2009-10-14 05:48:24 +02:00
|
|
|
};
|
2009-11-01 03:47:35 +01:00
|
|
|
|
|
|
|
|
|
2009-10-14 05:48:24 +02:00
|
|
|
|
|
|
|
|
|
2009-10-24 00:23:22 +02:00
|
|
|
template<typename RES>
|
|
|
|
|
inline typename Query<RES>::iterator
|
2009-11-01 03:47:35 +01:00
|
|
|
Query<RES>::resolveBy (QueryResolver const& resolver) const
|
|
|
|
|
{
|
|
|
|
|
PReso resultSet = resolver.issue (*this);
|
|
|
|
|
Result first = resultSet->prepareResolution();
|
2009-11-27 20:30:06 +01:00
|
|
|
Cursor& start = static_cast<Cursor&> (first); // note: type RES must be compatible!
|
2009-11-01 03:47:35 +01:00
|
|
|
return iterator (resultSet, start);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** notational convenience shortcut,
|
|
|
|
|
* synonymous to #resolveBy */
|
|
|
|
|
template<typename RES>
|
|
|
|
|
inline typename Query<RES>::iterator
|
|
|
|
|
Query<RES>::operator() (QueryResolver const& resolver) const
|
|
|
|
|
{
|
|
|
|
|
return resolveBy (resolver);
|
|
|
|
|
}
|
2009-10-24 00:23:22 +02:00
|
|
|
|
|
|
|
|
|
2009-11-12 02:15:02 +01:00
|
|
|
inline bool
|
2009-11-13 04:52:48 +01:00
|
|
|
QueryResolver::canHandle(Goal const& query) const
|
2009-11-12 02:15:02 +01:00
|
|
|
{
|
|
|
|
|
return canHandleQuery (query.getQID());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-11-25 02:29:52 +01:00
|
|
|
} // namespace lib
|
2009-10-14 05:48:24 +02:00
|
|
|
#endif
|