• 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
  • libnepomukcore
  • resource
nepomukmainmodel.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Nepomuk KDE project.
3  * Copyright (C) 2008-2010 Sebastian Trueg <trueg@kde.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 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  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB. If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #include "nepomukmainmodel.h"
22 #include "resourcemanager.h"
23 
24 #include <Soprano/Node>
25 #include <Soprano/Statement>
26 #include <Soprano/StatementIterator>
27 #include <Soprano/NodeIterator>
28 #include <Soprano/QueryResultIterator>
29 #include <Soprano/Query/QueryLanguage>
30 #include <Soprano/Util/DummyModel>
31 #include <Soprano/StorageModel>
32 
33 #include <Soprano/Backend>
34 #include <Soprano/PluginManager>
35 
36 #include <kglobal.h>
37 #include <kstandarddirs.h>
38 #include <kdebug.h>
39 #include <ksharedconfig.h>
40 #include <KConfigGroup>
41 
42 #include <QtCore/QTimer>
43 #include <QtCore/QMutex>
44 #include <QtCore/QMutexLocker>
45 #include <QtCore/QFile>
46 
47 using namespace Soprano;
48 
49 class Nepomuk2::MainModel::Private
50 {
51 public:
52  Private()
53  : virtuosoModel( 0 ),
54  dummyModel( 0 ),
55  m_initMutex( QMutex::Recursive ) {
56  }
57 
58  ~Private() {
59  delete virtuosoModel;
60  delete dummyModel;
61  }
62 
63  Soprano::StorageModel* virtuosoModel;
64  Soprano::Util::DummyModel* dummyModel;
65 
66  void init( bool forced ) {
67  QMutexLocker lock( &m_initMutex );
68 
69  if( !forced && virtuosoModel )
70  return;
71 
72  Soprano::PluginManager* pm = Soprano::PluginManager::instance();
73  const Soprano::Backend* backend = pm->discoverBackendByName( QLatin1String( "virtuosobackend" ) );
74 
75  if ( !backend || !backend->isAvailable() ) {
76  kError() << "Could not find virtuoso backend";
77  }
78 
79  Soprano::BackendSettings settings;
80 
81  KConfig config("nepomukserverrc");
82  KConfigGroup repoConfig = config.group( "main Settings" );
83  int portNumber = repoConfig.readEntry("Port", 0);
84  if(!portNumber) {
85  kError() << "Could not find virtuoso to connect to. Aborting";
86  return;
87  }
88 
89  settings << Soprano::BackendSetting( Soprano::BackendOptionHost, "localhost" );
90  settings << Soprano::BackendSetting( Soprano::BackendOptionPort, portNumber );
91  settings << Soprano::BackendSetting( Soprano::BackendOptionUsername, "dba" );
92  settings << Soprano::BackendSetting( Soprano::BackendOptionPassword, "dba" );
93  settings << Soprano::BackendSetting( "noStatementSignals", true );
94  settings << Soprano::BackendSetting( "fakeBooleans", false );
95  settings << Soprano::BackendSetting( "emptyGraphs", false );
96 
97  // FIXME: Can we really delete the model? What about open iterators?
98  if( virtuosoModel )
99  virtuosoModel->deleteLater();
100 
101  virtuosoModel = backend ? backend->createModel( settings ) : 0;
102  // Listen to the virtuoso model crashing?
103  }
104 
105  Soprano::Model* model() {
106  QMutexLocker lock( &m_initMutex );
107 
108  init( false );
109 
110  if ( virtuosoModel ) {
111  return virtuosoModel;
112  }
113  else {
114  if ( !dummyModel ) {
115  dummyModel = new Soprano::Util::DummyModel();
116  }
117  return dummyModel;
118  }
119  }
120 
121  QMutex m_initMutex;
122 };
123 
124 
125 Nepomuk2::MainModel::MainModel( QObject* parent )
126  : Soprano::Model(),
127  d( new Private() )
128 {
129  setParent( parent );
130 }
131 
132 
133 Nepomuk2::MainModel::~MainModel()
134 {
135  delete d;
136 }
137 
138 
139 bool Nepomuk2::MainModel::isValid() const
140 {
141  QMutexLocker lock( &d->m_initMutex );
142  return d->virtuosoModel;
143 }
144 
145 
146 bool Nepomuk2::MainModel::init()
147 {
148  d->init( true );
149  return isValid();
150 }
151 
152 void Nepomuk2::MainModel::disconnect()
153 {
154  QMutexLocker lock( &d->m_initMutex );
155  d->virtuosoModel->deleteLater();
156  d->virtuosoModel = 0;
157 }
158 
159 
160 Soprano::StatementIterator Nepomuk2::MainModel::listStatements( const Statement& partial ) const
161 {
162  Soprano::StatementIterator it = d->model()->listStatements( partial );
163  setError( d->model()->lastError() );
164  return it;
165 }
166 
167 
168 Soprano::NodeIterator Nepomuk2::MainModel::listContexts() const
169 {
170  Soprano::NodeIterator it = d->model()->listContexts();
171  setError( d->model()->lastError() );
172  return it;
173 }
174 
175 //
176 // Copied from services/storage/virtuosoinferencemodel.cpp
177 //
178 namespace {
179  const char* s_nepomukInferenceRuleSetName = "nepomukinference";
180 }
181 
182 Soprano::QueryResultIterator Nepomuk2::MainModel::executeQuery( const QString& query,
183  Soprano::Query::QueryLanguage language,
184  const QString& userQueryLanguage ) const
185 {
186  Soprano::QueryResultIterator it;
187  if(language == Soprano::Query::QueryLanguageSparqlNoInference) {
188  it = d->model()->executeQuery(query, Soprano::Query::QueryLanguageSparql);
189  }
190  else if(language == Soprano::Query::QueryLanguageSparql ) {
191  it = d->model()->executeQuery(QString::fromLatin1("DEFINE input:inference <%1> ")
192  .arg(QLatin1String(s_nepomukInferenceRuleSetName)) + query, language);
193  }
194  else {
195  it = d->model()->executeQuery(query, language, userQueryLanguage);
196  }
197  setError( d->model()->lastError() );
198  return it;
199 }
200 
201 
202 bool Nepomuk2::MainModel::containsStatement( const Statement& statement ) const
203 {
204  bool b = d->model()->containsStatement( statement );
205  setError( d->model()->lastError() );
206  return b;
207 }
208 
209 
210 bool Nepomuk2::MainModel::containsAnyStatement( const Statement &statement ) const
211 {
212  bool b = d->model()->containsAnyStatement( statement );
213  setError( d->model()->lastError() );
214  return b;
215 }
216 
217 
218 bool Nepomuk2::MainModel::isEmpty() const
219 {
220  bool b = d->model()->isEmpty();
221  setError( d->model()->lastError() );
222  return b;
223 }
224 
225 
226 int Nepomuk2::MainModel::statementCount() const
227 {
228  int c = d->model()->statementCount();
229  setError( d->model()->lastError() );
230  return c;
231 }
232 
233 
234 Soprano::Error::ErrorCode Nepomuk2::MainModel::addStatement( const Statement& statement )
235 {
236  Soprano::Error::ErrorCode c = d->model()->addStatement( statement );
237  setError( d->model()->lastError() );
238  return c;
239 }
240 
241 
242 Soprano::Error::ErrorCode Nepomuk2::MainModel::removeStatement( const Statement& statement )
243 {
244  Soprano::Error::ErrorCode c = d->model()->removeStatement( statement );
245  setError( d->model()->lastError() );
246  return c;
247 }
248 
249 
250 Soprano::Error::ErrorCode Nepomuk2::MainModel::removeAllStatements( const Statement& statement )
251 {
252  Soprano::Error::ErrorCode c = d->model()->removeAllStatements( statement );
253  setError( d->model()->lastError() );
254  return c;
255 }
256 
257 
258 Soprano::Node Nepomuk2::MainModel::createBlankNode()
259 {
260  Soprano::Node n = d->model()->createBlankNode();
261  setError( d->model()->lastError() );
262  return n;
263 }
264 
265 #include "nepomukmainmodel.moc"
Nepomuk2::ErrorCode
ErrorCode
Definition: resource.h:44
Nepomuk2::MainModel::createBlankNode
Soprano::Node createBlankNode()
Definition: nepomukmainmodel.cpp:258
Nepomuk2::MainModel::isValid
bool isValid() const
Check the connection to the Nepomuk server.
Definition: nepomukmainmodel.cpp:139
Nepomuk2::MainModel::MainModel
MainModel(QObject *parent=0)
Create a new main model.
Definition: nepomukmainmodel.cpp:125
Nepomuk2::MainModel::containsAnyStatement
bool containsAnyStatement(const Soprano::Statement &statement) const
Definition: nepomukmainmodel.cpp:210
QObject
Nepomuk2::MainModel::init
bool init()
Try connecting to the Nepomuk storage service.
Definition: nepomukmainmodel.cpp:146
Nepomuk2::MainModel::containsStatement
bool containsStatement(const Soprano::Statement &statement) const
Definition: nepomukmainmodel.cpp:202
Nepomuk2::MainModel::addStatement
Soprano::Error::ErrorCode addStatement(const Soprano::Statement &statement)
Definition: nepomukmainmodel.cpp:234
Nepomuk2::MainModel::statementCount
int statementCount() const
Definition: nepomukmainmodel.cpp:226
Nepomuk2::MainModel::listContexts
Soprano::NodeIterator listContexts() const
Definition: nepomukmainmodel.cpp:168
Nepomuk2::MainModel::listStatements
Soprano::StatementIterator listStatements(const Soprano::Statement &partial) const
Definition: nepomukmainmodel.cpp:160
resourcemanager.h
Nepomuk2::MainModel::disconnect
void disconnect()
This function explicitly marks the connection to soprano as disconnected.
Definition: nepomukmainmodel.cpp:152
Nepomuk2::MainModel::~MainModel
~MainModel()
Destructor.
Definition: nepomukmainmodel.cpp:133
nepomukmainmodel.h
Nepomuk2::MainModel::removeStatement
Soprano::Error::ErrorCode removeStatement(const Soprano::Statement &statement)
Definition: nepomukmainmodel.cpp:242
Nepomuk2::MainModel::isEmpty
bool isEmpty() const
Definition: nepomukmainmodel.cpp:218
Nepomuk2::MainModel::executeQuery
Soprano::QueryResultIterator executeQuery(const QString &query, Soprano::Query::QueryLanguage language, const QString &userQueryLanguage=QString()) const
Definition: nepomukmainmodel.cpp:182
Nepomuk2::MainModel::removeAllStatements
Soprano::Error::ErrorCode removeAllStatements(const Soprano::Statement &statement)
Definition: nepomukmainmodel.cpp:250
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