{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# CORESE client: update\n",
"\n",
"This notebook is intended to be used as a supplement to the _corese-query.jpynb_ notebook to test the local CORESE server. \n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"from __future__ import print_function"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Start the server if it is not already running\n",
"\n",
"Run the following command from the directory where the server is downloaded\n",
"\n",
"_java -jar -Dfile.encoding=UTF8 -Xmx20G corese-server-4.1.2.jar -e -lp -debug -pp profile.ttl_\n",
"\n",
"or if runing on Windows change the path and run the code below.\n",
"\n",
"NOTE: please notice that the server name/version in the command line should corresponds to your server executable."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"import os\n",
"import subprocess\n",
"\n",
"corese_path = os.path.normpath('C:/Users/abobashe/Documents/MonaLIA/CORESE')\n",
"command_line = 'start /w cmd /k java -jar -Dfile.encoding=UTF8 -Xmx20G corese-server-4.1.2.jar -e -lp -debug -pp profile.ttl'\n",
"\n",
"corese_server = subprocess.Popen(command_line, shell=True, cwd=corese_path)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"SPARQLWrapper ver. 1.8.5\n",
"Pandas ver. 0.23.4\n"
]
}
],
"source": [
"import SPARQLWrapper\n",
"import json\n",
"print('SPARQLWrapper ver.', SPARQLWrapper.__version__)\n",
"\n",
"from SPARQLWrapper import SPARQLWrapper, JSON, XML\n",
"from SPARQLWrapper import POST, POSTDIRECTLY\n",
"\n",
"import pandas as pd\n",
"print('Pandas ver.', pd.__version__)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"def sparql_service_to_dataframe(service, query):\n",
" \"\"\"\n",
" Helper function to convert SPARQL results into a Pandas DataFrame.\n",
" \n",
" Credit to Ted Lawless https://lawlesst.github.io/notebook/sparql-dataframe.html\n",
" \"\"\"\n",
" sparql = SPARQLWrapper(service)\n",
" sparql.setQuery(query)\n",
" sparql.setReturnFormat(JSON)\n",
" result = sparql.query()\n",
"\n",
" processed_results = json.load(result.response)\n",
" cols = processed_results['head']['vars']\n",
"\n",
" out = []\n",
" for row in processed_results['results']['bindings']:\n",
" item = []\n",
" for c in cols:\n",
" item.append(row.get(c, {}).get('value'))\n",
" out.append(item)\n",
"\n",
" return pd.DataFrame(out, columns=cols)\n",
"\n",
"\n",
"def sparql_service_update(service, update_query):\n",
" \"\"\"\n",
" Helper function to update (DELETE DATA, INSERT DATA, DELETE/INSERT) data.\n",
" \n",
" \"\"\"\n",
" sparql = SPARQLWrapper(service)\n",
" sparql.setMethod(POST)\n",
" sparql.setRequestMethod(POSTDIRECTLY)\n",
" sparql.setQuery(update_query)\n",
" result = sparql.query()\n",
" \n",
" #SPARQLWrapper is going to throw an exception if result.response.status != 200:\n",
" \n",
" return 'Done'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Update data"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"wds_Corese = 'http://localhost:8080/sparql'"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"insert = '''\n",
"PREFIX dc: \n",
"\n",
"INSERT DATA \n",
"{ dc:title \"Oliver Twist\"\n",
" dc:title \"David Copperfield\"\n",
"}\n",
"\n",
"'''\n",
"\n",
"delete = '''\n",
"PREFIX dc: \n",
"\n",
"DELETE DATA \n",
"{ dc:title \"Oliver Twist\" }\n",
"\n",
"'''\n",
"\n",
"update = '''\n",
"PREFIX dc: \n",
"\n",
"DELETE {?x dc:title \"Oliver Twist\"}\n",
"INSERT {?x dc:title \"Nicholas Nickleby\"}\n",
"WHERE {?x dc:title \"Oliver Twist\" }\n",
"'''"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Done'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sparql_service_update(wds_Corese, insert)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"verify_update = '''\n",
"PREFIX dc: \n",
"\n",
"SELECT * \n",
"WHERE {?x dc:title ?y }\n",
"'''"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"df = sparql_service_to_dataframe(wds_Corese, verify_update)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"require": [
"base/js/events",
"datatables.net",
"d3",
"chartjs",
"dt-config",
"dt-components",
"dt-graph-objects",
"dt-toolbar",
"dt-tooltips",
"jupyter-datatables"
],
"scrolled": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" x | \n",
" y | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" http://example/book1 | \n",
" Oliver Twist | \n",
"
\n",
" \n",
" 1 | \n",
" http://example/book2 | \n",
" David Copperfield | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" x y\n",
"0 http://example/book1 Oliver Twist \n",
"1 http://example/book2 David Copperfield"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.set_option('display.max_colwidth', -1) # if your Pandas version is < 1.0 then use -1 as second parameter, None otherwise\n",
"pd.set_option('display.precision', 3)\n",
"pd.set_option('display.max_rows', 9999)\n",
"\n",
"\n",
"df.head()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
},
"require": {
"paths": {
"buttons.colvis": "https://cdn.datatables.net/buttons/1.5.6/js/buttons.colVis.min",
"buttons.flash": "https://cdn.datatables.net/buttons/1.5.6/js/buttons.flash.min",
"buttons.html5": "https://cdn.datatables.net/buttons/1.5.6/js/buttons.html5.min",
"buttons.print": "https://cdn.datatables.net/buttons/1.5.6/js/buttons.print.min",
"chartjs": "https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.8.0/Chart",
"d3": "https://d3js.org/d3.v5.min",
"d3-array": "https://d3js.org/d3-array.v2.min",
"datatables.net": "https://cdn.datatables.net/1.10.18/js/jquery.dataTables",
"datatables.net-buttons": "https://cdn.datatables.net/buttons/1.5.6/js/dataTables.buttons.min",
"datatables.responsive": "https://cdn.datatables.net/responsive/2.2.2/js/dataTables.responsive.min",
"datatables.scroller": "https://cdn.datatables.net/scroller/2.0.0/js/dataTables.scroller.min",
"datatables.select": "https://cdn.datatables.net/select/1.3.0/js/dataTables.select.min",
"jszip": "https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min",
"moment": "https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.0/moment",
"pdfmake": "https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.min",
"vfsfonts": "https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts"
},
"shim": {
"buttons.colvis": {
"deps": [
"jszip",
"datatables.net-buttons"
]
},
"buttons.flash": {
"deps": [
"jszip",
"datatables.net-buttons"
]
},
"buttons.html5": {
"deps": [
"jszip",
"datatables.net-buttons"
]
},
"buttons.print": {
"deps": [
"jszip",
"datatables.net-buttons"
]
},
"chartjs": {
"deps": [
"moment"
]
},
"datatables.net": {
"exports": "$.fn.dataTable"
},
"datatables.net-buttons": {
"deps": [
"datatables.net"
]
},
"pdfmake": {
"deps": [
"datatables.net"
]
},
"vfsfonts": {
"deps": [
"datatables.net"
]
}
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}