def test_uri_and_curie(self): """ Compile a model of URI's and Curies and then test the various types """ self.single_file_generator('py', PythonGenerator, filtr=metadata_filter, comparator=compare_python) # Check that the interpretations are correct self.single_file_generator( 'jsonld', ContextGenerator, filtr=ldcontext_metadata_filter, comparator=lambda expected, actual: compare_rdf( expected, actual, fmt="json-ld")) self.single_file_generator('json', JSONLDGenerator, filtr=json_metadata_filter) module = compile_python(env.expected_path(self.model_name + '.py')) curie_obj = module.C1("ex:obj1", hasCurie="ex:curie", hasURI="http://example.org/test/uri", hasNcName="A123", id2="ex:id2") instance_jsonld = loads('{ "ex": "http://example.org/test/inst#" }') g = as_rdf( curie_obj, [env.input_path(self.model_name + '.jsonld'), instance_jsonld]) env.eval_single_file(env.expected_path('uriandcurie.ttl'), g.serialize(format='ttl').decode(), lambda s: s, compare_rdf)
def output_generator(dirname) -> None: with open(os.path.join(dirname, 'issue_80.json'), 'w') as f: f.write(as_json(example)) context = os.path.join(dirname, 'issue_80.context.jsonld') with open(context, 'w') as f: f.write( ContextGenerator( env.input_path('issue_80.yaml')).serialize()) with open(os.path.join(dirname, 'issue_80.ttl'), 'w') as f: f.write( as_rdf( example, contexts=context).serialize(format="turtle").decode())
def test_uriÎ_and_curie(self): """ Compile a model of URI's and Curies and then test the various types """ self.single_file_generator('py', PythonGenerator, filtr=metadata_filter) # Make sure the python is valid with open(os.path.join(self.source_path, self.model_name + '.py')) as f: model = f.read() spec = compile(model, 'test', 'exec') module = ModuleType('test') exec(spec, module.__dict__) # Check that the interpretations are correct msg = self.single_file_generator('jsonld', ContextGenerator, filtr=ldcontext_metadata_filter, fail_if_expected_missing=False) msg = self.single_file_generator('json', JSONLDGenerator, filtr=json_metadata_filter, fail_if_expected_missing=False) if msg: self.fail(msg) curie_obj = module.C1("ex:obj1", hasCurie="ex:curie", hasURI="http://example.org/test/uri", hasNcName="A123", id2="ex:id2") instance_jsonld = loads('{ "ex": "http://example.org/test/inst#" }') g = as_rdf(curie_obj, [ os.path.join(self.source_path, self.model_name + '.jsonld'), instance_jsonld ]) self.rdf_comparator( expected_rdf, g, os.path.join(self.target_path, self.model_name + '.jsonld'))
def test_issue_80(self): """ Make sure that types are generated as part of the output """ yaml_fname = os.path.join(sourcedir, 'issue_80.yaml') python = PythonGenerator(yaml_fname).serialize() print(self.header("Python")) print(python) spec = compile(python, 'test', 'exec') module = ModuleType('test') exec(spec, module.__dict__) example = module.Person("http://example.org/person/17", "Fred Jones", 43) # JSON Representation print(self.header("JSON")) print(as_json(example)) # Generate a context for this particular model print(self.header("Context")) context = ContextGenerator(yaml_fname).serialize() print(context) # RDF Representation print(self.header("RDF")) print(as_rdf(example, contexts=context).serialize(format="turtle").decode())
def test_nodeidentifier(self): context = """{ "@context": { "type": "@type", "OIO": "http://www.geneontology.org/formats/oboInOwl#", "dcterms": "http://purl.org/dc/terms/", "metatype": "https://w3id.org/biolink/biolinkml/type/", "owl": "http://www.w3.org/2002/07/owl#", "pav": "http://purl.org/pav/", "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdfs": "http://www.w3.org/2000/01/rdf-schema#", "skos": "http://www.w3.org/2004/02/skos/core#", "xsd": "http://www.w3.org/2001/XMLSchema#", "s": { "@id": "rdfs:subject" }, "t": { "@id": "rdfs:object" } } }""" EX = Namespace("http://example.org/tests/") class Root(NodeIdentifier): pass class Child1(Root): pass class Child2(Root): pass class Desc1(Child1): pass class Unassoc(NodeIdentifier): pass @dataclass class Pair(YAMLRoot): s: Child1 = None t: Child2 = None def __post_init__(self): if not isinstance(self.s, Child1): self.s = Child1(self.s) if not isinstance(self.t, Child2): self.t = Child2(self.t) super().__post_init__() s = Desc1(EX.descendant1) t = Child2(EX.child2) y = Pair(s, t) self.assertEqual( """{ "s": "http://example.org/tests/descendant1", "t": "http://example.org/tests/child2" }""", as_json(y)) self.assertEqual( """@prefix OIO: <http://www.geneontology.org/formats/oboInOwl#> . @prefix dcterms: <http://purl.org/dc/terms/> . @prefix metatype: <https://w3id.org/biolink/biolinkml/type/> . @prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix pav: <http://purl.org/pav/> . @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 xsd: <http://www.w3.org/2001/XMLSchema#> . [] a <biolink/biolinkml/tests/Pair> ; rdfs:object "http://example.org/tests/child2" ; rdfs:subject "http://example.org/tests/descendant1" . """, as_rdf(y, context).serialize(format="turtle").decode()) with self.assertRaises(ValueError): y = Pair(s, s)
def add_to_graph(e: YAMLRoot, g: Optional[Graph] = None) -> Graph: return as_rdf(e, context_jsonld, g)