mustrd 0.2.0__py3-none-any.whl → 0.2.0a1__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/mustrdAnzo.py CHANGED
@@ -1,220 +1,208 @@
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
- # https://github.com/Semantic-partners/mustrd/issues/73
34
- def manage_anzo_response(response: Response) -> str:
35
- content_string = response.content.decode("utf-8")
36
- if response.status_code == 200:
37
- return content_string
38
- elif response.status_code == 403:
39
- html = BeautifulSoup(content_string, 'html.parser')
40
- title_tag = html.title.string
41
- raise HTTPError(f"Anzo authentication error, status code: {response.status_code}, content: {title_tag}")
42
- else:
43
- raise RequestException(f"Anzo error, status code: {response.status_code}, content: {content_string}")
44
-
45
- def query_with_bindings(bindings: dict, when: str) -> str:
46
- values = ""
47
- for key, value in bindings.items():
48
- values += f"VALUES ?{key} {{{value.n3()}}} "
49
- split_query = when.lower().split("where {", 1)
50
- return f"{split_query[0].strip()} WHERE {{ {values} {split_query[1].strip()}"
51
-
52
- def execute_select (triple_store: dict, when: str, bindings: dict = None) -> str:
53
- if bindings:
54
- when = query_with_bindings(bindings, when)
55
- # Just remove ${fromSources} if we are executing a query step; the sources are defined using http parameters
56
- when = when.replace("${fromSources}", "")
57
- return execute_sparql(triple_store, False, when, triple_store['input_graph'])
58
-
59
- PARAMS = {
60
- # Update parameters for INSERT / DELETE
61
- True: {
62
- "default-graph-param": "using-graph-uri",
63
- "named-graph-param":"using-named-graph-uri",
64
- "Content-Type": "application/sparql-update"
65
- },
66
- # Query parameters for SELECT / CONSTRUCT
67
- False: {
68
- "default-graph-param": "default-graph-uri",
69
- "named-graph-param":"named-graph-uri",
70
- "Content-Type": "application/sparql-query"
71
- }
72
- }
73
- def execute_sparql(triple_store: dict, is_update: bool, sparql, graph: str, format: str = "application/sparql-results+json"):
74
- params = {
75
- "format" : format,
76
- "datasourceURI" : triple_store['gqe_uri'],
77
- "skipCache": "true",
78
- # Default and named datasets have different query param for query and update
79
- PARAMS[is_update]["default-graph-param"] : graph,
80
- PARAMS[is_update]["named-graph-param"] : graph
81
- }
82
- headers={"Content-Type": PARAMS[is_update]["Content-Type"]}
83
- try:
84
- response = manage_anzo_response(requests.post(url=f"https://{triple_store['url']}:{triple_store['port']}/sparql",
85
- params=params,
86
- auth=(triple_store['username'], triple_store['password']),
87
- headers=headers,
88
- data=sparql,
89
- verify=False))
90
- logging.debug(f'response {response}')
91
- return response
92
- except (ConnectionError, TimeoutError, HTTPError, ConnectTimeout) as e:
93
- logging.error(f'response {e}')
94
- raise
95
-
96
- def execute_update(triple_store: dict, when: str, bindings: dict = None) -> Graph:
97
- logging.debug(f"updating in anzo! {triple_store=} {when=}")
98
-
99
- # FIXME If query doesn't contain ${targetGraph}, then graph should be defined explicitly in the query
100
- substituted_query = when.replace("${usingSources}", "").replace(
101
- "${targetGraph}", f"<{triple_store['output_graph']}>")
102
-
103
- execute_sparql(triple_store, True, substituted_query, triple_store['input_graph'], "ttl")
104
-
105
- new_graph = execute_construct(triple_store, "construct {?s ?p ?o} { ?s ?p ?o }")
106
-
107
- logging.debug(f"new_graph={new_graph.serialize(format='ttl')}")
108
-
109
- return new_graph
110
-
111
-
112
- def execute_construct(triple_store: dict, when: str, bindings: dict = None) -> Graph:
113
- if bindings:
114
- when = query_with_bindings(bindings, when)
115
- response = execute_sparql(triple_store, False, when, triple_store['input_graph'], "ttl")
116
- return Graph().parse(data=response)
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
- clear_graph(triple_store, triple_store['input_graph'])
211
- clear_graph(triple_store, triple_store['output_graph'])
212
- serialized_given = given.serialize(format="nt")
213
-
214
- insert_query = f"INSERT DATA {{graph <{triple_store['input_graph']}>{{{serialized_given}}}}}"
215
- execute_sparql(triple_store, True, insert_query, None, None)
216
-
217
-
218
- def clear_graph(triple_store: dict, graph_uri: str):
219
- execute_sparql(triple_store, True, f"CLEAR GRAPH <{graph_uri}>", None, None)
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 execute_update_spec import execute_update_spec
32
+ from namespace import MUST
33
+
34
+
35
+ # https://github.com/Semantic-partners/mustrd/issues/73
36
+ def manage_anzo_response(response: Response) -> str:
37
+ content_string = response.content.decode("utf-8")
38
+ if response.status_code == 200:
39
+ return content_string
40
+ elif response.status_code == 403:
41
+ html = BeautifulSoup(content_string, 'html.parser')
42
+ title_tag = html.title.string
43
+ raise HTTPError(f"Anzo authentication error, status code: {response.status_code}, content: {title_tag}")
44
+ else:
45
+ raise RequestException(f"Anzo error, status code: {response.status_code}, content: {content_string}")
46
+
47
+
48
+ def query_with_bindings(bindings: dict, when: str) -> str:
49
+ values = ""
50
+ for key, value in bindings.items():
51
+ values += f"VALUES ?{key} {{{value.n3()}}} "
52
+ split_query = when.lower().split("where {", 1)
53
+ return f"{split_query[0].strip()} WHERE {{ {values} {split_query[1].strip()}"
54
+
55
+
56
+ def execute_select_mustrd_spec_stage(triple_store: dict, given: Graph, when: str, bindings: dict = None) -> str:
57
+ try:
58
+ upload_given(triple_store, given)
59
+ if bindings:
60
+ when = query_with_bindings(bindings, when)
61
+ data = {'datasourceURI': triple_store['gqe_uri'], 'query': when,
62
+ 'default-graph-uri': triple_store['input_graph'], 'skipCache': 'true'}
63
+ url = f"https://{triple_store['url']}:{triple_store['port']}/sparql?format=application/sparql-results+json"
64
+ input("Press enter to continue")
65
+ return manage_anzo_response(requests.post(url=url,
66
+ auth=(triple_store['username'], triple_store['password']),
67
+ data=data,
68
+ verify=False))
69
+ except (ConnectionError, TimeoutError, HTTPError, ConnectTimeout):
70
+ raise
71
+
72
+ @execute_update_spec.method(MUST.Anzo)
73
+ def execute_update_spec_stage_anzo(triple_store: dict, given: Graph, when: str, bindings: dict = None) -> Graph:
74
+ logging.info(f"updating in anzo! {triple_store=} {given=} {when=}")
75
+ upload_given(triple_store, given)
76
+ # input("have uploaded given")
77
+
78
+ input_graph = triple_store['input_graph']
79
+ output_graph = triple_store['output_graph']
80
+
81
+ substituted_query = when.replace("${usingSources}", f"USING <{input_graph}>").replace(
82
+ "${targetGraph}", f"<{output_graph}>")
83
+
84
+ data = {'datasourceURI': triple_store['gqe_uri'], 'update': substituted_query,
85
+ 'default-graph-uri': input_graph, 'skipCache': 'true'}
86
+ url = f"https://{triple_store['url']}:{triple_store['port']}/sparql?format=ttl"
87
+ response = manage_anzo_response(requests.post(url=url,
88
+ auth=(triple_store['username'],
89
+ triple_store['password']),
90
+ data=data,
91
+ verify=False))
92
+ logging.info(f'response {response}')
93
+ check_data = {'datasourceURI': triple_store['gqe_uri'], 'query': "construct {?s ?p ?o} { ?s ?p ?o }",
94
+ 'default-graph-uri': output_graph, 'skipCache': 'true'}
95
+ everything_response = manage_anzo_response(requests.post(url=url,
96
+ auth=(triple_store['username'],
97
+ triple_store['password']),
98
+ data=check_data,
99
+ verify=False))
100
+ # todo deal with error responses
101
+ new_graph = Graph().parse(data=everything_response)
102
+ logging.info(f"new_graph={new_graph.serialize(format='ttl')}")
103
+ return new_graph
104
+
105
+
106
+ def execute_construct_mustrd_spec_stage(triple_store: dict, given: Graph, when: str, bindings: dict = None) -> Graph:
107
+ try:
108
+ upload_given(triple_store, given)
109
+ if bindings:
110
+ when = query_with_bindings(bindings, when)
111
+ data = {'datasourceURI': triple_store['gqe_uri'], 'query': when,
112
+ 'default-graph-uri': triple_store['input_graph'], 'skipCache': 'true'}
113
+ url = f"https://{triple_store['url']}:{triple_store['port']}/sparql?format=ttl"
114
+ response = requests.post(url=url,
115
+ auth=(triple_store['username'],
116
+ triple_store['password']),
117
+ data=data,
118
+ verify=False)
119
+ logging.info(f'response {response}')
120
+ return Graph().parse(data=manage_anzo_response(response))
121
+ except (ConnectionError, TimeoutError, HTTPError, ConnectTimeout) as e:
122
+ logging.error(f'response {e}')
123
+ raise
124
+
125
+
126
+ # Get Given or then from the content of a graphmart
127
+ def get_spec_component_from_graphmart(triple_store: dict, graphmart: URIRef, layer: URIRef = None) -> ConjunctiveGraph:
128
+ try:
129
+ anzo_client = AnzoClient(triple_store['url'], triple_store['port'], triple_store['username'],
130
+ triple_store['password'])
131
+ return anzo_client.query_graphmart(graphmart=graphmart,
132
+ data_layers=layer,
133
+ query_string="CONSTRUCT {?s ?p ?o} WHERE {?s ?p ?o}",
134
+ skip_cache=True).as_quad_store().as_rdflib_graph()
135
+ except RuntimeError as e:
136
+ raise ConnectionError(f"Anzo connection error, {e}")
137
+
138
+
139
+ def get_query_from_querybuilder(triple_store: dict, folder_name: Literal, query_name: Literal) -> str:
140
+ query = f"""SELECT ?query WHERE {{
141
+ graph ?queryFolder {{
142
+ ?bookmark a <http://www.cambridgesemantics.com/ontologies/QueryPlayground#QueryBookmark>;
143
+ <http://openanzo.org/ontologies/2008/07/System#query> ?query;
144
+ <http://purl.org/dc/elements/1.1/title> "{query_name}"
145
+ }}
146
+ ?queryFolder a <http://www.cambridgesemantics.com/ontologies/QueryPlayground#QueryFolder>;
147
+ <http://purl.org/dc/elements/1.1/title> "{folder_name}"
148
+ }}"""
149
+ anzo_client = AnzoClient(triple_store['url'], triple_store['port'], triple_store['username'],
150
+ triple_store['password'])
151
+
152
+ result = anzo_client.query_journal(query_string=query).as_table_results().as_record_dictionaries()
153
+ if len(result) == 0:
154
+ raise FileNotFoundError(f"Query {query_name} not found in folder {folder_name}")
155
+ return result[0].get("query")
156
+
157
+
158
+ # https://github.com/Semantic-partners/mustrd/issues/102
159
+ def get_query_from_step(triple_store: dict, query_step_uri: URIRef):
160
+ query = f"""SELECT ?stepUri ?query WHERE {{
161
+ BIND(<{query_step_uri}> as ?stepUri)
162
+ graph ?g {{
163
+ ?stepUri a <http://cambridgesemantics.com/ontologies/Graphmarts#Step>;
164
+ <http://cambridgesemantics.com/ontologies/Graphmarts#transformQuery> ?query
165
+ }}
166
+ }}
167
+ # """
168
+ # query="""
169
+ # select ?g ?s ?p ?o { graph ?g { ?s ?p ?o }} limit 10
170
+ # """
171
+ anzo_client = AnzoClient(triple_store['url'], triple_store['port'], triple_store['username'],
172
+ triple_store['password'])
173
+ record_dictionaries = anzo_client.query_journal(query_string=query).as_table_results().as_record_dictionaries()
174
+
175
+ return record_dictionaries[0].get(
176
+ "query")
177
+
178
+
179
+ def upload_given(triple_store: dict, given: Graph):
180
+ logging.info(f"upload_given {triple_store} {given}")
181
+ if given:
182
+ try:
183
+ input_graph = triple_store['input_graph']
184
+ output_graph = triple_store['output_graph']
185
+
186
+ clear_graph(triple_store, input_graph)
187
+ clear_graph(triple_store, output_graph)
188
+ serialized_given = given.serialize(format="nt")
189
+ insert_query = f"INSERT DATA {{graph <{triple_store['input_graph']}>{{{serialized_given}}}}}"
190
+ data = {'datasourceURI': triple_store['gqe_uri'], 'update': insert_query}
191
+ response = requests.post(url=f"https://{triple_store['url']}:{triple_store['port']}/sparql",
192
+ auth=(triple_store['username'], triple_store['password']), data=data, verify=False)
193
+ manage_anzo_response(response)
194
+ except (ConnectionError, TimeoutError, HTTPError, ConnectTimeout):
195
+ raise
196
+
197
+
198
+ def clear_graph(triple_store: dict, graph_uri: str):
199
+ try:
200
+ clear_query = f"CLEAR GRAPH <{graph_uri}>"
201
+ data = {'datasourceURI': triple_store['gqe_uri'], 'update': clear_query}
202
+ url = f"https://{triple_store['url']}:{triple_store['port']}/sparql"
203
+ response = requests.post(url=url,
204
+ auth=(triple_store['username'], triple_store['password']), data=data, verify=False)
205
+ manage_anzo_response(response)
206
+ except (ConnectionError, TimeoutError, HTTPError, ConnectTimeout):
207
+ raise
208
+