• 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
resourceidentifier.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 (C) 2011 Vishesh Handa <handa.vish@gmail.com>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 
20 
21 #include "resourceidentifier.h"
22 #include "syncresource.h"
23 #include "classandpropertytree.h"
24 
25 #include <QtCore/QDateTime>
26 #include <QtCore/QSet>
27 
28 #include <Soprano/Node>
29 #include <Soprano/Model>
30 #include <Soprano/QueryResultIterator>
31 #include <Soprano/Vocabulary/NAO>
32 #include <Soprano/StatementIterator>
33 #include <Soprano/NodeIterator>
34 #include <Soprano/Vocabulary/RDFS>
35 #include <Soprano/Vocabulary/RDF>
36 #include "nie.h"
37 #include "nco.h"
38 
39 #include <KDebug>
40 
41 using namespace Soprano::Vocabulary;
42 using namespace Nepomuk2::Vocabulary;
43 
44 namespace {
46  template<typename T> QStringList resourcesToN3(const T& urls) {
47  QStringList n3;
48  Q_FOREACH(const QUrl& url, urls) {
49  n3 << Soprano::Node::resourceToN3(url);
50  }
51  return n3;
52  }
53 }
54 
55 Nepomuk2::ResourceIdentifier::ResourceIdentifier( Nepomuk2::StoreIdentificationMode mode,
56  Soprano::Model *model)
57  : Nepomuk2::Sync::ResourceIdentifier( model ),
58  m_mode( mode )
59 {
60  m_metaProperties.insert( NAO::created() );
61  m_metaProperties.insert( NAO::lastModified() );
62  m_metaProperties.insert( NAO::userVisible() );
63  m_metaProperties.insert( NAO::creator() );
64 }
65 
66 
67 bool Nepomuk2::ResourceIdentifier::exists(const KUrl& uri)
68 {
69  // Special case for blank nodes
70  if( uri.url().startsWith("_:") )
71  return false;
72 
73  QString query = QString::fromLatin1("ask { %1 ?p ?o . } ").arg( Soprano::Node::resourceToN3(uri) );
74  return m_model->executeQuery( query, Soprano::Query::QueryLanguageSparql ).boolValue();
75 }
76 
77 KUrl Nepomuk2::ResourceIdentifier::duplicateMatch(const KUrl& origUri,
78  const QSet<KUrl>& matchedUris )
79 {
80  Q_UNUSED( origUri );
81  //
82  // We return the uri that has the oldest nao:created
83  // For backwards compatibility we keep in mind that three are resources which do not have nao:created defined.
84  //
85  Soprano::QueryResultIterator it
86  = m_model->executeQuery(QString::fromLatin1("select ?r where { ?r %1 ?date . FILTER(?r in (%2)) . } ORDER BY ASC(?date) LIMIT 1")
87  .arg(Soprano::Node::resourceToN3(NAO::created()),
88  resourcesToN3(matchedUris).join(QLatin1String(","))),
89  Soprano::Query::QueryLanguageSparql);
90  if(it.next()) {
91  return it[0].uri();
92  }
93  else {
94  // FIXME: fallback to what? a random one from the set?
95  return KUrl();
96  }
97 }
98 
99 bool Nepomuk2::ResourceIdentifier::isIdentifyingProperty(const QUrl& uri)
100 {
101  if( m_metaProperties.contains( uri ) ) {
102  return false;
103  }
104  else {
105  return ClassAndPropertyTree::self()->isDefiningProperty(uri);
106  }
107 }
108 
109 namespace {
110  QUrl fetchResource(Soprano::Model* model, const QString& prop, const QString& value) {
111  QString query = QString::fromLatin1("select ?r where { ?r %1 %2 . } LIMIT 1")
112  .arg( prop, value );
113  Soprano::QueryResultIterator it = model->executeQuery( query, Soprano::Query::QueryLanguageSparqlNoInference );
114  if( it.next() ) {
115  return it[0].uri();
116  }
117 
118  return QUrl();
119  }
120 }
121 
122 bool Nepomuk2::ResourceIdentifier::runIdentification(const KUrl& uri)
123 {
124  //kDebug() << "Identifying : " << uri;
125  //
126  // Check if a uri with the same name exists
127  //
128  if( exists( uri ) ) {
129  manualIdentification( uri, uri );
130  return true;
131  }
132 
133  const Sync::SyncResource & res = simpleResource( uri );
134  //kDebug() << res;
135 
136  //
137  // Check if a uri with the same nie:url exists
138  //
139  QUrl nieUrl = res.nieUrl();
140  if( !nieUrl.isEmpty() ) {
141  QUrl newUri = fetchResource(m_model, QLatin1String("nie:url"), Soprano::Node::resourceToN3(nieUrl));
142  if (!newUri.isEmpty()) {
143  kDebug() << uri << " --> " << newUri;
144  manualIdentification( uri, newUri );
145  return true;
146  }
147 
148  return false;
149  }
150 
151  // If IdentifyNone mode, then we do not run the full identification
152  if( m_mode == IdentifyNone )
153  return false;
154 
155  //
156  // Check if it is a Contact has the same contactUID
157  //
158  QList<Soprano::Node> types = res.property( RDF::type() );
159  // HACK: We should ideally check for all nco:Contacts, but meh. No one really pushes
160  // a contactUID with a nco:Contact. Both Telepathy and Akonadi push it with a PersonContact
161  if( types.contains(NCO::PersonContact()) ) {
162  QList<Soprano::Node> ids = res.property(NCO::contactUID());
163  if( ids.size() == 1 ) {
164  QString id = ids.first().literal().toString();
165  if( id.isEmpty() )
166  return false;
167 
168  QUrl newUri = fetchResource(m_model, QLatin1String("nco:contactUID"), ids.first().toN3());
169  if( newUri.isEmpty() ) {
170  kDebug() << uri << " --> " << newUri;
171  manualIdentification( uri, newUri );
172  return true;
173  }
174  return false;
175  }
176  }
177 
178  // Never identify data objects
179  foreach(const Soprano::Node& t, res.property(RDF::type())) {
180  QSet<QUrl> allT = ClassAndPropertyTree::self()->allParents(t.uri());
181  allT << t.uri();
182  if( allT.contains(NIE::DataObject()) ) {
183  kDebug() << "Not identifying" << res.uri() << " - DataObject";
184  return false;
185  }
186  }
187 
188  // Run the normal identification procedure
189  return Sync::ResourceIdentifier::runIdentification( uri );
190 }
Nepomuk2::StoreIdentificationMode
StoreIdentificationMode
The identification mode used by storeResources().
Definition: datamanagement.h:349
resourceidentifier.h
Nepomuk2::Sync::SyncResource::nieUrl
KUrl nieUrl() const
Definition: syncresource.cpp:107
Nepomuk2::ResourceIdentifier::runIdentification
virtual bool runIdentification(const KUrl &uri)
This function returns true if identification was successful, and false if it was not.
Definition: resourceidentifier.cpp:122
classandpropertytree.h
Nepomuk2::ResourceIdentifier::ResourceIdentifier
ResourceIdentifier(Nepomuk2::StoreIdentificationMode mode, Soprano::Model *model)
Definition: resourceidentifier.cpp:55
Nepomuk2::ClassAndPropertyTree::self
static ClassAndPropertyTree * self()
Definition: classandpropertytree.cpp:591
Nepomuk2::Sync::SyncResource::property
QList< Soprano::Node > property(const KUrl &url) const
Definition: syncresource.cpp:138
Nepomuk2::Sync::SyncResource::uri
KUrl uri() const
Definition: syncresource.cpp:127
Nepomuk2::Sync::ResourceIdentifier::runIdentification
virtual bool runIdentification(const KUrl &uri)
This function returns true if identification was successful, and false if it was not.
Definition: syncresourceidentifier.cpp:113
Nepomuk2::IdentifyNone
All resources are treated as new ones.
Definition: datamanagement.h:356
Nepomuk2::ResourceIdentifier::duplicateMatch
virtual KUrl duplicateMatch(const KUrl &uri, const QSet< KUrl > &matchedUris)
Called during identification if there is more than one match for one resource.
Definition: resourceidentifier.cpp:77
Nepomuk2::ResourceIdentifier
Definition: resourceidentifier.h:31
Nepomuk2::ClassAndPropertyTree::allParents
QSet< QUrl > allParents(const QUrl &uri) const
Definition: classandpropertytree.cpp:109
Nepomuk2::ClassAndPropertyTree::isDefiningProperty
bool isDefiningProperty(const QUrl &uri) const
Definition: classandpropertytree.cpp:193
Nepomuk2::resourcesToN3
QStringList resourcesToN3(const T &urls)
Convert a list or set or QUrls into a list of N3 formatted strings.
Definition: nepomuktools.h:35
syncresource.h
Nepomuk2::Sync::SyncResource
A SyncResource is a convenient way of storing a set of properties and objects for a common subject...
Definition: syncresource.h:53
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