Skip to content

Commit

Permalink
USE SHAPE FOR TEMPLATE
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyseale committed Jan 5, 2024
1 parent e533fc2 commit 612b26a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
24 changes: 16 additions & 8 deletions code/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
# g.parse('../ontology/dprod/dprod-ontology.ttl', format='ttl')
# g.parse('../ontology/dprod/dprod-dcatprofile.ttl', format='ttl')
g.parse('../ontology/dprod/dprod.ttl', format='ttl')
g.add((DCAT.DataService, RDF.type, OWL.Class))
g.add((DCAT.Distribution, RDF.type, OWL.Class))
g.add((DCAT.Dataset, RDF.type, OWL.Class))

# Define the JSON-LD context
context = {
Expand Down Expand Up @@ -68,20 +65,31 @@ def reorder_list(list_to_order, reference_list):
def add_to_context(uri):
if isinstance(uri, rdflib.term.URIRef):
name = short_name(uri)
if name.endswith("Shape"):
name = name[:-len("Shape")]
types = list(g.objects(uri, RDF.type))
if OWL.Class in types or SH.NodeShape in types:
if SH.NodeShape in types:
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)
for s, p, o in g.triples((uri, SH.property, None)):
property_name = short_name(o)
property_name = property_name.replace(f'{name}-', '')
rdf_property = RdfProperty(name=property_name, uri=o)
class_obj.properties.append(rdf_property)
rdf_property.__dict__["domain"] = class_obj.targetClass
rdf_property.__dict__["domain_short"] = g.namespace_manager.normalizeUri(class_obj.targetClass)
rdf_property.__dict__["short_uri"] = g.namespace_manager.normalizeUri(o)
for s1, p1, o1 in g.triples((rdf_property.uri, None, None)):
rdf_property.__dict__[short_name(p1)] = o1
p1_name = short_name(p1)
if p1_name == 'class' or p1_name == 'datatype':
rdf_property.__dict__["range"] = o1
rdf_property.__dict__["range_short"] = g.namespace_manager.normalizeUri(o1)
rdf_property.__dict__[p1_name] = 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)}
Expand All @@ -97,7 +105,7 @@ def add_to_context(uri):

json_ld = {"@context": context}

classes = reorder_list(classes.values(), ['DataProduct', 'Distribution', 'DataService'])
classes = reorder_list(classes.values(), ['DataProduct', 'DataService', 'Distribution', 'Dataset'])
env = jinja2.Environment(loader=jinja2.FileSystemLoader(searchpath="../docs/respec/"))
template = env.get_template("template.html")
spec = template.render(classes=classes)
Expand Down
6 changes: 3 additions & 3 deletions docs/respec/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,13 @@ <h2>{{cls.name}}</h2>
<h2>{{prop.name}}</h2>
<table class="def propdef">
<tbody>
<tr><th>Identifier:</th> <td><code>dprod:{{prop.name}}</code></td></tr>
<tr><th>Identifier:</th><td><a href="{{prop.uri}}">dprod:{{prop.name}}</a></td></tr>
{% if prop.label is not none %}
<tr><th>Label:</th><td>{{prop.label}}</td></tr>
{% endif %}
<tr><th>Notes:</th><td>{{prop.description}}</td></tr>
<tr><th>Domain:</th><td>{{prop.domain}}</td></tr>
<tr><th>Range:</th><td>{{prop.range}}</td></tr>
<tr><th>Domain:</th><td><a href="{{prop.domain}}">{{prop.domain_short}}</a></td></tr>
<tr><th>Range:</th><td><a href="{{prop.range}}">{{prop.range_short}}</a></td></tr>
</tbody>
</table>
</section>
Expand Down

0 comments on commit 612b26a

Please sign in to comment.