-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add script to gen json-ld and respec
- Loading branch information
Showing
7 changed files
with
314 additions
and
20 deletions.
There are no files selected for viewing
Empty file.
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,98 @@ | ||
import re | ||
from dataclasses import dataclass, field, asdict | ||
from typing import List | ||
|
||
import jinja2 | ||
import rdflib | ||
from jinja2 import Environment, PackageLoader, select_autoescape | ||
from rdflib import Graph, URIRef | ||
from rdflib.namespace import OWL, RDF, RDFS, XSD, DCAT | ||
import json | ||
|
||
# Load the OWL ontology into an RDFlib graph | ||
g = Graph() | ||
g.parse('../ontology/dprod/dprod.ttl', format='ttl') | ||
|
||
# Define the JSON-LD context | ||
context = { | ||
"@vocab": str(RDF), | ||
"owl": str(OWL), | ||
"rdfs": str(RDFS), | ||
"xsd": str(XSD), | ||
"dcat": str(DCAT) | ||
} | ||
|
||
@dataclass | ||
class RdfProperty: | ||
name: str | ||
uri: URIRef | ||
description: str = None | ||
|
||
@dataclass | ||
class RdfClass: | ||
name: str | ||
uri: URIRef | ||
description: str = None | ||
inherits: list = field(default_factory=list) | ||
properties: List[RdfProperty] = field(default_factory=list) | ||
|
||
|
||
|
||
classes = {} | ||
|
||
|
||
def short_name(uri, split_on=r'/|#'): | ||
if uri is None: | ||
return None | ||
split = re.split(split_on, uri) | ||
return split[len(split) - 1] | ||
|
||
|
||
# Define a function to add classes and properties to the context | ||
def add_to_context(uri): | ||
if isinstance(uri, rdflib.term.URIRef): | ||
name = short_name(uri) | ||
types = list(g.objects(uri, RDF.type)) | ||
if OWL.Class in g.objects(uri, RDF.type): | ||
context[name] = {"@id": str(uri)} | ||
class_obj = RdfClass(name=name, uri=uri) | ||
classes[uri] = class_obj | ||
for s1, p1, o1 in g.triples((class_obj.uri, None, None)): | ||
class_obj.__dict__[short_name(p1)] = o1 | ||
for s, p, o in g.triples((None, RDFS.subClassOf, uri)): | ||
add_to_context(s) | ||
for s, p, o in g.triples((None, RDFS.domain, uri)): | ||
rdf_property = RdfProperty(name=short_name(s), uri=s) | ||
class_obj.properties.append(rdf_property) | ||
for s1, p1, o1 in g.triples((rdf_property.uri, None, None)): | ||
rdf_property.__dict__[short_name(p1)] = o1 | ||
|
||
|
||
elif OWL.ObjectProperty in types: | ||
for s, p, o in g.triples((uri, RDFS.range, None)): | ||
context[name] = {"@id": str(uri), "@type": str(o)} | ||
elif OWL.DatatypeProperty in types: | ||
range_uri = next(g.objects(uri, RDFS.range)) | ||
context[name] = {"@id": str(uri), "@type": str(range_uri)} | ||
# context[name] = {"@id": str(uri)} | ||
|
||
|
||
# Add classes and properties to the context | ||
for class_uri in g.subjects(): | ||
add_to_context(class_uri) | ||
|
||
json_ld = {"@context": context} | ||
classes = classes.values() | ||
|
||
env = jinja2.Environment(loader=jinja2.FileSystemLoader(searchpath="../docs/respec/")) | ||
template = env.get_template("template.html") | ||
spec = template.render(classes=classes) | ||
for c in classes: | ||
print(c.name) | ||
with open('../docs/assets/spec.html', 'w', encoding='utf-8') as f: | ||
f.write(spec) | ||
|
||
with open('../docs/assets/dprod.jsonld', 'w', encoding='utf-8') as f: | ||
f.write(json.dumps(json_ld, indent=4)) | ||
|
||
|
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,66 @@ | ||
{ | ||
"@context": { | ||
"@vocab": "http://www.w3.org/1999/02/22-rdf-syntax-ns#", | ||
"owl": "http://www.w3.org/2002/07/owl#", | ||
"rdfs": "http://www.w3.org/2000/01/rdf-schema#", | ||
"xsd": "http://www.w3.org/2001/XMLSchema#", | ||
"dcat": "http://www.w3.org/ns/dcat#", | ||
"inputPort": { | ||
"@id": "https://w3id.org/dprod/inputPort", | ||
"@type": "http://www.w3.org/ns/dcat#DataService" | ||
}, | ||
"DataProductLifecycleStatus": { | ||
"@id": "https://w3id.org/dprod/DataProductLifecycleStatus" | ||
}, | ||
"GraphQLDataService": { | ||
"@id": "https://w3id.org/dprod/GraphQLDataService" | ||
}, | ||
"CallbackDataService": { | ||
"@id": "https://w3id.org/dprod/CallbackDataService" | ||
}, | ||
"StreamingDataService": { | ||
"@id": "https://w3id.org/dprod/StreamingDataService" | ||
}, | ||
"ObjectDataService": { | ||
"@id": "https://w3id.org/dprod/ObjectDataService" | ||
}, | ||
"lifecycle": { | ||
"@id": "https://w3id.org/dprod/lifecycle", | ||
"@type": "http://www.w3.org/ns/dcat#DataProductLifecycle" | ||
}, | ||
"RESTDataService": { | ||
"@id": "https://w3id.org/dprod/RESTDataService" | ||
}, | ||
"dataProductOwner": { | ||
"@id": "https://w3id.org/dprod/dataProductOwner", | ||
"@type": "http://purl.org/dc/terms/Agent" | ||
}, | ||
"outputPort": { | ||
"@id": "https://w3id.org/dprod/outputPort", | ||
"@type": "http://www.w3.org/ns/dcat#DataService" | ||
}, | ||
"offersDistribution": { | ||
"@id": "https://w3id.org/dprod/offersDistribution", | ||
"@type": "http://www.w3.org/ns/dcat#Distribution" | ||
}, | ||
"QueuingDataService": { | ||
"@id": "https://w3id.org/dprod/QueuingDataService" | ||
}, | ||
"FileDataService": { | ||
"@id": "https://w3id.org/dprod/FileDataService" | ||
}, | ||
"DatabaseDataService": { | ||
"@id": "https://w3id.org/dprod/DatabaseDataService" | ||
}, | ||
"DataProduct": { | ||
"@id": "https://w3id.org/dprod/DataProduct" | ||
}, | ||
"Enumeration": { | ||
"@id": "https://w3id.org/dprod/Enumeration" | ||
}, | ||
"belongsToDataset": { | ||
"@id": "https://w3id.org/dprod/belongsToDataset", | ||
"@type": "http://www.w3.org/ns/dcat#Dataset" | ||
} | ||
} | ||
} |
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
Oops, something went wrong.