diff --git a/linkml_runtime/dumpers/rdflib_dumper.py b/linkml_runtime/dumpers/rdflib_dumper.py index 976a928f..5638eb1a 100644 --- a/linkml_runtime/dumpers/rdflib_dumper.py +++ b/linkml_runtime/dumpers/rdflib_dumper.py @@ -4,6 +4,7 @@ from typing import Optional, Any, Dict, Union from pydantic import BaseModel +import rdflib from curies import Converter from rdflib import Graph, URIRef, XSD from rdflib.term import Node, BNode, Literal diff --git a/linkml_runtime/utils/schemaview.py b/linkml_runtime/utils/schemaview.py index 78f94240..4598a7c9 100644 --- a/linkml_runtime/utils/schemaview.py +++ b/linkml_runtime/utils/schemaview.py @@ -113,9 +113,6 @@ class SchemaView(object): This class utilizes caching for efficient lookup operations. - TODO: decide how to use this in conjunction with the existing schemaloader, which injects - into the schema rather than providing dynamic methods. - See: - https://github.com/linkml/linkml/issues/59 - https://github.com/linkml/linkml/discussions/144 @@ -934,7 +931,10 @@ def get_uri(self, element: Union[ElementName, Element], imports=True, expand=Fal logging.warning(f'from_schema not populated for element {e.name}') schema = self.schema_map[self.in_schema(e.name)] pfx = schema.default_prefix - uri = f'{pfx}:{e_name}' + if pfx in self.namespaces(): + uri = f'{pfx}:{e_name}' + else: + uri = f'{pfx}{e_name}' if expand: return self.expand_curie(uri) else: @@ -942,7 +942,8 @@ def get_uri(self, element: Union[ElementName, Element], imports=True, expand=Fal def expand_curie(self, uri: str) -> str: """ - Expands a URI or CURIE to a full URI + Expands a URI or CURIE to a full URI. + :param uri: :return: URI as a string """ diff --git a/tests/test_issues/input/linkml_issue_1041.yaml b/tests/test_issues/input/linkml_issue_1041.yaml new file mode 100644 index 00000000..316fcfdb --- /dev/null +++ b/tests/test_issues/input/linkml_issue_1041.yaml @@ -0,0 +1,16 @@ +id: https://w3id.org/linkml/examples/personinfo +name: personinfo +prefixes: + linkml: https://w3id.org/linkml/ +imports: + - linkml:types +default_range: string + +classes: + Person: + attributes: + id: ## note: default range is string, NOT an identifier + full_name: + aliases: + phone: + age: \ No newline at end of file diff --git a/tests/test_issues/test_linkml_issue_1041.py b/tests/test_issues/test_linkml_issue_1041.py new file mode 100644 index 00000000..96e16c11 --- /dev/null +++ b/tests/test_issues/test_linkml_issue_1041.py @@ -0,0 +1,30 @@ +from unittest import TestCase + +from rdflib import RDF, URIRef + +from linkml_runtime.dumpers import rdflib_dumper +from linkml_runtime.utils.schemaview import SchemaView + +from tests.test_issues.environment import env +from tests.test_issues.models.linkml_issue_1041 import Person + + +class Issue1041TestCase(TestCase): + """ + https://github.com/linkml/linkml/issues/1041 + """ + env = env + + def test_issue_default_prefix(self): + view = SchemaView(env.input_path('linkml_issue_1041.yaml')) + uri = view.get_uri(view.get_class("Person"), expand=True) + person_uri = "https://w3id.org/linkml/examples/personinfo/Person" + self.assertEqual(uri, person_uri) + p = Person(id="ORCID:1234", full_name="Clark Kent", age="32", phone="555-555-5555") + g = rdflib_dumper.as_rdf_graph(p, view) + bnodes = g.subjects(RDF.type, URIRef(person_uri)) + self.assertEqual(1, len(list(bnodes))) + + +if __name__ == "__main__": + unittest.main()