-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstraint.cpp
More file actions
427 lines (343 loc) · 14.1 KB
/
constraint.cpp
File metadata and controls
427 lines (343 loc) · 14.1 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#include "constraint.h"
#include "querythread.h"
#include "vqbform.h"
#include "combobox.h"
#include <QList>
#include <QPainter>
#include <QTimer>
#include <QPoint>
#include <QAction>
#include <QLayout>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QComboBox>
#include <QLineEdit>
#include <kdebug.h>
#include <kstandardaction.h>
#include <kaction.h>
#include <krandom.h>
Constraint::Constraint(int constraintNo, QWidget * parent, bool isAttached, QString parentVarName, QString parentClassName)
: QGroupBox(QString("Constraint %1").arg(constraintNo + 1), parent)
{
m_constraintNo = constraintNo;
m_parent = (VqbForm *) parent;
m_parentVarName = parentVarName;
m_parentClass = parentClassName;
m_isAttached = isAttached;
init();
}
// ************* INIT AND EVENTS ***************
void Constraint::init()
{
if (m_isAttached) {
setTitle(QString("Constraint %1 (attached to %2)").arg(m_constraintNo + 1).arg(m_parentVarName));
}
setLayout(new QVBoxLayout());
m_relations << "contains" << "equals" ;
addConstraintLine(true); //isFirst = true
if (!m_isAttached) {
findSubjectsWithLabels(false); //filter = false
} else { //if it is attached
addSubjects(QList<StringPair>());
}
setAttribute(Qt::WA_DeleteOnClose); //delete when closed
QAction *removeAction = KStandardAction::close(this, SLOT(close()), this);
removeAction->setText(tr("Remove &constraint"));
removeAction->setStatusTip(tr("Removes this constraint from the query and GUI"));
removeAction->setShortcut(0);
QAction *refresh = KStandardAction::redisplay(this, SLOT(returnConstraint()), this);
refresh->setText(tr("&Refresh constraint"));
refresh->setShortcut(0);
/*QAction *attach = KStandardAction::forward( this, SLOT( attach() ), this );
attach->setText( tr("&Attach constraint") );
attach->setShortcut( 0 );
*/
this->addAction(removeAction);
this->addAction(refresh);
//this->addAction( attach );
this->setContextMenuPolicy(Qt::ActionsContextMenu);
}
void Constraint::paintEvent(QPaintEvent * event)
{
QGroupBox::paintEvent(event);
QPainter painter(this);
}
// ************* UTILITIES ***************
QString Constraint::getQueryConstraint()
{
QString s = constraintLines.first().s->itemData(constraintLines.first().s->currentIndex()).toString();
if (s.isEmpty()) {
return QString();
}
//Defining the instance
QString varS = constraintLines.first().s->varName;
QString q;
QString o;
QString p;
//if( constraintLines.count() > 1 ) {//if there's only one line, it will be generated below
q.append(varS + " a <" + s + "> . \n");
//}
//adding the constraint lines
int i = 0;
while (i < (constraintLines.count() - 1)) {
p = constraintLines[i].p->itemData(constraintLines[i].p->currentIndex()).toString();
q.append(varS + " <" + p + "> ");
varS = constraintLines[i+1].s->varName; //the subject variable of the next constraint line
q.append(varS + ". \n");
//q.append( varS + " a <" + s + "> .\n" ); //maybe needed, maybe not
i++;
}
//final (Literal) matching
p = constraintLines[i].p->itemData(constraintLines[i].p->currentIndex()).toString();
s = constraintLines[i].s->itemData(constraintLines[i].s->currentIndex()).toString(); //FIXME: might result in a NULL pointer
QString rel = constraintLines[i].rel->currentText();
o = constraintLines[i].o->currentText();
//if( !o.isEmpty() ) {
if (!p.isEmpty()) {
q.append("" + varS + " <" + p + "> ");
varS = constraintLines[i].o->varName;
q.append(varS + " . ");
QString filterStr;
if (rel == "equals") {
filterStr = QString(" FILTER regex(" + varS + ", '^" + o + "$', 'i') .\n");
} else if (rel == "contains") {
filterStr = QString(" FILTER regex(" + varS + ", '" + o + "', 'i') .\n") ;
}
//kDebug() << " *** filter string: " << filterStr;
q.append(filterStr);
}
/*}
else {
q.append( varS + " a <" + s + "> .\n" );
}
*/
return q;
}
QString Constraint::getRandomVarName()
{
return QString("?v" + QString::number(KRandom::random() % 80 + 20)) ;
}
// ************* ACTION SLOTS ***************
void Constraint::subjectSelected(int index)
{
//FIXME: remove all constraint lines above this one
QString classS = constraintLines.last().s->itemData(index).toString();
constraintLines.last().s->varClass = classS;//the combobox's class is the class of the selected item
findPredicatesForSubject(classS);
returnConstraint();
}
void Constraint::predicateSelected(int index)
{
//FIXME: remove all constraint lines above this one
QString predicate = constraintLines.last().p->itemData(index).toString();
constraintLines.last().p->varClass = predicate;//the combobox's class is the class of the selected item
findDomainForPredicate(predicate);
addVariableToCB(constraintLines.last().p);
//returnConstraint();
}
void Constraint::threadTerminated()
{
//kDebug() << "Thread is terminated.";
}
void Constraint::unblockPredicate()
{
constraintLines.last().p->blockSignals(false);
}
void Constraint::returnConstraint()
{
emit constraintChanged(m_constraintNo, getQueryConstraint());
}
void Constraint::close()
{
emit constraintChanged(m_constraintNo, QString());
//FIXME: remove variables from the output list
QGroupBox::close();
}
// ************* QUERYING SLOTS ***************
void Constraint::findSubjectsWithLabels(bool filter)
{
QueryThread *qt = new QueryThread(this);
connect(qt, SIGNAL(queryDone(QList<StringPair>)),
this, SLOT(addSubjects(QList<StringPair>)));
connect(qt, SIGNAL(terminated()),
this, SLOT(threadTerminated()));
if (filter && constraintLines.count() >= 2) { //if NOT the first line: filters on previous subject and predicate
//FIXME: replace with query below (don't use labels)
QString p = constraintLines.at(constraintLines.count() - 2).p->currentText();
QString s = constraintLines.at(constraintLines.count() - 2).s->currentText();
QString q = QString("SELECT DISTINCT ?label ?classP WHERE {"
" ?property <http://www.w3.org/2000/01/rdf-schema#label> \"%1\""
". ?property <http://www.w3.org/2000/01/rdf-schema#domain> ?classS "
". ?property <http://www.w3.org/2000/01/rdf-schema#range> ?classP "
". ?classS <http://www.w3.org/2000/01/rdf-schema#label> \"%2\""
". ?classP <http://www.w3.org/2000/01/rdf-schema#label> ?label }")
.arg(p).arg(s);
/*int indx = constraintLines.count() - 2;
p = constraintLines[indx].p->itemData( constraintLines[indx].p->currentIndex() ).toString();
s = constraintLines[indx].s->itemData( constraintLines[indx].s->currentIndex() ).toString();//FIXME: might result in a NULL pointer
QString q = QString("SELECT DISTINCT ?label ?classP WHERE {"
" ?property <http://www.w3.org/2000/01/rdf-schema#label> \"%1\""
". ?property <http://www.w3.org/2000/01/rdf-schema#domain> ?classS "
". ?property <http://www.w3.org/2000/01/rdf-schema#range> ?classP "
". ?classS <http://www.w3.org/2000/01/rdf-schema#label> \"%2\""
". ?classP <http://www.w3.org/2000/01/rdf-schema#label> ?label }")
.arg( p ).arg( s );*/
qt->setQuery(q);
} else {
qt->setQuery("SELECT DISTINCT ?label ?class WHERE"
"{ ?resource a ?class . ?class <http://www.w3.org/2000/01/rdf-schema#label> ?label }");
}
qt->start();
}
void Constraint::findPredicatesForSubject(QString subject)
{
QueryThread *qt = new QueryThread(this);
connect(qt, SIGNAL(queryDone(QList<StringPair>)),
this, SLOT(addPredicates(QList<StringPair>)));
QString query = QString("SELECT DISTINCT ?label ?property WHERE {"
" ?property <http://www.w3.org/2000/01/rdf-schema#domain> <%1> ."
" OPTIONAL { ?property <http://www.w3.org/2000/01/rdf-schema#label> ?label } }").arg(subject);
qt->setQuery(query);
qt->start();
}
void Constraint::findDomainForPredicate(QString predicate)
{
QueryThread *qt = new QueryThread(this);
connect(qt, SIGNAL(queryDone(QList<StringPair>)),
this, SLOT(addPredicateDomain(QList<StringPair>)));
//FIXME: check parent properties too? Or the foaf:Person is the subclass of owl:Thing, just Sorano doesn't know it?
QString query = QString("SELECT DISTINCT ?class WHERE {"
" <%1> <http://www.w3.org/2000/01/rdf-schema#range> ?class }").arg(predicate);
//query.arg( predicate );
qt->setQuery(query);
qt->start();
}
// ************* INSERTING SLOTS ***************
void Constraint::addConstraintLine(bool isFirst)
{
QHBoxLayout * layout = new QHBoxLayout();
ComboBox *cb1 = new ComboBox();
if (isFirst && m_isAttached) { //if this is the first line in an attached constraint
cb1->setEditText(m_parentVarName);
cb1->setEditable(false);
cb1->varName = m_parentVarName;
cb1->varClass = m_parentClass;
} else {
cb1->setEditable(true);
}
ComboBox *cb2 = new ComboBox();
ComboBox *cb3 = new ComboBox();
ComboBox *cb4 = new ComboBox();
cb2->setEditable(true);
cb4->setEditable(true);
layout->addWidget(cb1, 1);
layout->addWidget(cb2, 1);
layout->addWidget(cb3, 1);
layout->addWidget(cb4, 1);
((QBoxLayout*)(this->layout()))->addLayout(layout);
ConstraintLine cl;
cl.s = cb1;
cl.p = cb2;
cl.rel = cb3;
cl.o = cb4;
constraintLines.append(cl);
connect(cl.s, SIGNAL(currentIndexChanged(int)),
this, SLOT(subjectSelected(int)));
connect(cl.p, SIGNAL(currentIndexChanged(int)),
this, SLOT(predicateSelected(int)));
connect(cl.rel, SIGNAL(currentIndexChanged(int)),
this, SLOT(returnConstraint()));
connect(cl.o, SIGNAL(editTextChanged(QString)),
this, SLOT(returnConstraint()));
}
void Constraint::addSubjects(QList<StringPair> subjects)
{
constraintLines.last().s->clear();
constraintLines.last().p->clear();
//FIXME: might add variable action several times?
if (!m_isAttached) {
addVariableToCB(constraintLines.last().s);
}
addActionsToCB(constraintLines.last().s);
if (m_isAttached && constraintLines.count() == 1) { //if we are dealing with the varName of the previous constraint
constraintLines.last().s->addItem(m_parentVarName, m_parentClass);
return;
}
foreach(StringPair sp, subjects) {
constraintLines.last().s->addItem(sp.s1, sp.s2); //add s2 as the data associated to the item
//kDebug() << "++**++ Added item" << sp.s1 << sp.s2;
}
//constraintLines.last().s->setCurrentIndex( -1 );
}
void Constraint::addPredicates(QList<StringPair> predicates)
{
constraintLines.last().p->blockSignals(true); //don't trigger predicate domain population
constraintLines.last().p->clear();
foreach(StringPair sp, predicates) {
//FIXME: if label is empty, parse (I don't know what I meant by this) - ?????
constraintLines.last().p->addItem(sp.s1.isEmpty() ? sp.s2 : sp.s1, sp.s2); //add s2 as the data associated to the item
}
constraintLines.last().p->setCurrentIndex(-1); //insertItem( 0, "?p" );
constraintLines.last().p->blockSignals(false);
}
void Constraint::addPredicateDomain(QList<StringPair> subjects)
{
if (!subjects.isEmpty()) {
//kDebug() << "Comparing " << subjects.first().s1 << " to " << "http://www.w3.org/2000/01/rdf-schema#Literal";
if (subjects.first().s1 == "http://www.w3.org/2001/XMLSchema#string" ||
subjects.first().s1 == "http://www.w3.org/2000/01/rdf-schema#Literal") {
constraintLines.last().rel->clear();
constraintLines.last().rel->insertItems(0, m_relations);
addVariableToCB(constraintLines.last().o);
} else if (!subjects.empty()) {
//disable editable part
constraintLines.last().rel->setDisabled(true);
constraintLines.last().o->setDisabled(true);
//add new constraint line
addConstraintLine();
findSubjectsWithLabels(true);
}
}
}
void Constraint::addVariableToCB(ComboBox *cb)
{
QString v = getRandomVarName();
cb->setToolTip(v);
cb->varName = v;
QAction *add = KStandardAction::findNext(cb, SLOT(addToOutput()), this);
add->setText(QString("Add " + cb->varName + " to output"));
add->setStatusTip(tr("Adds the variable to the output list"));
add->setShortcut(0);
connect(cb, SIGNAL(addVarToOutput(QString)),
m_parent, SLOT(addVarToOutput(QString)));
foreach(QAction *a, cb->actions()) {
cb->removeAction(a);
}
cb->addAction(add);
cb->setContextMenuPolicy(Qt::ActionsContextMenu);
}
void Constraint::addActionsToCB(ComboBox *cb)
{
QAction *add = KStandardAction::findNext(cb, SLOT(addToOutput()), this);
add->setText(QString("Add " + cb->varName + " to output"));
add->setStatusTip(tr("Adds the variable to the output list"));
add->setShortcut(0);
QAction *attach = KStandardAction::redo(cb, SLOT(attach()), this);
attach->setText(QString("&Attach constraint to " + cb->varName));
attach->setShortcut(0);
cb->actions().clear();//clear previous actions
foreach(QAction *a, cb->actions()) {
cb->removeAction(a);
}
cb->addAction(add);
cb->addAction(attach);
cb->setContextMenuPolicy(Qt::ActionsContextMenu);
disconnect(cb, SIGNAL(addVarToOutput(QString)), //disconnect slots from that signal
0, 0);
connect(cb, SIGNAL(addVarToOutput(QString)),
m_parent, SLOT(addVarToOutput(QString)));
connect(cb, SIGNAL(attachConstraint(int, QString, QString)),
m_parent, SLOT(attachConstraint(int, QString, QString)));
}
#include "constraint.moc"