[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:       2010-05-26 12:31:09
Message-ID: 20100526123109.95C34AC8C2 () svn ! kde ! org
[Download RAW message or body]

SVN commit 1130804 by trueg:

Use Soprano::Graph to exchange data between the classes to get rid of the Model \
ownership problems which could lead to double deletion and, thus, crashes.

 M  +8 -42     lookupjob.cpp  
 M  +2 -6      lookupjob.h  
 M  +5 -3      opencalaistextmatchplugin.cpp  
 M  +18 -6     worker.cpp  
 M  +9 -6      worker.h  


--- trunk/playground/base/nepomuk-kde/scribo/plugins/opencalais/lookupjob.cpp \
#1130803:1130804 @@ -21,16 +21,12 @@
 #include "lookupjob.h"
 #include "opencalaisconfig.h"
 
-#include <Soprano/Model>
+#include <Soprano/Graph>
 #include <Soprano/Parser>
 #include <Soprano/PluginManager>
 #include <Soprano/Statement>
 #include <Soprano/Node>
 #include <Soprano/StatementIterator>
-#include <Soprano/Global>
-#include <Soprano/BackendSettings>
-#include <Soprano/Backend>
-#include <Soprano/StorageModel>
 
 #include <KLocale>
 #include <KDebug>
@@ -49,49 +45,27 @@
     QNetworkReply* lastReply;
 
     QString content;
-    Soprano::Model* resultModel;
-    bool modelRetrieved;
-
-    bool createModel();
+    Soprano::Graph resultGraph;
 };
 
 
-bool OpenCalais::LookupJob::Private::createModel()
-{
-    delete resultModel;
-
-    // sesame2 is waaaay faster then redland which is the default in Soprano but it \
                crashes sometimes. So that needs fixing first.
-//     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;
-}
-
-
 OpenCalais::LookupJob::LookupJob( QObject* parent )
     : KJob( parent ),
       d( new Private() )
 {
-    d->resultModel = 0;
     d->networkAccessManager = new QNetworkAccessManager( this );
 }
 
 
 OpenCalais::LookupJob::~LookupJob()
 {
-    // if no one took the model, we delete it
-    if ( !d->modelRetrieved )
-        delete d->resultModel;
     delete d;
 }
 
 
-Soprano::Model* OpenCalais::LookupJob::resultModel() const
+Soprano::Graph OpenCalais::LookupJob::resultGraph() const
 {
-    d->modelRetrieved = true;
-    return d->resultModel;
+    return d->resultGraph;
 }
 
 
@@ -103,10 +77,7 @@
 
 void OpenCalais::LookupJob::start()
 {
-    delete d->resultModel;
-    d->resultModel = 0;
-    d->modelRetrieved = false;
-
+    kDebug();
     QUrl url( "http://api.opencalais.com/enlighten/rest/" );
 
     QString key = Config::licenseKey();
@@ -157,23 +128,18 @@
 
 void OpenCalais::LookupJob::slotTransferResult()
 {
+    d->resultGraph.removeAllStatements();
+
     // FIXME: error handling
     QByteArray data = d->lastReply->readAll();
 //    kDebug() << d->lastReply->error() << d->lastReply->rawHeaderList() << data;
     d->lastReply->deleteLater();
 
-    if ( !d->createModel() ) {
-        setErrorText( i18n( "Failed to create Soprano memory model. Most likely the \
                installation misses Soprano backend plugins" ) );
-        emitResult();
-        return;
-    }
-
     const Soprano::Parser* parser = \
Soprano::PluginManager::instance()->discoverParserForSerialization( \
Soprano::SerializationRdfXml );  if ( parser ) {
         Soprano::StatementIterator it = parser->parseString( QString::fromUtf8( data \
), QUrl(), Soprano::SerializationRdfXml );  while ( it.next() ) {
-            Soprano::Statement s = *it;
-            d->resultModel->addStatement( s );
+            d->resultGraph.addStatement( *it );
         }
     }
 
--- trunk/playground/base/nepomuk-kde/scribo/plugins/opencalais/lookupjob.h \
#1130803:1130804 @@ -24,7 +24,7 @@
 #include <KJob>
 
 namespace Soprano {
-    class Model;
+    class Graph;
 }
 
 namespace OpenCalais {
@@ -38,12 +38,8 @@
 
         /**
          * OpenCalais reports the results as a graph.
-         *
-         * \return The model containing the data or 0 if no request has
-         * been finished successfully. The caller takes ownershop of the
-         * model.
          */
-        Soprano::Model* resultModel() const;
+        Soprano::Graph resultGraph() const;
 
     public Q_SLOTS:
         void start();
--- trunk/playground/base/nepomuk-kde/scribo/plugins/opencalais/opencalaistextmatchplugin.cpp \
#1130803:1130804 @@ -31,7 +31,6 @@
 
 #include <Nepomuk/Types/Class>
 
-#include <Soprano/Model>
 #include <Soprano/Graph>
 #include <Soprano/QueryResultIterator>
 #include <Soprano/Serializer>
@@ -53,6 +52,7 @@
     : TextMatchPlugin( parent ),
       m_lookupJob( 0 )
 {
+    kDebug();
     // build OpenCalais->PIMO type map
     // FIXME: complete the map
     m_typeMap.insert( Nepomuk::Vocabulary::OpenCalais::City(), \
Nepomuk::Vocabulary::PIMO::City() ); @@ -77,6 +77,7 @@
 
 void OpenCalaisTextMatchPlugin::doGetPossibleMatches( const QString& text )
 {
+    kDebug();
     // cancel previous jobs
     delete m_lookupJob;
     m_lookupJob = 0;
@@ -106,13 +107,14 @@
 
 void OpenCalaisTextMatchPlugin::slotResult( KJob* job )
 {
-    if ( Soprano::Model* model = static_cast<OpenCalais::LookupJob*>( job \
)->resultModel() ) { +    kDebug();
+    if ( !job->error() ) {
         // cancel previous worker start
         m_worker->disconnect( this );
         m_worker->cancel();
 
         // restart the worker
-        m_worker->setModel( model );
+        m_worker->setData( static_cast<OpenCalais::LookupJob*>( job )->resultGraph() \
                );
         connect( m_worker, SIGNAL( finished() ), this, SLOT( emitFinished() ) );
         connect( m_worker, SIGNAL( newMatch( Scribo::TextMatch ) ),
                  this, SLOT( addNewMatch( Scribo::TextMatch ) ),
--- trunk/playground/base/nepomuk-kde/scribo/plugins/opencalais/worker.cpp \
#1130803:1130804 @@ -34,6 +34,10 @@
 #include <Soprano/Serializer>
 #include <Soprano/StatementIterator>
 #include <Soprano/PluginManager>
+#include <Soprano/Global>
+#include <Soprano/BackendSettings>
+#include <Soprano/Backend>
+#include <Soprano/StorageModel>
 
 #include <KDebug>
 
@@ -41,7 +45,6 @@
 
 Worker::Worker( OpenCalaisTextMatchPlugin* plugin )
     : QThread( plugin ),
-      m_model( 0 ),
       m_plugin( plugin ),
       m_canceled( false )
 {
@@ -56,9 +59,18 @@
 void Worker::run()
 {
     m_canceled = false;
+    m_errorText.truncate( 0 );
 
+    Soprano::Model* model = Soprano::createModel( Soprano::BackendSettings() << \
Soprano::BackendSetting( Soprano::BackendOptionStorageMemory ) ); +    if ( !model ) \
{ +        m_errorText = i18n( "Failed to create Soprano memory model. Most likely \
the installation misses Soprano backend plugins" ); +        return;
+    }
+
+    model->addStatements( m_data.toList() );
+
     // select all instances that do not have a subject -> these should be the \
                extracted entities
-    Soprano::QueryResultIterator it = m_model->executeQuery( QString( "select * \
where { " +    Soprano::QueryResultIterator it = model->executeQuery( \
                QString::fromLatin1( "select * where { "
                                                                       "?r a ?type . \
                "
                                                                       "OPTIONAL { ?r \
                <http://s.opencalais.com/1/pred/subject> ?sub . } . "
                                                                       \
"FILTER(!bound(?sub)) . " @@ -67,7 +79,7 @@
     while ( !m_canceled && it.next() ) {
         Soprano::Node r = it["r"];
         Soprano::Graph graph;
-        Soprano::QueryResultIterator it2 = m_model->executeQuery( QString( \
"construct { ?entity ?ep ?eo . ?rel ?relp ?relo . } " +        \
Soprano::QueryResultIterator it2 = model->executeQuery( QString::fromLatin1( \
                "construct { ?entity ?ep ?eo . ?rel ?relp ?relo . } "
                                                                            "where { \
                "
                                                                            "?entity \
                <http://s.opencalais.com/1/pred/subject> %1 . "
                                                                            "?entity \
?ep ?eo . " @@ -84,7 +96,7 @@
         if ( m_canceled )
             break;
 
-        it2 = m_model->executeQuery( QString( "select ?name ?rel ?type ?offset \
?length where { " +        it2 = model->executeQuery( QString::fromLatin1( "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> . " @@ -124,7 +136,7 @@
             addNewMatch( entity );
 
             // find relations for the entity
-            Soprano::QueryResultIterator it3 = m_model->executeQuery( QString( \
"select ?verb ?offset ?length ?exact where { " +            \
Soprano::QueryResultIterator it3 = model->executeQuery( QString::fromLatin1( "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 . " @@ -155,7 +167,7 @@
         }
     }
 
-    delete m_model;
+    delete model;
 }
 
 
--- trunk/playground/base/nepomuk-kde/scribo/plugins/opencalais/worker.h \
#1130803:1130804 @@ -23,10 +23,9 @@
 
 #include <QtCore/QThread>
 
+#include <Soprano/Graph>
+
 class OpenCalaisTextMatchPlugin;
-namespace Soprano {
-    class Model;
-}
 namespace Scribo {
     class TextMatch;
 }
@@ -40,23 +39,27 @@
     Worker( OpenCalaisTextMatchPlugin* plugin );
     ~Worker();
 
-    void setModel( Soprano::Model* model ) {
-        m_model = model;
+    void setData( Soprano::Graph data ) {
+        m_data = data;
     }
 
     void run();
     void cancel();
 
+    QString errorText() const { return m_errorText; }
+
 Q_SIGNALS:
     void newMatch( const Scribo::TextMatch& );
 
 private:
     void addNewMatch( const Scribo::TextMatch& );
 
-    Soprano::Model* m_model;
+    Soprano::Graph m_data;
     OpenCalaisTextMatchPlugin* m_plugin;
 
     bool m_canceled;
+
+    QString m_errorText;
 };
 
 #endif


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

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