iseal-core/idss application profile.ipynb

234 lines
8.2 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "code",
"execution_count": 118,
"id": "solid-grove",
"metadata": {},
"outputs": [],
"source": [
"from rdflib import Graph\n",
"from rdflib.namespace import OWL, DC, DCTERMS, RDF, RDFS, SKOS, XSD\n",
"from rdflib import URIRef, BNode, Literal\n",
"import json\n",
"import os\n",
"import pandas as pd\n",
"import pylode"
]
},
{
"cell_type": "code",
"execution_count": 134,
"id": "spiritual-geography",
"metadata": {},
"outputs": [],
"source": [
"g = Graph()\n",
"\n",
"##namespace\n",
"NS = \"http://idss.org/terms/\"\n",
"## create ontology\n",
"iseal = URIRef(NS)\n",
"g.add((iseal, RDF.type, OWL.Ontology)) "
]
},
{
"cell_type": "code",
"execution_count": 135,
"id": "competitive-turkish",
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_excel('./idss_schema_fields.xlsx')\n",
"df.dropna(how='all', axis=1)\n",
"df.fillna('', inplace=True)\n",
"\n",
"\n",
"for index, row in df.iterrows() :\n",
" element_name = row['element name']\n",
" element_description = row['element description']\n",
" comment = row['element guidance']\n",
" example = row['element link for more information']\n",
" cardinality = row['element options']\n",
" prop_type = row['element type']\n",
" controlled_vocab = row['element controlled values or terms']\n",
" module = row['idss module']\n",
" module_cat = row['idss module category']\n",
" \n",
" ##module\n",
" moduleUri = URIRef(NS+module)\n",
" if not (None, SKOS.prefLabel, Literal(module)) in g:\n",
" ##create module as skos concept\n",
" g.add((moduleUri, RDF.type, OWL.Class)) ## SKOS.Concept\n",
" g.add((moduleUri, SKOS.prefLabel, Literal(module)))\n",
" ##element\n",
" if '-' in element_name:\n",
" concept = element_name.split(' - ')[0]\n",
" element = element_name.split(' - ')[1]\n",
" \n",
" conceptUri = URIRef(NS+concept.replace(\" \", \"_\"))\n",
" if not (None, SKOS.prefLabel, Literal(concept)) in g:\n",
" ##create concept as skos concept\n",
" g.add((conceptUri, RDF.type, OWL.Class)) ## SKOS.Concept\n",
" g.add((conceptUri, SKOS.prefLabel, Literal(concept)))\n",
" g.add((conceptUri, RDFS.subClassOf, moduleUri))\n",
" \n",
" ## create properties\n",
" elementURI = URIRef(NS+element.replace(\" \", \"_\"))\n",
" if prop_type == 'CONTROLLED VALUE': ## object property\n",
" g.add((elementURI, SKOS.prefLabel, Literal(element)))\n",
" g.add((elementURI, RDF.type, OWL.ObjectProperty))\n",
" g.add((elementURI, OWL.domain, conceptUri))\n",
" \n",
" ## create controlled vocab\n",
" cvURI = URIRef(NS+\"VOCAB_\"+element.replace(\" \", \"_\"))\n",
" g.add((cvURI, RDF.type, OWL.Class)) ## SKOS.Concept ## SKOS.Collection??\n",
" g.add((cvURI, SKOS.prefLabel, Literal(\"VOCAB \"+element)))\n",
" for term in controlled_vocab.split(\"||\"):\n",
" termURI = URIRef(NS+term.replace(\" \", \"_\").replace(\"|\", \"\"))\n",
" g.add((termURI, RDF.type, OWL.Class)) ## SKOS.Concept\n",
" g.add((termURI, SKOS.prefLabel, Literal(term)))\n",
" g.add((termURI, RDFS.subClassOf, cvURI)) ## SKOS.member???\n",
" g.add((elementURI, OWL.range, cvURI))\n",
" \n",
" ## cardinality\n",
" if cardinality == 'MULTI SELECT FROM CONTROL LIST':\n",
" br = BNode()\n",
" g.add((br, RDF.type, OWL.Restriction))\n",
" g.add((br, OWL.onProperty, elementURI))\n",
" g.add((br, OWL.someValuesFrom, cvURI))\n",
" g.add((conceptUri, RDFS.subClassOf, br))\n",
" else:\n",
" br = BNode()\n",
" g.add((br, RDF.type, OWL.Restriction))\n",
" g.add((br, OWL.onProperty, elementURI))\n",
" g.add((br, OWL.maxQualifiedCardinality, Literal(1)))\n",
" g.add((br, OWL.onClass, cvURI))\n",
" g.add((conceptUri, RDFS.subClassOf, br))\n",
" \n",
" #elif prop_type == 'URL': ## object property\n",
" # g.add((elementURI, RDF.type, OWL.ObjectProperty))\n",
" # g.add((elementURI, OWL.domain, conceptUri))\n",
" # g.add((elementURI, OWL.range, URIRef(\"\") ))\n",
" # g.add((elementURI, SKOS.prefLabel, Literal(element)))\n",
" else: ## datatype properties\n",
" g.add((elementURI, SKOS.prefLabel, Literal(element)))\n",
" g.add((elementURI, RDF.type, OWL.DatatypeProperty))\n",
" g.add((elementURI, OWL.domain, conceptUri))\n",
" range = None\n",
" if prop_type == 'DATE':\n",
" g.add((elementURI, OWL.range, XSD.date ))\n",
" range = XSD.date\n",
" elif prop_type == 'NUMERIC VALUE':\n",
" g.add((elementURI, OWL.range, XSD.float))\n",
" range = XSD.float\n",
" else:\n",
" g.add((elementURI, OWL.range, XSD.string))\n",
" range = XSD.string\n",
" ##cardinality\n",
" if cardinality == 'REPEAT VALUES':\n",
" br = BNode()\n",
" g.add((br, RDF.type, OWL.Restriction))\n",
" g.add((br, OWL.onProperty, elementURI))\n",
" g.add((br, OWL.someValuesFrom, range))\n",
" g.add((conceptUri, RDFS.subClassOf, br))\n",
" else:\n",
" br = BNode()\n",
" g.add((br, RDF.type, OWL.Restriction))\n",
" g.add((br, OWL.onProperty, elementURI))\n",
" g.add((br, OWL.maxQualifiedCardinality, Literal(1)))\n",
" g.add((br, OWL.onDataRange, range))\n",
" g.add((conceptUri, RDFS.subClassOf, br))\n",
" \n",
" \n",
" if comment:\n",
" g.add((elementURI, SKOS.scopeNote, Literal(comment)))\n",
" if example:\n",
" g.add((elementURI, RDFS.comment, Literal(example)))\n",
" if element_description:\n",
" g.add((elementURI, SKOS.definition, Literal(element_description)))\n",
" else:\n",
" print(element_name)\n",
" \n",
" \n",
" \n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "subject-asian",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 136,
"id": "sudden-elite",
"metadata": {},
"outputs": [],
"source": [
"## save graph\n",
"g.serialize(destination='idds.ttl', format='turtle')"
]
},
{
"cell_type": "code",
"execution_count": 137,
"id": "subsequent-zealand",
"metadata": {},
"outputs": [],
"source": [
"html = pylode.MakeDocco(\n",
" input_data_file=\"idds.ttl\",\n",
" outputformat=\"html\",\n",
" profile=\"ontdoc\"\n",
").document()"
]
},
{
"cell_type": "code",
"execution_count": 138,
"id": "occupied-caribbean",
"metadata": {},
"outputs": [],
"source": [
"f = open('documentation.html', 'w' )\n",
"f.write( html )\n",
"f.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "abstract-consortium",
"metadata": {},
"outputs": [],
"source": []
}
],
"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.9.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}