From 5b2668a17c845d9a929b5f3db8e99782588296de Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Mon, 3 Dec 2012 00:41:57 +0100 Subject: [PATCH] generic query representation (placeholder) ...planned to be replaced later by a real AST based implementation, which acutally parses the query definitions --- src/lib/query-text.cpp | 62 +++++++++++++++++++ src/lib/query-text.hpp | 108 ++++++++++++++++++++++++++++++++++ tests/40components.tests | 10 ++++ tests/lib/query-text-test.cpp | 107 +++++++++++++++++++++++++++++++++ 4 files changed, 287 insertions(+) create mode 100644 src/lib/query-text.cpp create mode 100644 src/lib/query-text.hpp create mode 100644 tests/lib/query-text-test.cpp diff --git a/src/lib/query-text.cpp b/src/lib/query-text.cpp new file mode 100644 index 000000000..6a4a8b3f6 --- /dev/null +++ b/src/lib/query-text.cpp @@ -0,0 +1,62 @@ +/* + QueryText - syntactical standard representation for queries + + Copyright (C) Lumiera.org + 2012, Hermann Vosseler + + 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 +#include +#include +//#include + +//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 diff --git a/src/lib/query-text.hpp b/src/lib/query-text.hpp new file mode 100644 index 000000000..c40c86680 --- /dev/null +++ b/src/lib/query-text.hpp @@ -0,0 +1,108 @@ +/* + QUERY-TEXT.hpp - syntactical standard representation for queries + + Copyright (C) Lumiera.org + 2012, Hermann Vosseler + + 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 + +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*/ diff --git a/tests/40components.tests b/tests/40components.tests index 2d2aa4dc8..0860bd3e5 100644 --- a/tests/40components.tests +++ b/tests/40components.tests @@ -524,6 +524,16 @@ return: 0 END +TEST "Generic Query representation" QueryText_test < + + 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 +//#include +#include +//#include + +//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