-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstardog_test.py
125 lines (88 loc) · 3.44 KB
/
stardog_test.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
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
# stardog_test
import time
# _start_time = time.time()
# print("Running...")
# print("Started at", _start_time, "and running...")
# def secondsToStr(t):
# return "%d:%02d:%02d.%03d" % \
# reduce(lambda ll,b : divmod(ll[0],b) + ll[1:],
# [(t*1000,),1000,60,60])
import stardog
from pprint import pprint
# help(stardog.Connection.add)
# exit(1)
# stardog query --reasoning rule_ex "SELECT ?s ?v { ?s :area ?v } LIMIT 10"
from stardog_credentails import *
def run_query(ontology_prefix=None):
print("Running SPARQL query...")
ontology_prefix = ontology_prefix or "http://vstu.ru/poas/ctrl_structs_2020-05_v1#"
# conn_details = {
# 'endpoint': 'http://localhost:5820',
# 'username': 'admin',
# 'password': 'admin'
# }
# dbname = "ctrlstrct_db"
# # dbname = "rule_ex"
# # graphname = "urn:graph1"
with stardog.Connection(dbname, **conn_details) as conn:
# query = """PREFIX onto: <http://vstu.ru/poas/ctrl_structs_2020-05_v1#>
# SELECT DISTINCT ?s ?o WHERE {
# # ?s a ?o .
# # ?o rdfs:subClassOf onto:Erroneous
# ?o rdfs:subClassOf onto:act
# }"""
# ?o a onto:act_begin
# ?s a ?o .
# SELECT ?s ?o WHERE { ?o rdfs:subClassOf onto:act }
# ?s a / rdfs:subClassOf onto:act . # prop chain!
# ?o a onto:act . # indirect cast does not work without reasoning!
query = """PREFIX onto: <%s>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT DISTINCT * WHERE {
# ?s a onto:DebugObj .
# ?o a onto:DebugObj .
?s a onto:correct_act .
# ?s a onto:act .
# ?s a onto:trace .
# ?s ?p ?o .
?s a ?o .
# ?p a owl:DatatypeProperty .
# ?s onto:next ?o .
# ?s onto:before ?o .
# ?s onto:next_sibling ?o .
# ?s onto:before ?o .
# ?o rdfs:subClassOf onto:Erroneous
# ?o rdfs:subClassOf onto:correct_act
}""" % ontology_prefix
# r = conn.select(query, reasoning=True)
# r = conn.select(query, reasoning=False)
r = conn.select(query, reasoning=bool(1))
pprint(r['results']['bindings'])
print(len(r['results']['bindings']), "total.")
def request_graph(ontology_prefix=None):
print("Running CONSTRUCT query...")
ontology_prefix = ontology_prefix or "http://vstu.ru/poas/ctrl_structs_2020-05_v1#"
with stardog.Connection(dbname, **conn_details) as conn:
query = """PREFIX onto: <%s>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
construct {?s ?p ?o} where {?s ?p ?o}
""" % ontology_prefix
r = conn.graph(query, reasoning=bool(1))
# pprint(r)
# print(len(r['results']['bindings']), "total.")
with open("stdg_db_dump.ttl", "wb") as f:
f.write(r)
def main(ontology_prefix=None) -> float:
_start_time = time.time()
try:
# run_query(ontology_prefix)
request_graph(ontology_prefix)
# pass
except Exception as e:
print(e)
pass
seconds = time.time() - _start_time
print("[%.2f sec elapsed]" % seconds, end=" ")
return seconds
if __name__ == '__main__':
main()