25 #include <QtCore/QTimer>
26 #include <QtCore/QFile>
30 #include <Soprano/QueryResultIterator>
31 #include <Soprano/PluginManager>
32 #include <Soprano/Serializer>
33 #include <Soprano/Parser>
34 #include <Soprano/Util/SimpleStatementIterator>
36 using namespace Nepomuk2;
37 using namespace Nepomuk2::Backup;
40 const QString& outputFile,
QObject* parent)
43 , m_inputFile(inputFile)
44 , m_outputFile(outputFile)
51 QTimer::singleShot( 0,
this, SLOT(doJob()) );
55 QList<QUrl> fetchGraphApps(Soprano::Model* model,
const QUrl& graph) {
56 QString query = QString::fromLatin1(
"select ?a where { %1 nao:maintainedBy ?a . }")
57 .arg( Soprano::Node::resourceToN3( graph) );
59 Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparqlNoInference );
67 QList<Soprano::Statement> fetchStatements(QSet<QUrl>& pushedUris, Soprano::Model* model,
const QUrl& uri) {
68 if( pushedUris.contains(uri) )
69 return QList<Soprano::Statement>();
71 QString query = QString::fromLatin1(
"select ?p ?o ?g where { graph ?g { %1 ?p ?o. } }")
72 .arg( Soprano::Node::resourceToN3(uri) );
74 QList<Soprano::Statement> stList;
76 Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparqlNoInference );
78 Soprano::Statement st( uri, it[0], it[1], it[2] );
81 graphs << st.context().uri();
83 pushedUris.insert( uri );
85 foreach(
const QUrl& g, graphs) {
86 stList << fetchStatements( pushedUris, model, g );
93 QUrl fetchGraph(Soprano::Model* model,
const QString& identifier) {
94 QString query = QString::fromLatin1(
"select ?r where { ?r a nrl:Graph ; nao:maintainedBy ?app ."
95 " ?app nao:identifier %1 . } LIMIT 1")
96 .arg( Soprano::Node::literalToN3( identifier ) );
98 Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparql );
106 void GraphGenerator::doJob()
108 Soprano::PluginManager* pg = Soprano::PluginManager::instance();
109 const Soprano::Serializer* serializer = pg->discoverSerializerForSerialization( Soprano::SerializationNQuads );
110 const Soprano::Parser* parser = pg->discoverParserForSerialization( Soprano::SerializationNQuads );
112 QFile input( m_inputFile );
114 QFile output( m_outputFile );
115 if( !output.open( QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text ) ) {
117 setErrorText( QString::fromLatin1(
"Could not open file %1").arg( m_outputFile ) );
122 QTextStream outputStream( &output );
126 kDebug() <<
"output:" << m_outputFile;
127 Soprano::StatementIterator it = parser->parseFile( m_inputFile, QUrl(), Soprano::SerializationNQuads );
128 if( parser->lastError() ) {
129 QString error = QString::fromLatin1(
"Failed to generate backup: %1").arg( parser->lastError().message() );
131 setErrorText( error );
137 QUrl nepomukGraph = fetchGraph( m_model, QLatin1String(
"nepomuk") );
139 Soprano::Statement st = it.current();
140 const QUrl origGraph = st.context().uri();
142 QList<Soprano::Statement> stList;
143 QList<QUrl> apps = fetchGraphApps( m_model, origGraph );
144 if( apps.isEmpty() ) {
145 st.setContext( nepomukGraph );
148 foreach(
const QUrl& app, apps) {
150 if( fit == appGraphHash.end() ) {
154 if( apps.size() == 1 ) {
155 fit = appGraphHash.insert( app, origGraph );
160 QString gq = QString::fromLatin1(
"select ?g where { ?g nao:maintainedBy %1. "
161 "FILTER NOT EXISTS { ?g nao:maintainedBy ?app. FILTER(?app!=%1) .}"
163 .arg( Soprano::Node::resourceToN3(app) );
165 Soprano::QueryResultIterator iter = m_model->executeQuery( gq, Soprano::Query::QueryLanguageSparql );
167 const QUrl graph = iter[0].uri();
168 fit = appGraphHash.insert( app, graph );
173 fit = appGraphHash.insert( app, origGraph );
178 st.setContext( fit.value() );
182 Soprano::Util::SimpleStatementIterator iter( stList );
183 serializer->serialize( iter, outputStream, Soprano::SerializationNQuads );
185 m_numStatements += stList.size();
188 emitPercent( count, m_inputCount );
192 QSet<QUrl> pushedUris;
195 QHashIterator<QUrl, QUrl> hit( appGraphHash );
196 while( hit.hasNext() ) {
199 QList<Soprano::Statement> stList;
200 stList << fetchStatements( pushedUris, m_model, hit.key() );
201 stList << fetchStatements( pushedUris, m_model, hit.value() );
203 Soprano::Util::SimpleStatementIterator iter( stList );
204 serializer->serialize( iter, outputStream, Soprano::SerializationNQuads );
206 m_numStatements += stList.size();
209 emitPercent( count, m_inputCount );
219 return m_numStatements;
GraphGenerator(Soprano::Model *model, const QString &inputFile, const QString &outputFile, QObject *parent)