Skip to content

Commit

Permalink
Merge branch 'base'
Browse files Browse the repository at this point in the history
  • Loading branch information
ajnelson-nist committed Mar 5, 2024
2 parents 3a65ded + 8a10ddf commit d1172d1
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/make -f

# Portions of this file contributed by NIST are governed by the following
# statement:
# Portions of this file contributed by NIST are governed by the
# following statement:
#
# This software was developed at the National Institute of Standards
# and Technology by employees of the Federal Government in the course
Expand Down
4 changes: 2 additions & 2 deletions dependencies/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/make -f

# Portions of this file contributed by NIST are governed by the following
# statement:
# Portions of this file contributed by NIST are governed by the
# following statement:
#
# This software was developed at the National Institute of Standards
# and Technology by employees of the Federal Government in the course
Expand Down
4 changes: 2 additions & 2 deletions ontology/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/make -f

# Portions of this file contributed by NIST are governed by the following
# statement:
# Portions of this file contributed by NIST are governed by the
# following statement:
#
# This software was developed at the National Institute of Standards
# and Technology by employees of the Federal Government in the course
Expand Down
161 changes: 161 additions & 0 deletions src/entail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#!/usr/bin/env python3

# Portions of this file contributed by NIST are governed by the
# following statement:
#
# This software was developed at the National Institute of Standards
# and Technology by employees of the Federal Government in the course
# of their official duties. Pursuant to Title 17 Section 105 of the
# United States Code, this software is not subject to copyright
# protection within the United States. NIST assumes no responsibility
# whatsoever for its use by other parties, and makes no guarantees,
# expressed or implied, about its quality, reliability, or any other
# characteristic.
#
# We would appreciate acknowledgement if the software is used.

"""
This script implements a restricted set of RDFS entailment patterns, sourced from here:
https://www.w3.org/TR/rdf11-mt/#patterns-of-rdfs-entailment-informative
The implementation is a subset of the 13 patterns, as well as restricting the entailments to only IRI-identified nodes.
"""

__version__ = "0.1.0"

import argparse
from typing import Dict

from rdflib import Graph, URIRef


def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("out_graph")
parser.add_argument("in_graph", nargs="+")
args = parser.parse_args()

out_graph = Graph()
in_graph = Graph()

for _in_graph_file in args.in_graph:
in_graph.parse(_in_graph_file)

in_and_out_graph = Graph()
in_and_out_graph += in_graph

pattern_to_construct_query: Dict[str, str] = {
"rdfs2": """\
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
CONSTRUCT {
?yyy rdf:type ?xxx .
}
WHERE {
?aaa rdfs:domain ?xxx .
?yyy ?aaa ?zzz .
FILTER isIRI(?aaa)
FILTER isIRI(?xxx)
FILTER isIRI(?yyy)
FILTER isIRI(?zzz)
}
""",
"rdfs3": """\
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
CONSTRUCT {
?zzz rdf:type ?xxx .
}
WHERE {
?aaa rdfs:range ?xxx .
?yyy ?aaa ?zzz .
FILTER isIRI(?aaa)
FILTER isIRI(?xxx)
FILTER isIRI(?yyy)
FILTER isIRI(?zzz)
}
""",
"rdfs5": """\
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
CONSTRUCT {
?xxx rdfs:subPropertyOf ?zzz .
}
WHERE {
?xxx rdfs:subPropertyOf ?yyy .
?yyy rdfs:subPropertyOf ?zzz .
FILTER isIRI(?xxx)
FILTER isIRI(?yyy)
FILTER isIRI(?zzz)
}
""",
"rdfs7": """\
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
CONSTRUCT {
?xxx ?bbb ?yyy .
}
WHERE {
?aaa rdfs:subPropertyOf ?bbb .
?xxx ?aaa ?yyy .
FILTER isIRI(?aaa)
FILTER isIRI(?bbb)
FILTER isIRI(?xxx)
FILTER isIRI(?yyy)
}
""",
"rdfs9": """\
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
CONSTRUCT {
?zzz rdf:type ?yyy .
}
WHERE {
?xxx rdfs:subClassOf ?yyy .
?zzz rdf:type ?xxx .
FILTER isIRI(?xxx)
FILTER isIRI(?yyy)
FILTER isIRI(?zzz)
}
""",
"rdfs11": """\
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
CONSTRUCT {
?xxx rdfs:subClassOf ?zzz .
}
WHERE {
?xxx rdfs:subClassOf ?yyy .
?yyy rdfs:subClassOf ?zzz .
FILTER isIRI(?xxx)
FILTER isIRI(?yyy)
FILTER isIRI(?zzz)
}
""",
}

tmp_graph: Graph
while True:
tmp_graph = Graph()
for pattern in pattern_to_construct_query:
construct_query = pattern_to_construct_query[pattern]
for construct_result in in_and_out_graph.query(construct_query):
assert isinstance(construct_result, tuple)
assert isinstance(construct_result[0], URIRef)
assert isinstance(construct_result[1], URIRef)
assert isinstance(construct_result[2], URIRef)
tmp_graph.add(
(construct_result[0], construct_result[1], construct_result[2])
)
if len(tmp_graph - in_and_out_graph) == 0:
break
for triple in tmp_graph.triples((None, None, None)):
in_and_out_graph.add(triple)
out_graph.add(triple)

out_graph.serialize(args.out_graph)


if __name__ == "__main__":
main()
2 changes: 2 additions & 0 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
exemplars-entailment.ttl
exemplars-expanded.ttl
monolithic.ttl
32 changes: 28 additions & 4 deletions tests/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/make -f

# Portions of this file contributed by NIST are governed by the following
# statement:
# Portions of this file contributed by NIST are governed by the
# following statement:
#
# This software was developed at the National Institute of Standards
# and Technology by employees of the Federal Government in the course
Expand All @@ -28,14 +28,14 @@ all: \
monolithic.ttl

.pyshacl.done.log: \
exemplars.ttl \
exemplars-expanded.ttl \
monolithic.ttl
source $(top_srcdir)/venv/bin/activate \
&& pyshacl \
--metashacl \
--ont-graph monolithic.ttl \
--shacl monolithic.ttl \
exemplars.ttl
exemplars-expanded.ttl
touch $@

.pytest.done.log: \
Expand All @@ -54,8 +54,32 @@ check: \
clean:
@rm -f \
.*.done.log \
exemplars-entailment.ttl \
exemplars-expanded.ttl \
monolithic.ttl

exemplars-entailment.ttl: \
$(top_srcdir)/.venv.done.log \
$(top_srcdir)/src/entail.py \
exemplars.ttl \
monolithic.ttl
source $(top_srcdir)/venv/bin/activate \
&& python3 $(top_srcdir)/src/entail.py \
_$@ \
exemplars.ttl \
monolithic.ttl
mv _$@ $@

exemplars-expanded.ttl: \
exemplars-entailment.ttl
source $(top_srcdir)/venv/bin/activate \
&& rdfpipe \
--output-format turtle \
exemplars.ttl \
$< \
> _$@
mv _$@ $@

monolithic.ttl: \
$(top_srcdir)/.venv.done.log \
$(top_srcdir)/dependencies/imports-transitive.ttl \
Expand Down
4 changes: 2 additions & 2 deletions tests/test_exemplar_coverage.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python3

# Portions of this file contributed by NIST are governed by the following
# statement:
# Portions of this file contributed by NIST are governed by the
# following statement:
#
# This software was developed at the National Institute of Standards
# and Technology by employees of the Federal Government in the course
Expand Down

0 comments on commit d1172d1

Please sign in to comment.