[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    KDE/kdelibs/nepomuk
From:       Sebastian Trueg <sebastian () trueg ! de>
Date:       2010-09-09 9:16:43
Message-ID: 20100909091643.25166AC884 () svn ! kde ! org
[Download RAW message or body]

SVN commit 1173407 by trueg:

New QueryParser flag DetectFilenamePattern which enables a very simple heuristic
detecting filename pattern like *.mp3 which are then converted into ComparitionTerms
matching on the filename with a regular expression rather than a simple LiteralTerm.


 M  +47 -7     query/queryparser.cpp  
 M  +24 -4     query/queryparser.h  
 M  +3 -1      test/CMakeLists.txt  
 A             test/qtest_querytostring.h   [License: LGPL]
 M  +33 -8     test/queryparsertest.cpp  
 M  +2 -0      test/queryparsertest.h  
 M  +1 -12     test/querytest.cpp  


--- trunk/KDE/kdelibs/nepomuk/query/queryparser.cpp #1173406:1173407
@@ -27,6 +27,7 @@
 #include "negationterm.h"
 #include "comparisonterm.h"
 #include "dateparser_p.h"
+#include "nfo.h"
 
 #include <QtCore/QRegExp>
 #include <QtCore/QSet>
@@ -222,6 +223,31 @@
         }
     }
 
+    // A filename pattern needs to contain one dot and at least one '*' or '?':
+    // *.mp3
+    // hello?.txt
+    // hello?.*
+    // test*.???
+    bool isFilenamePattern( const QString& s )
+    {
+        return( !s.contains(' ') &&
+                s.count('.') == 1 &&
+                s.count('*') + s.count('?') > 0 );
+    }
+
+    Nepomuk::Query::ComparisonTerm createFilenamePatternTerm( const QString& s )
+    {
+        QString regex = QRegExp::escape(s);
+        regex.replace( "\\*", QLatin1String( ".*" ) );
+        regex.replace( "\\?", QLatin1String( "." ) );
+        regex.replace("\\", "\\\\");
+        regex.prepend('^');
+        regex.append('$');
+        return Nepomuk::Query::ComparisonTerm( Nepomuk::Vocabulary::NFO::fileName(),
+                                               Nepomuk::Query::LiteralTerm( regex ),
+                                               \
Nepomuk::Query::ComparisonTerm::Regexp ); +    }
+
 #ifndef Q_CC_MSVC
 #warning Make the parser handle different data, time, and datetime encodings as well \
as suffixes like MB or GB  #endif
@@ -333,13 +359,6 @@
 }
 
 
-Nepomuk::Query::Query Nepomuk::Query::QueryParser::parseQuery( const QString& query \
                )
-{
-    QueryParser parser;
-    return parser.parse( query );
-}
-
-
 class Nepomuk::Query::QueryParser::Private
 {
 public:
@@ -544,7 +563,12 @@
                 }
                 else {
                     kDebug() << "matched literal at" << pos << value;
+                    if( flags&DetectFilenamePattern && isFilenamePattern(value) ) {
+                        term = createFilenamePatternTerm( value );
+                    }
+                    else {
                     term = LiteralTerm( createLiteral( value, \
flags&QueryTermGlobbing ) ); +                    }
                     if ( !positiveTerm(s_plainTermRx.cap( 1 ) ) ) {
                         term = NegationTerm::negateTerm( term );
                     }
@@ -589,3 +613,19 @@
     final.setTerm( resolveFields( final.term(), this ) );
     return final;
 }
+
+
+// static
+Nepomuk::Query::Query Nepomuk::Query::QueryParser::parseQuery( const QString& query \
) +{
+    QueryParser parser;
+    return parser.parse( query );
+}
+
+
+// static
+Nepomuk::Query::Query Nepomuk::Query::QueryParser::parseQuery( const QString& query, \
ParserFlags flags ) +{
+    QueryParser parser;
+    return parser.parse( query, flags );
+}
--- trunk/KDE/kdelibs/nepomuk/query/queryparser.h #1173406:1173407
@@ -1,6 +1,6 @@
 /*
    This file is part of the Nepomuk KDE project.
-   Copyright (C) 2007-2009 Sebastian Trueg <trueg@kde.org>
+   Copyright (C) 2007-2010 Sebastian Trueg <trueg@kde.org>
 
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
@@ -36,8 +36,8 @@
          *
          * \warning This is NOT a SPARQL parser.
          *
-         * The QueryParser can be used to parse queries and convert them into
-         * Query instances.
+         * The QueryParser can be used to parse user queries, ie. queries that the \
user +         * would enter in any search interface, and convert them into Query \
                instances.
          *
          * The syntax is fairly simple: plain strings match to LiteralTerm terms,
          * URIs need to be N3-encoded, when using white space parenthesis need to
@@ -121,7 +121,17 @@
                  *
                  * This is disabled by default.
                  */
-                QueryTermGlobbing = 0x1
+                QueryTermGlobbing = 0x1,
+
+                /**
+                 * Try to detect filename pattern like *.mp3
+                 * or hello*.txt and create appropriate ComparisonTerm
+                 * instances using ComparisonTerm::Regexp instead of
+                 * ComparisonTerm::Contains.
+                 *
+                 * \since 4.6
+                 */
+                DetectFilenamePattern = 0x2
             };
             Q_DECLARE_FLAGS( ParserFlags, ParserFlag )
 
@@ -173,6 +183,16 @@
              */
             static Query parseQuery( const QString& query );
 
+            /**
+             * \overload
+             *
+             * \param query The query string to parse
+             * \param flags a set of flags influencing the parsing process.
+             *
+             * \since 4.6
+             */
+            static Query parseQuery( const QString& query, ParserFlags flags );
+
         private:
             class Private;
             Private* const d;
--- trunk/KDE/kdelibs/nepomuk/test/CMakeLists.txt #1173406:1173407
@@ -6,7 +6,9 @@
 
 # Query tests
 # --------------------------------------------
-kde4_add_unit_test(queryparsertest NOGUI queryparsertest.cpp)
+set(queryparsertest_SRC queryparsertest.cpp)
+soprano_add_ontology(queryparsertest_SRC \
"${SHAREDDESKTOPONTOLOGIES_ROOT_DIR}/nie/nfo.trig" "NFO" "Nepomuk::Vocabulary" \
"trig") +kde4_add_unit_test(queryparsertest NOGUI ${queryparsertest_SRC})
 target_link_libraries(queryparsertest nepomuk nepomukquery
   ${QT_QTTEST_LIBRARY}
   ${SOPRANO_LIBRARIES}
--- trunk/KDE/kdelibs/nepomuk/test/queryparsertest.cpp #1173406:1173407
@@ -20,6 +20,7 @@
 */
 
 #include "queryparsertest.h"
+#include "qtest_querytostring.h"
 #include "queryparser.h"
 #include "query.h"
 #include "literalterm.h"
@@ -28,6 +29,7 @@
 #include "orterm.h"
 #include "negationterm.h"
 #include "comparisonterm.h"
+#include "nfo.h"
 
 #include <QtTest>
 #include <qtest_kde.h>
@@ -101,6 +103,9 @@
     QTest::addColumn<QString>( "queryString" );
     QTest::addColumn<Nepomuk::Query::Query>( "query" );
 
+    // the invalid query
+    QTest::newRow( "empty query string" ) << QString() << Query();
+
     // simple literal queries
     QTest::newRow( "simple literal query" ) << QString( "Hello" ) << Query( \
                LiteralTerm( "Hello" ) );
     QTest::newRow( "literal with spaces without quotes" ) << QString( "Hello World" \
) << Query( AndTerm( LiteralTerm("Hello"), LiteralTerm("World" ) ) ); @@ -135,9 \
+140,6 @@  
     // or queries
     QTest::newRow( "or: two literals" )          << QString( "Hello OR World" ) << \
                Query( OrTerm( LiteralTerm( "Hello" ), LiteralTerm( "World" ) ) );
-
-    // the invalid query
-    QTest::newRow( "empty query string" ) << QString() << Query();
 }
 
 
@@ -148,9 +150,6 @@
 
     Query q = QueryParser::parseQuery( queryString );
 
-//    qDebug() << "Wanted query:" << query;
-//    qDebug() << "Parsed query:" << q;
-
     QCOMPARE( q, query );
 }
 
@@ -176,9 +175,35 @@
     QueryParser p;
     Query q = p.parse( queryString, QueryParser::QueryTermGlobbing );
 
-//     qDebug() << "Wanted query:" << query;
-//     qDebug() << "Parsed query:" << q;
+    QCOMPARE( q, query );
+}
 
+
+void QueryParserTest::testQueryParserDetectFilenamePattern_data()
+{
+    QTest::addColumn<QString>( "queryString" );
+    QTest::addColumn<Nepomuk::Query::Query>( "query" );
+
+    QTest::newRow( "DetectFilenamePattern1" ) << QString( "*.mp3" ) << Query( \
ComparisonTerm( Nepomuk::Vocabulary::NFO::fileName(), +                               \
LiteralTerm( "^.*\\.mp3$" ), +                                                        \
ComparisonTerm::Regexp ) ); +    QTest::newRow( "DetectFilenamePattern2" ) << \
QString( "hello?.txt" ) << Query( ComparisonTerm( \
Nepomuk::Vocabulary::NFO::fileName(), +                                               \
LiteralTerm( "^hello.\\.txt$" ), +                                                    \
ComparisonTerm::Regexp ) ); +    QTest::newRow( "DetectFilenamePattern3" ) << \
QString( "*.???" ) << Query( ComparisonTerm( Nepomuk::Vocabulary::NFO::fileName(), +  \
LiteralTerm( "^.*\\....$" ), +                                                        \
ComparisonTerm::Regexp ) ); +}
+
+
+void QueryParserTest::testQueryParserDetectFilenamePattern()
+{
+    QFETCH( QString, queryString );
+    QFETCH( Nepomuk::Query::Query, query );
+
+    QueryParser p;
+    Query q = p.parse( queryString, QueryParser::DetectFilenamePattern );
+
     QCOMPARE( q, query );
 }
 
--- trunk/KDE/kdelibs/nepomuk/test/queryparsertest.h #1173406:1173407
@@ -38,6 +38,8 @@
     void testQueryParser();
     void testQueryParserWithGlobbing_data();
     void testQueryParserWithGlobbing();
+    void testQueryParserDetectFilenamePattern_data();
+    void testQueryParserDetectFilenamePattern();
 
 private:
     KTempDir* m_storageDir;
--- trunk/KDE/kdelibs/nepomuk/test/querytest.cpp #1173406:1173407
@@ -20,6 +20,7 @@
 */
 
 #include "querytest.h"
+#include "qtest_querytostring.h"
 
 #include "query.h"
 #include "filequery.h"
@@ -55,19 +56,7 @@
 
 using namespace Nepomuk::Query;
 
-namespace QTest {
-template<>
-char* toString(const Nepomuk::Query::Query& query) {
-    return qstrdup( query.toString().toUtf8().data() );
-}
 
-template<>
-char* toString(const Nepomuk::Query::Term& term) {
-    return qstrdup( term.toString().toUtf8().data() );
-}
-}
-
-
 // this is a tricky one as we nee to match the variable names and order of the \
queries exactly.  void QueryTest::testToSparql_data()
 {


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic