Esempio n. 1
0
 def test_biolink_shexeval(self) -> None:
     base_dir = os.path.abspath(
         os.path.join(os.path.dirname(__file__), '..', 'data'))
     g = CFGraph()
     g.load(os.path.join(base_dir, 'validation', 'biolink-model.ttl'),
            format="turtle")
     evaluator = ShExEvaluator(
         g, os.path.join(base_dir, 'schemas', 'meta.shex'),
         "https://biolink.github.io/biolink-model/ontology/biolink.ttl",
         "http://bioentity.io/vocab/SchemaDefinition")
     result = evaluator.evaluate(debug=False)
     for rslt in result:
         if not rslt.result:
             print(f"Error: {rslt.reason}")
     self.assertTrue(all(r.result for r in result))
Esempio n. 2
0
 def shextest(self,
              rdf_file: str,
              shex_file: str,
              focus: str,
              cfgraph: bool = False) -> None:
     base_dir = os.path.abspath(
         os.path.join(os.path.dirname(__file__), '..'))
     g = CFGraph() if cfgraph else Graph()
     g.load(os.path.join(base_dir, 'rdf', rdf_file), format="turtle")
     evaluator = ShExEvaluator(
         g, os.path.join(base_dir, 'shex', shex_file), focus,
         "http://w3id.org/biolink/vocab/SchemaDefinition")
     result = evaluator.evaluate(debug=False)
     for rslt in result:
         if not rslt.result:
             print(f"Error: {rslt.reason}")
     self.assertTrue(all(r.result for r in result))
Esempio n. 3
0
def evaluate_cli(argv: Optional[Union[str, List[str]]] = None,
                 prog: Optional[str] = None) -> int:
    if isinstance(argv, str):
        argv = argv.split()
    opts = genargs(prog).parse_args(argv if argv is not None else sys.argv[1:])
    if opts.sparql or opts.gdbslurper:
        opts.slurper = True
    if opts.slurper and opts.flattener:
        print("Error: Cannot combine slurper and flattener graphs",
              file=sys.stderr)
        return 2
    if not opts.sparql and not opts.slurper and \
            (opts.printsparql or opts.printsparqlresults or opts.graphname is not None or opts.persistbnodes):
        print(
            "Error: printsparql, pringsparqlresults, graphname and persistbnodes are SPARQL only",
            file=sys.stderr)
    if not opts.format:
        opts.format = guess_format(opts.rdf)
    if not opts.format:
        print(
            'Error: Cannot determine RDF format from file name - use "--format" option',
            file=sys.stderr)
        return 3
    if opts.slurper or opts.gdbslurper:
        g = SlurpyGraphWithAgent(opts.rdf,
                                 agent=opts.useragent,
                                 gdb_slurper=opts.gdbslurper)
        if opts.printsparql:
            g.debug_slurps = True
        if opts.printsparqlresults:
            g.debug_slurps = True
            g.add_result_hook(QueryResultPrinter)
        if opts.graphname is not None:
            g.graph_name = opts.graphname
        if opts.persistbnodes:
            g.persistent_bnodes = True
    else:
        g = CFGraph() if opts.flattener else Graph()
        if '\n' in opts.rdf or '\r' in opts.rdf:
            g.parse(data=opts.rdf, format=opts.format)
        else:
            g.load(opts.rdf, format=opts.format)

    if not (opts.focus or opts.allsubjects or opts.sparql):
        print(
            'Error: You must specify one or more graph focus nodes, supply a SPARQL query, or use the "-A" option',
            file=sys.stderr)
        return 4

    start = []
    if opts.start:
        start.append(opts.start)
    if opts.usetype:
        start.append(START_TYPE(RDF.type))
    if opts.startpredicate:
        start.append(START_TYPE(opts.startpredicate))
    if not start:
        start.append(START)
    if opts.sparql:
        # TODO: switch to a generator idiom all the way through
        if opts.focus is None:
            opts.focus = []
        elif not isinstance(opts.focus, list):
            opts.focus = [opts.focus]
        opts.focus += list(
            SPARQLQuery(opts.rdf,
                        opts.sparql,
                        print_query=opts.printsparql,
                        print_results=opts.printsparqlresults,
                        user_agent=opts.useragent).focus_nodes())

    def result_sink(rslt: EvaluationResult) -> bool:
        if not rslt.result:
            if evaluator.nerrors == 1:
                print("Errors:")
            else:
                print()
            print(
                f"  Focus: {rslt.focus}\n  Start: {rslt.start}\n  Reason: {str(rslt.reason)}"
            )
            return not opts.stoponerror and (not opts.stopafter or
                                             evaluator.nnodes < opts.stopafter)
        return not opts.stopafter or evaluator.nnodes < opts.stopafter

    evaluator = ShExEvaluator(g,
                              opts.shex,
                              opts.focus,
                              start,
                              rdf_format=opts.format,
                              debug=opts.debug,
                              output_sink=result_sink)
    evaluator.evaluate()
    return 1 if evaluator.nerrors else 0
Esempio n. 4
0
 def test_subjects(self):
     g = CFGraph()
     g.add((EX.foo, RDF.type, Literal("samsonite")))
     g.add((EX.foo, EX.carries, EX.dogs))
     self.assertEqual(2, len(list(g.subjects())))
Esempio n. 5
0
 def build_graph() -> CFGraph:
     g = CFGraph()
     g.parse(data=rdf, format='turtle')
     return g
Esempio n. 6
0
 def test_notebook(self):
     rdf = f'<{s1}> <{p1}> ("a" "b" "c" "d" "e" <{o1}> "b").'
     g = CFGraph()
     g.parse(data=rdf, format="turtle")
     self.assertEqual([(s1, p1)], sorted(g.subject_predicates(o1)))
     self.assertEqual([s1], sorted(g.subjects(p1, o1)))