-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueryData.py
72 lines (63 loc) · 2.75 KB
/
queryData.py
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
# This Python file uses the following encoding: utf-8
#!/usr/bin/python
from SPARQLWrapper import SPARQLWrapper, JSON
tante_query = """
PREFIX wrr: <http://github.com/frimelle/Wissensrepraesentation/relation/>
SELECT *
WHERE { { ?kind wrr:sohnVon ?eltern . } UNION { ?kind wrr:tochterVon ?eltern . }
{?tante wrr:schwesterVon ?eltern . } }
"""
cousine_query = """
PREFIX wrr: <http://github.com/frimelle/Wissensrepraesentation/relation/>
SELECT *
WHERE { { ?kind wrr:sohnVon ?eltern . } UNION { ?kind wrr:tochterVon ?eltern . }
{?tanteOnkel wrr:schwesterVon ?eltern . } UNION {?tanteOnkel wrr:bruderVon ?eltern . }
{?cousine wrr:tochterVon ?tanteOnkel . }}
"""
cousin_query = """
PREFIX wrr: <http://github.com/frimelle/Wissensrepraesentation/relation/>
SELECT *
WHERE { { ?kind wrr:sohnVon ?eltern . } UNION { ?kind wrr:tochterVon ?eltern . }
{?tanteOnkel wrr:schwesterVon ?eltern . } UNION {?tanteOnkel wrr:bruderVon ?eltern . }
{?cousin wrr:sohnVon ?tanteOnkel . }}
"""
nichte_query = """
PREFIX wrr: <http://github.com/frimelle/Wissensrepraesentation/relation/>
SELECT *
WHERE { { ?kind wrr:bruderVon ?geschwister . } UNION { ?kind wrr:schwesterVon ?geschwister . }
{?nichte wrr:tochterVon ?geschwister . }}
"""
ehemann_query = """
PREFIX wrr: <http://github.com/frimelle/Wissensrepraesentation/relation/>
SELECT *
WHERE { { ?nichte wrr:ehemannVon ?kind . } }
"""
def queryData( sparql_query ):
try:
sparql = SPARQLWrapper("http://localhost:8000/sparql/")
sparql.setQuery( sparql_query )
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
return results
except Exception, e:
raise e
### Query nach Tante
results_tante = queryData( tante_query )
for result in results_tante["results"]["bindings"]:
print result["tante"]['value'] + " tante von " + result["kind"]['value']
### Query nach Cousine
results_cousine = queryData( cousine_query )
for result in results_cousine["results"]["bindings"]:
print result["cousine"]['value'] + " cousine von " + result["kind"]['value']
### Query nach Cousin
results_cousin = queryData( cousin_query )
for result in results_cousin["results"]["bindings"]:
print result["cousin"]['value'] + " cousin von " + result["kind"]['value']
### Query nach Nichte
results_nichte = queryData( nichte_query )
for result in results_nichte["results"]["bindings"]:
print result["nichte"]['value'] + " nichte von " + result["kind"]['value']
### Query nach Ehemann
results_ehemann = queryData( ehemann_query )
for result in results_ehemann["results"]["bindings"]:
print result["nichte"]['value'] + " ehemann von " + result["kind"]['value']