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

List:       kde-commits
Subject:    playground/base/nepomuk-kde/scribo/plugins/opencalais
From:       Sebastian Trueg <sebastian () trueg ! de>
Date:       2009-07-02 15:03:06
Message-ID: 1246546986.124220.13985.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 990512 by trueg:

perform all the long tasks in a separate thread to not block the gui

 M  +1 -0      CMakeLists.txt  
 M  +10 -5     lookupjob.cpp  
 M  +3 -2      lookupjob.h  
 M  +20 -97    opencalaistextmatchplugin.cpp  
 M  +5 -2      opencalaistextmatchplugin.h  
 A             worker.cpp   [License: LGPL (v2+)]
 A             worker.h   [License: LGPL (v2+)]


--- trunk/playground/base/nepomuk-kde/scribo/plugins/opencalais/CMakeLists.txt \
#990511:990512 @@ -24,6 +24,7 @@
 set(OCTMP_SCRS
   lookupjob.cpp
   opencalaistextmatchplugin.cpp
+  worker.cpp
   )
 
 soprano_add_ontology(OCTMP_SCRS ${CMAKE_CURRENT_SOURCE_DIR}/opencalais.rdfs \
                "OpenCalais" "Nepomuk::Vocabulary" "rdfxml")
--- trunk/playground/base/nepomuk-kde/scribo/plugins/opencalais/lookupjob.cpp \
#990511:990512 @@ -52,6 +52,7 @@
 
     QString content;
     Soprano::Model* resultModel;
+    bool modelRetrieved;
 
     bool createModel();
 };
@@ -62,9 +63,9 @@
     delete resultModel;
 
     // sesame2 is waaaay faster then redland which is the default in Soprano
-     if ( const Soprano::Backend* b = \
                Soprano::PluginManager::instance()->discoverBackendByName( "sesame2" \
                ) )
-         resultModel = b->createModel( Soprano::BackendSettings() << \
                Soprano::BackendSetting( Soprano::BackendOptionStorageMemory ) );
-     else
+//      if ( const Soprano::Backend* b = \
Soprano::PluginManager::instance()->discoverBackendByName( "sesame2" ) ) +//          \
resultModel = b->createModel( Soprano::BackendSettings() << Soprano::BackendSetting( \
Soprano::BackendOptionStorageMemory ) ); +//      else
         resultModel = Soprano::createModel( Soprano::BackendSettings() << \
Soprano::BackendSetting( Soprano::BackendOptionStorageMemory ) );  
     return resultModel;
@@ -82,13 +83,16 @@
 
 OpenCalais::LookupJob::~LookupJob()
 {
-    delete d->resultModel;
+    // if no one took the model, we delete it
+    if ( !d->modelRetrieved )
+        delete d->resultModel;
     delete d;
 }
 
 
-const Soprano::Model* OpenCalais::LookupJob::resultModel() const
+Soprano::Model* OpenCalais::LookupJob::resultModel() const
 {
+    d->modelRetrieved = true;
     return d->resultModel;
 }
 
@@ -103,6 +107,7 @@
 {
     delete d->resultModel;
     d->resultModel = 0;
+    d->modelRetrieved = false;
 
     QUrl url(  "http://api.opencalais.com/enlighten/rest/" );
 
--- trunk/playground/base/nepomuk-kde/scribo/plugins/opencalais/lookupjob.h \
#990511:990512 @@ -40,9 +40,10 @@
          * OpenCalais reports the results as a graph.
          *
          * \return The model containing the data or 0 if no request has
-         * been finished successfully.
+         * been finished successfully. The caller takes ownershop of the
+         * model.
          */
-        const Soprano::Model* resultModel() const;
+        Soprano::Model* resultModel() const;
 
     public Q_SLOTS:
         void start();
--- trunk/playground/base/nepomuk-kde/scribo/plugins/opencalais/opencalaistextmatchplugin.cpp \
#990511:990512 @@ -20,6 +20,8 @@
 
 #include "opencalaistextmatchplugin.h"
 #include "lookupjob.h"
+#include "worker.h"
+
 #include "entity.h"
 #include "statement.h"
 #include "pimo.h"
@@ -40,7 +42,9 @@
 
 #include <QtCore/QCoreApplication>
 
+Q_DECLARE_METATYPE( Scribo::TextMatch )
 
+
 OpenCalaisTextMatchPlugin::OpenCalaisTextMatchPlugin( QObject* parent, const \
QVariantList& )  : TextMatchPlugin( parent ),
       m_lookupJob( 0 )
@@ -55,6 +59,10 @@
     m_typeMap.insert( Nepomuk::Vocabulary::OpenCalais::Position(), \
                Nepomuk::Vocabulary::PIMO::PersonRole() );
     m_typeMap.insert( Nepomuk::Vocabulary::OpenCalais::Continent(), \
                Nepomuk::Vocabulary::PIMO::Location() );
     m_typeMap.insert( Nepomuk::Vocabulary::OpenCalais::URL(), \
Nepomuk::Vocabulary::NFO::Website() ); +
+    m_worker = new Worker( this );
+
+    qRegisterMetaType<Scribo::TextMatch>();
 }
 
 
@@ -79,107 +87,22 @@
 
 void OpenCalaisTextMatchPlugin::slotResult( KJob* job )
 {
-    if ( const Soprano::Model* model = static_cast<OpenCalais::LookupJob*>( job \
                )->resultModel() ) {
-        // select all instances that do not have a subject -> these should be the \
                extracted entities
-        Soprano::QueryResultIterator it = model->executeQuery( QString( "select * \
                where { "
-                                                                        "?r a ?type \
                . "
-                                                                        "OPTIONAL { \
                ?r <http://s.opencalais.com/1/pred/subject> ?sub . } . "
-                                                                        \
                "FILTER(!bound(?sub)) . "
-                                                                        "}" ),
-                                                               \
                Soprano::Query::QueryLanguageSparql );
-        while ( it.next() ) {
-            Soprano::Node r = it["r"];
-            Soprano::Graph graph;
-            Soprano::QueryResultIterator it2 = model->executeQuery( QString( \
                "construct { ?entity ?ep ?eo . ?rel ?relp ?relo . } "
-                                                                             "where \
                { "
-                                                                             \
                "?entity <http://s.opencalais.com/1/pred/subject> %1 . "
-                                                                             \
                "?entity ?ep ?eo . "
-                                                                             "?rel a \
                <http://s.opencalais.com/1/type/sys/RelevanceInfo> . "
-                                                                             "?rel \
                <http://s.opencalais.com/1/pred/subject> %1 . "
-                                                                             "?rel \
                ?relp ?relo . "
-                                                                             "}" )
-                                                                    .arg( r.toN3() \
                ),
-                                                                    \
                Soprano::Query::QueryLanguageSparql );
-            while ( it2.next() ) {
-                graph << it2.currentStatement();
-            }
+    if ( Soprano::Model* model = static_cast<OpenCalais::LookupJob*>( job \
)->resultModel() ) { +        // cancel previous worker start
+        m_worker->disconnect( this );
+        m_worker->cancel();
 
-            it2 = model->executeQuery( QString( "select ?name ?rel ?type ?offset \
                ?length where { "
-                                                "%1 \
                <http://s.opencalais.com/1/pred/name> ?name . "
-                                                "%1 a ?type . "
-                                                "?relInfo a \
                <http://s.opencalais.com/1/type/sys/RelevanceInfo> . "
-                                                "?relInfo \
                <http://s.opencalais.com/1/pred/subject> %1 . "
-                                                "?relInfo \
                <http://s.opencalais.com/1/pred/relevance> ?rel . "
-                                                "?info a \
                <http://s.opencalais.com/1/type/sys/InstanceInfo> . "
-                                                "?info \
                <http://s.opencalais.com/1/pred/subject> %1 . "
-                                                "?info \
                <http://s.opencalais.com/1/pred/offset> ?offset . "
-                                                "?info \
                <http://s.opencalais.com/1/pred/length> ?length . "
-                                                "}" )
-                                       .arg( r.toN3() ),
-                                       Soprano::Query::QueryLanguageSparql );
-            if ( it2.next() ) {
-                Nepomuk::Types::Class type( matchPimoType( it2["type"].uri() ) );
-                QString name = it2["name"].toString();
-
-                // FIXME: actually the opencalais resource should be used as the \
                pimo:hasOtherRepresentation of the pimo thing
-                //        but that would mean that Entity needs more information or \
                the graph needs to be really used
-                Scribo::Entity entity( name, type, graph );
-                do {
-                    double rel = it2["rel"].literal().toDouble();
-                    int offset = it2["offset"].literal().toInt();
-                    int length = it2["length"].literal().toInt();
-
-                    Scribo::TextOccurrence oc;
-                    oc.setStartPos( offset );
-                    oc.setLength( length );
-                    oc.setRelevance( rel );
-                    entity.addOccurrence( oc );
-
-                    kDebug() << type << type.label() << name << rel << offset << \
                length;
-                } while ( it2.next() );
-
-                addNewMatch( entity );
-
-                // find relations for the entity
-                Soprano::QueryResultIterator it3 = model->executeQuery( QString( \
                "select ?verb ?offset ?length ?exact where { "
-                                                                                 "?s \
                a <http://s.opencalais.com/1/type/em/r/GenericRelations> . "
-                                                                                 "?s \
                <http://s.opencalais.com/1/pred/relationsubject> %1 . "
-                                                                                 "?s \
                <http://s.opencalais.com/1/pred/verb> ?verb . "
-                                                                                 \
                "?info a <http://s.opencalais.com/1/type/sys/InstanceInfo> . "
-                                                                                 \
                "?info <http://s.opencalais.com/1/pred/subject> ?s . "
-                                                                                 \
                "?info <http://s.opencalais.com/1/pred/offset> ?offset . "
-                                                                                 \
                "?info <http://s.opencalais.com/1/pred/length> ?length . "
-                                                                                 \
                "?info <http://s.opencalais.com/1/pred/exact> ?exact . "
-                                                                                 "}" \
                )
-                                                                        .arg( \
                r.toN3() ),
-                                                                        \
                Soprano::Query::QueryLanguageSparql );
-                if ( it3.next() ) {
-                    QString verb = it3["verb"].toString();
-                    QString exact = it3["exact"].toString();
-                    int offset = it3["offset"].literal().toInt();
-                    int length = it3["length"].literal().toInt();
-
-                    // FIXME: get the graph
-                    Scribo::Statement s( verb, entity, exact, Soprano::Graph() );
-
-                    Scribo::TextOccurrence oc;
-                    oc.setStartPos( offset );
-                    oc.setLength( length );
-                    s.addOccurrence( oc );
-
-                    addNewMatch( s );
-                }
-            }
-
-            // a little bit of fake async
-            QCoreApplication::processEvents();
-        }
+        // restart the worker
+        m_worker->setModel( model );
+        connect( m_worker, SIGNAL( finished() ), this, SLOT( emitFinished() ) );
+        connect( m_worker, SIGNAL( newMatch( Scribo::TextMatch ) ),
+                 this, SLOT( addNewMatch( Scribo::TextMatch ) ),
+                 Qt::QueuedConnection );
+        m_worker->start();
     }
     else {
-        kDebug() << "no result";
+        emitFinished();
     }
-
-    emitFinished();
 }
 
 
--- trunk/playground/base/nepomuk-kde/scribo/plugins/opencalais/opencalaistextmatchplugin.h \
#990511:990512 @@ -33,6 +33,7 @@
     class LookupJob;
 }
 class KJob;
+class Worker;
 
 class OpenCalaisTextMatchPlugin : public Scribo::TextMatchPlugin
 {
@@ -42,6 +43,8 @@
     OpenCalaisTextMatchPlugin( QObject* parent, const QVariantList& );
     ~OpenCalaisTextMatchPlugin();
 
+    QUrl matchPimoType( const QUrl& openCalaisType );
+
 protected:
     void doGetPossibleMatches( const QString& text );
 
@@ -49,10 +52,10 @@
     void slotResult( KJob* );
 
 private:
-    QUrl matchPimoType( const QUrl& openCalaisType );
-
     OpenCalais::LookupJob* m_lookupJob;
     QHash<QUrl, QUrl> m_typeMap;
+
+    Worker* m_worker;
 };
 
 #endif


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

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