| 1 |
|
| 2 |
import sys |
| 3 |
|
| 4 |
from pyfactxx import coras |
| 5 |
from pyfactxx.coras.cli import load_and_parse |
| 6 |
import rdflib |
| 7 |
|
| 8 |
|
| 9 |
def describe(crs, node, nsm, visited, with_inferences=False, level=1): |
| 10 |
|
| 11 |
if node in visited: |
| 12 |
return |
| 13 |
|
| 14 |
a_po = list(crs._graph.predicate_objects(node)) |
| 15 |
b_po = list(crs._query_graph.predicate_objects(node)) if with_inferences else [] |
| 16 |
|
| 17 |
a_set = set(a_po) |
| 18 |
predicate_objects = list(a_po) + [po for po in b_po if po not in a_set] |
| 19 |
|
| 20 |
for row in predicate_objects: |
| 21 |
|
| 22 |
if level == 1 or row[0] not in (rdflib.URIRef('http://www.w3.org/2000/01/rdf-schema#subClassOf'), rdflib.URIRef('http://www.w3.org/2002/07/owl#equivalentClass')): |
| 23 |
print('\t'*level, row[0].n3(nsm), end='') |
| 24 |
|
| 25 |
if isinstance(row[1], rdflib.BNode): |
| 26 |
print(' ', row[1].n3(nsm)) |
| 27 |
describe(crs, row[1], nsm, visited | {node}, with_inferences, level + 1) |
| 28 |
else: |
| 29 |
print(' ', row[1].n3(nsm)) |
| 30 |
|
| 31 |
if len(sys.argv) < 3: |
| 32 |
print("Usage: describe.py <ontology filename> <object URI> [<with_inferences? (0/1)>]") |
| 33 |
else: |
| 34 |
|
| 35 |
filename = sys.argv[1] |
| 36 |
URI = sys.argv[2] |
| 37 |
|
| 38 |
if len(sys.argv) > 3: |
| 39 |
with_inferences = (sys.argv[3] == '1') |
| 40 |
else: |
| 41 |
with_inferences = False |
| 42 |
|
| 43 |
crs = coras.Coras() |
| 44 |
load_and_parse(crs, filename) |
| 45 |
|
| 46 |
if with_inferences: |
| 47 |
crs.realise() |
| 48 |
|
| 49 |
nsm = crs._graph.namespace_manager |
| 50 |
node = rdflib.URIRef(URI) |
| 51 |
|
| 52 |
|
| 53 |
print(node.n3(nsm)) |
| 54 |
describe(crs, node, nsm, set(), with_inferences) |