Beispiel #1
0
def test_case1():
    # d_graph = validator.read_graphs(l6_clip_point, l6_input_polygon)
    d_graph = rdfutil.load_from_source(case_data)
    g_shape = rdfutil.load_from_source(case_shape)
    # conforms, results_graph, results_text = validator.advanced_validate(d_graph, g_shape)
    conforms, results_graph, results_text = validator.validate_data(
        d_graph, g_shape, ont)
    # print(results_graph.serialize(format='turtle').decode('utf-8'))
    dg.graph_2_file(results_graph, 'case1_report.ttl')
    # with codecs.open('case1_report.txt', 'w', 'utf-8') as f:
    # 	f.write(str(results_text))
    print('DONE')
Beispiel #2
0
def meta_validate(shacl_graph, inference='rdfs', **kwargs):
    shacl_shacl_graph = meta_validate.shacl_shacl_graph
    if shacl_shacl_graph is None:
        from os import path
        import pickle
        import sys
        if getattr(sys, 'frozen', False):
            # runs in a pyinstaller bundle
            here_dir = sys._MEIPASS
            pickle_file = path.join(here_dir, "shacl-shacl.pickle")
        else:
            here_dir = path.dirname(__file__)
            pickle_file = path.join(here_dir, "shacl-shacl.pickle")
        with open(pickle_file, 'rb') as shacl_pickle:
            u = pickle.Unpickler(shacl_pickle, fix_imports=False)
            shacl_shacl_store = u.load()
        shacl_shacl_graph = rdflib.Graph(
            store=shacl_shacl_store,
            identifier="http://www.w3.org/ns/shacl-shacl")
        meta_validate.shacl_shacl_graph = shacl_shacl_graph
    shacl_graph = load_from_source(shacl_graph,
                                   rdf_format=kwargs.pop(
                                       'shacl_graph_format', None),
                                   multigraph=True)
    _ = kwargs.pop('meta_shacl', None)
    return validate(shacl_graph,
                    shacl_graph=shacl_shacl_graph,
                    inference=inference,
                    **kwargs)
Beispiel #3
0
 def test_watershedDelineation(self):
     s_graph = rdfutil.load_from_source(case_shape)
     # not support rules
     conforms, results_graph, results_text = validator.validate_data(
         self.d_graph, s_graph, ont)
     print()
     print(conforms)
     print(results_text)
     dg.graph_2_file(results_graph, 'case3_report.ttl')
     print('DONE')
Beispiel #4
0
class TestPyGeoc(object):
    d_graph = rdfutil.load_from_source(case_data)

    @pytest.mark.skip()
    def test_watershedDelineation(self):
        s_graph = rdfutil.load_from_source(case_shape)
        # not support rules
        conforms, results_graph, results_text = validator.validate_data(
            self.d_graph, s_graph, ont)
        print()
        print(conforms)
        print(results_text)
        dg.graph_2_file(results_graph, 'case3_report.ttl')
        print('DONE')

    case_rules = rdfutil.load_from_source('./pygeoc_rules.ttl')

    def test_rules(self):
        conforms, expanded_graph = validator.infer_with_extended_graph(
            self.d_graph, self.case_rules)
        print(conforms)
        dg.graph_2_file(expanded_graph, 'case3_rules_results.ttl')
Beispiel #5
0
def meta_validate(shacl_graph: Union[GraphLike, str],
                  inference: Optional[str] = 'rdfs',
                  **kwargs):
    shacl_shacl_graph = meta_validate.graph_cache
    shacl_graph = load_from_source(shacl_graph,
                                   rdf_format=kwargs.pop(
                                       'shacl_graph_format', None),
                                   multigraph=True)
    _ = kwargs.pop('meta_shacl', None)
    return validate(shacl_graph,
                    shacl_graph=shacl_shacl_graph,
                    inference=inference,
                    **kwargs)
Beispiel #6
0
def meta_validate(shacl_graph, inference='rdfs', **kwargs):
    shacl_shacl_graph = meta_validate.shacl_shacl_graph
    if shacl_shacl_graph is None:
        from os import path
        import pickle
        here_dir = path.dirname(__file__)
        pickle_file = path.join(here_dir, "shacl-shacl.pickle")
        with open(pickle_file, 'rb') as shacl_pickle:
            u = pickle.Unpickler(shacl_pickle, fix_imports=False)
            shacl_shacl_store = u.load()
        shacl_shacl_graph = rdflib.Graph(store=shacl_shacl_store, identifier="http://www.w3.org/ns/shacl-shacl")
        meta_validate.shacl_shacl_graph = shacl_shacl_graph
    shacl_graph = load_from_source(shacl_graph,
                                  rdf_format=kwargs.pop('shacl_graph_format', None))
    _ = kwargs.pop('meta_shacl', None)
    return validate(shacl_graph, shacl_graph=shacl_shacl_graph, inference=inference, **kwargs)
Beispiel #7
0
def test_data_on_the_fly_case1():
    points = '../data/Heihe_Meteorology_Station_Distribution.shp'
    basin = '../data/Heihe_Basin_Boundary_2010.shp'
    # read raw input data and translate into data graphs
    clip_point = dg.vector_graph('clip_features', points,
                                 'Meteorology_Station')
    in_polygon = dg.vector_graph('in_features', basin, 'Basin')
    # functionality-level data graph
    func_graph = dg.functionality_data_graph(ArcGIS.clip_analysis, clip_point,
                                             in_polygon)
    # all
    data_graph = func_graph + clip_point + in_polygon
    dg.graph_2_file(data_graph, 'all.ttl')
    # validate input data against shapes graphs
    g_shape = rdfutil.load_from_source(l6_shape)
    conforms, results_graph, results_text = validator.validate_data(
        data_graph, g_shape, ont)
    dg.graph_2_file(results_graph, 'case1_report_2.ttl')
Beispiel #8
0
def validate(data_graph,
             *args,
             shacl_graph=None,
             ont_graph=None,
             advanced=False,
             inference=None,
             abort_on_error=False,
             **kwargs):
    """
    :param data_graph: rdflib.Graph or file path or web url of the data to validate
    :type data_graph: rdflib.Graph | str
    :param args:
    :type args: list
    :param shacl_graph: rdflib.Graph or file path or web url of the SHACL Shapes graph to use to validate the data graph
    :type shacl_graph: rdflib.Graph | str
    :param ont_graph: rdflib.Graph or file path or web url of an extra ontology document to mix into the data graph
    :type ont_graph: rdflib.Graph | str
    :param inference:
    :type inference: str | None
    :param abort_on_error:
    :type abort_on_error: bool | None
    :param kwargs:
    :return:
    """

    if kwargs.get('debug', False):
        log_handler.setLevel(logging.DEBUG)
        log.setLevel(logging.DEBUG)
    apply_patches()
    do_check_dash_result = kwargs.pop('check_dash_result', False)
    do_check_sht_result = kwargs.pop('check_sht_result', False)
    if kwargs.get('meta_shacl', False):
        to_meta_val = shacl_graph or data_graph
        conforms, v_r, v_t = meta_validate(to_meta_val,
                                           inference=inference,
                                           **kwargs)
        if not conforms:
            msg = "Shacl File does not validate against the Shacl Shapes Shacl file.\n{}"\
                  .format(v_t)
            log.error(msg)
            raise ReportableRuntimeError(msg)
    do_owl_imports = kwargs.pop('do_owl_imports', False)
    data_graph_format = kwargs.pop('data_graph_format', None)
    data_graph = load_from_source(
        data_graph,
        rdf_format=data_graph_format,
        multigraph=True,
        do_owl_imports=False)  # no imports on data_graph
    ont_graph_format = kwargs.pop('ont_graph_format', None)
    if ont_graph is not None:
        ont_graph = load_from_source(ont_graph,
                                     rdf_format=ont_graph_format,
                                     multigraph=True,
                                     do_owl_imports=do_owl_imports)
    shacl_graph_format = kwargs.pop('shacl_graph_format', None)
    if shacl_graph is not None:
        shacl_graph = load_from_source(shacl_graph,
                                       rdf_format=shacl_graph_format,
                                       multigraph=True,
                                       do_owl_imports=do_owl_imports)
    try:
        validator = Validator(data_graph,
                              shacl_graph=shacl_graph,
                              ont_graph=ont_graph,
                              options={
                                  'inference': inference,
                                  'abort_on_error': abort_on_error,
                                  'advanced': advanced,
                                  'logger': log
                              })
        conforms, report_graph, report_text = validator.run()
    except ValidationFailure as e:
        conforms = False
        report_graph = e
        report_text = "Validation Failure - {}".format(e.message)
    if do_check_dash_result:
        passes = check_dash_result(validator.target_graph, report_graph,
                                   shacl_graph or data_graph)
        return passes, report_graph, report_text
    if do_check_sht_result:
        (sht_graph, sht_result_node) = kwargs.pop('sht_validate',
                                                  (False, None))
        passes = check_sht_result(report_graph, sht_graph or shacl_graph
                                  or data_graph, sht_result_node)
        return passes, report_graph, report_text
    do_serialize_report_graph = kwargs.pop('serialize_report_graph', False)
    if do_serialize_report_graph and isinstance(report_graph, rdflib.Graph):
        if not (isinstance(do_serialize_report_graph, str)):
            do_serialize_report_graph = 'turtle'
        report_graph = report_graph.serialize(None,
                                              encoding='utf-8',
                                              format=do_serialize_report_graph)
    return conforms, report_graph, report_text
Beispiel #9
0
from validator import GeoValidator
from rdflib import Namespace, Graph
from pyshacl import rdfutil
from datagraph import DataGraph
from utils import DATA, GEO, ArcGIS, PROCESS
import pytest

validator = GeoValidator()
# ontologies
qudt = '../../ont/QUDT ALL UNITS.ttl'
data = '../../ont/data.owl'
dcat = '../../ont/dcat2.ttl'
geo_file = '../../ont/geosparql_vocab_all.rdf'
sf_file = '../../ont/sf.rdf'
process_file = '../../ont/gis-process.owl'
geo = rdfutil.load_from_source(geo_file)
ont = validator.read_graphs(geo_file, data, sf_file, dcat, qudt)
ont2 = validator.read_graphs(data, dcat, process_file)

case_data = './pygeoc_data.ttl'
case_shape = './pygeoc_shapes.ttl'
dg = DataGraph()


class TestPyGeoc(object):
    d_graph = rdfutil.load_from_source(case_data)

    @pytest.mark.skip()
    def test_watershedDelineation(self):
        s_graph = rdfutil.load_from_source(case_shape)
        # not support rules
Beispiel #10
0
# -*- coding: utf-8 -*-
#
"""
https://github.com/RDFLib/pySHACL/issues/40

"""
from pyshacl import validate
from pyshacl.rdfutil import load_from_source

with open("./test/issues/test_040/sample-network.ttl", "r") as f:
    data_graph = load_from_source(f)

shacl_graph = load_from_source("./test/issues/test_040/03-Network.ttl")


def test_040():
    conforms, g, s = validate(data_graph=data_graph,
                              shacl_graph=shacl_graph,
                              ont_graph=shacl_graph,
                              inference='rdfs')
    assert conforms


if __name__ == "__main__":
    test_040()
Beispiel #11
0
def validate(
    data_graph: Union[GraphLike, str, bytes],
    *args,
    shacl_graph: Optional[Union[GraphLike, str, bytes]] = None,
    ont_graph: Optional[Union[GraphLike, str, bytes]] = None,
    advanced: Optional[bool] = False,
    inference: Optional[str] = None,
    abort_on_error: Optional[bool] = False,
    **kwargs,
):
    """
    :param data_graph: rdflib.Graph or file path or web url of the data to validate
    :type data_graph: rdflib.Graph | str | bytes
    :param args:
    :type args: list
    :param shacl_graph: rdflib.Graph or file path or web url of the SHACL Shapes graph to use to
    validate the data graph
    :type shacl_graph: rdflib.Graph | str | bytes
    :param ont_graph: rdflib.Graph or file path or web url of an extra ontology document to mix into the data graph
    :type ont_graph: rdflib.Graph | str | bytes
    :param advanced: Enable advanced SHACL features, default=False
    :type advanced: bool | None
    :param inference: One of "rdfs", "owlrl", "both", "none", or None
    :type inference: str | None
    :param abort_on_error:
    :type abort_on_error: bool | None
    :param kwargs:
    :return:
    """

    if kwargs.get('debug', False):
        log_handler.setLevel(logging.DEBUG)
        log.setLevel(logging.DEBUG)
    apply_patches()
    do_check_dash_result = kwargs.pop('check_dash_result', False)  # type: bool
    do_check_sht_result = kwargs.pop('check_sht_result', False)  # type: bool
    if kwargs.get('meta_shacl', False):
        to_meta_val = shacl_graph or data_graph
        conforms, v_r, v_t = meta_validate(to_meta_val,
                                           inference=inference,
                                           **kwargs)
        if not conforms:
            msg = "Shacl File does not validate against the Shacl Shapes Shacl file.\n{}".format(
                v_t)
            log.error(msg)
            raise ReportableRuntimeError(msg)
    do_owl_imports = kwargs.pop('do_owl_imports', False)
    data_graph_format = kwargs.pop('data_graph_format', None)
    # force no owl imports on data_graph
    data_graph = load_from_source(data_graph,
                                  rdf_format=data_graph_format,
                                  multigraph=True,
                                  do_owl_imports=False)
    ont_graph_format = kwargs.pop('ont_graph_format', None)
    if ont_graph is not None:
        ont_graph = load_from_source(ont_graph,
                                     rdf_format=ont_graph_format,
                                     multigraph=True,
                                     do_owl_imports=do_owl_imports)
    shacl_graph_format = kwargs.pop('shacl_graph_format', None)
    if shacl_graph is not None:
        # SHACL spec requires rdf BOOL literals to operate in a very specific way
        rdflib_bool_patch()
        shacl_graph = load_from_source(shacl_graph,
                                       rdf_format=shacl_graph_format,
                                       multigraph=True,
                                       do_owl_imports=do_owl_imports)
        rdflib_bool_unpatch()
    try:
        validator = Validator(
            data_graph,
            shacl_graph=shacl_graph,
            ont_graph=ont_graph,
            options={
                'inference': inference,
                'abort_on_error': abort_on_error,
                'advanced': advanced,
                'logger': log
            },
        )
        conforms, report_graph, report_text = validator.run()
    except ValidationFailure as e:
        conforms = False
        report_graph = e
        report_text = "Validation Failure - {}".format(e.message)
    if do_check_dash_result:
        passes = check_dash_result(validator.target_graph, report_graph,
                                   shacl_graph or data_graph)
        return passes, report_graph, report_text
    if do_check_sht_result:
        (sht_graph, sht_result_node) = kwargs.pop('sht_validate',
                                                  (False, None))
        if not sht_result_node:
            raise RuntimeError(
                "Cannot check SHT result if SHT graph and result node are not given."
            )
        passes = check_sht_result(report_graph, sht_graph or shacl_graph
                                  or data_graph, sht_result_node)
        return passes, report_graph, report_text
    do_serialize_report_graph = kwargs.pop('serialize_report_graph', False)
    if do_serialize_report_graph and isinstance(report_graph, rdflib.Graph):
        if not (isinstance(do_serialize_report_graph, str)):
            do_serialize_report_graph = 'turtle'
        report_graph = report_graph.serialize(None,
                                              encoding='utf-8',
                                              format=do_serialize_report_graph)
    return conforms, report_graph, report_text
Beispiel #12
0
from pyshacl import rdfutil
from datagraph import DataGraph
from utils import DATA, GEO, ArcGIS, PROCESS

validator = GeoValidator()

l6_clip_point = '../data_graphs/L6_clip_point.ttl'
l6_input_polygon = '../data_graphs/L6_input_polygon.ttl'
l6_data_all = '../data_graphs/L6_all.ttl'
l6_shape = '../shapes/L6_SparqlShape.ttl'
qudt = '../ont/QUDT ALL UNITS.ttl'
data = '../ont/data.owl'
geo_file = '../ont/geosparql_vocab_all.rdf'
sf_file = '../ont/sf.rdf'

geo = rdfutil.load_from_source(geo_file)
ont = validator.read_graphs(geo_file, data, sf_file)

# g_geo = validator.read_graph(geo)
# v_shape = validator.read_graph(v_shape_file)


def test_case1():
    # d_graph = validator.read_graphs(l6_clip_point, l6_input_polygon)
    d_graph_all = rdfutil.load_from_source(l6_data_all)
    g_shape = rdfutil.load_from_source(l6_shape)
    # conforms, results_graph, results_text = validator.advanced_validate(d_graph, g_shape)
    conforms, results_graph, results_text = validator.validate_data(
        d_graph_all, g_shape, ont)
    # print(results_graph.serialize(format='turtle').decode('utf-8'))
    dg.graph_2_file(results_graph, 'case1_report.ttl')