Exemplo n.º 1
0
def test_useOnlyExtraShapeGraph():
    dataG = loadGraph('data/badBuilding.ttl')
    shapeG = loadGraph('data/extraShapes.ttl')
    v = Validator(useDefaultShapes=False)  # do not use default shapes
    result = v.validate(dataG, shacl_graphs=[shapeG])
    assert not result.conforms, 'expect constraint violations in badBuilding.ttl'
    assert len(result.violationGraphs) == 5, 'unexpected # of violations'
Exemplo n.º 2
0
def test_useExtraShapeGraph():
    dataG = loadGraph('data/badBuilding.ttl')
    shapeG = loadGraph('data/extraShapes.ttl')
    v = Validator()
    # (conforms, violationList, results_text) = v.validate(dataG,
    result = v.validate(dataG, shacl_graphs=[shapeG])
    assert not result.conforms, 'expect constraint violations in badBuilding.ttl'
    assert len(result.violationGraphs) == 10, 'unexpected # of violations'
    print(result.textOutput)
Exemplo n.º 3
0
def main():
    parser = argparse.ArgumentParser(description='pySHACL wrapper for reporting constraint violating triples.')
    parser.add_argument('data', metavar='DataGraph', type=argparse.FileType('rb'),
                        help='Data graph file.')
    parser.add_argument('-s', '--shacl', dest='shacl', action='append', nargs='?',
                        help='SHACL shapes graph files (accumulative) (in addition to BrickShape.ttl).')
    parser.add_argument('-e', '--ont-graph', dest='ont', action='append', nargs='?',
                        help='Ontology graph files (accumulative) (in addition to Brick.ttl).')
    parser.add_argument('--noBrickSchema', dest='noBrickSchema', action='store_true',
                        default=False, help='Do not use Brick.ttl as an ontology file.')
    parser.add_argument('--noDefaultShapes', dest='noDefaultShapes', action='store_true',
                        default=False, help='Do not use BrickShape.ttl as an shape file.')
    parser.add_argument('-i', '--inference', dest='inference', action='store',
                        default='rdfs', choices=('none', 'rdfs', 'owlrl', 'both'),
                        help='Type of inference against data graph before validating.')
    parser.add_argument('-m', '--metashacl', dest='metashacl', action='store_true',
                        default=False,
                        help='Validate SHACL shapes graph against shacl-shacl '
                        'shapes graph before validating data graph.')
    parser.add_argument('-a', '--advanced', dest='advanced', action='store_true',
                        default=False,
                        help='Enable features from SHACL Advanced Features specification.')
    parser.add_argument('--abort', dest='abort', action='store_true',
                        default=False, help='Abort on first error.')
    parser.add_argument('-d', '--debug', dest='debug', action='store_true',
                        default=False, help='Output additional runtime messages.')

    args = parser.parse_args()

    dataG = Graph()
    dataG = dataG.parse(args.data, format='turtle')

    shaclGraphs = []
    if args.shacl:
        for shaclFile in args.shacl:
            shaclG = Graph()
            shaclG.parse(shaclFile, format='turtle')
            shaclGraphs.append(shaclG)

    ontGraphs = []
    if args.ont:
        for ontFile in args.ont:
            ontG = Graph()
            ontG.parse(ontFile, format='turtle')
            ontGraphs.append(ontG)

    v = Validator(useBrickSchema=(False if args.noBrickSchema else True),
                       useDefaultShapes=(False if args.noDefaultShapes else True))
    result = v.validate(dataG, ont_graphs=ontGraphs, shacl_graphs=shaclGraphs,
                        inference=args.inference, abort_on_error=args.abort,
                        advanced=args.advanced, meta_shacl=args.metashacl, debug=args.debug)
    print(result.textOutput)
    exit(0 if result.conforms else -1)
Exemplo n.º 4
0
def test_useExtraOntGraphShapeGraph():
    dataG = loadGraph('data/badBuilding.ttl')
    ontG1 = loadGraph('data/extraOntology1.ttl')
    ontG2 = loadGraph('data/extraOntology2.ttl')
    v = Validator()

    # Without extra shapes for the extra ontology files
    # we shouldn't see more violations
    result = v.validate(dataG, ont_graphs=[ontG1])
    assert not result.conforms, 'expect constraint violations in badBuilding.ttl'
    assert len(result.violationGraphs) == 5, 'unexpected # of violations'

    result = v.validate(dataG, ont_graphs=[ontG1, ontG2])
    assert not result.conforms, 'expect constraint violations in badBuilding.ttl'
    assert len(result.violationGraphs) == 5, 'unexpected # of violations'

    shapeG1 = loadGraph('data/extraShapes.ttl')
    shapeG2 = loadGraph('data/extraShapesWithExtraOnt.ttl')

    # Add one extraShape file
    result = v.validate(dataG,
                        ont_graphs=[ontG1, ontG2],
                        shacl_graphs=[shapeG1])
    assert not result.conforms, 'expect constraint violations in badBuilding.ttl'
    assert len(result.violationGraphs) == 10, 'unexpected # of violations'

    # Add second extraShape file that goes with the extra ontology
    result = v.validate(dataG,
                        ont_graphs=[ontG1, ontG2],
                        shacl_graphs=[shapeG1, shapeG2])
    assert not result.conforms, 'expect constraint violations in badBuilding.ttl'
    assert len(result.violationGraphs) == 12, 'unexpected # of violations'
Exemplo n.º 5
0
def test_validate_ok():
    dataG = loadGraph('data/goodBuilding.ttl')
    v = Validator()
    result = v.validate(dataG)
    assert result.conforms, 'expect no constraint violations in goodBuilding.ttl'
    assert len(result.violationGraphs) == 0, 'unexpected # of violations'
Exemplo n.º 6
0
def test_validate_error():
    dataG = loadGraph('data/badBuilding.ttl')
    v = Validator()
    result = v.validate(dataG)
    assert not result.conforms, 'expect constraint violations in badBuilding.ttl'
    assert len(result.violationGraphs) == 5, 'unexpected # of violations'
Exemplo n.º 7
0
def main():
    parser = argparse.ArgumentParser(
        description=
        "pySHACL wrapper for reporting constraint violating triples.")
    parser.add_argument(
        "data",
        metavar="DataGraph",
        type=argparse.FileType("rb"),
        help="Data graph file.",
    )
    parser.add_argument(
        "-s",
        "--shacl",
        dest="shacl",
        action="append",
        nargs="?",
        help=
        "SHACL shapes graph files (accumulative) (in addition to BrickShape.ttl).",
    )
    parser.add_argument(
        "-e",
        "--ont-graph",
        dest="ont",
        action="append",
        nargs="?",
        help="Ontology graph files (accumulative) (in addition to Brick.ttl).",
    )
    parser.add_argument(
        "--noBrickSchema",
        dest="noBrickSchema",
        action="store_true",
        default=False,
        help="Do not use Brick.ttl as an ontology file.",
    )
    parser.add_argument(
        "--noDefaultShapes",
        dest="noDefaultShapes",
        action="store_true",
        default=False,
        help="Do not use BrickShape.ttl as an shape file.",
    )
    parser.add_argument(
        "-i",
        "--inference",
        dest="inference",
        action="store",
        default="rdfs",
        choices=("none", "rdfs", "owlrl", "both"),
        help="Type of inference against data graph before validating.",
    )
    parser.add_argument(
        "-m",
        "--metashacl",
        dest="metashacl",
        action="store_true",
        default=False,
        help="Validate SHACL shapes graph against shacl-shacl "
        "shapes graph before validating data graph.",
    )
    parser.add_argument(
        "-a",
        "--advanced",
        dest="advanced",
        action="store_true",
        default=False,
        help="Enable features from SHACL Advanced Features specification.",
    )
    parser.add_argument(
        "--abort",
        dest="abort",
        action="store_true",
        default=False,
        help="Abort on first error.",
    )
    parser.add_argument(
        "-d",
        "--debug",
        dest="debug",
        action="store_true",
        default=False,
        help="Output additional runtime messages.",
    )

    args = parser.parse_args()

    dataG = Graph()
    dataG = dataG.parse(args.data, format="turtle")

    shaclGraphs = []
    if args.shacl:
        for shaclFile in args.shacl:
            shaclG = Graph()
            shaclG.parse(shaclFile, format="turtle")
            shaclGraphs.append(shaclG)

    ontGraphs = []
    if args.ont:
        for ontFile in args.ont:
            ontG = Graph()
            ontG.parse(ontFile, format="turtle")
            ontGraphs.append(ontG)

    v = Validator(
        useBrickSchema=(False if args.noBrickSchema else True),
        useDefaultShapes=(False if args.noDefaultShapes else True),
    )
    result = v.validate(
        dataG,
        ont_graphs=ontGraphs,
        shacl_graphs=shaclGraphs,
        inference=args.inference,
        abort_on_error=args.abort,
        advanced=args.advanced,
        meta_shacl=args.metashacl,
        debug=args.debug,
    )
    print(result.textOutput)
    exit(0 if result.conforms else -1)