Commit a8f6f1a8 authored by lambdamusic's avatar lambdamusic

show implicit types

parent ef6fb3d2
......@@ -73,12 +73,16 @@ CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
@click.group(invoke_without_command=True, context_settings=CONTEXT_SETTINGS)
@click.option(
'--verbose',
'-v',
'--extra',
'-x',
is_flag=True,
help='Print out version info and debug messages.')
help=
'EXTRA-DATA: by default only declared classes/properties get extracted, but with this option all implicit types and predicates are returned.'
)
@click.option(
'--verbose', '-v', is_flag=True, help='VERBOSE: print out debug messages.')
@click.pass_context
def main_cli(ctx, verbose=False):
def main_cli(ctx, extra=False, verbose=False):
"""
Ontospy is a command line inspector for RDF/OWL models. Use the --help option with one of the commands listed below to find out more, or visit http://lambdamusic.github.io/Ontospy
"""
......@@ -86,6 +90,7 @@ Ontospy is a command line inspector for RDF/OWL models. Use the --help option wi
if ctx.obj is None: # Fix for bug (as of 3.0)
# https://github.com/pallets/click/issues/888
ctx.obj = {}
ctx.obj['EXTRA'] = extra
ctx.obj['VERBOSE'] = verbose
ctx.obj['STIME'] = sTime
......@@ -148,6 +153,7 @@ def gendocs(ctx,
"""Generate documentation for an ontology in html or markdown format
"""
verbose = ctx.obj['VERBOSE']
extra = ctx.obj['EXTRA']
sTime = ctx.obj['STIME']
print_opts = {
'labels': verbose,
......@@ -273,6 +279,7 @@ def lib(ctx,
Work with a local library of RDF models. If no option or argument is passed, by default the library contents are listed.
"""
verbose = ctx.obj['VERBOSE']
extra = ctx.obj['EXTRA']
sTime = ctx.obj['STIME']
print_opts = {
'labels': verbose,
......@@ -355,7 +362,7 @@ def lib(ctx,
##
## CHECK / ANALYZE / SCAN COMMAND
## SCAN COMMAND
##
......@@ -370,13 +377,15 @@ def lib(ctx,
def scan(ctx, sources=None, endpoint=False):
"""Search an RDF source for ontology entities and print out a report.
"""
extra = ctx.obj['EXTRA']
verbose = ctx.obj['VERBOSE']
sTime = ctx.obj['STIME']
print_opts = {
'labels': verbose,
'extra': extra,
}
if sources or (sources and endpoint):
action_analyze(sources, endpoint, print_opts, verbose)
action_analyze(sources, endpoint, print_opts, verbose, extra)
eTime = time.time()
tTime = eTime - sTime
printDebug("\n-----------\n" + "Time: %0.2fs" % tTime, "comment")
......@@ -416,6 +425,7 @@ def shell(sources=None):
def serial(ctx, source, output_format):
"""Serialize an RDF graph to a format of choice.
"""
extra = ctx.obj['EXTRA']
verbose = ctx.obj['VERBOSE']
sTime = ctx.obj['STIME']
print_opts = {
......@@ -470,6 +480,7 @@ def utils(
):
"""Miscellaneous utilities.
"""
extra = ctx.obj['EXTRA']
verbose = ctx.obj['VERBOSE']
sTime = ctx.obj['STIME']
print_opts = {
......
......@@ -61,15 +61,30 @@ from .manager import *
# ===========
def action_analyze(sources, endpoint=None, print_opts=False, verbose=False):
def action_analyze(sources,
endpoint=None,
print_opts=False,
verbose=False,
extra=False):
"""
Load up a model into ontospy and analyze it
"""
for x in sources:
click.secho("Parsing %s..." % str(x), fg='white')
if extra:
hide_base_schemas = False
hide_implicit_types = False
else:
hide_base_schemas = True
hide_implicit_types = True
if endpoint:
g = Ontospy(sparql_endpoint=sources[0], verbose=verbose)
g = Ontospy(
sparql_endpoint=sources[0],
verbose=verbose,
hide_base_schemas=hide_base_schemas,
hide_implicit_types=hide_implicit_types)
printDebug("Extracting classes info")
g.build_classes()
printDebug("..done")
......@@ -77,7 +92,11 @@ def action_analyze(sources, endpoint=None, print_opts=False, verbose=False):
g.build_properties()
printDebug("..done")
else:
g = Ontospy(uri_or_path=sources, verbose=verbose)
g = Ontospy(
uri_or_path=sources,
verbose=verbose,
hide_base_schemas=hide_base_schemas,
hide_implicit_types=hide_implicit_types)
shellPrintOverview(g, print_opts)
......
......@@ -54,7 +54,17 @@ class Ontospy(object):
"""
def __init__(self, uri_or_path=None, data=None, file_obj=None, rdf_format="", verbose=False, hide_base_schemas=True, sparql_endpoint=None, credentials=None, build_all=True):
def __init__(self,
uri_or_path=None,
data=None,
file_obj=None,
rdf_format="",
verbose=False,
hide_base_schemas=True,
hide_implicit_types=True,
sparql_endpoint=None,
credentials=None,
build_all=True):
"""
Load the graph in memory, then setup all necessary attributes.
"""
......@@ -84,11 +94,16 @@ class Ontospy(object):
# finally:
if uri_or_path or data or file_obj:
self.load_rdf(uri_or_path, data, file_obj, rdf_format, verbose, hide_base_schemas)
self.load_rdf(uri_or_path, data, file_obj, rdf_format, verbose,
hide_base_schemas, hide_implicit_types)
if build_all:
self.build_all(verbose=verbose, hide_base_schemas=hide_base_schemas)
self.build_all(
verbose=verbose,
hide_base_schemas=hide_base_schemas,
hide_implicit_types=hide_implicit_types)
elif sparql_endpoint: # by default entities are not extracted
self.load_sparql(sparql_endpoint, verbose, hide_base_schemas, credentials)
self.load_sparql(sparql_endpoint, verbose, hide_base_schemas,
hide_implicit_types, credentials)
else:
pass
......@@ -106,7 +121,14 @@ class Ontospy(object):
else:
return "<Ontospy object created but not initialized (use the `load_rdf` method to load an rdf schema)>"
def load_rdf(self, uri_or_path=None, data=None, file_obj=None, rdf_format="", verbose=False, hide_base_schemas=True):
def load_rdf(self,
uri_or_path=None,
data=None,
file_obj=None,
rdf_format="",
verbose=False,
hide_base_schemas=True,
hide_implicit_types=True):
"""Load an RDF source into an ontospy/rdflib graph"""
loader = RDFLoader(verbose=verbose)
loader.load(uri_or_path, data, file_obj, rdf_format)
......@@ -115,7 +137,11 @@ class Ontospy(object):
self.sparqlHelper = SparqlHelper(self.rdflib_graph)
self.namespaces = sorted(self.rdflib_graph.namespaces())
def load_sparql(self, sparql_endpoint, verbose=False, hide_base_schemas=True, credentials=None):
def load_sparql(self,
sparql_endpoint,
verbose=False,
hide_base_schemas=True,
hide_implicit_types=True, credentials=None):
"""
Set up a SPARQLStore backend as a virtual ontospy graph
......@@ -151,7 +177,10 @@ class Ontospy(object):
# === methods to build python objects === #
# ------------
def build_all(self, verbose=False, hide_base_schemas=True):
def build_all(self,
verbose=False,
hide_base_schemas=True,
hide_implicit_types=True):
"""
Extract all ontology entities from an RDF graph and construct Python representations of them.
"""
......@@ -163,7 +192,7 @@ class Ontospy(object):
if verbose:
printDebug("Ontologies.........: %d" % len(self.all_ontologies), "comment")
self.build_classes(hide_base_schemas)
self.build_classes(hide_base_schemas, hide_implicit_types)
if verbose:
printDebug("Classes............: %d" % len(self.all_classes), "comment")
......@@ -254,7 +283,7 @@ class Ontospy(object):
# RDFS:class vs OWL:class cf. http://www.w3.org/TR/owl-ref/ section 3.1
#
def build_classes(self, hide_base_schemas=True):
def build_classes(self, hide_base_schemas=True, hide_implicit_types=True):
"""
2015-06-04: removed sparql 1.1 queries
2015-05-25: optimized via sparql queries in order to remove BNodes
......@@ -268,7 +297,8 @@ class Ontospy(object):
self.all_classes = [] # @todo: keep adding?
qres = self.sparqlHelper.getAllClasses(hide_base_schemas=hide_base_schemas)
qres = self.sparqlHelper.getAllClasses(hide_base_schemas,
hide_implicit_types)
# print("rdflib query done")
for class_tuple in qres:
......
......@@ -72,9 +72,10 @@ class SparqlHelper(object):
# RDF/OWL CLASSES
# ..................
def getAllClasses(self, hide_base_schemas=True):
def getAllClasses(self, hide_base_schemas=True, hide_implicit_types=True):
"""
hide_base_schemas: by default, obscure all RDF/RDFS/OWL/XML stuff
* hide_base_schemas: by default, obscure all RDF/RDFS/OWL/XML stuff
* hide_implicit_types: don't make any inference based on rdf:type declarations
"""
query = """SELECT DISTINCT ?x ?c
WHERE {
......@@ -90,8 +91,7 @@ class SparqlHelper(object):
{ ?y rdfs:domain ?x }
union
{ ?y rdfs:range ?x }
# union
# { ?y rdf:type ?x }
%s
} .
OPTIONAL { ?x a ?c }
......@@ -102,8 +102,8 @@ class SparqlHelper(object):
}
ORDER BY ?x
"""
if hide_base_schemas:
query = query % """FILTER(
BIT_BASE_SCHEMAS = """FILTER(
!STRSTARTS(STR(?x), "http://www.w3.org/2002/07/owl")
&& !STRSTARTS(STR(?x), "http://www.w3.org/1999/02/22-rdf-syntax-ns")
&& !STRSTARTS(STR(?x), "http://www.w3.org/2000/01/rdf-schema")
......@@ -111,8 +111,17 @@ class SparqlHelper(object):
&& !STRSTARTS(STR(?x), "http://www.w3.org/XML/1998/namespace")
&& (!isBlank(?x))
) ."""
else:
query = query % ""
BIT_IMPLICIT_TYPES = """union
{ ?y rdf:type ?x }"""
if hide_base_schemas == False: # ..then do not filter out XML stuff
BIT_BASE_SCHEMAS = ""
if hide_implicit_types == True: # .. then do not add extra clause
BIT_IMPLICIT_TYPES = ""
query = query % (BIT_IMPLICIT_TYPES, BIT_BASE_SCHEMAS)
# print(query)
qres = self.rdflib_graph.query(query)
return list(qres)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment