22 #include <Soprano/Backend>
23 #include <Soprano/PluginManager>
24 #include <Soprano/Global>
25 #include <Soprano/Version>
26 #include <Soprano/StorageModel>
27 #include <Soprano/QueryResultIterator>
28 #include <Soprano/Node>
29 #include <Soprano/Error/Error>
30 #include <Soprano/Vocabulary/RDF>
31 #include <Soprano/Vocabulary/NAO>
32 #include <Soprano/Vocabulary/NRL>
34 #include <KStandardDirs>
36 #include <KConfigGroup>
37 #include <KSharedConfig>
39 #include <KNotification>
41 #include <KIO/DeleteJob>
42 #include <kdbusconnectionpool.h>
44 #include <QtCore/QTimer>
45 #include <QtCore/QFile>
46 #include <QtCore/QThread>
47 #include <QtCore/QCoreApplication>
48 #include <QtDBus/QDBusConnection>
50 using namespace Soprano::Vocabulary;
53 QString createStoragePath(
const QString& repositoryId )
55 return KStandardDirs::locateLocal(
"data",
"nepomuk/repository/" + repositoryId +
'/' );
64 m_classAndPropertyTree( 0 ),
65 m_inferenceModel( 0 ),
66 m_dataManagementModel( 0 ),
67 m_dataManagementAdaptor( 0 ),
69 m_ontologyLoader( 0 ),
72 m_dummyModel =
new Soprano::Util::DummyModel();
95 delete m_dataManagementAdaptor;
96 m_dataManagementAdaptor = 0;
98 setParentModel(m_dummyModel);
99 delete m_dataManagementModel;
100 m_dataManagementModel = 0;
102 delete m_inferenceModel;
103 m_inferenceModel = 0;
105 delete m_classAndPropertyTree;
106 m_classAndPropertyTree = 0;
111 delete m_ontologyLoader;
112 m_ontologyLoader = 0;
120 Q_ASSERT( m_state == CLOSED );
126 m_backend = Soprano::PluginManager::instance()->discoverBackendByName( QLatin1String(
"virtuosobackend" ) );
128 KNotification::event(
"failedToStart",
129 i18nc(
"@info - notification message",
130 "Nepomuk Semantic Desktop needs the Virtuoso RDF server to store its data. "
131 "Installing the Virtuoso Soprano plugin is mandatory for using Nepomuk." ),
132 KIcon(
"application-exit" ).pixmap( 32, 32 ),
134 KNotification::Persistent );
136 emit opened(
this,
false );
139 else if ( !m_backend->isAvailable() ) {
140 KNotification::event(
"failedToStart",
141 i18nc(
"@info - notification message",
142 "Nepomuk Semantic Desktop needs the Virtuoso RDF server to store its data. "
143 "Installing the Virtuoso server and ODBC driver is mandatory for using Nepomuk." ),
144 KIcon(
"application-exit" ).pixmap( 32, 32 ),
146 KNotification::Persistent );
148 emit opened(
this,
false );
154 KConfigGroup repoConfig = KSharedConfig::openConfig(
"nepomukserverrc" )->group( name() +
" Settings" );
155 QString basePath = repoConfig.readPathEntry(
"Storage Dir", QString() );
157 if( basePath.isEmpty() ) {
158 m_basePath = createStoragePath(name());
160 repoConfig.writeEntry(
"GraphMigrationRequired",
false);
163 m_basePath = basePath;
165 m_storagePath = m_basePath +
"data/" + m_backend->pluginName();
166 Soprano::BackendSettings settings = readVirtuosoSettings();
168 if ( !KStandardDirs::makeDir( m_storagePath ) ) {
169 kDebug() <<
"Failed to create storage folder" << m_storagePath;
171 emit opened(
this,
false );
175 kDebug() <<
"opening repository '" << name() <<
"' at '" << m_basePath <<
"'";
181 QObject* qobj =
dynamic_cast<QObject*
>(
const_cast<Soprano::Backend*
>(m_backend));
182 connect( qobj, SIGNAL(virtuosoInitParameters(
int, QString)),
183 this, SLOT(slotVirtuosoInitParameters(
int,QString)) );
187 Soprano::settingInSettings( settings, Soprano::BackendOptionStorageDir ).setValue( m_storagePath );
188 m_model = m_backend->createModel( settings );
190 kDebug() <<
"Unable to create model for repository" << name();
191 kError() << m_backend->lastError();
193 emit opened(
this,
false );
198 QRegExp regex(
"(6\\.1\\.[6789])");
199 if( !m_virtuosoVersion.contains(regex) ) {
200 kError() <<
"NepomukStorage only works with virtuoso 6.1.6 and beyond";
204 connect(m_model, SIGNAL(virtuosoStopped(
bool)),
this, SLOT(slotVirtuosoStopped(
bool)));
206 kDebug() <<
"Successfully created new model for repository" << name();
218 setParentModel( m_model );
221 repoConfig.writeEntry(
"Used Soprano Backend", m_backend->pluginName() );
222 repoConfig.writePathEntry(
"Storage Dir", m_basePath );
223 repoConfig.writeEntry(
"Port", m_port );
227 emit opened(
this,
true );
234 return m_backend->pluginName();
240 Soprano::BackendSettings Nepomuk2::Repository::readVirtuosoSettings()
const
242 Soprano::BackendSettings settings;
244 KConfigGroup repoConfig = KSharedConfig::openConfig(
"nepomukserverrc" )->group( name() +
" Settings" );
245 const int maxMem = repoConfig.readEntry(
"Maximum memory", 50 );
248 settings << Soprano::BackendSetting(
"buffers", qMax( 4, maxMem-30 )*100 );
251 settings << Soprano::BackendSetting(
"CheckpointInterval", 10 );
254 settings << Soprano::BackendSetting(
"MinAutoCheckpointSize", 200000 );
257 settings << Soprano::BackendSetting(
"fulltextindex",
"sync" );
260 settings << Soprano::BackendSetting(
"forcedstart",
true );
264 settings << Soprano::BackendSetting(
"ServerThreads", 100 );
267 settings << Soprano::BackendSetting(
"noStatementSignals",
true );
270 settings << Soprano::BackendSetting(
"fakeBooleans",
false );
271 settings << Soprano::BackendSetting(
"emptyGraphs",
false );
284 emit loaded(
this,
false);
288 m_ontologyLoader =
new OntologyLoader(
this,
this );
289 connect( m_ontologyLoader, SIGNAL(ontologyUpdateFinished(
bool)),
290 this, SLOT(slotOntologiesLoaded(
bool)) );
291 m_ontologyLoader->updateLocalOntologies();
294 void Nepomuk2::Repository::slotOntologiesLoaded(
bool somethingChanged)
296 updateInference(somethingChanged);
297 if( m_state == OPEN ) {
299 emit loaded(
this,
true);
303 void Nepomuk2::Repository::updateInference(
bool ontologiesChanged)
308 QString query = QString::fromLatin1(
"select ?g ?abr where { ?r %1 ?g ; %2 ?abr . }")
309 .arg( Soprano::Node::resourceToN3( NAO::hasDefaultNamespace() ),
310 Soprano::Node::resourceToN3( NAO::hasDefaultNamespaceAbbreviation() ) );
312 Soprano::QueryResultIterator it = executeQuery( query, Soprano::Query::QueryLanguageSparql );
314 QString ontology = it[0].toString();
315 QString prefix = it[1].toString();
317 prefixes.insert( prefix, ontology );
321 QString command = QString::fromLatin1(
"DB.DBA.XML_SET_NS_DECL( '%1', '%2', 2 )")
322 .arg( prefix, ontology );
324 executeQuery( command, Soprano::Query::QueryLanguageUser, QLatin1String(
"sql") );
328 if( m_dataManagementAdaptor )
329 m_dataManagementAdaptor->setPrefixes(prefixes);
332 m_classAndPropertyTree->rebuildTree(
this);
333 m_inferenceModel->updateOntologyGraphs(ontologiesChanged);
336 void Nepomuk2::Repository::slotVirtuosoInitParameters(
int port,
const QString& version)
339 m_virtuosoVersion = version;
343 void Nepomuk2::Repository::slotVirtuosoStopped(
bool normalExit)
346 kDebug() <<
"Virtuoso was killed or crashed. Restarting the repository.";
356 delete m_dataManagementAdaptor;
357 m_dataManagementAdaptor = 0;
359 setParentModel( m_inferenceModel );
360 connect(m_model, SIGNAL(virtuosoStopped(
bool)),
this, SLOT(slotVirtuosoStopped(
bool)));
362 delete m_dataManagementModel;
363 m_dataManagementModel = 0;
368 if( m_dataManagementModel && m_dataManagementAdaptor )
371 closePublicInterface();
375 m_dataManagementModel =
new DataManagementModel(m_classAndPropertyTree, m_inferenceModel,
this);
376 setParentModel(m_dataManagementModel);
379 connect(m_model, SIGNAL(virtuosoStopped(
bool)),
this, SLOT(slotVirtuosoStopped(
bool)));
383 QDBusConnection con = KDBusConnectionPool::threadConnection();
384 con.registerObject(QLatin1String(
"/datamanagement"), m_dataManagementAdaptor,
385 QDBusConnection::ExportScriptableContents);
388 #include "repository.moc"
void openPublicInterface()
Registers the datamangement interface this is used to communicate with the rest of world...
Represents the main Nepomuk model.
void opened(Repository *, bool success)
Emitted when the Repository successfully opened.
QString usedSopranoBackend() const
void closePublicInterface()
Switches off the datamanagement interface that is used to communicate with the rest of the world...
void open()
Will emit the opened signal.
Repository(const QString &name)