def run_test(self, manifest_uri: str, num_entries: Optional[int]=None, verbose: bool=True, debug: bool=False, stop_on_fail: bool=False, debug_slurps: bool=False, save_graph_dir: Optional[str]=None) \ -> List[EvaluationResult]: """ Run the test identified by manifest_uri :param manifest_uri: uri of manifest :param num_entries: number of manifest elements to test :param verbose: True means talk about it :param debug: debug setting for shex evaluator :param stop_on_fail: True means run until failure :param debug_slurps: True means emit SPARQL_slurper statistics :param save_graph_dir: If present, save the final graph in this directory :return: """ manifest = loads(self.fetch_uri(manifest_uri)) rval: List[EvaluationResult] = [] for case in manifest: if verbose: print(case._as_json_dumps()) sparql_endpoint = case.data.replace("Endpoint: ", "") shex = self.fetch_uri(case.schemaURL) evaluator = ShExEvaluator(schema=shex, debug=debug) prefixes = PrefixLibrary(shex, SKOS=SKOS) sparql_query = case.queryMap.replace("SPARQL '''", "").replace("'''@START", "") dfs: List[str] = self.get_sparql_dataframe(sparql_endpoint, sparql_query) dfs_slice = dfs[:num_entries] if num_entries is not None else dfs for df in dfs_slice: slurper = SlurpyGraphWithAgent(sparql_endpoint) # slurper.debug_slurps = debug_slurps prefixes.add_bindings(slurper) print(f"Evaluating: {df}") results = evaluator.evaluate(rdf=slurper, focus=df, debug=debug, debug_slurps=debug_slurps, over_slurp=False) rval += results if save_graph_dir: element_name = df.rsplit('/', 1)[1] file_name = os.path.join(save_graph_dir, element_name + '.ttl') print(f"Writing: {file_name}") slurper.serialize(file_name, format="turtle") if stop_on_fail and not all(r.result for r in results): break return rval
def test_add_shex_filename(self): """ Test adding Shex from a file """ filename = os.path.join(os.path.dirname(__file__), '..', 'data', 't1.shex') pl = PrefixLibrary(filename) self.assertEqual("""PREFIX drugbank: <http://wifo5-04.informatik.uni-mannheim.de/drugbank/resource/drugbank/> PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>""", str(pl).strip()) self.assertEqual(URIRef("http://wifo5-04.informatik.uni-mannheim.de/drugbank/resource/drugbank/junk"), pl.DRUGBANK.junk)
def test_eric(self): p = PrefixLibrary(rdf) for result in ShExEvaluator( rdf=rdf, schema=schema, focus=p.INST.Eric, start=p.SCHOOL.Enrollee).evaluate(debug=False): print( f"{result.focus}: {'Passing' if result.result else 'Failing'}: \n{result.reason}" ) self.assertFalse(result.result)
def schema(self, shex: Optional[Union[str, ShExJ.Schema]]) -> None: """ Set the schema to be used. Schema can either be a ShExC or ShExJ string or a pre-parsed schema. :param shex: Schema """ self.pfx = None if shex is not None: if isinstance(shex, ShExJ.Schema): self._schema = shex else: shext = shex.strip() loader = SchemaLoader() if ('\n' in shex or '\r' in shex) or shext[0] in '#<_: ': self._schema = loader.loads(shex) else: self._schema = loader.load(shex) if isinstance(shex, str) else shex if self._schema is None: raise ValueError("Unable to parse shex file") self.pfx = PrefixLibrary(loader.schema_text)
def test_mappings_rdf(self): """ Test the imported mappings in the biolink metamodel """ # Generate context and use it to create the RDF self.single_file_generator('jsonld', ContextGenerator, filtr=ldcontext_metadata_filter) # Generate a copy of the JSON representation of the model context_loc = os.path.join(self.source_path, self.model_name + ".jsonld") context_args = {"context": [LOCAL_METAMODEL_LDCONTEXT_FILE, context_loc]} self.single_file_generator('json', JSONLDGenerator, serialize_args=context_args, filtr=json_metadata_filter) # Make a fresh copy of the RDF and validate it as well self.single_file_generator('ttl', RDFGenerator, serialize_args=context_args, comparator=GeneratorTestCase.rdf_comparator) g = Graph() rdf_file = os.path.join(sourcedir, 'meta_mappings.ttl') g.load(rdf_file, format='turtle') ns = PrefixLibrary() ns.add_rdf(g) ns['FULL'] = "http://example.org/fulluri/" ns['EX'] = "http://example.org/mappings/" ns['META'] = "https://w3id.org/biolink/biolinkml/meta/" # Make sure that the expected triples got added self.assertEqual({ns.EX.slot1_close, ns.FULL.slot1_close}, set(g.objects(ns.EX.s1, ns.SKOS.closeMatch))) self.assertEqual({ns.EX.slot1, ns.FULL.slot1}, set(g.objects(ns.EX.s1, ns.SKOS.exactMatch))) self.assertEqual(ns.EX.s3, g.value(ns.EX.s1, ns.META.deprecated_element_has_exact_replacement, any=False)) self.assertEqual(ns.EX.s4, g.value(ns.EX.s1, ns.META.deprecated_element_has_possible_replacement, any=False)) self.assertEqual({ns.EX.class1_close, ns.FULL.class1_close}, set(g.objects(ns.EX.C1, ns.SKOS.closeMatch))) self.assertEqual({ns.EX.class1, ns.FULL.class1}, set(g.objects(ns.EX.C1, ns.SKOS.exactMatch))) self.assertEqual(ns.EX.c2, g.value(ns.EX.C1, ns.META.deprecated_element_has_exact_replacement, any=False)) self.assertEqual(ns.EX.c3, g.value(ns.EX.C1, ns.META.deprecated_element_has_possible_replacement, any=False)) if DO_SHEX_VALIDATION: EX = Namespace("http://example.org/mappings/") focus = EX.testMetamodelMappings start = METAMODEL_NAMESPACE.SchemaDefinition results = ShExEvaluator(g, LOCAL_SHEXJ_FILE_NAME, focus, start).evaluate(debug=False) self.assertTrue(self._evaluate_shex_results(results)) else: print("*** RDF Model validation step was skipped. Set: tests.__init__.DO_SHEX_VALIDATION to run it")
def test_multiple_evaluate(self): """ Test calling evaluate multiple times in a row """ p = PrefixLibrary(shex) e = ShExEvaluator(rdf=rdf, schema=shex, focus=p.EX.s) # conformant for _ in range(NUM_ITERS): self.assertTrue(e.evaluate()[0].result) # non-conformant for _ in range(NUM_ITERS): self.assertFalse(e.evaluate(focus=p.EX.a)[0].result)
def test_add_rdf_file(self): """ Test adding RDF directly from a file """ filename = os.path.join(os.path.dirname(__file__), '..', 'data', 'earl_report.ttl') pl = PrefixLibrary() self.assertEqual("""PREFIX xml: <http://www.w3.org/XML/1998/namespace> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX dc: <http://purl.org/dc/terms/> PREFIX doap: <http://usefulinc.com/ns/doap#> PREFIX earl: <http://www.w3.org/ns/earl#> PREFIX ex: <http://example.org/> PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX ns1: <http://purl.org/dc/elements/1.1/>""", str(pl.add_rdf(filename)).strip()) g = Graph() g.load(filename, format="turtle") pl = PrefixLibrary() self.assertEqual("""PREFIX xml: <http://www.w3.org/XML/1998/namespace> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX dc: <http://purl.org/dc/terms/> PREFIX doap: <http://usefulinc.com/ns/doap#> PREFIX earl: <http://www.w3.org/ns/earl#> PREFIX ex: <http://example.org/> PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX ns1: <http://purl.org/dc/elements/1.1/>""", str(pl.add_rdf(g)).strip())
def test_nsname(self): """ Test the nsname method """ pl = PrefixLibrary("""@prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix wikibase: <http://wikiba.se/ontology-beta#> . @prefix wds: <http://www.wikidata.org/entity/statement/> . @prefix wdata: <https://www.wikidata.org/wiki/Special:EntityData/> . @prefix skos: <http://www.w3.org/2004/02/skos/core#> . @prefix schema: <http://schema.org/> . @prefix cc: <http://creativecommons.org/ns#> . @prefix geo: <http://www.opengis.net/ont/geosparql#> . @prefix prov: <http://www.w3.org/ns/prov#> . @prefix wdref: <http://www.wikidata.org/reference/> . @prefix wdv: <http://www.wikidata.org/value/> . @prefix wd: <http://www.wikidata.org/entity/> . @prefix wdt: <http://www.wikidata.org/prop/direct/> . @prefix wdtn: <http://www.wikidata.org/prop/direct-normalized/> . @prefix p: <http://www.wikidata.org/prop/> . @prefix ps: <http://www.wikidata.org/prop/statement/> . @prefix psv: <http://www.wikidata.org/prop/statement/value/> . @prefix psn: <http://www.wikidata.org/prop/statement/value-normalized/> . @prefix pq: <http://www.wikidata.org/prop/qualifier/> . @prefix pqv: <http://www.wikidata.org/prop/qualifier/value/> . @prefix pqn: <http://www.wikidata.org/prop/qualifier/value-normalized/> . @prefix pr: <http://www.wikidata.org/prop/reference/> . @prefix prv: <http://www.wikidata.org/prop/reference/value/> . @prefix prn: <http://www.wikidata.org/prop/reference/value-normalized/> . @prefix wdno: <http://www.wikidata.org/prop/novalue/> . and some junk""") self.assertEqual("wdt:penguins", pl.nsname("http://www.wikidata.org/prop/direct/penguins")) self.assertEqual("p:polarbear", pl.nsname("http://www.wikidata.org/prop/polarbear")) self.assertEqual("psn:elf", pl.nsname("http://www.wikidata.org/prop/statement/value-normalized/elf")) self.assertEqual("http://www.wikidata1.org/prop/qualifier/", pl.nsname("http://www.wikidata1.org/prop/qualifier/"))
def test_add_rdf_str(self): """ Test adding RDF directly from a string """ pl = PrefixLibrary() rdf = """ @prefix dc: <http://purl.org/dc/elements/1.1/> . @prefix dcterms: <http://purl.org/dc/terms/> . @prefix doap: <http://usefulinc.com/ns/doap#> . @prefix foaf: <http://xmlns.com/foaf/0.1/> . @prefix ex: <http://example.org/test/> . ex:Sam a foaf:Person.""" pl.add_rdf(rdf) self.assertEqual("""PREFIX xml: <http://www.w3.org/XML/1998/namespace> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX dcterms: <http://purl.org/dc/terms/> PREFIX doap: <http://usefulinc.com/ns/doap#> PREFIX foaf: <http://xmlns.com/foaf/0.1/> PREFIX ex: <http://example.org/test/>""", str(pl).strip())
def test_add_to_object(self): """ Test the PrefixLibrary add_to_object function """ class TargetObj: pass pl = PrefixLibrary(""" PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX prov: <http://www.w3.org/ns/prov#> PREFIX p: <http://www.wikidata.org/prop/> PREFIX pr: <http://www.wikidata.org/prop/reference/> PREFIX prv: <http://www.wikidata.org/prop/reference/value/> PREFIX pv: <http://www.wikidata.org/prop/value/> PREFIX ps: <http://www.wikidata.org/prop/statement/> PREFIX gw: <http://genewiki.shape/>""") self.assertEqual(8, pl.add_to_object(TargetObj)) self.assertEqual(URIRef('http://www.w3.org/ns/prov#spiders'), TargetObj.PROV.spiders) class TargetObj2: GW: int = 42 output = StringIO() with redirect_stdout(output): self.assertEqual(7, pl.add_to_object(TargetObj2)) self.assertTrue(output.getvalue().strip().startswith("Warning: GW is already defined in namespace "))
def test_fail(self): pl = PrefixLibrary(shex) results = ShExEvaluator().evaluate(rdf, shex, focus=pl.EX.s, debug=False) self.assertTrue(results[0].result) results = ShExEvaluator().evaluate(rdf, shex, focus=pl.EX.t) self.assertFalse(results[0].result) self.assertEqual('Focus: http://example.org/ex/t not in graph', results[0].reason) results2 = ShExEvaluator().evaluate(rdf, shex, focus=[pl.EX.s, pl.EX.t2]) self.assertTrue(results2[0].result) self.assertFalse(results2[1].result) self.assertEqual('Focus: http://example.org/ex/t2 not in graph', results2[1].reason)
def test_add_shex_url(self): """ Test adding ShEx from a URL """ pl = PrefixLibrary( "https://raw.githubusercontent.com/SuLab/Genewiki-ShEx/master/diseases/wikidata-disease-ontology.shex") self.assertEqual("""PREFIX wd: <http://www.wikidata.org/entity/> PREFIX wdt: <http://www.wikidata.org/prop/direct/> PREFIX p: <http://www.wikidata.org/prop/> PREFIX prov: <http://www.w3.org/ns/prov#> PREFIX pq: <http://www.wikidata.org/prop/qualifier/> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX prv: <http://www.wikidata.org/prop/reference/value/> PREFIX pr: <http://www.wikidata.org/prop/reference/> PREFIX ps: <http://www.wikidata.org/prop/statement/> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX schema: <http://schema.org/> PREFIX do: <http://purl.obolibrary.org/obo/DOID_> PREFIX doio: <http://identifiers.org/doid/> PREFIX mir: <http://www.ebi.ac.uk/miriam/main/collections/>""", str(pl).strip())
def test_fail(self): """ Test max cardinality of 0 AND error reporting """ datadir = os.path.abspath( os.path.join(os.path.dirname(__file__), 'data')) shexpath = os.path.join(datadir, 'issue_20.shex') rdfpath = os.path.join(datadir, 'issue_20.ttl') expectedpath = os.path.join(datadir, 'issue_20.errors') pl = PrefixLibrary(rdfpath) output = StringIO() with redirect_stdout(output): evaluate_cli(f"{rdfpath} {shexpath} -fn {pl.EX.BPM1}") evaluate_cli(f"{rdfpath} {shexpath} -fn {pl.EX.BPM2}") if not os.path.exists(expectedpath): with open(expectedpath, 'w') as f: f.write(output.getvalue()) self.assertTrue(False, "Output created, rerun") with open(expectedpath) as f: expected = f.read() self.maxDiff = None self.assertEqual(expected, output.getvalue())
class ShExEvaluator: """ Shape Expressions Evaluator """ def __init__( self, rdf: Optional[Union[str, Graph]] = None, schema: Optional[Union[str, ShExJ.Schema]] = None, focus: Optional[URIPARM] = None, start: STARTPARM = None, rdf_format: str = "turtle", debug: bool = False, debug_slurps: bool = False, over_slurp: bool = None, output_sink: Optional[Callable[[EvaluationResult], bool]] = None) -> None: """ Evaluator constructor. All of the parameters below can be set in the constructor or at runtime :param rdf: RDF string, file name, URL or Graph for evaluation. :param schema: ShEx Schema to evaluate. Can be ShExC, ShExJ or a pre-parsed schema :param focus: focus node(s). If absent, all non-BNode subjects in the graph are evaluated :param start: start node(s). If absent, the START node in the schema is used :param rdf_format: format for RDF. Default: "Turtle" :param debug: emit semi-helpful debug information :param debug: debug graph fetch calls :param over_slurp: Controls whether SPARQL slurper does exact or over slurps :param output_sink: Function for accepting evaluation results and returns whether to keep evaluating """ self.pfx: PrefixLibrary = None self.rdf_format = rdf_format self.g = None self.rdf = rdf self._schema = None self.schema = schema self._focus = None self.focus = focus self.start = start self.debug = debug self.debug_slurps = debug_slurps self.over_slurp = over_slurp self.output_sink = output_sink self.nerrors = 0 self.nnodes = 0 self.eval_result = [] @property def rdf(self) -> str: """ :return: The rendering of whatever RDF is currently being evaluated """ return self.g.serialize(format=self.rdf_format).decode() @rdf.setter def rdf(self, rdf: Optional[Union[str, Graph]]) -> None: """ Set the RDF DataSet to be evaulated. If ``rdf`` is a string, the presence of a return is the indicator that it is text instead of a location. :param rdf: File name, URL, representation of rdflib Graph """ if isinstance(rdf, Graph): self.g = rdf else: self.g = Graph() if isinstance(rdf, str): if '\n' in rdf or '\r' in rdf: self.g.parse(data=rdf, format=self.rdf_format) elif ':' in rdf: self.g.parse(location=rdf, format=self.rdf_format) else: self.g.parse(source=rdf, format=self.rdf_format) @property def schema(self) -> Optional[str]: """ :return: The ShExC representation of the schema if one is supplied """ return str(ShExC(self._schema)) if self._schema else None @schema.setter def schema(self, shex: Optional[Union[str, ShExJ.Schema]]) -> None: """ Set the schema to be used. Schema can either be a ShExC or ShExJ string or a pre-parsed schema. :param shex: Schema """ self.pfx = None if shex is not None: if isinstance(shex, ShExJ.Schema): self._schema = shex else: shext = shex.strip() loader = SchemaLoader() if ('\n' in shex or '\r' in shex) or shext[0] in '#<_: ': self._schema = loader.loads(shex) else: self._schema = loader.load(shex) if isinstance( shex, str) else shex if self._schema is None: raise ValueError("Unable to parse shex file") self.pfx = PrefixLibrary(loader.schema_text) @property def focus(self) -> Optional[List[URIRef]]: """ :return: The list of focus nodes (if any) """ return self._focus @property def foci(self) -> List[URIRef]: """ :return: The current set of focus nodes """ return self._focus if self._focus else sorted( [s for s in set(self.g.subjects()) if isinstance(s, URIRef)]) @focus.setter def focus(self, focus: Optional[URIPARM]) -> None: """ Set the focus node(s). If no focus node is specified, the evaluation will occur for all non-BNode graph subjects. Otherwise it can be a string, a URIRef or a list of string/URIRef combinations :param focus: None if focus should be all URIRefs in the graph otherwise a URI or list of URI's """ self._focus = normalize_uriparm(focus) if focus else None @property def start(self) -> STARTPARM: """ :return: The schema start node(s) """ return self._start @start.setter def start(self, start: STARTPARM) -> None: self._start = normalize_startparm(start) if start else [START] def evaluate( self, rdf: Optional[Union[str, Graph]] = None, shex: Optional[Union[str, ShExJ.Schema]] = None, focus: Optional[URIPARM] = None, start: STARTPARM = None, rdf_format: Optional[str] = None, debug: Optional[bool] = None, debug_slurps: Optional[bool] = None, over_slurp: Optional[bool] = None, output_sink: Optional[Callable[[EvaluationResult], bool]] = None ) -> List[EvaluationResult]: if rdf is not None or shex is not None or focus is not None or start is not None: evaluator = ShExEvaluator( rdf=rdf if rdf is not None else self.g, schema=shex if shex is not None else self._schema, focus=focus if focus is not None else self.focus, start=start if start is not None else self.start if self.start else START, rdf_format=rdf_format if rdf_format is not None else self.rdf_format, output_sink=output_sink if output_sink is not None else self.output_sink) else: evaluator = self self.eval_result = [] if evaluator.output_sink is None: def sink(e: EvaluationResult) -> bool: self.eval_result.append(e) return True evaluator.output_sink = sink processing = True self.nerrors = 0 self.nnodes = 0 if START in evaluator.start and evaluator._schema.start is None: self.nerrors += 1 evaluator.output_sink( EvaluationResult(False, None, None, 'START node is not specified')) return self.eval_result # Experimental -- xfer all ShEx namespaces to g if self.pfx and evaluator.g is not None: self.pfx.add_bindings(evaluator.g) cntxt = Context(evaluator.g, evaluator._schema) cntxt.debug_context.debug = debug if debug is not None else self.debug cntxt.debug_context.trace_slurps = debug_slurps if debug_slurps is not None else self.debug_slurps cntxt.over_slurp = self.over_slurp if over_slurp is not None else self.over_slurp for focus in evaluator.foci: self.nnodes += 1 start_list: List[Union[URIRef, START]] = [] for start in evaluator.start: if start is START: start_list.append(evaluator._schema.start) elif isinstance(start, START_TYPE): start_list += list( evaluator.g.objects(focus, start.start_predicate)) else: start_list.append(start) if start_list: for start_node in start_list: map_ = FixedShapeMap() map_.add(ShapeAssociation(focus, start_node)) cntxt.reset() success, fail_reasons = isValid(cntxt, map_) if not success: self.nerrors += 1 if not evaluator.output_sink( EvaluationResult( success, focus, start_node, '\n'.join(fail_reasons) if not success else '')): processing = False break else: self.nerrors += 1 evaluator.output_sink( EvaluationResult(False, focus, None, "No start node located")) if not processing: break return self.eval_result
start = @gw:cancer gw:cancer { p:P1748 { prov:wasDerivedFrom @<reference> }+ } <reference> { pr:P248 IRI ; pr:P813 xsd:dateTime ; pr:P699 LITERAL } """ loc_prefixes = PrefixLibrary(None, wikidata="http://www.wikidata.org/entity/", gw="http://genewiki.shape/") class ShExEvaluatorTestCase(unittest.TestCase): def test_empty_constructor(self): evaluator = ShExEvaluator() # rdflib no longer emits unused prefixes -- an empty evaluator is now empty self.assertEqual("", evaluator.rdf.strip()) self.assertIsNone(evaluator.schema) self.assertIsNone(evaluator.focus) self.assertEqual([], evaluator.foci) self.assertEqual([START], evaluator.start) self.assertEqual("turtle", evaluator.rdf_format) self.assertTrue(isinstance(evaluator.g, Graph))
def test_edge_cases(self): """ Test some of the edge cases """ # Test a default URL shex = "PREFIX : <http://example.org/sample/>" pl = PrefixLibrary(shex) print(str(pl).strip())
def test_add_rdf_url(self): """ Test adding RDF from a URL """ pl = PrefixLibrary() pl.add_rdf("https://raw.githubusercontent.com/prefixcommons/biocontext/master/registry/go_context.jsonld", format="json-ld") self.assertEqual("""PREFIX xml: <http://www.w3.org/XML/1998/namespace> PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX wb: <http://identifiers.org/wormbase/> PREFIX kegg_ligand: <http://www.genome.jp/dbget-bin/www_bget?cpd:> PREFIX pso_git: <https://github.com/Planteome/plant-stress-ontology/issues/> PREFIX ncbigene: <http://identifiers.org/ncbigene/> PREFIX kegg_reaction: <http://www.genome.jp/dbget-bin/www_bget?rn:> PREFIX go_ref: <http://purl.obolibrary.org/obo/go/references/> PREFIX vega: <http://vega.sanger.ac.uk/id/> PREFIX zfin: <http://identifiers.org/zfin/> PREFIX pfam: <http://pfam.xfam.org/family/> PREFIX sgn: <http://identifiers.org/sgn/> PREFIX reactome: <http://identifiers.org/reactome/> PREFIX interpro: <http://identifiers.org/interpro/> PREFIX unirule: <http://www.uniprot.org/unirule/> PREFIX dictybase: <http://identifiers.org/dictybase.gene/> PREFIX po_git: <https://github.com/Planteome/plant-ontology/issues/> PREFIX aspgd_locus: <http://identifiers.org/aspgd.locus/> PREFIX sgd: <http://identifiers.org/sgd/> PREFIX hgnc: <http://identifiers.org/hgnc/> PREFIX dictybase_gene_name: <http://dictybase.org/gene/> PREFIX tair: <http://identifiers.org/tair.locus/> PREFIX ensemblfungi: <http://www.ensemblgenomes.org/id/> PREFIX wikipedia: <http://en.wikipedia.org/wiki/> PREFIX psi-mod: <http://www.ebi.ac.uk/ontology-lookup/?termId=MOD:> PREFIX rgd: <http://identifiers.org/rgd/> PREFIX pmid: <http://www.ncbi.nlm.nih.gov/pubmed/> PREFIX xenbase: <http://identifiers.org/xenbase/> PREFIX maizegdb: <http://maizegdb.org/gene_center/gene/> PREFIX hamap: <http://hamap.expasy.org/unirule/> PREFIX to_git: <https://github.com/Planteome/plant-trait-ontology/issues/> PREFIX mesh: <http://n2t.net/MESH:> PREFIX gr_protein: <http://identifiers.org/gramene.protein/> PREFIX pombase: <http://identifiers.org/pombase/> PREFIX ena: <http://www.ebi.ac.uk/ena/data/view/> PREFIX ec: <http://www.expasy.org/enzyme/> PREFIX uniprotkb: <http://identifiers.org/uniprot/> PREFIX mgi: <http://identifiers.org/mgi/> PREFIX gomodel: <http://model.geneontology.org/> PREFIX kegg_pathway: <http://identifiers.org/kegg.pathway/> PREFIX doi: <http://dx.doi.org/> PREFIX panther: <http://identifiers.org/panther.family/> PREFIX fb: <http://identifiers.org/flybase/> PREFIX ensembl: <http://identifiers.org/ensembl/> PREFIX cgd: <http://identifiers.org/cgd/> PREFIX gr_gene: <http://identifiers.org/gramene.gene/> PREFIX kegg_enzyme: <http://identifiers.org/kegg.enzyme/> PREFIX cacao: <http://gowiki.tamu.edu/wiki/index.php/> PREFIX po_ref: <http://planteome.org/po_ref/> PREFIX uniprotkb-subcell: <http://www.uniprot.org/locations/> PREFIX nif_subcellular: <http://www.neurolex.org/wiki/> PREFIX genedb: <http://identifiers.org/genedb/> PREFIX apidb_plasmodb: <http://www.plasmodb.org/gene/> PREFIX rnacentral: <http://rnacentral.org/rna/> PREFIX rfam: <http://rfam.sanger.ac.uk/family/> PREFIX obo_sf2_po: <http://sourceforge.net/p/obo/plant-ontology-po-term-requests/> PREFIX uniparc: <http://www.uniprot.org/uniparc/> PREFIX gdb: <http://www.gdb.org/gdb-bin/genera/accno?accessionNum=GDB:> PREFIX dbsnp: <http://identifiers.org/dbsnp/> PREFIX maizegdb_locus: <http://identifiers.org/maizegdb.locus/> PREFIX mo: <http://mged.sourceforge.net/ontologies/MGEDontology.php#> PREFIX plana_ref: <http://purl.obolibrary.org/obo/plana/references/> PREFIX cas: <http://identifiers.org/cas/> PREFIX complexportal: <https://www.ebi.ac.uk/complexportal/complex/> PREFIX jstor: <http://www.jstor.org/stable/> PREFIX gr_qtl: <http://identifiers.org/gramene.qtl/> PREFIX vbrc: <http://vbrc.org/query.asp?web_id=VBRC:> PREFIX eo_git: <https://github.com/Planteome/plant-environment-ontology/issues/> PREFIX tgd: <http://identifiers.org/tgd/> PREFIX obo_sf2_peco: <https://sourceforge.net/p/obo/plant-environment-ontology-eo/> PREFIX metacyc: <http://identifiers.org/metacyc/> PREFIX omim: <http://omim.org/entry/> PREFIX intact: <http://identifiers.org/intact/> PREFIX ensembl_geneid: <http://www.ensembl.org/id/> PREFIX uniprotkb-kw: <http://www.uniprot.org/keywords/> PREFIX eupathdb: <http://eupathdb.org/gene/>""", str(pl).strip())
def test_wikidata_2(self): pfx = PrefixLibrary(shex_schema, wikidata="http://www.wikidata.org/entity/") evaluator = ShExEvaluator(self.test_path, shex_schema, pfx.WIKIDATA.Q18557112) print(evaluator.evaluate(start=pfx.GW.cancer, debug=False))
def test_basics(self): """ Test basic functions """ pl = PrefixLibrary() print(str(pl)) g = Graph() pl.add_bindings(g) self.assertEqual("""@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix xml: <http://www.w3.org/XML/1998/namespace> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .""", g.serialize(format="turtle").decode().strip()) pl = PrefixLibrary("""@prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix wikibase: <http://wikiba.se/ontology-beta#> . @prefix wds: <http://www.wikidata.org/entity/statement/> . @prefix wdata: <https://www.wikidata.org/wiki/Special:EntityData/> . @prefix skos: <http://www.w3.org/2004/02/skos/core#> . @prefix schema: <http://schema.org/> . @prefix cc: <http://creativecommons.org/ns#> . @prefix geo: <http://www.opengis.net/ont/geosparql#> . @prefix prov: <http://www.w3.org/ns/prov#> . @prefix wdref: <http://www.wikidata.org/reference/> . @prefix wdv: <http://www.wikidata.org/value/> . @prefix wd: <http://www.wikidata.org/entity/> . @prefix wdt: <http://www.wikidata.org/prop/direct/> . @prefix wdtn: <http://www.wikidata.org/prop/direct-normalized/> . @prefix p: <http://www.wikidata.org/prop/> . @prefix ps: <http://www.wikidata.org/prop/statement/> . @prefix psv: <http://www.wikidata.org/prop/statement/value/> . @prefix psn: <http://www.wikidata.org/prop/statement/value-normalized/> . @prefix pq: <http://www.wikidata.org/prop/qualifier/> . @prefix pqv: <http://www.wikidata.org/prop/qualifier/value/> . @prefix pqn: <http://www.wikidata.org/prop/qualifier/value-normalized/> . @prefix pr: <http://www.wikidata.org/prop/reference/> . @prefix prv: <http://www.wikidata.org/prop/reference/value/> . @prefix prn: <http://www.wikidata.org/prop/reference/value-normalized/> . @prefix wdno: <http://www.wikidata.org/prop/novalue/> . and some junk""") self.assertEqual( [('OWL', Namespace('http://www.w3.org/2002/07/owl#')), ('WIKIBASE', Namespace('http://wikiba.se/ontology-beta#')), ('WDS', Namespace('http://www.wikidata.org/entity/statement/')), ('WDATA', Namespace('https://www.wikidata.org/wiki/Special:EntityData/')), ('SKOS', Namespace('http://www.w3.org/2004/02/skos/core#')), ('SCHEMA', Namespace('http://schema.org/')), ('CC', Namespace('http://creativecommons.org/ns#')), ('GEO', Namespace('http://www.opengis.net/ont/geosparql#')), ('PROV', Namespace('http://www.w3.org/ns/prov#')), ('WDREF', Namespace('http://www.wikidata.org/reference/')), ('WDV', Namespace('http://www.wikidata.org/value/')), ('WD', Namespace('http://www.wikidata.org/entity/')), ('WDT', Namespace('http://www.wikidata.org/prop/direct/')), ('WDTN', Namespace('http://www.wikidata.org/prop/direct-normalized/')), ('P', Namespace('http://www.wikidata.org/prop/')), ('PS', Namespace('http://www.wikidata.org/prop/statement/')), ('PSV', Namespace('http://www.wikidata.org/prop/statement/value/')), ('PSN', Namespace('http://www.wikidata.org/prop/statement/value-normalized/')), ('PQ', Namespace('http://www.wikidata.org/prop/qualifier/')), ('PQV', Namespace('http://www.wikidata.org/prop/qualifier/value/')), ('PQN', Namespace('http://www.wikidata.org/prop/qualifier/value-normalized/')), ('PR', Namespace('http://www.wikidata.org/prop/reference/')), ('PRV', Namespace('http://www.wikidata.org/prop/reference/value/')), ('PRN', Namespace('http://www.wikidata.org/prop/reference/value-normalized/')), ('WDNO', Namespace('http://www.wikidata.org/prop/novalue/'))], [e for e in pl] ) pl = PrefixLibrary(""" PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX prov: <http://www.w3.org/ns/prov#> PREFIX p: <http://www.wikidata.org/prop/> PREFIX pr: <http://www.wikidata.org/prop/reference/> PREFIX prv: <http://www.wikidata.org/prop/reference/value/> PREFIX pv: <http://www.wikidata.org/prop/value/> PREFIX ps: <http://www.wikidata.org/prop/statement/> PREFIX gw: <http://genewiki.shape/> start = @gw:cancer gw:cancer { p:P1748 { prov:wasDerivedFrom @<reference> }+ } <reference> { pr:P248 IRI ; pr:P813 xsd:dateTime ; pr:P699 LITERAL }""", foaf=known_prefixes.FOAF, owl=known_prefixes.OWL, rdfs=standard_prefixes.RDFS) self.assertEqual( [('XSD', Namespace('http://www.w3.org/2001/XMLSchema#')), ('PROV', Namespace('http://www.w3.org/ns/prov#')), ('P', Namespace('http://www.wikidata.org/prop/')), ('PR', Namespace('http://www.wikidata.org/prop/reference/')), ('PRV', Namespace('http://www.wikidata.org/prop/reference/value/')), ('PV', Namespace('http://www.wikidata.org/prop/value/')), ('PS', Namespace('http://www.wikidata.org/prop/statement/')), ('GW', Namespace('http://genewiki.shape/')), ('FOAF', Namespace('http://xmlns.com/foaf/0.1/')), ('OWL', Namespace('http://www.w3.org/2002/07/owl#')), ('RDFS', Namespace('http://www.w3.org/2000/01/rdf-schema#'))], [e for e in pl]) pl = PrefixLibrary(None, ex="http://example.org/") self.assertEqual("http://example.org/", str(pl.EX)) known_prefixes.add_bindings(g) self.assertEqual("""@prefix dc: <http://purl.org/dc/elements/1.1/> . @prefix dcterms: <http://purl.org/dc/terms/> . @prefix doap: <http://usefulinc.com/ns/doap#> . @prefix foaf: <http://xmlns.com/foaf/0.1/> . @prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . @prefix skos: <http://www.w3.org/2004/02/skos/core#> . @prefix xml: <http://www.w3.org/XML/1998/namespace> . @prefix xmlns: <http://www.w3.org/XML/1998/namespace> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> .""", g.serialize(format="turtle").decode().strip())
import sys from rdflib import URIRef, XSD from pyshex import PrefixLibrary """ This module is used to test the PrefixLibrary's ability to inject namespaces directoy into the containing module It is used in conjunction with test_prefixlib.test_add_to_module """ pl = PrefixLibrary(""" PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX prov: <http://www.w3.org/ns/prov#> PREFIX p: <http://www.wikidata.org/prop/> PREFIX pr: <http://www.wikidata.org/prop/reference/> PREFIX prv: <http://www.wikidata.org/prop/reference/value/> PREFIX pv: <http://www.wikidata.org/prop/value/> PREFIX ps: <http://www.wikidata.org/prop/statement/> PREFIX gw: <http://genewiki.shape/>""") pl.add_to_object(sys.modules[__name__]) def sample(name: str) -> URIRef: return PROV[name] pl.add_rdf('@prefix XSD: <http://nonxml.com/item#> .') pl.add_to_object(sys.modules[__name__], override=True) def rdf(name: str) -> URIRef: return XSD[name]