• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

Nepomuk-Core

  • sources
  • kde-4.12
  • kdelibs
  • nepomuk-core
  • services
  • storage
  • backup
graphgenerator.cpp
Go to the documentation of this file.
1 /*
2  * <one line to give the library's name and an idea of what it does.>
3  * Copyright 2013 Vishesh Handa <me@vhanda.in>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of
8  * the License or (at your option) version 3 or any later version
9  * accepted by the membership of KDE e.V. (or its successor approved
10  * by the membership of KDE e.V.), which shall act as a proxy
11  * defined in Section 14 of version 3 of the license.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #include "graphgenerator.h"
24 
25 #include <QtCore/QTimer>
26 #include <QtCore/QFile>
27 
28 #include <KDebug>
29 
30 #include <Soprano/QueryResultIterator>
31 #include <Soprano/PluginManager>
32 #include <Soprano/Serializer>
33 #include <Soprano/Parser>
34 #include <Soprano/Util/SimpleStatementIterator>
35 
36 using namespace Nepomuk2;
37 using namespace Nepomuk2::Backup;
38 
39 GraphGenerator::GraphGenerator(Soprano::Model* model, const QString& inputFile,
40  const QString& outputFile, QObject* parent)
41  : KJob(parent)
42  , m_model(model)
43  , m_inputFile(inputFile)
44  , m_outputFile(outputFile)
45  , m_numStatements(0)
46 {
47 }
48 
49 void GraphGenerator::start()
50 {
51  QTimer::singleShot( 0, this, SLOT(doJob()) );
52 }
53 
54 namespace {
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) );
58 
59  Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparqlNoInference );
60  QList<QUrl> apps;
61  while( it.next() )
62  apps << it[0].uri();
63 
64  return apps;
65  }
66 
67  QList<Soprano::Statement> fetchStatements(QSet<QUrl>& pushedUris, Soprano::Model* model, const QUrl& uri) {
68  if( pushedUris.contains(uri) )
69  return QList<Soprano::Statement>();
70 
71  QString query = QString::fromLatin1("select ?p ?o ?g where { graph ?g { %1 ?p ?o. } }")
72  .arg( Soprano::Node::resourceToN3(uri) );
73 
74  QList<Soprano::Statement> stList;
75  QSet<QUrl> graphs;
76  Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparqlNoInference );
77  while( it.next() ) {
78  Soprano::Statement st( uri, it[0], it[1], it[2] );
79  stList << st;
80 
81  graphs << st.context().uri();
82  }
83  pushedUris.insert( uri );
84 
85  foreach(const QUrl& g, graphs) {
86  stList << fetchStatements( pushedUris, model, g );
87  }
88 
89  return stList;
90  }
91 }
92 namespace {
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 ) );
97 
98  Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparql );
99  if( it.next() )
100  return it[0].uri();
101 
102  return QUrl();
103  }
104 }
105 
106 void GraphGenerator::doJob()
107 {
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 );
111 
112  QFile input( m_inputFile );
113 
114  QFile output( m_outputFile );
115  if( !output.open( QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text ) ) {
116  setError(1);
117  setErrorText( QString::fromLatin1("Could not open file %1").arg( m_outputFile ) );
118  emitResult();
119  return;
120  }
121 
122  QTextStream outputStream( &output );
123 
124  QHash<QUrl, QUrl> appGraphHash;
125 
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() );
130  setError(1);
131  setErrorText( error );
132  emitResult();
133  return;
134  }
135 
136  int count=0;
137  QUrl nepomukGraph = fetchGraph( m_model, QLatin1String("nepomuk") );
138  while( it.next() ) {
139  Soprano::Statement st = it.current();
140  const QUrl origGraph = st.context().uri();
141 
142  QList<Soprano::Statement> stList;
143  QList<QUrl> apps = fetchGraphApps( m_model, origGraph );
144  if( apps.isEmpty() ) {
145  st.setContext( nepomukGraph );
146  stList << st;
147  }
148  foreach(const QUrl& app, apps) {
149  QHash< QUrl, QUrl >::iterator fit = appGraphHash.find( app );
150  if( fit == appGraphHash.end() ) {
151  m_inputCount++;
152  // If there is only one app, then you can take assume origGraph
153  // to only be maintained by app
154  if( apps.size() == 1 ) {
155  fit = appGraphHash.insert( app, origGraph );
156  }
157  else {
158  // We will need to find a graph for each app which is only
159  // maintained by 'app' and not other applications
160  QString gq = QString::fromLatin1("select ?g where { ?g nao:maintainedBy %1. "
161  "FILTER NOT EXISTS { ?g nao:maintainedBy ?app. FILTER(?app!=%1) .}"
162  "} LIMIT 1")
163  .arg( Soprano::Node::resourceToN3(app) );
164 
165  Soprano::QueryResultIterator iter = m_model->executeQuery( gq, Soprano::Query::QueryLanguageSparql );
166  if( iter.next() ) {
167  const QUrl graph = iter[0].uri();
168  fit = appGraphHash.insert( app, graph );
169  }
170  else {
171  // The app only exists in graphs shared by others. Now what?
172  Q_ASSERT(0);
173  fit = appGraphHash.insert( app, origGraph );
174  }
175  }
176  }
177 
178  st.setContext( fit.value() );
179  stList << st;
180  }
181 
182  Soprano::Util::SimpleStatementIterator iter( stList );
183  serializer->serialize( iter, outputStream, Soprano::SerializationNQuads );
184 
185  m_numStatements += stList.size();
186  if( m_inputCount ) {
187  count++;
188  emitPercent( count, m_inputCount );
189  }
190  }
191 
192  QSet<QUrl> pushedUris;
193 
194  // Push all the graphs
195  QHashIterator<QUrl, QUrl> hit( appGraphHash );
196  while( hit.hasNext() ) {
197  hit.next();
198 
199  QList<Soprano::Statement> stList;
200  stList << fetchStatements( pushedUris, m_model, hit.key() );
201  stList << fetchStatements( pushedUris, m_model, hit.value() );
202 
203  Soprano::Util::SimpleStatementIterator iter( stList );
204  serializer->serialize( iter, outputStream, Soprano::SerializationNQuads );
205 
206  m_numStatements += stList.size();
207  if( m_inputCount ) {
208  count++;
209  emitPercent( count, m_inputCount );
210  }
211  }
212 
213  emitResult();
214 }
215 
216 
217 int GraphGenerator::numStatements()
218 {
219  return m_numStatements;
220 }
Nepomuk2::Backup::GraphGenerator::numStatements
int numStatements()
Definition: graphgenerator.cpp:217
QHash
QObject
graphgenerator.h
Nepomuk2::Backup::GraphGenerator::GraphGenerator
GraphGenerator(Soprano::Model *model, const QString &inputFile, const QString &outputFile, QObject *parent)
Definition: graphgenerator.cpp:39
Nepomuk2::Backup::GraphGenerator::start
virtual void start()
Definition: graphgenerator.cpp:49
KJob
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Nepomuk-Core

Skip menu "Nepomuk-Core"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal