• 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
statementgenerator.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 "statementgenerator.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/Util/SimpleStatementIterator>
34 #include <Soprano/Vocabulary/NAO>
35 
36 #include "nfo.h"
37 
38 using namespace Nepomuk2;
39 using namespace Nepomuk2::Backup;
40 using namespace Nepomuk2::Vocabulary;
41 using namespace Soprano::Vocabulary;
42 
43 StatementGenerator::StatementGenerator(Soprano::Model* model, const QString& inputFile,
44  const QString& outputFile, QObject* parent)
45  : KJob(parent)
46  , m_model(model)
47  , m_inputFile(inputFile)
48  , m_outputFile(outputFile)
49  , m_filter(Filter_None)
50  , m_statementCount(0)
51 {
52 }
53 
54 void StatementGenerator::start()
55 {
56  QTimer::singleShot( 0, this, SLOT(doJob()) );
57 }
58 
59 void StatementGenerator::setFilter(StatementGenerator::Filter filter)
60 {
61  m_filter = filter;
62 }
63 
64 void StatementGenerator::doJob()
65 {
66  Soprano::PluginManager* pg = Soprano::PluginManager::instance();
67  const Soprano::Serializer* serializer = pg->discoverSerializerForSerialization( Soprano::SerializationNQuads );
68 
69  QFile input( m_inputFile );
70  kDebug() << "INPUT: " << m_inputFile;
71  if( !input.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
72  setError(1);
73  setErrorText( QString::fromLatin1("Could not open file %1").arg( m_inputFile ) );
74  emitResult();
75  return;
76  }
77 
78  QFile output( m_outputFile );
79  if( !output.open( QIODevice::ReadWrite | QIODevice::Append | QIODevice::Text ) ) {
80  setError(1);
81  setErrorText( QString::fromLatin1("Could not open file %1").arg( m_outputFile ) );
82  emitResult();
83  return;
84  }
85 
86  QTextStream inputStream( &input );
87  QTextStream outputStream( &output );
88 
89  int count = 0;
90  while( !inputStream.atEnd() ) {
91  const QUrl uri( inputStream.readLine() );
92  kDebug() << "Processing" << uri;
93  QList<Soprano::Statement> stList;
94 
95  if( m_filter == Filter_None ) {
96  stList << fetchNonDiscardableStatements(uri);
97  if( stList.count() ) {
98  QStringList properties;
99  properties << "nao:lastModified" << "nao:created" << "rdf:type" << "nie:url";
100 
101  stList << fetchProperties(uri, properties);
102  }
103  }
104  else if( m_filter == Filter_TagsAndRatings ) {
105  if( hasType(uri, NFO::FileDataObject()) ) {
106  QList<QString> fileProperties;
107  fileProperties << "rdf:type" << "nao:lastModified" << "nao:created"
108  << "nie:url" << "nao:numericRating" << "nao:hasTag" << "nao:description";
109 
110  stList << fetchProperties(uri, fileProperties);
111  }
112  else {
113  bool hasPrefLabel = false;
114  Soprano::Statement identifierSt;
115 
116  QList<Soprano::Statement> tagStatements = fetchAllStatements(uri);
117  foreach(const Soprano::Statement& st, tagStatements) {
118  // Do not link to other Nepmouk objects
119  // FIXME: What about the symbols for tags?
120  if( st.object().isResource() && st.object().uri().scheme() == QLatin1String("nepomuk") )
121  continue;
122 
123  if( st.predicate().uri() == NAO::prefLabel() )
124  hasPrefLabel = true;
125  else if( st.predicate().uri() == NAO::identifier() )
126  identifierSt = st;
127 
128  stList << st;
129  }
130 
131  if( !hasPrefLabel ) {
132  identifierSt.setPredicate( NAO::prefLabel() );
133  stList << identifierSt;
134  }
135  }
136  }
137 
138  if( !stList.isEmpty() ) {
139  kDebug() << stList;
140  Soprano::Util::SimpleStatementIterator iter( stList );
141  serializer->serialize( iter, outputStream, Soprano::SerializationNQuads );
142 
143  m_statementCount += stList.size();
144  }
145 
146  if( m_resourceCount ) {
147  count++;
148  emitPercent( count, m_resourceCount );
149  }
150  }
151 
152  emitResult();
153 }
154 
155 namespace {
159  bool isValid(const Soprano::Statement& st) {
160  if( !st.isValid() )
161  return false;
162 
163  if( !st.subject().isResource() )
164  return false;
165 
166  if( !st.context().isResource() )
167  return false;
168 
169  Soprano::Node object = st.object();
170  if( !object.isResource() && !object.isLiteral() )
171  return false;
172 
173  if( object.isLiteral() ) {
174  Soprano::LiteralValue lv = object.literal();
175  if( lv.dataTypeUri().isEmpty() ) {
176  // Plain nodes in Nepomuk don't have a language tag
177  if( lv.isPlain() && !lv.toString().isEmpty() )
178  return true;
179  return false;
180  }
181 
182  if( lv.isString() && lv.toString().isEmpty() )
183  return false;
184  }
185 
186  return true;
187  }
188 }
189 
190 bool StatementGenerator::hasType(const QUrl& uri, const QUrl& type)
191 {
192  QString query = QString::fromLatin1("ask where { %1 a %2 . }")
193  .arg( Soprano::Node::resourceToN3(uri),
194  Soprano::Node::resourceToN3(type) );
195 
196  return m_model->executeQuery( query, Soprano::Query::QueryLanguageSparql ).boolValue();
197 }
198 
199 QList<Soprano::Statement> StatementGenerator::fetchProperties(const QUrl& uri, QStringList properties)
200 {
201  QList<Soprano::Statement> stList;
202  QString query = QString::fromLatin1("select distinct ?p ?o ?g where { graph ?g { %1 ?p ?o. } "
203  " FILTER(?p in (%2)) . }")
204  .arg( Soprano::Node::resourceToN3(uri),
205  properties.join(",") );
206 
207  Soprano::QueryResultIterator it = m_model->executeQuery( query, Soprano::Query::QueryLanguageSparqlNoInference );
208  while( it.next() ) {
209  Soprano::Statement st( uri, it[0], it[1], it[2] );
210  if( isValid(st) )
211  stList << st;
212  }
213 
214  return stList;
215 }
216 
217 QList< Soprano::Statement > StatementGenerator::fetchNonDiscardableStatements(const QUrl& uri)
218 {
219  QString query = QString::fromLatin1("select distinct ?p ?o ?g where { graph ?g { %1 ?p ?o. } "
220  " FILTER(!(?p in (nao:lastModified, nao:created, rdf:type, nie:url))) ."
221  " ?g a nrl:InstanceBase ."
222  " FILTER NOT EXISTS { ?g a nrl:DiscardableInstanceBase . } }")
223  .arg( Soprano::Node::resourceToN3(uri) );
224 
225  QList<Soprano::Statement> stList;
226  Soprano::QueryResultIterator it = m_model->executeQuery( query, Soprano::Query::QueryLanguageSparqlNoInference );
227  while( it.next() ) {
228  Soprano::Statement st( uri, it[0], it[1], it[2] );
229  if( isValid(st) )
230  stList << st;
231  }
232 
233  return stList;
234 }
235 
236 QList< Soprano::Statement > StatementGenerator::fetchAllStatements(const QUrl& uri)
237 {
238  QString query = QString::fromLatin1("select distinct ?p ?o ?g where { graph ?g { %1 ?p ?o. } }")
239  .arg( Soprano::Node::resourceToN3(uri) );
240 
241  QList<Soprano::Statement> stList;
242  Soprano::QueryResultIterator it = m_model->executeQuery( query, Soprano::Query::QueryLanguageSparqlNoInference );
243  while( it.next() ) {
244  Soprano::Statement st( uri, it[0], it[1], it[2] );
245  if( isValid(st) )
246  stList << st;
247  }
248 
249  return stList;
250 }
Nepomuk2::Backup::StatementGenerator::StatementGenerator
StatementGenerator(Soprano::Model *model, const QString &inputFile, const QString &outputFile, QObject *parent)
Definition: statementgenerator.cpp:43
Nepomuk2::Backup::StatementGenerator::Filter_None
Definition: statementgenerator.h:46
QObject
Nepomuk2::Backup::StatementGenerator::start
virtual void start()
Definition: statementgenerator.cpp:54
Nepomuk2::Backup::StatementGenerator::Filter_TagsAndRatings
Definition: statementgenerator.h:47
Nepomuk2::Backup::StatementGenerator::Filter
Filter
Definition: statementgenerator.h:45
Nepomuk2::Backup::StatementGenerator::setFilter
void setFilter(Filter filter)
Definition: statementgenerator.cpp:59
statementgenerator.h
KJob
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:09 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