Skip to content

Commit

Permalink
define REC relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
gtfierro committed Aug 7, 2024
1 parent 1f60fb2 commit 761b4e6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
19 changes: 18 additions & 1 deletion bricksrc/relationships.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from rdflib import Literal
from .namespaces import A, OWL, RDFS, BRICK, VCARD, QUDT, SDO, RDF, BSH, XSD
from .namespaces import A, OWL, RDFS, BRICK, VCARD, QUDT, SDO, RDF, BSH, XSD, REC
from .env import env

"""
Defining Brick relationships
Expand Down Expand Up @@ -194,3 +195,19 @@
RDFS.label: Literal("is sub-meter of", lang="en"),
},
}

# add REC relationships by mining them from the REC ontology
rec = env.get_graph("https://w3id.org/rec")
# for all objects of sh:path, read out the sh:nodeKind and sh:datatype
query = """SELECT ?path ?nodeKind ?datatype WHERE {
?p sh:path ?path .
OPTIONAL { ?p sh:nodeKind ?nodeKind }
OPTIONAL { ?p sh:datatype ?datatype }
}"""
for row in rec.query(query):
if row["path"] not in relationships:
relationships[row["path"]] = {}
if row["datatype"]:
relationships[row["path"]][A] = [OWL.DatatypeProperty]
if row["nodeKind"]:
relationships[row["path"]][A] = [OWL.ObjectProperty, OWL.AsymmetricProperty, OWL.IrreflexiveProperty]
8 changes: 5 additions & 3 deletions generate_brick.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,12 +631,13 @@ def define_relationships(definitions, superprop=None, graph=G):
return

for prop, propdefn in definitions.items():
if isinstance(prop, str):
if not isinstance(prop, URIRef):
prop = BRICK[prop]
if superprop is not None:
graph.add((prop, RDFS.subPropertyOf, superprop))

graph.add((prop, RDFS.subPropertyOf, BRICK.Relationship))
if prop.startswith(BRICK):
graph.add((prop, RDFS.subPropertyOf, BRICK.Relationship))

# define property types
prop_types = propdefn.get(A, [])
Expand All @@ -650,7 +651,8 @@ def define_relationships(definitions, superprop=None, graph=G):
define_relationships(subproperties_def, prop, graph=graph)

# generate a SHACL Property Shape for this relationship
propshape = BSH[f"{prop.split('#')[-1]}Shape"]
qname = graph.namespace_manager.qname(prop)
propshape = BSH[f"{qname.replace(':','_')}Shape"]
graph.add((propshape, A, SH.PropertyShape))
graph.add((propshape, SH.path, prop))
if "range" in propdefn.keys():
Expand Down

0 comments on commit 761b4e6

Please sign in to comment.