-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fe1bee0
commit dfba793
Showing
2 changed files
with
184 additions
and
2 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,182 @@ | ||
{ | ||
"nbformat": 4, | ||
"nbformat_minor": 0, | ||
"metadata": { | ||
"colab": { | ||
"provenance": [] | ||
}, | ||
"kernelspec": { | ||
"name": "python3", | ||
"display_name": "Python 3" | ||
}, | ||
"language_info": { | ||
"name": "python" | ||
} | ||
}, | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"# Example: Zinc Powder from a Supplier\n", | ||
"\n", | ||
"Let's describe an instance of some zinc powder with a set of properties defined in the specification sheet from the manufacturer!\n" | ||
], | ||
"metadata": { | ||
"id": "1wseTQGaB4x9" | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"# Install and import dependencies\n", | ||
"!pip install jsonschema rdflib > /dev/null\n", | ||
"\n", | ||
"import json\n", | ||
"import rdflib" | ||
], | ||
"metadata": { | ||
"id": "p8-laAJFCqkH" | ||
}, | ||
"execution_count": 5, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"The JSON-LD data that we will use is:" | ||
], | ||
"metadata": { | ||
"id": "jcTVz9-DEh3m" | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"jsonld = {\n", | ||
" \"@context\": \"https://raw.githubusercontent.com/emmo-repo/domain-electrochemistry/master/context.json\",\n", | ||
" \"@type\": [\"Zinc\", \"Powder\"],\n", | ||
" \"schema:manufacturer\": {\n", | ||
" \"@id\": \"https://www.wikidata.org/wiki/Q680841\",\n", | ||
" \"schema:name\": \"Sigma-Aldrich\"\n", | ||
" },\n", | ||
" \"schema:productID\": \"324930\",\n", | ||
" \"schema:url\": \"https://www.sigmaaldrich.com/NO/en/product/aldrich/324930\",\n", | ||
" \"hasProperty\": [\n", | ||
" {\n", | ||
" \"@type\": [\"D95ParticleSize\", \"ConventionalProperty\"],\n", | ||
" \"hasNumericalPart\": {\n", | ||
" \"@type\": \"Real\",\n", | ||
" \"hasNumericalValue\": 150\n", | ||
" },\n", | ||
" \"hasMeasurementUnit\": \"emmo:MicroMetre\",\n", | ||
" \"dc:source\": \"https://www.sigmaaldrich.com/NO/en/product/aldrich/324930\"\n", | ||
" }\n", | ||
" ]\n", | ||
"}" | ||
], | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"id": "gohQKEBrF2QP", | ||
"outputId": "8f7574b5-32a2-4e43-80a7-76d996479a28" | ||
}, | ||
"execution_count": 11, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"name": "stdout", | ||
"text": [ | ||
"(rdflib.term.URIRef('https://www.wikidata.org/wiki/Q680841'), rdflib.term.URIRef('https://schema.org/name'), rdflib.term.Literal('Sigma-Aldrich'))\n", | ||
"(rdflib.term.BNode('Nc95e8898daef460e92f102f978c57630'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('https://w3id.org/emmo/domain/electrochemistry#electrochemistry_02d2d1d1_241c_429b_b4e7_31f2c3dc4835'))\n", | ||
"(rdflib.term.BNode('N9622032c417e4e21918ccb2f84ff1be7'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('http://emmo.info/emmo#EMMO_18d180e4_5e3e_42f7_820c_e08951223486'))\n", | ||
"(rdflib.term.BNode('Nfc73816cdf3e4970aebf113d2188ff03'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('https://w3id.org/emmo/domain/chemicalsubstance#substance_9bd78e1c_a4dc_41b6_8013_adb51df1ffdc'))\n", | ||
"(rdflib.term.BNode('Nfc73816cdf3e4970aebf113d2188ff03'), rdflib.term.URIRef('http://emmo.info/emmo#EMMO_e1097637_70d2_4895_973f_2396f04fa204'), rdflib.term.BNode('Nc95e8898daef460e92f102f978c57630'))\n", | ||
"(rdflib.term.BNode('Nfc73816cdf3e4970aebf113d2188ff03'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('https://w3id.org/emmo/domain/electrochemistry#electrochemistry_ee479886_6805_4018_95e1_500185e44215'))\n", | ||
"(rdflib.term.BNode('N9622032c417e4e21918ccb2f84ff1be7'), rdflib.term.URIRef('http://emmo.info/emmo#EMMO_faf79f53_749d_40b2_807c_d34244c192f4'), rdflib.term.Literal('150', datatype=rdflib.term.URIRef('http://www.w3.org/2001/XMLSchema#integer')))\n", | ||
"(rdflib.term.BNode('Nfc73816cdf3e4970aebf113d2188ff03'), rdflib.term.URIRef('https://schema.org/manufacturer'), rdflib.term.URIRef('https://www.wikidata.org/wiki/Q680841'))\n", | ||
"(rdflib.term.BNode('Nc95e8898daef460e92f102f978c57630'), rdflib.term.URIRef('http://purl.org/dc/elements/1.1/source'), rdflib.term.Literal('https://www.sigmaaldrich.com/NO/en/product/aldrich/324930'))\n", | ||
"(rdflib.term.BNode('Nc95e8898daef460e92f102f978c57630'), rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), rdflib.term.URIRef('http://emmo.info/emmo#EMMO_d8aa8e1f_b650_416d_88a0_5118de945456'))\n", | ||
"(rdflib.term.BNode('Nfc73816cdf3e4970aebf113d2188ff03'), rdflib.term.URIRef('https://schema.org/url'), rdflib.term.Literal('https://www.sigmaaldrich.com/NO/en/product/aldrich/324930'))\n", | ||
"(rdflib.term.BNode('Nc95e8898daef460e92f102f978c57630'), rdflib.term.URIRef('http://emmo.info/emmo#EMMO_8ef3cd6d_ae58_4a8d_9fc0_ad8f49015cd0'), rdflib.term.BNode('N9622032c417e4e21918ccb2f84ff1be7'))\n", | ||
"(rdflib.term.BNode('Nfc73816cdf3e4970aebf113d2188ff03'), rdflib.term.URIRef('https://schema.org/productID'), rdflib.term.Literal('324930'))\n", | ||
"(rdflib.term.BNode('Nc95e8898daef460e92f102f978c57630'), rdflib.term.URIRef('http://emmo.info/emmo#EMMO_bed1d005_b04e_4a90_94cf_02bc678a8569'), rdflib.term.URIRef('http://emmo.info/emmo#MicroMetre'))\n", | ||
"(rdflib.term.Literal('Sigma-Aldrich'),)\n" | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"Now let's see how a machine would process this by reading it into an RDF Graph! We can then use SPARQL to write a query and list all the things the machine thinks is in this file..." | ||
], | ||
"metadata": { | ||
"id": "in30p-x4H91Y" | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"# Create a new graph\n", | ||
"g = rdflib.Graph()\n", | ||
"\n", | ||
"g.parse(data=json.dumps(jsonld), format=\"json-ld\")\n", | ||
"\n", | ||
"query_all = \"\"\"\n", | ||
"SELECT ?subject ?predicate ?object\n", | ||
"WHERE {\n", | ||
" ?subject ?predicate ?object\n", | ||
"}\n", | ||
"\"\"\"\n", | ||
"\n", | ||
"# Execute the SPARQL query\n", | ||
"all_the_things = g.query(query_all)\n", | ||
"\n", | ||
"# Print the results\n", | ||
"for row in all_the_things:\n", | ||
" print(row)\n" | ||
], | ||
"metadata": { | ||
"id": "zWibLw6NIrrq" | ||
}, | ||
"execution_count": null, | ||
"outputs": [] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"Now, let's write a SPARQL query to get back some specific thing...like what is the name of the manufacturer?" | ||
], | ||
"metadata": { | ||
"id": "C-w1TbxkI4W5" | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"query = \"\"\"\n", | ||
"PREFIX schema: <https://schema.org/>\n", | ||
"\n", | ||
"SELECT ?manufacturerName\n", | ||
"WHERE {\n", | ||
" ?product schema:manufacturer ?manufacturer .\n", | ||
" ?manufacturer schema:name ?manufacturerName .\n", | ||
"}\n", | ||
"\"\"\"\n", | ||
"\n", | ||
"# Execute the SPARQL query\n", | ||
"results = g.query(query)\n", | ||
"\n", | ||
"# Print the results\n", | ||
"for row in results:\n", | ||
" print(row)\n" | ||
], | ||
"metadata": { | ||
"id": "6bXHGG4cI-kr" | ||
}, | ||
"execution_count": null, | ||
"outputs": [] | ||
} | ||
] | ||
} |
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