2009-10-14 05:48:24 +02:00
|
|
|
/*
|
2012-11-26 01:22:01 +01:00
|
|
|
QUERY-RESOLVER.hpp - framework for resolving generic queries
|
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
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2016-11-03 18:20:10 +01:00
|
|
|
/** @file §§§
|
|
|
|
|
** TODO §§§
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2012-12-01 08:44:07 +01:00
|
|
|
#ifndef LUMIERA_QUERY_RESOLVER_H
|
|
|
|
|
#define LUMIERA_QUERY_RESOLVER_H
|
2009-10-14 05:48:24 +02:00
|
|
|
|
2009-10-17 21:31:03 +02:00
|
|
|
#include "lib/iter-adapter.hpp"
|
2012-12-01 08:44:07 +01:00
|
|
|
#include "common/query.hpp"
|
2009-10-14 05:48:24 +02:00
|
|
|
|
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>
|
2014-04-03 22:42:48 +02:00
|
|
|
#include <functional>
|
|
|
|
|
#include <memory>
|
2009-11-04 04:56:25 +01:00
|
|
|
#include <string>
|
2009-10-14 05:48:24 +02:00
|
|
|
|
2014-04-03 22:42:48 +02:00
|
|
|
using std::function;
|
2009-10-14 05:48:24 +02:00
|
|
|
|
2012-12-01 08:44:07 +01:00
|
|
|
namespace lumiera {
|
2009-10-14 05:48:24 +02:00
|
|
|
|
2009-10-30 18:37:08 +01:00
|
|
|
using boost::noncopyable;
|
2009-10-30 20:33:44 +01:00
|
|
|
using boost::scoped_ptr;
|
2009-11-04 04:56:25 +01:00
|
|
|
using std::string;
|
2009-10-24 00:23:22 +02:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2014-09-08 00:30:11 +02:00
|
|
|
/** Allow to take and transfer ownership of a result set */
|
2014-04-03 22:42:48 +02:00
|
|
|
typedef std::shared_ptr<Resolution> PReso;
|
2009-10-30 18:37:08 +01:00
|
|
|
|
|
|
|
|
|
2009-10-29 04:32:00 +01:00
|
|
|
/**
|
2012-11-26 01:22:01 +01:00
|
|
|
* ABC representing the result set
|
2009-10-29 04:32:00 +01:00
|
|
|
* of an individual query resolution
|
|
|
|
|
*/
|
|
|
|
|
class Resolution
|
2012-11-26 01:22:01 +01:00
|
|
|
: boost::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();
|
|
|
|
|
|
2014-09-08 00:30:11 +02:00
|
|
|
/** IterAdapter attached here */
|
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,
|
2014-09-08 00:30:11 +02:00
|
|
|
* assuming that the entity to implement this function remains available
|
2010-06-19 03:36:46 +02:00
|
|
|
* 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
|
|
|
|
2014-09-08 03:37:41 +02:00
|
|
|
using ResolutionMechanism = function<Resolution*(Goal const&)>;
|
|
|
|
|
|
2009-10-30 20:33:44 +01:00
|
|
|
void installResolutionCase (Goal::QueryID const&,
|
2014-09-08 03:37:41 +02:00
|
|
|
ResolutionMechanism);
|
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-12-01 08:44:07 +01:00
|
|
|
} // namespace lumiera
|
2009-10-14 05:48:24 +02:00
|
|
|
#endif
|