-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
base implementation of sparql endpoint methods to add or remove datas…
…ets #1890 (remove would be part of the delete method).
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import requests | ||
|
||
from factsheet.oekg.connection import update_endpoint | ||
|
||
|
||
def add_datasets_to_scenario(scenario_uuid, datasets, dataset_type): | ||
""" | ||
Function to add datasets to a scenario bundle in Jena Fuseki. | ||
""" | ||
for dataset in datasets: | ||
sparql_query = f""" | ||
PREFIX oeo: <http://example.org/ontology#> | ||
INSERT DATA {{ | ||
GRAPH <http://example.org/scenario/{scenario_uuid}> {{ | ||
oeo:{dataset} a oeo:{dataset_type}Dataset . | ||
}} | ||
}} | ||
""" | ||
response = send_sparql_update(sparql_query) | ||
if not response.ok: | ||
return False # Return False if any query fails | ||
return True | ||
|
||
|
||
def remove_datasets_from_scenario(scenario_uuid, datasets, dataset_type): | ||
""" | ||
Function to remove datasets from a scenario bundle in Jena Fuseki. | ||
""" | ||
for dataset in datasets: | ||
sparql_query = f""" | ||
PREFIX oeo: <http://example.org/ontology#> | ||
DELETE DATA {{ | ||
GRAPH <http://example.org/scenario/{scenario_uuid}> {{ | ||
oeo:{dataset} a oeo:{dataset_type}Dataset . | ||
}} | ||
}} | ||
""" | ||
response = send_sparql_update(sparql_query) | ||
if not response.ok: | ||
return False # Return False if any query fails | ||
return True | ||
|
||
|
||
def send_sparql_update(query): | ||
""" | ||
Helper function to send a SPARQL update query to Fuseki. | ||
""" | ||
headers = {"Content-Type": "application/sparql-update"} | ||
response = requests.post(update_endpoint, data=query, headers=headers) | ||
return response |