-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquerythread.cpp
More file actions
238 lines (198 loc) · 7.96 KB
/
Copy pathquerythread.cpp
File metadata and controls
238 lines (198 loc) · 7.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "querythread.h"
#include "vqbglobal.h"
#include <QString>
#include <QList>
#include <QStringList>
#include <QMutex>
#include <QTimer>
#include <QSet>
#include <QPair>
#include <QtAlgorithms>
#include <Soprano/Soprano>
#include <Soprano/Model>
#include <Soprano/Client/DBusClient>
#include <Soprano/Client/DBusModel>
#include <Soprano/Util/DummyModel>
#include <Soprano/QueryResultIterator>
#include <Soprano/Inference/InferenceModel>
#include <kdebug.h>
QueryThread::QueryThread(QObject *parent)
: QThread(parent)
{
qRegisterMetaType<QList<QStringPair> >("QList<QStringPair>");//user-defined types need to be registered to be used as signals
}
void QueryThread::run()
{
if(m_queryMode == QueryThread::SingleQuery) {
//prefixes are not added here, but after the whole query has been built
singleQuery();
}
else if(m_queryMode == QueryThread::SingleQueryPairs) {
m_query = VqbGlobal::addPrefixes(m_query);
singleQueryPairs();
}
else if(m_queryMode == QueryThread::IncrementalQuery) {
//prefixes are not added here, but after the whole query has been built
incrementalQuery();
}
}
void QueryThread::singleQuery()
{
//NOTE: for fast results, just select DISTINCT with LIMIT 300
QString s = "SELECT DISTINCT " + this->m_varName + " WHERE { " + this->m_query + " } LIMIT 300";
//add prefixes
s = VqbGlobal::addPrefixes( s );
kDebug() << "---000--- Running query: " << s;
Soprano::Model* m = QueryThread::nepomukMainModel();
Soprano::QueryResultIterator it = m->executeQuery( s, Soprano::Query::QueryLanguageSparql );
QSet<QString> results;
QList<Soprano::BindingSet> allStatements = it.allBindings();
kDebug() << "---000--- " << allStatements.count();
QString val;
foreach (Soprano::BindingSet s, allStatements ) {
Soprano::Node n = s.value(this->m_varName.replace("?", ""));
if ( n.isResource() ) {
val = n.uri().toString();
val = VqbGlobal::prefixForm( val );
} else if ( n.isLiteral() ) {
QString dtUri = n.literal().dataTypeUri().toString();
val = "\"" + n.literal().toString() + "\"" + (dtUri.isEmpty() ? "" : "^^<"+ dtUri +">");
}
//emit(resultFound(val));//notification of a new item
results.insert(val);
}
kDebug() << "---000--- Query done.";
emit queryDone(results.toList());
}
void QueryThread::singleQueryPairs()
{
//kDebug() << "\n\n *** Querying: " << m_query;
Soprano::Model* m = QueryThread::nepomukMainModel();
Soprano::QueryResultIterator it = m->executeQuery(m_query, Soprano::Query::QueryLanguageSparql);
QList<QStringPair> res;
QList<Soprano::BindingSet> allStatements = it.allBindings();
QPair<QString,QString> val;
Soprano::Node n;
foreach(Soprano::BindingSet s, allStatements) {
n = s.value(0);
if (n.isResource()) {
val.first = n.uri().toString();
//val = VisualQueryBuilderConsts::getPrefixForm( val );
} else if (n.isLiteral()) {
//val = "\"" + n.literal().toString() + "\"^^<"+ n.literal().dataTypeUri().toString()+">";
val.first = n.literal().toString();
//kDebug() << "__||__ Datatype: " << n.dataType();
}
n = s.value(1);
if (n.isResource()) {
val.second = n.uri().toString();
val.second = VqbGlobal::prefixForm( val.second );
} else if (n.isLiteral()) {
//val = "\"" + n.literal().toString() + "\"^^<"+ n.literal().dataTypeUri().toString()+">";
val.second = n.literal().toString();
//kDebug() << "__||__ Datatype: " << n.dataType();
}
//kDebug() << "Found: " << val.first << val.second;
res.append(val);
}
//kDebug() << "\n\n ===> Results: " << ": \n";
//FIXME: try to find a more efficient solution (that doesn't create the whole list again)
qSort(res.begin(), res.end(), QueryThread::caseInsensitiveLessThan);
emit queryDone(res);
}
void QueryThread::incrementalQuery()
{
//NOTE: for fast results, just select DISTINCT with LIMIT 300
QString s = "SELECT DISTINCT " + this->m_varName + " WHERE { " + this->m_query + " } LIMIT 300";
//add prefixes
s = VqbGlobal::addPrefixes( s );
kDebug() << "---000--- Running query: " << s;
Soprano::Model* m = QueryThread::nepomukMainModel();
Soprano::QueryResultIterator it = m->executeQuery( s, Soprano::Query::QueryLanguageSparql );
//QSet<QString> results;
QList<Soprano::BindingSet> allStatements = it.allBindings();
//kDebug() << "---000--- " << allStatements.count();
QString val;
foreach (Soprano::BindingSet s, allStatements ) {
Soprano::Node n = s.value(this->m_varName.replace("?", ""));
if ( n.isResource() ) {
val = n.uri().toString();
val = VqbGlobal::prefixForm( val );
} else if ( n.isLiteral() ) {
QString dtUri = n.literal().dataTypeUri().toString();
val = "\"" + n.literal().toString() + "\"" + (dtUri.isEmpty() ? "" : "^^<"+ dtUri +">");
}
//if(!results.contains(val)) {
//kDebug() << "Item found: " << val;
emit(resultFound(val));//notification of a new item
// results.insert(val);
//}
}
kDebug() << "---000--- Query done.";
}
void QueryThread::setQuery(QString query, QString varName, QueryMode queryMode)
{
m_query = query;
m_varName = varName;
m_queryMode = queryMode;
}
/**************** utility functions ************/
static Soprano::Model *s_model = 0;
//static Soprano::Inference::InferenceModel *s_im = 0;
Soprano::Model* QueryThread::nepomukMainModel()
{
// we use a dummy test model here
if (!s_model) {
static Soprano::Client::DBusClient client("org.kde.NepomukServer");
s_model = client.createModel("main");
}
if (!s_model) {
s_model = new Soprano::Util::DummyModel();
}
/*if (!s_im) {
s_im = new Soprano::Inference::InferenceModel(s_model);
//s_im->addStatements( s_model->listStatements().allStatements() );
s_im->performInference();
}
*/
//return s_im;
return s_model;
}
QStringList QueryThread::queryResults( QString query, QString freeVar )
{
QString s = "SELECT DISTINCT " + freeVar + " WHERE { " + query + " } LIMIT 300";//"SELECT * WHERE {" + query + "} LIMIT 1";
//add prefixes
s = VqbGlobal::addPrefixes( s );
//kDebug() << "--- Running query: " << s;
Soprano::Model* m = QueryThread::nepomukMainModel();
Soprano::QueryResultIterator it = m->executeQuery( s, Soprano::Query::QueryLanguageSparql );
QStringList res;
QList<Soprano::BindingSet> allStatements = it.allBindings();
QString val;
foreach (Soprano::BindingSet s, allStatements ) {
Soprano::Node n = s.value(freeVar.replace("?", ""));
if ( n.isResource() ) {
val = n.uri().toString();
val = VqbGlobal::prefixForm( val );
} else if ( n.isLiteral() ) {
QString dtUri = n.literal().dataTypeUri().toString();
val = "\"" + n.literal().toString() + "\"" + (dtUri.isEmpty() ? "" : "^^<"+ dtUri +">");
}
//kDebug() << "--- Found: " << val;
res << val;
}
return res;
}
int QueryThread::countQueryResults( QString query )
{
query = VqbGlobal::addPrefixes(query);
Soprano::QueryResultIterator it = QueryThread::nepomukMainModel()->executeQuery( query, Soprano::Query::QueryLanguageSparql );
return it.allBindings().count();
}
bool QueryThread::caseInsensitiveLessThan(const QStringPair &s1, const QStringPair &s2)
{
return (s1.first.toLower() == s2.first.toLower() ?
s1.second.toLower() < s2.second.toLower() :
s1.first.toLower() < s2.first.toLower());
}
#include "querythread.moc"