mustrd 0.2.0__py3-none-any.whl → 0.2.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mustrd/README.adoc +210 -210
- mustrd/TestResult.py +136 -136
- mustrd/logger_setup.py +48 -48
- mustrd/model/catalog-v001.xml +5 -5
- mustrd/model/mustrdShapes.ttl +253 -253
- mustrd/model/mustrdTestShapes.ttl +24 -24
- mustrd/model/ontology.ttl +494 -494
- mustrd/model/test-resources/resources.ttl +60 -60
- mustrd/model/triplestoreOntology.ttl +174 -174
- mustrd/model/triplestoreshapes.ttl +41 -41
- mustrd/mustrd.py +787 -787
- mustrd/mustrdAnzo.py +236 -220
- mustrd/mustrdGraphDb.py +125 -125
- mustrd/mustrdRdfLib.py +56 -56
- mustrd/mustrdTestPlugin.py +327 -328
- mustrd/namespace.py +125 -125
- mustrd/run.py +106 -106
- mustrd/spec_component.py +690 -690
- mustrd/steprunner.py +166 -166
- mustrd/templates/md_ResultList_leaf_template.jinja +18 -18
- mustrd/templates/md_ResultList_template.jinja +8 -8
- mustrd/templates/md_stats_template.jinja +2 -2
- mustrd/test/test_mustrd.py +4 -4
- mustrd/utils.py +38 -38
- {mustrd-0.2.0.dist-info → mustrd-0.2.1.dist-info}/LICENSE +21 -21
- {mustrd-0.2.0.dist-info → mustrd-0.2.1.dist-info}/METADATA +4 -2
- mustrd-0.2.1.dist-info/RECORD +31 -0
- mustrd/mustrdQueryProcessor.py +0 -136
- mustrd-0.2.0.dist-info/RECORD +0 -32
- {mustrd-0.2.0.dist-info → mustrd-0.2.1.dist-info}/WHEEL +0 -0
- {mustrd-0.2.0.dist-info → mustrd-0.2.1.dist-info}/entry_points.txt +0 -0
mustrd/mustrdAnzo.py
CHANGED
@@ -1,220 +1,236 @@
|
|
1
|
-
"""
|
2
|
-
MIT License
|
3
|
-
|
4
|
-
Copyright (c) 2023 Semantic Partners Ltd
|
5
|
-
|
6
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
-
of this software and associated documentation files (the "Software"), to deal
|
8
|
-
in the Software without restriction, including without limitation the rights
|
9
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
-
copies of the Software, and to permit persons to whom the Software is
|
11
|
-
furnished to do so, subject to the following conditions:
|
12
|
-
|
13
|
-
The above copyright notice and this permission notice shall be included in all
|
14
|
-
copies or substantial portions of the Software.
|
15
|
-
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
-
SOFTWARE.
|
23
|
-
"""
|
24
|
-
|
25
|
-
import requests
|
26
|
-
from pyanzo import AnzoClient
|
27
|
-
from rdflib import Graph, ConjunctiveGraph, Literal, URIRef
|
28
|
-
from requests import ConnectTimeout, Response, HTTPError, RequestException, ConnectionError
|
29
|
-
from bs4 import BeautifulSoup
|
30
|
-
import logging
|
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
|
-
# Get Given or then from the content of a graphmart
|
120
|
-
def get_spec_component_from_graphmart(triple_store: dict, graphmart: URIRef, layer: URIRef = None) -> ConjunctiveGraph:
|
121
|
-
try:
|
122
|
-
anzo_client = AnzoClient(triple_store['url'], triple_store['port'],
|
123
|
-
username=triple_store['username'],
|
124
|
-
password=triple_store['password'])
|
125
|
-
return anzo_client.query_graphmart(graphmart=graphmart,
|
126
|
-
data_layers=layer,
|
127
|
-
query_string="CONSTRUCT {?s ?p ?o} WHERE {?s ?p ?o}",
|
128
|
-
skip_cache=True).as_quad_store().as_rdflib_graph()
|
129
|
-
except RuntimeError as e:
|
130
|
-
raise ConnectionError(f"Anzo connection error, {e}")
|
131
|
-
|
132
|
-
|
133
|
-
def get_query_from_querybuilder(triple_store: dict, folder_name: Literal, query_name: Literal) -> str:
|
134
|
-
query = f"""SELECT ?query WHERE {{
|
135
|
-
graph ?queryFolder {{
|
136
|
-
?bookmark a <http://www.cambridgesemantics.com/ontologies/QueryPlayground#QueryBookmark>;
|
137
|
-
<http://openanzo.org/ontologies/2008/07/System#query> ?query;
|
138
|
-
<http://purl.org/dc/elements/1.1/title> "{query_name}"
|
139
|
-
}}
|
140
|
-
?queryFolder a <http://www.cambridgesemantics.com/ontologies/QueryPlayground#QueryFolder>;
|
141
|
-
<http://purl.org/dc/elements/1.1/title> "{folder_name}"
|
142
|
-
}}"""
|
143
|
-
anzo_client = AnzoClient(triple_store['url'], triple_store['port'],
|
144
|
-
username=triple_store['username'],
|
145
|
-
password=triple_store['password'])
|
146
|
-
|
147
|
-
result = anzo_client.query_journal(query_string=query).as_table_results().as_record_dictionaries()
|
148
|
-
if len(result) == 0:
|
149
|
-
raise FileNotFoundError(f"Query {query_name} not found in folder {folder_name}")
|
150
|
-
return result[0].get("query")
|
151
|
-
|
152
|
-
|
153
|
-
# https://github.com/Semantic-partners/mustrd/issues/102
|
154
|
-
def get_query_from_step(triple_store: dict, query_step_uri: URIRef) -> str:
|
155
|
-
query = f"""SELECT ?stepUri ?query WHERE {{
|
156
|
-
BIND(<{query_step_uri}> as ?stepUri)
|
157
|
-
?stepUri a <http://cambridgesemantics.com/ontologies/Graphmarts#Step>;
|
158
|
-
<http://cambridgesemantics.com/ontologies/Graphmarts#transformQuery> ?query
|
159
|
-
}}
|
160
|
-
# """
|
161
|
-
anzo_client = AnzoClient(triple_store['url'], triple_store['port'],
|
162
|
-
username=triple_store['username'],
|
163
|
-
password=triple_store['password'])
|
164
|
-
record_dictionaries = anzo_client.query_journal(query_string=query).as_table_results().as_record_dictionaries()
|
165
|
-
|
166
|
-
return record_dictionaries[0].get(
|
167
|
-
"query")
|
168
|
-
|
169
|
-
def get_queries_from_templated_step(triple_store: dict, query_step_uri: URIRef) -> dict:
|
170
|
-
|
171
|
-
query = f"""SELECT ?stepUri ?param_query ?query_template WHERE {{
|
172
|
-
BIND(<{query_step_uri}> as ?stepUri)
|
173
|
-
?stepUri a <http://cambridgesemantics.com/ontologies/Graphmarts#Step> ;
|
174
|
-
<http://cambridgesemantics.com/ontologies/Graphmarts#parametersTemplate> ?param_query ;
|
175
|
-
<http://cambridgesemantics.com/ontologies/Graphmarts#template> ?query_template .
|
176
|
-
}}
|
177
|
-
"""
|
178
|
-
anzo_client = AnzoClient(triple_store['url'], triple_store['port'],
|
179
|
-
username=triple_store['username'],
|
180
|
-
password=triple_store['password'])
|
181
|
-
record_dictionaries = anzo_client.query_journal(query_string=query).as_table_results().as_record_dictionaries()
|
182
|
-
return record_dictionaries[0]
|
183
|
-
|
184
|
-
|
185
|
-
def get_queries_for_layer(triple_store: dict, graphmart_layer_uri: URIRef):
|
186
|
-
query = f"""PREFIX graphmarts: <http://cambridgesemantics.com/ontologies/Graphmarts#>
|
187
|
-
PREFIX anzo: <http://openanzo.org/ontologies/2008/07/Anzo#>
|
188
|
-
SELECT ?query ?param_query ?query_template
|
189
|
-
{{ <{graphmart_layer_uri}> graphmarts:step ?step .
|
190
|
-
?step anzo:index ?index ;
|
191
|
-
anzo:orderedValue ?query_step .
|
192
|
-
?query_step graphmarts:enabled true ;
|
193
|
-
OPTIONAL {{ ?query_step
|
194
|
-
graphmarts:parametersTemplate ?param_query ;
|
195
|
-
graphmarts:template ?query_template ;
|
196
|
-
. }}
|
197
|
-
OPTIONAL {{ ?query_step
|
198
|
-
graphmarts:transformQuery ?query ;
|
199
|
-
. }}
|
200
|
-
}}
|
201
|
-
ORDER BY ?index"""
|
202
|
-
anzo_client = AnzoClient(triple_store['url'], triple_store['port'],
|
203
|
-
username=triple_store['username'],
|
204
|
-
password=triple_store['password'])
|
205
|
-
return anzo_client.query_journal(query_string=query).as_table_results().as_record_dictionaries()
|
206
|
-
|
207
|
-
|
208
|
-
def upload_given(triple_store: dict, given: Graph):
|
209
|
-
logging.debug(f"upload_given {triple_store} {given}")
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
1
|
+
"""
|
2
|
+
MIT License
|
3
|
+
|
4
|
+
Copyright (c) 2023 Semantic Partners Ltd
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
8
|
+
in the Software without restriction, including without limitation the rights
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
14
|
+
copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
+
SOFTWARE.
|
23
|
+
"""
|
24
|
+
|
25
|
+
import requests
|
26
|
+
from pyanzo import AnzoClient
|
27
|
+
from rdflib import Graph, ConjunctiveGraph, Literal, URIRef
|
28
|
+
from requests import ConnectTimeout, Response, HTTPError, RequestException, ConnectionError
|
29
|
+
from bs4 import BeautifulSoup
|
30
|
+
import logging
|
31
|
+
from .namespace import MUST
|
32
|
+
|
33
|
+
|
34
|
+
# https://github.com/Semantic-partners/mustrd/issues/73
|
35
|
+
def manage_anzo_response(response: Response) -> str:
|
36
|
+
content_string = response.content.decode("utf-8")
|
37
|
+
if response.status_code == 200:
|
38
|
+
return content_string
|
39
|
+
elif response.status_code == 403:
|
40
|
+
html = BeautifulSoup(content_string, 'html.parser')
|
41
|
+
title_tag = html.title.string
|
42
|
+
raise HTTPError(f"Anzo authentication error, status code: {response.status_code}, content: {title_tag}")
|
43
|
+
else:
|
44
|
+
raise RequestException(f"Anzo error, status code: {response.status_code}, content: {content_string}")
|
45
|
+
|
46
|
+
|
47
|
+
def query_with_bindings(bindings: dict, when: str) -> str:
|
48
|
+
values = ""
|
49
|
+
for key, value in bindings.items():
|
50
|
+
values += f"VALUES ?{key} {{{value.n3()}}} "
|
51
|
+
split_query = when.lower().split("where {", 1)
|
52
|
+
return f"{split_query[0].strip()} WHERE {{ {values} {split_query[1].strip()}"
|
53
|
+
|
54
|
+
def execute_select (triple_store: dict, when: str, bindings: dict = None) -> str:
|
55
|
+
try:
|
56
|
+
if bindings:
|
57
|
+
when = query_with_bindings(bindings, when)
|
58
|
+
when = when.replace("${fromSources}", f"FROM <{triple_store['input_graph']}>\nFROM <{triple_store['output_graph']}>").replace(
|
59
|
+
"${targetGraph}", f"<{triple_store['output_graph']}>")
|
60
|
+
data = {'datasourceURI': triple_store['gqe_uri'], 'query': when,
|
61
|
+
'skipCache': 'true'}
|
62
|
+
url = f"https://{triple_store['url']}:{triple_store['port']}/sparql?format=application/sparql-results+json"
|
63
|
+
return manage_anzo_response(requests.post(url=url,
|
64
|
+
auth=(triple_store['username'], triple_store['password']),
|
65
|
+
data=data,
|
66
|
+
verify=False))
|
67
|
+
except (ConnectionError, TimeoutError, HTTPError, ConnectTimeout):
|
68
|
+
raise
|
69
|
+
|
70
|
+
def execute_update(triple_store: dict, when: str, bindings: dict = None) -> Graph:
|
71
|
+
logging.debug(f"updating in anzo! {triple_store=} {when=}")
|
72
|
+
input_graph = triple_store['input_graph']
|
73
|
+
output_graph = triple_store['output_graph']
|
74
|
+
|
75
|
+
substituted_query = when.replace("${usingSources}", f"USING <{triple_store['input_graph']}> \nUSING <{triple_store['output_graph']}>").replace(
|
76
|
+
"${targetGraph}", f"<{output_graph}>")
|
77
|
+
|
78
|
+
data = {'datasourceURI': triple_store['gqe_uri'], 'update': substituted_query,
|
79
|
+
'default-graph-uri': input_graph, 'skipCache': 'true'}
|
80
|
+
url = f"https://{triple_store['url']}:{triple_store['port']}/sparql?format=ttl"
|
81
|
+
response = manage_anzo_response(requests.post(url=url,
|
82
|
+
auth=(triple_store['username'],
|
83
|
+
triple_store['password']),
|
84
|
+
data=data,
|
85
|
+
verify=False))
|
86
|
+
logging.debug(f'response {response}')
|
87
|
+
check_data = {'datasourceURI': triple_store['gqe_uri'], 'query': "construct {?s ?p ?o} { ?s ?p ?o }",
|
88
|
+
'default-graph-uri': output_graph, 'skipCache': 'true'}
|
89
|
+
everything_response = manage_anzo_response(requests.post(url=url,
|
90
|
+
auth=(triple_store['username'],
|
91
|
+
triple_store['password']),
|
92
|
+
data=check_data,
|
93
|
+
verify=False))
|
94
|
+
# todo deal with error responses
|
95
|
+
new_graph = Graph().parse(data=everything_response)
|
96
|
+
logging.debug(f"new_graph={new_graph.serialize(format='ttl')}")
|
97
|
+
return new_graph
|
98
|
+
|
99
|
+
|
100
|
+
def execute_construct(triple_store: dict, when: str, bindings: dict = None) -> Graph:
|
101
|
+
try:
|
102
|
+
if bindings:
|
103
|
+
when = query_with_bindings(bindings, when)
|
104
|
+
data = {'datasourceURI': triple_store['gqe_uri'], 'query': when,
|
105
|
+
'default-graph-uri': triple_store['input_graph'], 'skipCache': 'true'}
|
106
|
+
url = f"https://{triple_store['url']}:{triple_store['port']}/sparql?format=ttl"
|
107
|
+
response = requests.post(url=url,
|
108
|
+
auth=(triple_store['username'],
|
109
|
+
triple_store['password']),
|
110
|
+
data=data,
|
111
|
+
verify=False)
|
112
|
+
logging.debug(f'response {response}')
|
113
|
+
return Graph().parse(data=manage_anzo_response(response))
|
114
|
+
except (ConnectionError, TimeoutError, HTTPError, ConnectTimeout) as e:
|
115
|
+
logging.error(f'response {e}')
|
116
|
+
raise
|
117
|
+
|
118
|
+
|
119
|
+
# Get Given or then from the content of a graphmart
|
120
|
+
def get_spec_component_from_graphmart(triple_store: dict, graphmart: URIRef, layer: URIRef = None) -> ConjunctiveGraph:
|
121
|
+
try:
|
122
|
+
anzo_client = AnzoClient(triple_store['url'], triple_store['port'],
|
123
|
+
username=triple_store['username'],
|
124
|
+
password=triple_store['password'])
|
125
|
+
return anzo_client.query_graphmart(graphmart=graphmart,
|
126
|
+
data_layers=layer,
|
127
|
+
query_string="CONSTRUCT {?s ?p ?o} WHERE {?s ?p ?o}",
|
128
|
+
skip_cache=True).as_quad_store().as_rdflib_graph()
|
129
|
+
except RuntimeError as e:
|
130
|
+
raise ConnectionError(f"Anzo connection error, {e}")
|
131
|
+
|
132
|
+
|
133
|
+
def get_query_from_querybuilder(triple_store: dict, folder_name: Literal, query_name: Literal) -> str:
|
134
|
+
query = f"""SELECT ?query WHERE {{
|
135
|
+
graph ?queryFolder {{
|
136
|
+
?bookmark a <http://www.cambridgesemantics.com/ontologies/QueryPlayground#QueryBookmark>;
|
137
|
+
<http://openanzo.org/ontologies/2008/07/System#query> ?query;
|
138
|
+
<http://purl.org/dc/elements/1.1/title> "{query_name}"
|
139
|
+
}}
|
140
|
+
?queryFolder a <http://www.cambridgesemantics.com/ontologies/QueryPlayground#QueryFolder>;
|
141
|
+
<http://purl.org/dc/elements/1.1/title> "{folder_name}"
|
142
|
+
}}"""
|
143
|
+
anzo_client = AnzoClient(triple_store['url'], triple_store['port'],
|
144
|
+
username=triple_store['username'],
|
145
|
+
password=triple_store['password'])
|
146
|
+
|
147
|
+
result = anzo_client.query_journal(query_string=query).as_table_results().as_record_dictionaries()
|
148
|
+
if len(result) == 0:
|
149
|
+
raise FileNotFoundError(f"Query {query_name} not found in folder {folder_name}")
|
150
|
+
return result[0].get("query")
|
151
|
+
|
152
|
+
|
153
|
+
# https://github.com/Semantic-partners/mustrd/issues/102
|
154
|
+
def get_query_from_step(triple_store: dict, query_step_uri: URIRef) -> str:
|
155
|
+
query = f"""SELECT ?stepUri ?query WHERE {{
|
156
|
+
BIND(<{query_step_uri}> as ?stepUri)
|
157
|
+
?stepUri a <http://cambridgesemantics.com/ontologies/Graphmarts#Step>;
|
158
|
+
<http://cambridgesemantics.com/ontologies/Graphmarts#transformQuery> ?query
|
159
|
+
}}
|
160
|
+
# """
|
161
|
+
anzo_client = AnzoClient(triple_store['url'], triple_store['port'],
|
162
|
+
username=triple_store['username'],
|
163
|
+
password=triple_store['password'])
|
164
|
+
record_dictionaries = anzo_client.query_journal(query_string=query).as_table_results().as_record_dictionaries()
|
165
|
+
|
166
|
+
return record_dictionaries[0].get(
|
167
|
+
"query")
|
168
|
+
|
169
|
+
def get_queries_from_templated_step(triple_store: dict, query_step_uri: URIRef) -> dict:
|
170
|
+
|
171
|
+
query = f"""SELECT ?stepUri ?param_query ?query_template WHERE {{
|
172
|
+
BIND(<{query_step_uri}> as ?stepUri)
|
173
|
+
?stepUri a <http://cambridgesemantics.com/ontologies/Graphmarts#Step> ;
|
174
|
+
<http://cambridgesemantics.com/ontologies/Graphmarts#parametersTemplate> ?param_query ;
|
175
|
+
<http://cambridgesemantics.com/ontologies/Graphmarts#template> ?query_template .
|
176
|
+
}}
|
177
|
+
"""
|
178
|
+
anzo_client = AnzoClient(triple_store['url'], triple_store['port'],
|
179
|
+
username=triple_store['username'],
|
180
|
+
password=triple_store['password'])
|
181
|
+
record_dictionaries = anzo_client.query_journal(query_string=query).as_table_results().as_record_dictionaries()
|
182
|
+
return record_dictionaries[0]
|
183
|
+
|
184
|
+
|
185
|
+
def get_queries_for_layer(triple_store: dict, graphmart_layer_uri: URIRef):
|
186
|
+
query = f"""PREFIX graphmarts: <http://cambridgesemantics.com/ontologies/Graphmarts#>
|
187
|
+
PREFIX anzo: <http://openanzo.org/ontologies/2008/07/Anzo#>
|
188
|
+
SELECT ?query ?param_query ?query_template
|
189
|
+
{{ <{graphmart_layer_uri}> graphmarts:step ?step .
|
190
|
+
?step anzo:index ?index ;
|
191
|
+
anzo:orderedValue ?query_step .
|
192
|
+
?query_step graphmarts:enabled true ;
|
193
|
+
OPTIONAL {{ ?query_step
|
194
|
+
graphmarts:parametersTemplate ?param_query ;
|
195
|
+
graphmarts:template ?query_template ;
|
196
|
+
. }}
|
197
|
+
OPTIONAL {{ ?query_step
|
198
|
+
graphmarts:transformQuery ?query ;
|
199
|
+
. }}
|
200
|
+
}}
|
201
|
+
ORDER BY ?index"""
|
202
|
+
anzo_client = AnzoClient(triple_store['url'], triple_store['port'],
|
203
|
+
username=triple_store['username'],
|
204
|
+
password=triple_store['password'])
|
205
|
+
return anzo_client.query_journal(query_string=query).as_table_results().as_record_dictionaries()
|
206
|
+
|
207
|
+
|
208
|
+
def upload_given(triple_store: dict, given: Graph):
|
209
|
+
logging.debug(f"upload_given {triple_store} {given}")
|
210
|
+
|
211
|
+
try:
|
212
|
+
input_graph = triple_store['input_graph']
|
213
|
+
output_graph = triple_store['output_graph']
|
214
|
+
clear_graph(triple_store, input_graph)
|
215
|
+
clear_graph(triple_store, output_graph)
|
216
|
+
serialized_given = given.serialize(format="nt")
|
217
|
+
insert_query = f"INSERT DATA {{graph <{triple_store['input_graph']}>{{{serialized_given}}}}}"
|
218
|
+
data = {'datasourceURI': triple_store['gqe_uri'], 'update': insert_query}
|
219
|
+
response = requests.post(url=f"https://{triple_store['url']}:{triple_store['port']}/sparql",
|
220
|
+
auth=(triple_store['username'], triple_store['password']), data=data, verify=False)
|
221
|
+
manage_anzo_response(response)
|
222
|
+
except (ConnectionError, TimeoutError, HTTPError, ConnectTimeout):
|
223
|
+
raise
|
224
|
+
|
225
|
+
|
226
|
+
def clear_graph(triple_store: dict, graph_uri: str):
|
227
|
+
try:
|
228
|
+
clear_query = f"CLEAR GRAPH <{graph_uri}>"
|
229
|
+
data = {'datasourceURI': triple_store['gqe_uri'], 'update': clear_query}
|
230
|
+
url = f"https://{triple_store['url']}:{triple_store['port']}/sparql"
|
231
|
+
response = requests.post(url=url,
|
232
|
+
auth=(triple_store['username'], triple_store['password']), data=data, verify=False)
|
233
|
+
manage_anzo_response(response)
|
234
|
+
except (ConnectionError, TimeoutError, HTTPError, ConnectTimeout):
|
235
|
+
raise
|
236
|
+
|