Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional SHACL support to Ontospy/gendocs #107

Merged
merged 20 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
fbe099f
Add module to determine SHACL property constraints
balon Dec 2, 2021
5fee4eb
Formatting changes to browser_shapeinfo.html
balon Dec 3, 2021
4a6d48c
Add shape properties to browser_shapeinfo.html
balon Dec 3, 2021
944e835
Function to check if an OWL.Class is SH.NodeShape
balon Dec 6, 2021
85dff84
For SHACL NodeShapes, replace tree-diagram in HTML
balon Dec 7, 2021
a53acec
Formatting changes to browser_classinfo.html
balon Dec 7, 2021
1750e87
Add 'Property Shapes' to browser_classinfo.html
balon Dec 7, 2021
f586950
Adjust notion of "Top-level property" to account for SHACL shapes
balon Dec 9, 2021
153d5fa
Merge branch 'hotfix-SHACL-shapes-usage' into feature-SHACL-gendocs
balon Dec 9, 2021
141fe7a
Fix whitespace nit reported by Git
ajnelson-nist Dec 16, 2021
ae42fbf
Match new query's spelling to getSKOSDirectSupers
ajnelson-nist Dec 16, 2021
6fa1b3b
Rely on implied sh:targetClass
ajnelson-nist Dec 16, 2021
1d7e289
Merge branch 'hotfix-SHACL-shapes-usage' into feature-SHACL-gendocs
ajnelson-nist Dec 16, 2021
2eb8543
Have class-shape confirmation function infer targetClass
ajnelson-nist Dec 16, 2021
805d237
Merge branch 'master' into feature-SHACL-gendocs
balon Jan 20, 2022
c509dd8
scigraph2 rdf test
lambdamusic Mar 11, 2022
9c3961b
Rename variable to prevent package name conflict; start type annotati…
ajnelson-nist Apr 11, 2022
8217329
Restore whitespace
ajnelson-nist Apr 11, 2022
b4dd695
Flag problem spot
ajnelson-nist Apr 11, 2022
9bad9d9
Rework shacl_helper.py
ajnelson-nist Apr 27, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion ontospy/core/ontospy.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from .rdf_loader import RDFLoader
from .entities import *
from .sparql_helper import SparqlHelper
from .shacl_helper import build_shacl_constraints


class Ontospy(object):
Expand Down Expand Up @@ -630,6 +631,11 @@ def build_shapes(self):
exit += [c]
self.toplayer_shapes = exit

# compute SHACL property constraints once classes and shapes have been built
shacl_constraints = build_shacl_constraints(self)
for onto_class in self.all_classes:
onto_class.shacl_constraints = shacl_constraints.get(onto_class, [])


def build_individuals(self):
"""
Expand Down Expand Up @@ -783,7 +789,8 @@ def getInferredPropertiesForClass(self, aClass, rel="domain_of"):

# add properties from Owl:Thing ie the inference layer

topLevelProps = [p for p in self.all_properties if p.domains == []]
properties_applicable_by_shapes = {x[0] for x in self.sparqlHelper.getPropsApplicableByShapes()}
topLevelProps = [p for p in self.all_properties if p.domains == [] and not p.uri in properties_applicable_by_shapes]
if topLevelProps:
_list.append({self.OWLTHING: topLevelProps})

Expand Down
468 changes: 468 additions & 0 deletions ontospy/core/shacl_helper.py

Large diffs are not rendered by default.

26 changes: 25 additions & 1 deletion ontospy/core/sparql_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,30 @@ def getPropDirectSupers(self, aURI):
""" % (aURI))
return list(qres)

def getPropsApplicableByShapes(self):
"""
Find all properties that should not be "top-level" because they
apply to some class via some SHACL PropertyShape.
"""

# This query finds classes by explicit class statement, and
# infers the class being a SHACL Shape from the domain of
# sh:property. From SHACL specification section 2.1.3.3,
# "Implicit Class Targets", a reflective sh:targetClass is
# implied.
# https://www.w3.org/TR/shacl/#implicit-targetClass
qres = self.rdflib_graph.query("""SELECT ?nProperty
WHERE {
{
{ ?nClass a rdfs:Class . }
UNION
{ ?nClass a owl:Class . }
}
?nClass sh:property/sh:path ?nProperty .
}
""")
return list(qres)

# ..................
# SKOS
# ..................
Expand Down Expand Up @@ -385,4 +409,4 @@ def getSKOSDirectSubs(self, aURI):
FILTER (!isBlank(?x))
}
""" % (aURI, aURI))
return list(qres)
return list(qres)
2 changes: 1 addition & 1 deletion ontospy/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import rdflib
from rdflib import RDFS, RDF, BNode
from rdflib.namespace import OWL, DC
from rdflib.namespace import OWL, DC, SH
DEFAULT_LANGUAGE = "en"

import sys, os, subprocess, random, platform
Expand Down
Loading