generic query representation (placeholder)
...planned to be replaced later by a real AST based implementation, which acutally parses the query definitions
This commit is contained in:
parent
d306bb3cdf
commit
5b2668a17c
4 changed files with 287 additions and 0 deletions
62
src/lib/query-text.cpp
Normal file
62
src/lib/query-text.cpp
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
QueryText - syntactical standard representation for queries
|
||||
|
||||
Copyright (C) Lumiera.org
|
||||
2012, Hermann Vosseler <Ichthyostega@web.de>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License as
|
||||
published by the Free Software Foundation; either version 2 of
|
||||
the License, or (at your option) any later version.
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
* *****************************************************/
|
||||
|
||||
|
||||
//#include "lib/util.hpp"
|
||||
//#include "lib/symbol.hpp"
|
||||
//#include "include/logging.h"
|
||||
#include "lib/query-text.hpp"
|
||||
|
||||
//#include <boost/noncopyable.hpp>
|
||||
#include <boost/functional/hash.hpp>
|
||||
#include <string>
|
||||
//#include <map>
|
||||
|
||||
//using std::map;
|
||||
using std::string;
|
||||
|
||||
//using util::contains;
|
||||
//using util::isnil;
|
||||
|
||||
namespace lib {
|
||||
|
||||
namespace { // internal details
|
||||
|
||||
} // internal details
|
||||
|
||||
|
||||
/** support using queries in hashtables.
|
||||
* @warning right now (2012) the dummy implementation of QueryText
|
||||
* doesn't normalise the query in any way, which makes the
|
||||
* generated hash value dependent on the actual textual
|
||||
* form used to build the QueryText. This is not how
|
||||
* it's intended to work, it should rely on a normalised
|
||||
* form after parsing the query definition.
|
||||
*/
|
||||
HashVal
|
||||
hash_value (QueryText const& entry)
|
||||
{
|
||||
return boost::hash_value (entry.definition_);
|
||||
}
|
||||
|
||||
|
||||
} // namespace lib
|
||||
108
src/lib/query-text.hpp
Normal file
108
src/lib/query-text.hpp
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
/*
|
||||
QUERY-TEXT.hpp - syntactical standard representation for queries
|
||||
|
||||
Copyright (C) Lumiera.org
|
||||
2012, Hermann Vosseler <Ichthyostega@web.de>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License as
|
||||
published by the Free Software Foundation; either version 2 of
|
||||
the License, or (at your option) any later version.
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
/** @file query-text.hpp
|
||||
** A generic syntactical representation for all kinds of queries.
|
||||
** Within Lumiera, its a common pattern to query for parts to be combined,
|
||||
** instead of using a hard wired builder logic. Consequently, there are various
|
||||
** flavours of queries used by different subsystems. As a common denominator,
|
||||
** we use a syntactical query representation, based on predicate notation
|
||||
** (mathematical logic, using prolog syntax). While subsystems typically
|
||||
** might resolve a specialised query directly, as a fallback this
|
||||
** syntactical representation allows for \em generic query dispatch.
|
||||
** And it can be used as intermediary format for remolding queries.
|
||||
**
|
||||
** @note as of 12/2012 this AST-representation is not defined at all;
|
||||
** instead we use a plain string as placeholder for the "real thing"
|
||||
** @todo actually build the term representation /////////////////////////////TICKET #899
|
||||
**
|
||||
** @see QueryText_test usage example
|
||||
**
|
||||
*/
|
||||
|
||||
|
||||
#ifndef LIB_QUERY_TEXT_H
|
||||
#define LIB_QUERY_TEXT_H
|
||||
|
||||
#include "lib/error.hpp"
|
||||
#include "lib/hash-value.h"
|
||||
#include "lib/util.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace lib {
|
||||
|
||||
using lib::HashVal;
|
||||
using std::string;
|
||||
|
||||
/**
|
||||
* Syntactical query representation.
|
||||
* Used as a backbone to allow for generic queries and to
|
||||
* enable programmatically rebuilding and remolding of queries.
|
||||
*
|
||||
* @todo this is placeholder code and just embeds a string
|
||||
* with the raw query definition, instead of parsing
|
||||
* the definition and transforming it into an AST
|
||||
*
|
||||
* @see Query
|
||||
*/
|
||||
class QueryText
|
||||
{
|
||||
string definition_;
|
||||
|
||||
|
||||
public:
|
||||
QueryText() { }
|
||||
|
||||
explicit
|
||||
QueryText (string const& syntacticRepr) :
|
||||
definition_ (syntacticRepr)
|
||||
{ }
|
||||
|
||||
// using default copy/assignment
|
||||
|
||||
operator string() const
|
||||
{
|
||||
return definition_;
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
empty() const
|
||||
{
|
||||
return definition_.empty();
|
||||
}
|
||||
|
||||
bool
|
||||
hasAtom(string const& predSymbol)
|
||||
{
|
||||
return util::contains (definition_, predSymbol);
|
||||
}
|
||||
|
||||
|
||||
friend HashVal hash_value (QueryText const& entry);
|
||||
};
|
||||
|
||||
|
||||
} // namespace lib
|
||||
#endif /*LIB_QUERY_TEXT_H*/
|
||||
|
|
@ -524,6 +524,16 @@ return: 0
|
|||
END
|
||||
|
||||
|
||||
TEST "Generic Query representation" QueryText_test <<END
|
||||
return: 0
|
||||
END
|
||||
|
||||
|
||||
TEST "Query support utilities" QueryUtils_test <<END
|
||||
return: 0
|
||||
END
|
||||
|
||||
|
||||
TEST "RefArray_test" RefArray_test <<END
|
||||
return: 0
|
||||
END
|
||||
|
|
|
|||
107
tests/lib/query-text-test.cpp
Normal file
107
tests/lib/query-text-test.cpp
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
/*
|
||||
QueryText(Test) - verify syntactical query representation
|
||||
|
||||
Copyright (C) Lumiera.org
|
||||
2012, Hermann Vosseler <Ichthyostega@web.de>
|
||||
|
||||
This program is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU General Public License as
|
||||
published by the Free Software Foundation; either version 2 of
|
||||
the License, or (at your option) any later version.
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
* *****************************************************/
|
||||
|
||||
|
||||
|
||||
#include "lib/test/run.hpp"
|
||||
#include "lib/query-util.hpp"
|
||||
#include "lib/query-text.hpp"
|
||||
#include "lib/util.hpp"
|
||||
|
||||
//#include <boost/lexical_cast.hpp>
|
||||
//#include <iostream>
|
||||
#include <string>
|
||||
//#include <map>
|
||||
|
||||
//using boost::lexical_cast;
|
||||
using util::contains;
|
||||
using util::isnil;
|
||||
using std::string;
|
||||
//using std::cout;
|
||||
//using std::endl;
|
||||
|
||||
namespace lib {
|
||||
namespace test{
|
||||
|
||||
namespace { // test fixture...
|
||||
|
||||
} //(End) test fixture
|
||||
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* @test cover basic properties of the generic syntactical query representation.
|
||||
* - build from query string
|
||||
*
|
||||
* @todo this is placeholder code. The actual query representation needs to be written /////////////////////////////TICKET #899
|
||||
* @see QueryText
|
||||
*/
|
||||
class QueryText_test : public Test
|
||||
{
|
||||
|
||||
virtual void run (Arg)
|
||||
{
|
||||
emptyRepresentation();
|
||||
build_from_string();
|
||||
useHashValue();
|
||||
}
|
||||
|
||||
|
||||
void emptyRepresentation ()
|
||||
{
|
||||
QueryText noText;
|
||||
|
||||
CHECK (isnil (noText));
|
||||
CHECK (isnil (string(noText)));
|
||||
}
|
||||
|
||||
|
||||
void build_from_string ()
|
||||
{
|
||||
QueryText eternal ("beats(lumiera, duke_nukem_forever).");
|
||||
CHECK (contains (string(eternal), "lumiera"));
|
||||
CHECK (contains (string(eternal), "beats"));
|
||||
CHECK (contains (string(eternal), "duke_nukem_forever"));
|
||||
CHECK (eternal.hasAtom ("lumiera"));
|
||||
}
|
||||
|
||||
|
||||
void useHashValue ()
|
||||
{
|
||||
QueryText one("one");
|
||||
QueryText two("two");
|
||||
|
||||
CHECK (0 < hash_value(one));
|
||||
CHECK (0 < hash_value(two));
|
||||
CHECK (hash_value(one) != hash_value(two));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
/** Register this test class... */
|
||||
LAUNCHER(QueryText_test, "unit common");
|
||||
|
||||
}} // namespace lib::test
|
||||
Loading…
Reference in a new issue