diff --git a/docs/generators/jsonld-context.rst b/docs/generators/jsonld-context.rst index 18cec34644..4d0b94b0c1 100644 --- a/docs/generators/jsonld-context.rst +++ b/docs/generators/jsonld-context.rst @@ -138,6 +138,8 @@ Docs Command Line ^^^^^^^^^^^^ +``--normalize-prefixes`` is described under :ref:`prefix-normalization`. + .. currentmodule:: linkml.generators.jsonldcontextgen .. click:: linkml.generators.jsonldcontextgen:cli diff --git a/docs/generators/owl.rst b/docs/generators/owl.rst index f9ee4b57e5..b5cd53a4a0 100644 --- a/docs/generators/owl.rst +++ b/docs/generators/owl.rst @@ -116,6 +116,13 @@ for each property (§5.8.1). Slots without the role keep their object or data pr projection, also when a metaclass declares them; LinkML then puns the annotated element as an individual, as it does for all metadata. +Prefix normalization +^^^^^^^^^^^^^^^^^^^^ + +``--normalize-prefixes`` writes the conventional names of well-known namespaces, the same in +every generator; see :ref:`prefix-normalization`. + + Enums and PermissibleValues ^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/docs/generators/shacl.rst b/docs/generators/shacl.rst index 3e88f0090f..42767f04ce 100644 --- a/docs/generators/shacl.rst +++ b/docs/generators/shacl.rst @@ -87,6 +87,8 @@ Example Output: Command Line ^^^^^^^^^^^^ +``--normalize-prefixes`` is described under :ref:`prefix-normalization`. + .. currentmodule:: linkml.generators.shaclgen .. click:: linkml.generators.shaclgen:cli diff --git a/docs/generators/shex.rst b/docs/generators/shex.rst index 6bebcf67b0..8db18d939c 100644 --- a/docs/generators/shex.rst +++ b/docs/generators/shex.rst @@ -64,6 +64,8 @@ Docs Command Line ^^^^^^^^^^^^ +``--normalize-prefixes`` is described under :ref:`prefix-normalization`. + .. currentmodule:: linkml.generators.shexgen .. click:: linkml.generators.shexgen:cli diff --git a/docs/generators/sparql.rst b/docs/generators/sparql.rst index bc2fcd2ef3..f1c4ac5d3b 100644 --- a/docs/generators/sparql.rst +++ b/docs/generators/sparql.rst @@ -60,6 +60,8 @@ Docs Command Line ^^^^^^^^^^^^ +``--normalize-prefixes`` is described under :ref:`prefix-normalization`. + .. currentmodule:: linkml.generators.sparqlgen .. click:: linkml.generators.sparqlgen:cli diff --git a/docs/generators/sssom.rst b/docs/generators/sssom.rst index 73f0df407c..5d0f9dc8d2 100644 --- a/docs/generators/sssom.rst +++ b/docs/generators/sssom.rst @@ -16,6 +16,8 @@ Docs Command Line ^^^^^^^^^^^^ +``--normalize-prefixes`` is described under :ref:`prefix-normalization`. + .. click:: linkml.generators.sssomgen:cli :prog: gen-sssom :nested: full diff --git a/docs/generators/yarrrml.rst b/docs/generators/yarrrml.rst index 80b8608f86..57a8c09d22 100644 --- a/docs/generators/yarrrml.rst +++ b/docs/generators/yarrrml.rst @@ -133,6 +133,8 @@ Overview Command Line ------------ +``--normalize-prefixes`` is described under :ref:`prefix-normalization`. + .. code:: bash linkml generate yarrrml path/to/schema.yaml > mappings.yml diff --git a/docs/schemas/uris-and-mappings.md b/docs/schemas/uris-and-mappings.md index 090f51e4b9..d39fcca48b 100644 --- a/docs/schemas/uris-and-mappings.md +++ b/docs/schemas/uris-and-mappings.md @@ -50,6 +50,116 @@ LinkML also provides a way to import prefix maps to avoid repetitively declaring best practice to explicitly declare prefix maps, and to use the linter to check for consistency with standard prefix registries like bioregistry and prefix.cc. +(prefix-normalization)= +### Conventional prefix names in generated artifacts + +A schema may declare its own name for a well-known namespace, such as `dce` for +`http://purl.org/dc/elements/1.1/`. By default every generator writes the names the schema +declares. With `--normalize-prefixes` the generators write the conventional name, here `dc`, and +all of them write the same name for each namespace. Only names change; every IRI stays the same. + +| generator | output for `dce: http://purl.org/dc/elements/1.1/` | +|---|---| +| gen-owl, gen-shacl, gen-shex, gen-sparql, gen-yarrrml, gen-sssom | `dc` only | +| gen-jsonld-context | `dc` in what it writes; `dce` stays defined | + +The two groups differ because their prefixes do different jobs: + +- In a self-contained document such as Turtle, ShExC or SPARQL, a prefix only abbreviates IRIs + within that document: prefixes "are not a formal part of the RDF data model" + ([RDF 1.1 Concepts §1.4](https://www.w3.org/TR/rdf11-concepts/#vocabularies)). The document + uses the conventional name alone. It renames only the prefixes it declares. +- A JSON-LD context is shared with the data written against it. A compact IRI whose prefix the + context does not define is read as an IRI of its own + ([JSON-LD 1.1 §4.1.5](https://www.w3.org/TR/json-ld11/#compact-iris)), so data written as + `dce:title` would stop expanding. The context keeps every name the schema declares and adds the + conventional one, so data may use either. JSON-LD compaction picks the shortest name + ([JSON-LD 1.1 API, IRI Compaction](https://www.w3.org/TR/json-ld11-api/#iri-compaction)), so + compacted data may still use the declared one. With `--no-prefixes` nothing is renamed, since the + prefixes then come from another context, such as the one gen-prefix-map writes. + +The conventional names come from two curated sources, the `linked_data` context of +[prefixmaps](https://github.com/linkml/prefixmaps) and the names rdflib binds by default; +crowd-sourced registries such as prefix.cc are not used. One plan per schema decides the names. + +A conventional name is given to a namespace unless the schema's documents already write that name +for another one. In each output format a prefix declaration reaches no further than the document +that carries it: a Turtle or SPARQL directive "associates a prefix label with an IRI" in that +document ([Turtle 1.1 §2.4](https://www.w3.org/TR/turtle/#sec-iri), +[SPARQL 1.1 §4.1.1.1](https://www.w3.org/TR/sparql11-query/#prefNames)), a JSON-LD context defines +terms for the data that names the context +([JSON-LD 1.1 §3.1](https://www.w3.org/TR/json-ld11/#the-context)), and a CURIE is read with the +"prefix binding ... in scope" where it is written +([CURIE Syntax 1.0 §3](https://www.w3.org/TR/curie/#s_syntax)). A declaration in an imported schema +binds nothing in a generated document that does not repeat it. So a name is taken when + +- the schema declares it itself, for another namespace, or +- only an import declares it and it is in use: an element of the schema or its imports is identified + by an IRI of that namespace (`class_uri`, `slot_uri`, a type's `uri`, `enum_uri`, a permissible + value's `meaning`, or the IRI derived from the element's name), is mapped to one (a class, slot, + enum or permissible value), is written with the prefix in a CURIE, or names it in `id_prefixes`, + or a schema lists it in `emit_prefixes`, or +- no schema declares it and a CURIE, `id_prefixes` or `emit_prefixes` names it all the same; it is + then read with a default CURIE map or as an unknown namespace. + +A prefix that only an import declares and nothing uses is needed by no document generated for the +schema, so it holds no name. This is what makes `schema` available for `https://schema.org/`, which +schema.org calls its "preferred form in examples" while "both 'https://schema.org' and +'http://schema.org' are fine" ([schema.org FAQ](https://schema.org/docs/faq.html#19)). The two are different namespaces, +since IRIs are compared as strings +([RDF 1.1 Concepts §3.2](https://www.w3.org/TR/rdf11-concepts/#section-IRIs)). `linkml:types`, which +nearly every schema imports, declares `schema` for `http://schema.org/` and uses it only in the +mappings of its types, which none of the generators offering this option writes. A schema that +declares `schema` for `https://schema.org/` next to it is refused by gen-jsonld-context, gen-shex +and gen-sssom (`Prefix: schema mismatch`), and gen-owl and gen-shacl read the name as the import +declares it and write `http://schema.org/` IRIs. So a schema declares another name, such as `sdo` +(or writes full IRIs), and `--normalize-prefixes` writes `schema`: + +```yaml +prefixes: + sdo: https://schema.org/ +imports: + - linkml:types # declares schema: http://schema.org/ +classes: + Person: + class_uri: sdo:Person +``` + +| | default | `--normalize-prefixes` | +|---|---|---| +| gen-owl, gen-shacl, … | `sdo:Person` | `schema:Person` | +| gen-jsonld-context | defines `sdo` | defines `sdo` and `schema`, both as `https://schema.org/` | + +If the schema also writes `http://schema.org/`, for example with `slot_uri: schema:legacyName`, the +name `schema` is in use for that namespace and `sdo` keeps its name. A prefix also keeps its +declared name, with a warning, when two namespaces want the same conventional name, or when the +name is that of a class, slot or permissible value, which a JSON-LD context uses as a term. In a +self-contained document every CURIE denotes what it does without the option, whatever the plan +gives away: a prefix that its CURIEs are written with is not given to another namespace. + +Giving a name that an import declares to another namespace has two consequences outside the +generated documents, which is why the generators report it as a warning: + +- LinkML's own loaders and dumpers expand the CURIEs of data with the prefixes of the schema and + its imports. Data that writes `schema:Thing` is `http://schema.org/Thing` to `linkml-convert`, + and `https://schema.org/Thing` to a JSON-LD processor that reads it with the normalized context. + Write such data with the name the schema declares, here `sdo`, which both read alike. +- A context generated for the import itself defines every prefix the import declares, used or not. + Data that names that context and then the normalized context of the importing schema gets the + latter's meaning for the name, since in JSON-LD "duplicate context terms are overridden using a + most-recently-defined-wins mechanism" + ([JSON-LD 1.1 §4.1](https://www.w3.org/TR/json-ld11/#advanced-context-usage)). A name the import + uses is not affected: the importing schema's context does not redefine it. + +The conventional names depend on the installed prefixmaps and rdflib: a release that adds a +namespace to the `linked_data` context changes what the option writes for a schema that declares +another name for it. Pin both where generated artifacts are committed. + +gen-prefix-map doesn't offer the option, since a prefix map gives each namespace one name, the +schema's. gen-python, gen-pydantic and gen-namespaces don't either, since their prefix names are part +of the generated code's interface, and gen-rdf and gen-jsonld write CURIEs as literal data. To +change the names that users of a schema write, rename the prefixes in the schema itself. + ## class uri and slot uri Slot and class URIs in LinkML provide the *meaning* for a class or slot, and give a robust and unique place to consistently find information about a class or slot in a LinkML model on the web. diff --git a/packages/linkml/src/linkml/generators/common/prefixes.py b/packages/linkml/src/linkml/generators/common/prefixes.py new file mode 100644 index 0000000000..129c4dfc31 --- /dev/null +++ b/packages/linkml/src/linkml/generators/common/prefixes.py @@ -0,0 +1,471 @@ +"""Conventional prefix names for ``--normalize-prefixes``. + +A schema may declare its own name for a well-known namespace, such as ``sdo`` for +``https://schema.org/``. With ``--normalize-prefixes`` every generator that prints +prefix names writes the conventional name, ``schema``, and all of them write the +same name for each namespace: :func:`preferred_prefixes` computes one plan per +schema, as a :class:`curies.Converter` whose records hold the preferred name and +the other declared names as synonyms. + +In each output format a prefix declaration reaches no further than the document +that carries it: a Turtle or SPARQL directive "associates a prefix label with an +IRI" in that document (Turtle 1.1 §2.4, SPARQL 1.1 §4.1.1.1), a JSON-LD context +defines terms for the data that names the context (JSON-LD 1.1 §3.1), and a CURIE +is read with the "prefix binding ... in scope" where it is written (CURIE Syntax +1.0 §3). A declaration in an imported schema binds nothing in a generated +document that does not repeat it. So what a conventional name can collide with +is another namespace that the schema's own documents write under that name +(:func:`written_prefixes`), not a prefix that an import declares and nothing +uses: ``linkml:types`` declares ``schema`` for ``http://schema.org/`` +and uses it only in the mappings of its types, which none of the generators +offering the option writes, so a schema's ``sdo`` for ``https://schema.org/`` +becomes ``schema``. LinkML itself keeps reading ``schema`` in data as the +import declares it, which the plan reports as a warning. + +How a generator applies the plan depends on what its prefixes are: + +- In a self-contained document (Turtle, RDF/XML, ShExC, SPARQL, YARRRML, SSSOM) a + prefix only abbreviates IRIs in that document; prefixes "are not a formal part + of the RDF data model" (RDF 1.1 Concepts §1.4). Such a document uses the + preferred names only, except that a prefix its own CURIEs are written with + keeps what it denotes (:func:`document_prefixes`). +- A JSON-LD context is shared with the data written against it, and a compact + IRI whose prefix the context no longer defines is read as an IRI of its own + (JSON-LD 1.1 §4.1.5). The context keeps every declared name and adds the + preferred one. + +>>> from linkml_runtime.utils.schemaview import SchemaView +>>> sv = SchemaView(''' +... id: http://example.org/s +... name: s +... prefixes: {ex: http://example.org/, sdo: https://schema.org/} +... default_prefix: ex +... classes: +... Person: {class_uri: sdo:Person} +... ''') +>>> converter = preferred_prefixes(sv) +>>> converter.standardize_curie("sdo:Person") +'schema:Person' +>>> converter.expand("sdo:Person") == converter.expand("schema:Person") == "https://schema.org/Person" +True +""" + +import logging +from collections.abc import Callable, Collection, Mapping +from functools import lru_cache +from typing import TYPE_CHECKING, Any + +import click +from curies import Converter, Record + +from linkml_runtime.utils.formatutils import camelcase, underscore +from linkml_runtime.utils.schemaview import SchemaView + +if TYPE_CHECKING: + from rdflib import Graph + + from linkml_runtime.linkml_model.meta import ClassDefinition, EnumDefinition, PermissibleValue, SlotDefinition + +logger = logging.getLogger(__name__) + + +def well_known_prefix_map() -> dict[str, str]: + """Return a mapping from namespace URI to its conventional prefix name. + + The names come from two curated sources, which name no namespace differently: + + - the ``linked_data`` context of `prefixmaps `_, + curated by the LinkML maintainers (``sh`` for SHACL, ``schema`` for + ``http://schema.org/``); + - the names rdflib binds by default (``Graph(bind_namespaces="rdflib")``), + which the RDF generators serialize with (``schema`` for + ``https://schema.org/``, ``dc`` for DCMI elements). + + prefixmaps' broader ``merged`` context is not used: it gives a namespace the + name of the first registry in its merge order that lists it, which for W3C + vocabularies is often the upper-cased Bioregistry name, such as ``ADMS`` for + ``http://www.w3.org/ns/adms#`` or ``TIME`` for ``http://www.w3.org/2006/time#``. + """ + return dict(_cached_well_known_prefix_map()) + + +@lru_cache(maxsize=1) +def _cached_well_known_prefix_map() -> dict[str, str]: + """Internal cached builder for well_known_prefix_map().""" + from prefixmaps import load_context + from rdflib import Graph + + ns_to_prefix = {rec.namespace: rec.prefix for rec in load_context("linked_data").prefix_expansions} + for prefix, namespace in Graph(bind_namespaces="rdflib").namespaces(): + if str(prefix): + ns_to_prefix[str(namespace)] = str(prefix) + return ns_to_prefix + + +def prefix_renames(prefixes: Mapping[str, str], taken: Collection[str] = frozenset()) -> dict[str, str]: + """Return the renames ``{prefix: conventional name}`` for the *prefixes* that documents write. + + *prefixes* maps each prefix that the documents write to its namespace + (:func:`written_prefixes`). A prefix is renamed to the conventional name of its + namespace (:func:`well_known_prefix_map`) when that name belongs to the + namespace alone: a name among *prefixes* belongs to the namespaces written + under it, because shared outputs keep every such name, and any other name to + the namespaces that want it. A name in *taken*, the other names of the outputs + such as JSON-LD terms, belongs to none. A prefix that is not renamed keeps its + name, with a warning. + + >>> prefix_renames({"sdo": "https://schema.org/", "ex": "http://example.org/"}) + {'sdo': 'schema'} + >>> prefix_renames({"sdo": "https://schema.org/", "schema": "http://schema.org/"}) + {} + >>> prefix_renames({"sdo": "https://schema.org/"}, taken={"schema"}) + {} + """ + known = well_known_prefix_map() + wanted = {prefix: known[str(ns)] for prefix, ns in prefixes.items() if known.get(str(ns), prefix) != prefix} + held: dict[str, set[str]] = {} + for prefix, ns in prefixes.items(): + held.setdefault(prefix, set()).add(str(ns)) + wanting: dict[str, set[str]] = {} + for prefix, name in wanted.items(): + wanting.setdefault(name, set()).add(str(prefixes[prefix])) + renames = {} + for prefix, name in sorted(wanted.items()): + owners = held.get(name) or wanting[name] + if name in taken or owners != {str(prefixes[prefix])}: + logger.warning( + "Prefix %r is not renamed to %r: that name also denotes another namespace or term", prefix, name + ) + else: + renames[prefix] = name + return renames + + +def preferred_prefixes(sv: SchemaView) -> Converter: + """Return the prefix names that ``--normalize-prefixes`` writes for the schema of *sv*. + + Each record's prefix is the name every generator writes for its namespace, and + its synonyms are the namespace's other names, which shared outputs keep. The + plan is computed from the schema, not from one output, so all generators + agree: a name that the schema's documents write (:func:`written_prefixes`) + stays with its namespace, and a conventional name that is a term of the + schema's JSON-LD context, such as the name of a slot, is not given to a prefix + in any output. + + A conventional name that only an import declares, for a namespace nothing + uses, is given away with a warning: LinkML's loaders and dumpers expand the + CURIEs of data with the prefixes of the schema and its imports, so they keep + reading that name as the import declares it. A name that nothing declares + and a CURIE, ``id_prefixes`` or ``emit_prefixes`` uses all the same is read + with a default CURIE map or as an unknown namespace, and is given to no + namespace. + """ + imported = _imported_prefixes(sv) + used_names, iris = _written_references(sv) + written = _written_prefixes(sv, imported, used_names, iris) + renames = prefix_renames(written, taken=_term_names(sv) | (used_names - written.keys())) + for prefix, name in renames.items(): + others = [ns for ns in imported.get(name, ()) if ns != written[prefix]] + if others and name not in written: + logger.warning( + "Prefix %r is written for <%s>. An import declares it for <%s>, which nothing uses; LinkML " + "still reads it so in data", + name, + written[prefix], + ">, <".join(others), + ) + names: dict[str, list[str]] = {} + for prefix, ns in written.items(): + names.setdefault(ns, []).append(prefix) + records = [] + for ns, ns_names in names.items(): + preferred = next((renames[name] for name in ns_names if name in renames), ns_names[0]) + records.append(Record(prefix=preferred, uri_prefix=ns, prefix_synonyms=[n for n in ns_names if n != preferred])) + return Converter(records) + + +def written_prefixes(sv: SchemaView) -> dict[str, str]: + """Return the prefixes that the documents generated for the schema of *sv* write, with their namespaces. + + A prefix is scoped to the document that declares it, so these are the names a + conventional name can collide with: + + - every prefix the schema declares itself, which its JSON-LD context defines + for the data written against it, and + - a prefix that only an import declares, when it is in use: an element of the + schema or its imports is identified by an IRI of its namespace or mapped to + one, or is written with the prefix in a CURIE, or names it among its + ``id_prefixes``, or a schema lists it in ``emit_prefixes``. The context of + the importing schema defines such a prefix too, and data may combine it with + the context of the import. + + A prefix that only an import declares and nothing uses is needed by no + document generated for the schema, so its name is free: ``linkml:types`` + declares ``schema`` for ``http://schema.org/`` and uses it only in the mappings + of its types, which none of the generators offering the option writes. + + >>> sv = SchemaView(''' + ... id: http://example.org/s + ... name: s + ... prefixes: {ex: http://example.org/, linkml: https://w3id.org/linkml/, sdo: https://schema.org/} + ... default_prefix: ex + ... imports: [linkml:types] + ... classes: + ... Person: {class_uri: sdo:Person} + ... ''') + >>> "schema" in written_prefixes(sv), "xsd" in written_prefixes(sv) + (False, True) + >>> preferred_prefixes(sv).standardize_curie("sdo:Person") + 'schema:Person' + """ + return _written_prefixes(sv, _imported_prefixes(sv), *_written_references(sv)) + + +def _written_prefixes( + sv: SchemaView, imported: Mapping[str, list[str]], names: Collection[str], iris: Collection[str] +) -> dict[str, str]: + """Implement :func:`written_prefixes` for the prefixes *imported* that the imports declare and + the *names* and *iris* that the elements are written with (:func:`_written_references`).""" + written = {str(p.prefix_prefix): str(p.prefix_reference) for p in sv.schema.prefixes.values()} + for name, namespaces in imported.items(): + if name not in written: + in_use = [ns for ns in namespaces if any(iri.startswith(ns) for iri in iris)] + if in_use or name in names: + written[name] = (in_use or namespaces)[0] + return written + + +def _imported_prefixes(sv: SchemaView) -> dict[str, list[str]]: + """Return the prefixes that the imports of *sv*'s schema declare, each with its namespaces. + + Imports may declare one name for several namespaces; all are kept, in the order of the + imports closure, so that the name is held if any of them is in use. + """ + imported: dict[str, list[str]] = {} + for schema_name in sv.imports_closure(): + if schema_name != sv.schema.name: + for p in sv.schema_map[schema_name].prefixes.values(): + namespaces = imported.setdefault(str(p.prefix_prefix), []) + if str(p.prefix_reference) not in namespaces: + namespaces.append(str(p.prefix_reference)) + return imported + + +_MAPPING_SLOTS = ( + "mappings", + "exact_mappings", + "close_mappings", + "related_mappings", + "narrow_mappings", + "broad_mappings", +) + + +def _written_references(sv: SchemaView) -> tuple[set[str], set[str]]: + """Return what the elements of *sv*'s schema and its imports are written with: the prefix names + of the CURIEs and the IRIs. + + These are the references that identify an element: the IRI of a class, slot, type, enum or + subset, as declared (``class_uri``, ``slot_uri``, ``uri``, ``enum_uri``) and as derived from its + name, and the ``meaning`` of a permissible value; and the mappings of a class, slot, enum or + permissible value. None of the generators offering the option writes the mappings of a type. + A CURIE counts by the prefix it is written with, so it needs no expansion; the names in + ``id_prefixes`` and ``emit_prefixes`` count too. + """ + references: list[str] = [] + names: set[str] = set() + for schema in sv.schema_map.values(): + names.update(str(prefix) for prefix in schema.emit_prefixes) + + def add_mappings(definition: "ClassDefinition | SlotDefinition | EnumDefinition | PermissibleValue") -> None: + for slot in _MAPPING_SLOTS: + references.extend(str(mapping) for mapping in getattr(definition, slot)) + names.update(str(prefix) for prefix in getattr(definition, "id_prefixes", ())) + + for element in sv.all_elements().values(): + references.extend(sv.get_uri(element, expand=True, native=native) for native in (False, True)) + for type_ in sv.all_types().values(): + references.append(str(type_.uri or "")) + nested_slots: list[SlotDefinition] = [] + for cls in sv.all_classes().values(): + references.append(str(cls.class_uri or "")) + add_mappings(cls) + nested_slots += [*cls.attributes.values(), *cls.slot_usage.values()] + for slot in [*sv.all_slots(attributes=False).values(), *nested_slots]: + references.append(str(slot.slot_uri or "")) + add_mappings(slot) + for enum in sv.all_enums().values(): + references.append(str(enum.enum_uri or "")) + add_mappings(enum) + for value in enum.permissible_values.values(): + references.append(str(value.meaning or "")) + add_mappings(value) + names |= curie_prefixes(references) + return names, {reference for reference in references if curie_prefixes(reference) == set()} + + +def _term_names(sv: SchemaView) -> set[str]: + """Return the names a JSON-LD context of *sv*'s schema uses as terms or bare values. + + These are the class and slot names as the context generator writes them and + the texts of permissible values, which a ``@type: @vocab`` slot reads as terms + first (JSON-LD 1.1 API, IRI Expansion, step 5). + """ + names = {camelcase(name) for name in sv.all_classes()} + names |= {underscore(slot.alias or slot.name) for slot in sv.all_slots().values()} + names |= {text for enum in sv.all_enums().values() for text in enum.permissible_values} + return names + + +def document_prefixes(declared: Mapping[str, str], plan: Converter, used: Collection[str] = frozenset()) -> Converter: + """Return the prefixes of a self-contained document that declares *declared* (name to namespace) under *plan*. + + Each record's prefix is the name the document uses for a namespace it declares, + the preferred one of *plan* where the plan has one, and its synonyms are the + declared names it replaces. *used* are the prefixes of the CURIEs the document + writes. A declaration of a name that *plan* gives to another namespace is + dropped unless the document uses it; a name the document uses, declared or not, + is given to no other namespace. A CURIE rewritten with the returned converter + (:func:`standardize_prefix`) then denotes what it did; one that the document + writes as it stands needs the declaration of its own prefix kept beside. + + >>> plan = Converter([Record(prefix="schema", uri_prefix="https://schema.org/", prefix_synonyms=["sdo"])]) + >>> declared = {"sdo": "https://schema.org/", "schema": "http://schema.org/"} + >>> [(r.prefix, r.uri_prefix, r.prefix_synonyms) for r in document_prefixes(declared, plan).records] + [('schema', 'https://schema.org/', ['sdo'])] + >>> sorted((r.prefix, r.uri_prefix) for r in document_prefixes(declared, plan, used={"sdo", "schema"}).records) + [('schema', 'http://schema.org/'), ('sdo', 'https://schema.org/')] + """ + names: dict[str, list[str]] = {} + for name, namespace in declared.items(): + planned = plan.prefix_map.get(name) + if planned is None or planned == str(namespace) or name in used: + names.setdefault(str(namespace), []).append(name) + records = [] + for namespace, ns_names in names.items(): + preferred = plan.reverse_prefix_map.get(namespace, ns_names[0]) + if preferred in used and preferred not in ns_names: + preferred = ns_names[0] + records.append( + Record(prefix=preferred, uri_prefix=namespace, prefix_synonyms=[n for n in ns_names if n != preferred]) + ) + return Converter(records) + + +def prefix_declarations(document: Converter, declared: Mapping[str, str]) -> dict[str, str]: + """Return the declarations of *document* (:func:`document_prefixes`), name to namespace, in the + order in which *declared* names the namespaces. + + A converter keeps its records sorted by name; a document that has nothing to rename is + written as it is without the option, its declarations in the order the generator gives them. + + >>> plan = Converter([Record(prefix="schema", uri_prefix="https://schema.org/", prefix_synonyms=["sdo"])]) + >>> declared = {"zed": "http://example.org/z/", "sdo": "https://schema.org/", "abc": "http://example.org/a/"} + >>> prefix_declarations(document_prefixes(declared, plan), declared) + {'zed': 'http://example.org/z/', 'schema': 'https://schema.org/', 'abc': 'http://example.org/a/'} + """ + order = {str(namespace): position for position, namespace in enumerate(dict.fromkeys(declared.values()))} + records = sorted(document.records, key=lambda record: order[record.uri_prefix]) + return {record.prefix: record.uri_prefix for record in records} + + +def added_preferred_prefixes( + plan: Converter, defined: Mapping[str, str], terms: Collection[str] = frozenset() +) -> Converter: + """Return the preferred names that an output keeping its *defined* prefixes adds. + + A JSON-LD context is shared with the data written against it, so it keeps every + prefix it defines (name to namespace) and adds the preferred name of the + namespace from *plan* (:func:`preferred_prefixes`); data using either name + expands alike. Each record's synonyms are the defined names it stands for. + A preferred name in *terms*, or defined for another namespace, is not added. + + >>> plan = Converter([Record(prefix="schema", uri_prefix="https://schema.org/", prefix_synonyms=["sdo"])]) + >>> [(r.prefix, r.prefix_synonyms) for r in added_preferred_prefixes(plan, {"sdo": "https://schema.org/"}).records] + [('schema', ['sdo'])] + """ + synonyms: dict[str, list[str]] = {} + for prefix in sorted(defined): + name = plan.standardize_prefix(prefix) + if name in (None, prefix) or name in terms or defined.get(name, defined[prefix]) != defined[prefix]: + continue + synonyms.setdefault(name, []).append(prefix) + return Converter( + [Record(prefix=name, uri_prefix=defined[names[0]], prefix_synonyms=names) for name, names in synonyms.items()] + ) + + +def standardize_prefix(text: str, converter: Converter) -> str: + """Return *text* with the prefix of a compact IRI replaced by its preferred name. + + Only the part before the first colon changes, so templates such as + ``ex:Person/$(id)`` keep their rest. A suffix starting with ``//`` makes *text* + an absolute IRI (JSON-LD 1.1 §4.1.5), which is left alone. + + >>> converter = Converter([Record(prefix="schema", uri_prefix="https://schema.org/", prefix_synonyms=["sdo"])]) + >>> standardize_prefix("sdo:Person", converter), standardize_prefix("https://schema.org/x", converter) + ('schema:Person', 'https://schema.org/x') + """ + prefix, colon, rest = text.partition(":") + if colon and not rest.startswith("//"): + preferred = converter.standardize_prefix(prefix) + if preferred is not None: + return preferred + colon + rest + return text + + +def curie_prefixes(node: Any) -> set[str]: + """Return the prefixes of the compact IRIs among the string values of the JSON structure *node*. + + >>> sorted(curie_prefixes({"s": "ex:a/$(id)", "o": ["sdo:Person", "https://schema.org/x", "text"]})) + ['ex', 'sdo'] + """ + if isinstance(node, dict): + return set().union(*(curie_prefixes(value) for value in node.values())) + if isinstance(node, list): + return set().union(*(curie_prefixes(value) for value in node)) + if isinstance(node, str): + prefix, colon, rest = node.partition(":") + if colon and not rest.startswith("//"): + return {prefix} + return set() + + +def standardize_values(node: Any, converter: Converter) -> Any: + """Return the JSON structure *node* with :func:`standardize_prefix` applied to its string values. + + Keys are left alone: in a JSON-LD context they are the terms data refers to. + Dicts and lists keep their type, such as a list that a YAML dumper writes in + flow style. + """ + if isinstance(node, dict): + return type(node)((key, standardize_values(value, converter)) for key, value in node.items()) + if isinstance(node, list): + return type(node)(standardize_values(value, converter) for value in node) + if isinstance(node, str): + return standardize_prefix(node, converter) + return node + + +def bind_preferred_prefixes(graph: "Graph", converter: Converter) -> None: + """Bind the preferred name of each namespace of *converter* in *graph*. + + A name that rdflib or the metamodel bound for another namespace is taken over; + prefixes only abbreviate IRIs in the serialization (Turtle 1.1 §2.4), so no IRI + changes. + """ + from rdflib import Namespace + + for record in converter.records: + graph.bind(record.prefix, Namespace(record.uri_prefix), override=True, replace=True) + + +def normalize_prefixes_option(f: Callable) -> Callable: + """Add ``--normalize-prefixes`` to the command line of a generator that honours it.""" + return click.option( + "--normalize-prefixes/--no-normalize-prefixes", + default=False, + show_default=True, + help="Write the conventional names of well-known namespaces (e.g. schema for https://schema.org/ " + "where the schema declares sdo), the same in every generator that offers the option.", + )(f) diff --git a/packages/linkml/src/linkml/generators/jsonldcontextgen.py b/packages/linkml/src/linkml/generators/jsonldcontextgen.py index 4916fa07c3..636e870bf2 100644 --- a/packages/linkml/src/linkml/generators/jsonldcontextgen.py +++ b/packages/linkml/src/linkml/generators/jsonldcontextgen.py @@ -14,6 +14,13 @@ from rdflib import SKOS, XSD, Namespace from linkml._version import __version__ +from linkml.generators.common.prefixes import ( + added_preferred_prefixes, + normalize_prefixes_option, + preferred_prefixes, + standardize_prefix, + standardize_values, +) from linkml.utils.deprecation import XSD_ANYURI_AS_IRI_OPTION, deprecated_fields, deprecation_warning from linkml.utils.generator import Generator, shared_arguments from linkml_runtime.linkml_model.meta import ClassDefinition, EnumDefinition, SlotDefinition @@ -52,6 +59,9 @@ class ContextGenerator(Generator): prefixes: bool | None = True flatprefixes: bool | None = False fix_multivalue_containers: bool | None = False + normalize_prefixes: bool = False + """Also define the conventional prefix names of :mod:`linkml.generators.common.prefixes`, + e.g. ``dc`` where the schema declares ``dce``, and write compact IRIs with them.""" exclude_imports: bool = False """If True, elements from imported schemas won't be included in the generated context""" exclude_external_imports: bool = False @@ -184,9 +194,19 @@ def end_schema( self.context_body["@base"] = os.path.relpath(base, os.path.dirname(self.schema.source_file)) else: self.context_body["@base"] = base + preferred = None + if self.normalize_prefixes and prefixes: + preferred = added_preferred_prefixes( + preferred_prefixes(self.prefix_schemaview()), + {prefix: str(self.namespaces[prefix]) for prefix in self.emit_prefixes}, + terms=self.context_body.keys() | self.slot_class_maps.keys(), + ) if prefixes: - for prefix in sorted(self.emit_prefixes): - url = str(self.namespaces[prefix]) + urls = {prefix: str(self.namespaces[prefix]) for prefix in self.emit_prefixes} + if preferred is not None: + urls |= {record.prefix: record.uri_prefix for record in preferred.records} + for prefix in sorted(urls): + url = urls[prefix] # Derived from line # ~5223 in pyld/lib/jsonld.py if bool(re.match(r".*[:/\?#\[\]@]$", url)) or flatprefixes: context_content[prefix] = url @@ -200,6 +220,8 @@ def end_schema( context_content[k] = v for k, v in self.slot_class_maps.items(): context_content[k] = v + if preferred is not None: + context_content = standardize_values(json.loads(as_json(context_content)), preferred) context["@context"] = context_content if output and not self.embed_context_in_frame: with open(output, "w", encoding="UTF-8") as outf: @@ -227,6 +249,8 @@ def end_schema( if root_name: root_cls = self.schema.classes[root_name] frame["@type"] = root_cls.class_uri or root_cls.name + if preferred is not None: + frame["@type"] = standardize_prefix(frame["@type"], preferred) for prop, rule in self.frame_body.items(): frame[prop] = rule @@ -603,6 +627,7 @@ def serialize( help="Use class_uri/slot_uri CURIEs as class and non-identifier slot context keys; identifier slots retain their " + "slot-name key and map to @id.", ) +@normalize_prefixes_option @click.version_option(__version__, "-V", "--version") def cli(yamlfile, emit_frame, embed_context_in_frame, output, **args): """Generate jsonld @context definition from LinkML model""" diff --git a/packages/linkml/src/linkml/generators/owlgen.py b/packages/linkml/src/linkml/generators/owlgen.py index 522910a5b2..7336689feb 100644 --- a/packages/linkml/src/linkml/generators/owlgen.py +++ b/packages/linkml/src/linkml/generators/owlgen.py @@ -20,6 +20,7 @@ from linkml import METAMODEL_NAMESPACE_NAME from linkml._version import __version__ from linkml.generators.common.annotations import declared_annotation +from linkml.generators.common.prefixes import bind_preferred_prefixes, normalize_prefixes_option, preferred_prefixes from linkml.utils.deprecation import XSD_ANYURI_AS_IRI_OPTION, deprecation_warning from linkml.utils.generator import Generator, shared_arguments from linkml.utils.language_tags import LanguageTagResolver @@ -123,6 +124,10 @@ class OwlSchemaGenerator(Generator): """Suffix to add to the schema name to create the ontology URI, e.g. .owl.ttl""" # ObjectVars + normalize_prefixes: bool = False + """Write the conventional prefix names of :mod:`linkml.generators.common.prefixes`, + e.g. ``dc`` where the schema declares ``dce`` for ``http://purl.org/dc/elements/1.1/``.""" + diff_stable: bool = False """Reduce blank-node label churn. See :ref:`rdf-in-version-control`.""" @@ -391,6 +396,8 @@ def as_graph(self) -> Graph: self.graph.bind(prefix, self.metamodel.namespaces[prefix]) for pfx in schema.prefixes.values(): self.graph.namespace_manager.bind(pfx.prefix_prefix, URIRef(pfx.prefix_reference)) + if self.normalize_prefixes: + bind_preferred_prefixes(graph, preferred_prefixes(self.schemaview)) graph.add((base, RDF.type, OWL.Ontology)) # Add main schema elements @@ -2018,6 +2025,7 @@ def slot_owl_type(self, slot: SlotDefinition) -> URIRef: "https://linkml.io/linkml/howtos/collaborative-development.html#rdf-in-version-control" ), ) +@normalize_prefixes_option @click.version_option(__version__, "-V", "--version") def cli(yamlfile: str, metadata_profile: str, **kwargs: Any) -> None: """Generate an OWL representation of a LinkML model diff --git a/packages/linkml/src/linkml/generators/shaclgen.py b/packages/linkml/src/linkml/generators/shaclgen.py index bc49e61d3c..9f2a53285e 100644 --- a/packages/linkml/src/linkml/generators/shaclgen.py +++ b/packages/linkml/src/linkml/generators/shaclgen.py @@ -13,6 +13,7 @@ from linkml._version import __version__ from linkml.generators.common.annotations import declared_annotation +from linkml.generators.common.prefixes import bind_preferred_prefixes, normalize_prefixes_option, preferred_prefixes from linkml.generators.common.subproperty import get_subproperty_values from linkml.generators.shacl.shacl_data_type import ShaclDataType from linkml.generators.shacl.shacl_ifabsent_processor import ShaclIfAbsentProcessor @@ -145,6 +146,10 @@ class ShaclGenerator(Generator): ignores any per-slot ``in_language``. """ + normalize_prefixes: bool = False + """Write the conventional prefix names of :mod:`linkml.generators.common.prefixes`, + e.g. ``dc`` where the schema declares ``dce`` for ``http://purl.org/dc/elements/1.1/``.""" + diff_stable: bool = False """Reduce blank-node label churn. See :ref:`rdf-in-version-control`.""" @@ -213,6 +218,8 @@ def as_graph(self) -> Graph: for pfx in self.schema.prefixes.values(): g.bind(str(pfx.prefix_prefix), pfx.prefix_reference) + if self.normalize_prefixes: + bind_preferred_prefixes(g, preferred_prefixes(self.schemaview)) for c in sv.all_classes(imports=not self.exclude_imports).values(): @@ -965,6 +972,7 @@ def add_simple_data_type(func: Callable, r: ElementName) -> None: "https://linkml.io/linkml/howtos/collaborative-development.html#rdf-in-version-control" ), ) +@normalize_prefixes_option @click.version_option(__version__, "-V", "--version") def cli(yamlfile, **args): """Generate SHACL turtle from a LinkML model""" diff --git a/packages/linkml/src/linkml/generators/shexgen.py b/packages/linkml/src/linkml/generators/shexgen.py index 0fdddb92cb..1a02f2c8f1 100644 --- a/packages/linkml/src/linkml/generators/shexgen.py +++ b/packages/linkml/src/linkml/generators/shexgen.py @@ -13,6 +13,7 @@ from linkml import METAMODEL_NAMESPACE, METAMODEL_NAMESPACE_NAME from linkml._version import __version__ +from linkml.generators.common.prefixes import bind_preferred_prefixes, normalize_prefixes_option, preferred_prefixes from linkml.generators.common.subproperty import get_subproperty_values from linkml.utils.generator import Generator, shared_arguments from linkml_runtime.linkml_model.meta import ( @@ -44,6 +45,9 @@ class ShExGenerator(Generator): # ObjectVars diff_stable: bool = False """Reduce blank-node label churn in RDF output. See :ref:`rdf-in-version-control`.""" + normalize_prefixes: bool = False + """Write the conventional prefix names of :mod:`linkml.generators.common.prefixes`, + e.g. ``dc`` where the schema declares ``dce`` for ``http://purl.org/dc/elements/1.1/``.""" shex: Schema = field(default_factory=lambda: Schema()) # ShEx Schema being generated shapes: list = field(default_factory=lambda: []) @@ -181,10 +185,14 @@ def end_schema(self, output: str | None = None, **_) -> str: g = Graph() g.parse(data=shex, format="json-ld", version="1.1") g.bind("owl", OWL) + if self.normalize_prefixes: + bind_preferred_prefixes(g, preferred_prefixes(self.prefix_schemaview())) shex = canonicalize_rdf_graph(g, output_format="turtle", diff_stable=self.diff_stable) elif self.format == "shex": g = Graph() self.namespaces.load_graph(g) + if self.normalize_prefixes: + bind_preferred_prefixes(g, preferred_prefixes(self.prefix_schemaview())) shex = str(ShExC(self.shex, base=sfx(self.namespaces._base), namespaces=g)) if output: @@ -271,6 +279,7 @@ def _get_subproperty_values(self, slot: SlotDefinition) -> list: "https://linkml.io/linkml/howtos/collaborative-development.html#rdf-in-version-control" ), ) +@normalize_prefixes_option @click.version_option(__version__, "-V", "--version") def cli(yamlfile, **args): """Generate a ShEx Schema for a LinkML model""" diff --git a/packages/linkml/src/linkml/generators/sparqlgen.py b/packages/linkml/src/linkml/generators/sparqlgen.py index 3497164246..78aebcf914 100644 --- a/packages/linkml/src/linkml/generators/sparqlgen.py +++ b/packages/linkml/src/linkml/generators/sparqlgen.py @@ -9,16 +9,27 @@ from jinja2 import Template from linkml._version import __version__ +from linkml.generators.common.prefixes import ( + curie_prefixes, + document_prefixes, + normalize_prefixes_option, + preferred_prefixes, + prefix_declarations, + standardize_prefix, +) from linkml.utils.generator import Generator, shared_arguments -from linkml_runtime.linkml_model.meta import Prefix +from linkml_runtime.linkml_model.meta import Element, Prefix from linkml_runtime.utils.formatutils import underscore from linkml_runtime.utils.schemaview import SchemaView logger = logging.getLogger(__name__) +# The prefixes of the CURIEs that the template writes itself. +_TEMPLATE_PREFIXES = frozenset({"rdf", "linkml"}) + template = """ -{% for pfxn, pfx in schema.prefixes.items() -%} -PREFIX {{pfxn}}: <{{pfx.prefix_reference}}> +{% for pfxn, pfx in prefixes.items() -%} +PREFIX {{pfxn}}: <{{pfx}}> {% endfor %} {% for cn, c in schema.classes.items() if not c.mixin and not c.abstract %} @@ -30,11 +41,11 @@ # @CHECK permitted_{{cn}} SELECT ?g ?s ?p WHERE { GRAPH ?g { - ?s rdf:type {{ schema_view.get_uri(cn) }} ; + ?s rdf:type {{ uri(cn) }} ; ?p ?o . FILTER ( ?p NOT IN ( {% for sn in schema_view.class_slots(cn) -%} - {{ schema_view.get_uri(schema_view.get_slot(sn, attributes=True)) }}, + {{ uri(schema_view.get_slot(sn, attributes=True)) }}, {% endfor -%} rdf:type )) } @@ -52,11 +63,11 @@ ?subject ?predicate WHERE { GRAPH ?graph { - ?subject rdf:type {{ schema_view.get_uri(cn) }} . - FILTER NOT EXISTS { ?subject {{ schema_view.get_uri(slot) }} ?o } + ?subject rdf:type {{ uri(cn) }} . + FILTER NOT EXISTS { ?subject {{ uri(slot) }} ?o } } VALUES ?check { linkml:required } - VALUES ?predicate { {{schema_view.get_uri(slot)}} } + VALUES ?predicate { {{uri(slot)}} } {{ extra }} } {{ limit }} {% endif %} @@ -71,19 +82,19 @@ ?object WHERE { GRAPH ?graph { - ?subject rdf:type {{ schema_view.get_uri(cn) }} ; + ?subject rdf:type {{ uri(cn) }} ; ?predicate ?object . FILTER NOT EXISTS { ?object rdf:type ?otype . FILTER ( ?otype IN ( {% for a in schema_view.class_descendants(slot.range) -%} - {{ schema_view.get_uri(a) }} + {{ uri(a) }} {{ ", " if not loop.last else "" }} {% endfor -%} )) } } VALUES ?check { linkml:range } - VALUES ?predicate { {{ schema_view.get_uri(slot) }} } + VALUES ?predicate { {{ uri(slot) }} } {{ extra }} } {{ limit }} {% endif %} @@ -140,13 +151,21 @@ class SparqlGenerator(Generator): named_graphs: list[str] | None = None limit: int | None = None sparql: str | None = None + normalize_prefixes: bool = False + """Write the conventional prefix names of :mod:`linkml.generators.common.prefixes`, + e.g. ``dc`` where the schema declares ``dce`` for ``http://purl.org/dc/elements/1.1/``.""" def __post_init__(self): super().__post_init__() + # The plan reads the schema as given, before materialization adds the imports' prefixes. + self._prefix_plan = preferred_prefixes(self.schemaview) if self.normalize_prefixes else None # Materialization rewrites the schema (attributes -> slots, extra prefixes), so work on a # copy: mutating the shared SchemaView corrupts the input for any generator that runs after # us under gen-project (#3770). Copying the whole view preserves import-resolution context. self.schemaview = deepcopy(self.schemaview) + # The copy compares equal to the original, so it would be served the original's cached + # elements, which the plan above has filled, and materialization would rewrite those. + self.schemaview.set_modified() materialize_schema(self.schemaview) self.schema = self.schemaview.schema self.queries = self.generate_sparql(named_graphs=self.named_graphs, limit=self.limit) @@ -161,7 +180,32 @@ def generate_sparql(self, named_graphs=None, limit: int = None): limit = f"LIMIT {limit}" else: limit = "" - sparql = template_obj.render(schema_view=self.schemaview, schema=self.schema, limit=limit, extra=extra) + sv = self.schemaview + prefixes = {str(name): str(p.prefix_reference) for name, p in self.schema.prefixes.items()} + plan = None + rendered: list[str] = [] + + def uri(element: Element | str) -> str: + curie = sv.get_uri(element) + rendered.append(curie) + return curie if plan is None else standardize_prefix(curie, plan) + + def render() -> str: + return template_obj.render( + schema_view=sv, schema=self.schema, prefixes=prefixes, uri=uri, limit=limit, extra=extra + ) + + if self._prefix_plan is not None: + # A first rendering tells which prefixes the queries use, so that each keeps what it + # denotes. The CURIEs of the elements are rewritten with the plan; the named graphs and + # the template's own CURIEs are written as they stand, so their prefixes stay declared. + render() + verbatim = curie_prefixes(list(named_graphs or [])) | _TEMPLATE_PREFIXES + plan = document_prefixes(prefixes, self._prefix_plan, curie_prefixes(rendered) | verbatim) + declared = prefixes + prefixes = prefix_declarations(plan, declared) + prefixes |= {name: namespace for name, namespace in declared.items() if name in verbatim} + sparql = render() self.sparql = sparql queries = self.split_sparql(sparql) return queries @@ -196,6 +240,7 @@ def split_sparql(sparql: str) -> dict[str, str]: @shared_arguments(SparqlGenerator) @click.command(name="sparql") @click.option("--dir", "-d", help="Directory in which queries will be deposited") +@normalize_prefixes_option @click.version_option(__version__, "-V", "--version") def cli(yamlfile, dir, **kwargs): """Generate SPARQL queries for validation diff --git a/packages/linkml/src/linkml/generators/sssomgen.py b/packages/linkml/src/linkml/generators/sssomgen.py index ade9c92c4f..368c12e98b 100644 --- a/packages/linkml/src/linkml/generators/sssomgen.py +++ b/packages/linkml/src/linkml/generators/sssomgen.py @@ -5,6 +5,14 @@ import click from linkml._version import __version__ +from linkml.generators.common.prefixes import ( + curie_prefixes, + document_prefixes, + normalize_prefixes_option, + preferred_prefixes, + prefix_declarations, + standardize_prefix, +) from linkml.utils.generator import Generator, shared_arguments from linkml_runtime.linkml_model.meta import ( LINKML, @@ -58,6 +66,14 @@ class SSSOMGenerator(Generator): "exact_mappings": "skos:exactMatch", } + # The columns whose values are entity references, i.e. CURIEs (SSSOM: ``EntityReference`` slots) + curie_columns = frozenset({"subject_id", "predicate_id", "object_id", "match_type"}) + + # ObjectVars + normalize_prefixes: bool = False + """Write the conventional prefix names of :mod:`linkml.generators.common.prefixes`, + e.g. ``dc`` where the schema declares ``dce`` for ``http://purl.org/dc/elements/1.1/``.""" + def __post_init__(self): super().__post_init__() self.sourcefile = self.schema @@ -163,6 +179,17 @@ def end_schema(self, context: str = None, **_) -> None: metadata["creator_id"] = "linkml_user" metadata["mapping_date"] = date.today().strftime("%Y-%m-%d") metadata["curie_map"] = {k: v.prefix_reference for k, v in schema.prefixes.items()} + rows = self.table_as_list + if self.normalize_prefixes: + columns = [i for i, name in enumerate(self.msdf_columns) if name in self.curie_columns] + curies = {row[i] for row in rows for i in columns} + plan = document_prefixes( + metadata["curie_map"], preferred_prefixes(self.prefix_schemaview()), curie_prefixes(list(curies)) + ) + metadata["curie_map"] = prefix_declarations(plan, metadata["curie_map"]) + rows = [ + [standardize_prefix(cell, plan) if i in columns else cell for i, cell in enumerate(row)] for row in rows + ] with open(self.output_file, "w", encoding="UTF-8") as sssom_tsv: for k, v in metadata.items(): if k != "curie_map": @@ -176,12 +203,13 @@ def end_schema(self, context: str = None, **_) -> None: # Write column names first sssom_tsv.writelines("\t".join(self.msdf_columns) + "\n") # Write the msdf next - sssom_tsv.writelines("\t".join(i) + "\n" for i in self.table_as_list) + sssom_tsv.writelines("\t".join(i) + "\n" for i in rows) @shared_arguments(SSSOMGenerator) @click.command(name="sssom") @click.option("-o", "--output", help="Output file name") +@normalize_prefixes_option @click.version_option(__version__, "-V", "--version") def cli(yamlfile, **kwargs): """Generate SSSOM TSV to represent a LinkML model""" diff --git a/packages/linkml/src/linkml/generators/yarrrmlgen.py b/packages/linkml/src/linkml/generators/yarrrmlgen.py index 887c0eaae3..ba98673571 100644 --- a/packages/linkml/src/linkml/generators/yarrrmlgen.py +++ b/packages/linkml/src/linkml/generators/yarrrmlgen.py @@ -8,6 +8,14 @@ import yaml from linkml._version import __version__ +from linkml.generators.common.prefixes import ( + curie_prefixes, + document_prefixes, + normalize_prefixes_option, + preferred_prefixes, + prefix_declarations, + standardize_values, +) from linkml.utils.generator import Generator, shared_arguments from linkml_runtime.linkml_model.meta import ClassDefinition from linkml_runtime.utils.rdf_terms import TermKind, type_term @@ -46,6 +54,9 @@ class YarrrmlGenerator(Generator): """YARRRML source shorthand, e.g. data.json~jsonpath or data.csv~csv.""" iterator_template: str | None = None """JSONPath iterator template; supports {Class}.""" + normalize_prefixes: bool = False + """Write the conventional prefix names of :mod:`linkml.generators.common.prefixes`, + e.g. ``dc`` where the schema declares ``dce`` for ``http://purl.org/dc/elements/1.1/``.""" def __post_init__(self): super().__post_init__() @@ -181,6 +192,10 @@ def as_dict(self) -> dict[str, Any]: mappings[str(cls.name)] = mapping_dict prefixes = self._prefixes_with_defaults() + if self.normalize_prefixes: + plan = document_prefixes(prefixes, preferred_prefixes(sv), curie_prefixes(mappings)) + prefixes = prefix_declarations(plan, prefixes) + mappings = standardize_values(mappings, plan) return {"prefixes": prefixes, "mappings": mappings} def _is_json_source(self) -> bool: @@ -371,6 +386,7 @@ def _po_list_for_class(self, c: ClassDefinition, inline_owners: dict) -> list[di "--iterator-template", help='JSONPath iterator template; supports {Class}, default: "$[*]"', ) +@normalize_prefixes_option @click.version_option(__version__, "-V", "--version") def cli(yamlfile, source, iterator_template, **args): """Generate YARRRML mappings from a LinkML schema.""" diff --git a/packages/linkml/src/linkml/utils/generator.py b/packages/linkml/src/linkml/utils/generator.py index 21008233e2..43fbf3ebd3 100644 --- a/packages/linkml/src/linkml/utils/generator.py +++ b/packages/linkml/src/linkml/utils/generator.py @@ -61,6 +61,7 @@ ) from linkml_runtime.utils.formatutils import camelcase, underscore from linkml_runtime.utils.namespaces import Namespaces +from linkml_runtime.utils.schemaview import load_schema_wrap logger = logging.getLogger(__name__) @@ -285,6 +286,7 @@ def __post_init__(self) -> None: schema = self.schema if isinstance(schema, Path): schema = str(schema) + self._schema_source = schema if self.uses_schemaloader: self._initialize_using_schemaloader(schema) @@ -805,6 +807,28 @@ def slot_range_path(self, slot_or_name: str | SlotDefinition) -> list[str]: # Class return self.class_identifier_path(slot.range, bool(slot.inlined)) + def prefix_schemaview(self) -> SchemaView: + """Return a SchemaView of the schema as given, with its imports unmerged. + + Prefix normalization reads the schema's own prefixes and those of its imports + from it (:func:`~linkml.generators.common.prefixes.preferred_prefixes`), the + same way for generators built on SchemaView and on SchemaLoader, whose + resolved schema has its imports merged. + """ + if self.schemaview is not None: + return self.schemaview + source = self._schema_source + if isinstance(source, Generator): + return source.prefix_schemaview() + if not isinstance(source, str | SchemaDefinition): + source = self.schema + elif isinstance(source, str) and "\n" in source and self.base_dir: + # SchemaView resolves local imports against the schema's file, which YAML text + # has none of; resolve them against base_dir, as SchemaLoader does. + source = load_schema_wrap(source) + source.source_file = os.path.join(self.base_dir, f"{source.name}.yaml") + return SchemaView(source, importmap=self.importmap, base_dir=self.base_dir) + def aliased_slot_name(self, slot: SlotDefinitionName | SlotDefinition) -> SlotDefinitionName: """Return the overloaded slot name -- the alias if one exists otherwise the actual name diff --git a/tests/linkml/test_data/test_sqlite.py b/tests/linkml/test_data/test_sqlite.py index 38dce4ffb8..8b28979d91 100644 --- a/tests/linkml/test_data/test_sqlite.py +++ b/tests/linkml/test_data/test_sqlite.py @@ -71,9 +71,17 @@ def test_mixin(tmp_outputs): assert diff == "" +@pytest.fixture +def restore_csv_field_size_limit(): + """Restore the process-wide CSV field size limit that a test changes.""" + limit = csv.field_size_limit() + yield + csv.field_size_limit(limit) + + @pytest.mark.parametrize("diff", [1, -1]) @pytest.mark.parametrize("size", [20, 200000]) -def test_csv_limit(tmp_outputs, diff, size): +def test_csv_limit(tmp_outputs, diff, size, restore_csv_field_size_limit): """ Tests https://github.com/linkml/linkml/issues/815 """ diff --git a/tests/linkml/test_generators/test_normalize_prefixes.py b/tests/linkml/test_generators/test_normalize_prefixes.py new file mode 100644 index 0000000000..2bc67b8cea --- /dev/null +++ b/tests/linkml/test_generators/test_normalize_prefixes.py @@ -0,0 +1,1544 @@ +"""Tests for ``--normalize-prefixes`` in every generator that writes prefix names. + +The option writes the conventional name of a well-known namespace, such as ``schema`` for +``https://schema.org/`` where a schema declares ``sdo``, and every generator writes the same +name (:mod:`linkml.generators.common.prefixes`). Only names change: + +- a self-contained document (OWL, SHACL, ShEx, SPARQL, YARRRML, SSSOM) means the same with and + without the option, since prefixes are "not a formal part of the RDF data model" + (RDF 1.1 Concepts §1.4; Turtle 1.1 §2.4); +- a JSON-LD context or a prefix map keeps every declared name and adds the conventional one, so + data written against it expands the same (JSON-LD 1.1 §4.1.5). + +A prefix is scoped to the document that declares it, so a conventional name is unavailable only where +the schema's documents write it for another namespace; a prefix that only an import declares and +nothing uses holds no name. +""" + +import json +import logging +import re +import textwrap +from collections.abc import Callable +from dataclasses import dataclass +from pathlib import Path +from typing import Any + +import pytest +import yaml +from click.testing import CliRunner +from curies import Converter, Record +from jsonasobj import as_json +from prefixmaps import load_context +from pyparsing import ParseResults +from pyshexc.parser_impl.generate_shexj import parse as parse_shexc +from rdflib import Graph +from rdflib.compare import isomorphic +from rdflib.plugins.shared.jsonld.context import Context +from rdflib.plugins.sparql.parser import parseQuery +from rdflib.plugins.sparql.parserutils import CompValue + +from linkml.cli.main import linkml as linkml_cli +from linkml.generators.common.prefixes import ( + added_preferred_prefixes, + document_prefixes, + preferred_prefixes, + prefix_declarations, + prefix_renames, + standardize_prefix, + well_known_prefix_map, + written_prefixes, +) +from linkml.generators.jsonldcontextgen import ContextGenerator +from linkml.generators.owlgen import OwlSchemaGenerator +from linkml.generators.prefixmapgen import PrefixGenerator +from linkml.generators.shaclgen import ShaclGenerator +from linkml.generators.shexgen import ShExGenerator +from linkml.generators.sparqlgen import SparqlGenerator +from linkml.generators.sssomgen import SSSOMGenerator +from linkml.generators.yarrrmlgen import YarrrmlGenerator +from linkml_runtime.utils.schemaview import SchemaView + +SCHEMA_ORG = "https://schema.org/" +SCHEMA_ORG_HTTP = "http://schema.org/" +DC_ELEMENTS = "http://purl.org/dc/elements/1.1/" +DC_TERMS = "http://purl.org/dc/terms/" +FOAF = "http://xmlns.com/foaf/0.1/" +TIME = "http://www.w3.org/2006/time#" +BIBO = "http://purl.org/ontology/bibo/" +PROV = "http://www.w3.org/ns/prov#" +SKOS = "http://www.w3.org/2004/02/skos/core#" +NOT_PROV = "http://example.org/not-prov/" + + +def _schema(name: str, prefixes: str, body: str, default_prefix: str = "ex", imports: str = "linkml:types") -> str: + """A schema with the ``ex`` and ``linkml`` prefixes, the given extra prefix lines, imports and elements.""" + return ( + f"id: https://example.org/{name}\nname: {name}\ndefault_prefix: {default_prefix}\nprefixes:\n" + " ex: https://example.org/\n linkml: https://w3id.org/linkml/\n" + + textwrap.indent(textwrap.dedent(prefixes), " ") + + "imports:\n" + + "".join(f" - {i}\n" for i in imports.split()) + + textwrap.dedent(body) + ) + + +# Each case: the schema files (the main one is schema.yaml) and the name every generator writes for each namespace. +CASES: dict[str, tuple[dict[str, str], dict[str, str]]] = { + "prov_alias": ( + { + "schema.yaml": _schema( + "prov", + "w3prov: http://www.w3.org/ns/prov#\n", + """\ + classes: + Agent: + class_uri: w3prov:Agent + exact_mappings: [w3prov:Person] + attributes: + id: {identifier: true, range: uriorcurie} + label: {slot_uri: w3prov:value} + delegate: {range: Agent, multivalued: true, slot_uri: w3prov:actedOnBehalfOf} + """, + ) + }, + {PROV: "prov"}, + ), + # linkml:types declares ``schema`` for http://schema.org/ and writes it in no document, and the + # generators do not agree on a schema that declares ``schema`` for https://schema.org/ itself (three + # refuse it, two read the import's namespace), so the schema declares ``sdo``, which becomes ``schema`` + "schema_org_alias": ( + { + "schema.yaml": _schema( + "sdo", + "sdo: https://schema.org/\n", + """\ + classes: + Person: + class_uri: sdo:Person + attributes: + id: {identifier: true, range: uriorcurie} + knows: {range: Person, multivalued: true, slot_uri: sdo:knows} + """, + ) + }, + {SCHEMA_ORG: "schema"}, + ), + # an import declares ``prov`` for another namespace, which nothing uses, so the name is free + "import_declares_an_unused_name": ( + { + "module.yaml": _schema("module", "prov: http://example.org/not-prov/\n", ""), + "schema.yaml": _schema( + "holder", + "w3prov: http://www.w3.org/ns/prov#\n", + """\ + classes: + Agent: + class_uri: w3prov:Agent + """, + imports="linkml:types module", + ), + }, + {PROV: "prov"}, + ), + # an import declares ``prov`` for another namespace and identifies a class with it, so it keeps the name + "import_uses_a_name": ( + { + "module.yaml": _schema( + "module", + "prov: http://example.org/not-prov/\n", + """\ + classes: + Provision: + class_uri: prov:Provision + attributes: + id: {identifier: true, range: uriorcurie} + """, + ), + "schema.yaml": _schema( + "holder", + "w3prov: http://www.w3.org/ns/prov#\n", + """\ + classes: + Agent: + class_uri: w3prov:Agent + attributes: + provision: {range: Provision} + """, + imports="linkml:types module", + ), + }, + {PROV: "w3prov", NOT_PROV: "prov"}, + ), + "dce_alias": ( + { + "schema.yaml": _schema( + "dce", + "dce: http://purl.org/dc/elements/1.1/\n", + """\ + classes: + Record: + attributes: + title: {slot_uri: dce:title} + """, + ) + }, + {DC_ELEMENTS: "dc"}, + ), + # both namespaces want ``schema``, which linkml:types declares for http://schema.org/ + "http_and_https_schema_org": ( + { + "schema.yaml": _schema( + "both", + "sdo: https://schema.org/\nsdohttp: http://schema.org/\n", + """\ + classes: + Person: + class_uri: sdo:Person + attributes: + full_name: {slot_uri: sdo:name} + old_name: {slot_uri: sdohttp:alternateName} + """, + ) + }, + {SCHEMA_ORG: "sdo", SCHEMA_ORG_HTTP: "schema"}, + ), + # linkml:types declares ``schema`` for http://schema.org/, which this schema uses + "imported_name_in_use": ( + { + "schema.yaml": _schema( + "imported", + "sdo: https://schema.org/\n", + """\ + classes: + Person: + class_uri: sdo:Person + attributes: + full_name: {slot_uri: sdo:name} + legacy: {slot_uri: "schema:legacyName"} + """, + ) + }, + {SCHEMA_ORG: "sdo", SCHEMA_ORG_HTTP: "schema"}, + ), + # ``dc`` stays DCMI terms' name in shared outputs, so DCMI elements cannot take it + "dc_terms_and_dce": ( + { + "schema.yaml": _schema( + "dcboth", + "dc: http://purl.org/dc/terms/\ndce: http://purl.org/dc/elements/1.1/\n", + """\ + classes: + Doc: + attributes: + title: {slot_uri: dc:title} + creator: {slot_uri: dce:creator} + """, + ) + }, + {DC_TERMS: "dcterms", DC_ELEMENTS: "dce"}, + ), + "case_only_alias": ( + { + "schema.yaml": _schema( + "case", + "FOAF: http://xmlns.com/foaf/0.1/\n", + """\ + classes: + Agent: + class_uri: FOAF:Agent + attributes: + nick: {slot_uri: FOAF:nick} + """, + ) + }, + {FOAF: "foaf"}, + ), + # ``time`` is a slot, so a JSON-LD context has a term of that name + "prefix_name_is_a_slot": ( + { + "schema.yaml": _schema( + "termclash", + "owltime: http://www.w3.org/2006/time#\n", + """\ + classes: + Interval: + class_uri: owltime:Interval + attributes: + begin: {slot_uri: owltime:hasBeginning} + time: {range: string} + """, + ) + }, + {TIME: "owltime"}, + ), + # ``dc`` is a slot with a class-scoped range + "scoped_slot_named_like_a_prefix": ( + { + "schema.yaml": _schema( + "scoped", + "dce: http://purl.org/dc/elements/1.1/\n", + """\ + slots: + dc: {range: string} + classes: + Doc: + slots: [dc] + slot_usage: + dc: {range: integer} + attributes: + title: {slot_uri: dce:title} + """, + ) + }, + {DC_ELEMENTS: "dce"}, + ), + # ``dc`` is a permissible value, which a ``@type: @vocab`` slot reads as a term first + "enum_value_named_like_a_prefix": ( + { + "schema.yaml": _schema( + "vocab", + "dce: http://purl.org/dc/elements/1.1/\n", + """\ + classes: + Doc: + attributes: + title: {slot_uri: dce:title} + vocabulary: {range: VocabularyEnum} + enums: + VocabularyEnum: + permissible_values: + dc: {meaning: dce:title} + other: {} + """, + ) + }, + {DC_ELEMENTS: "dce"}, + ), + "alias_as_default_prefix": ( + { + "schema.yaml": _schema( + "dflt", + "w3prov: http://www.w3.org/ns/prov#\n", + """\ + classes: + Agent: + attributes: + id: {identifier: true, range: uriorcurie} + label: {range: string} + delegate: {range: Agent, slot_uri: w3prov:actedOnBehalfOf} + """, + default_prefix="w3prov", + ) + }, + {PROV: "prov"}, + ), + "alias_in_type_and_enum_meaning": ( + { + "schema.yaml": _schema( + "typeuri", + "w3prov: http://www.w3.org/ns/prov#\n", + """\ + types: + ProvTime: + typeof: string + uri: w3prov:InstantaneousEvent + classes: + Activity: + class_uri: w3prov:Activity + attributes: + started: {range: ProvTime} + role: {range: RoleEnum, slot_uri: w3prov:hadRole} + enums: + RoleEnum: + permissible_values: + Author: {meaning: w3prov:Role} + """, + ) + }, + {PROV: "prov"}, + ), + "prefixes_declared_in_an_import": ( + { + "module.yaml": _schema( + "module", + "mybibo: http://purl.org/ontology/bibo/\nw3prov: http://www.w3.org/ns/prov#\n", + """\ + classes: + Book: + class_uri: mybibo:Book + attributes: + source: {slot_uri: w3prov:hadPrimarySource} + """, + ), + "schema.yaml": _schema( + "main", + "", + """\ + classes: + Thesis: + is_a: Book + class_uri: mybibo:Thesis + """, + imports="linkml:types module", + ), + }, + {BIBO: "bibo", PROV: "prov"}, + ), + # rdflib binds ``schema`` to https://schema.org/ by default; the schema's own declaration wins + "conventional_http_schema_org": ( + { + "schema.yaml": _schema( + "httpschema", + "schema: http://schema.org/\n", + """\ + classes: + Person: + class_uri: schema:Person + attributes: + full_name: {slot_uri: schema:name} + """, + ) + }, + {SCHEMA_ORG_HTTP: "schema"}, + ), +} + + +def _write(tmp_path: Path, case: str) -> str: + """Write the files of *case* to *tmp_path* and return the path of its main schema.""" + for name, text in CASES[case][0].items(): + (tmp_path / name).write_text(text, encoding="utf-8") + return str(tmp_path / "schema.yaml") + + +# ── Self-contained documents ────────────────────────────────────────── + + +def _rdf_prefixes(text: str, rdf_format: str) -> dict[str, str]: + return {str(p): str(n) for p, n in Graph(bind_namespaces="none").parse(data=text, format=rdf_format).namespaces()} + + +def _prefix_lines(text: str) -> dict[str, str]: + """The ``PREFIX name: `` declarations of ShExC and SPARQL.""" + return dict(re.findall(r"^PREFIX\s+([\w.-]*):\s*<([^>]*)>", text, flags=re.MULTILINE)) + + +def _expand_curies(node: Any, prefixes: dict[str, str]) -> Any: + """Expand every string of *node* whose prefix *prefixes* declares.""" + if isinstance(node, dict): + return {key: _expand_curies(value, prefixes) for key, value in node.items()} + if isinstance(node, list): + return [_expand_curies(value, prefixes) for value in node] + if isinstance(node, str): + prefix, colon, rest = node.partition(":") + if colon and prefix in prefixes and not rest.startswith("//"): + return prefixes[prefix] + rest + return node + + +def _sparql_meaning(text: str) -> dict[str, Any]: + """Each query of a SPARQL file as its parse tree, with prefixed names expanded by its own PREFIX lines. + + rdflib's query algebra binds one prefix per namespace, so it cannot read a file that declares + two names for one namespace, which SPARQL allows; the parse tree has the names as written. + """ + + def expand(node: Any, prefixes: dict[str, str]) -> Any: + if isinstance(node, CompValue): + if node.name == "pname": + return prefixes[node["prefix"] if "prefix" in node else ""] + node.get("localname", "") + return node.name, tuple((key, expand(value, prefixes)) for key, value in node.items()) + if isinstance(node, list | ParseResults): + return tuple(expand(value, prefixes) for value in node) + return str(node) + + meaning = {} + for name, query in SparqlGenerator.split_sparql(text).items(): + prologue, body = parseQuery(query) + prefixes = {(d["prefix"] if "prefix" in d else ""): str(d["iri"]) for d in prologue if d.name == "PrefixDecl"} + meaning[name] = expand(body, prefixes) + return meaning + + +def _sssom(text: str) -> tuple[dict[str, str], list[list[str]]]: + """The ``curie_map`` and the rows of an SSSOM TSV.""" + curie_map, rows, in_map = {}, [], False + for line in text.splitlines(): + if line.startswith("#curie_map:"): + in_map = True + elif in_map and line.startswith("# "): + name, _, iri = line[2:].partition(": ") + curie_map[name] = iri + elif not line.startswith("#"): + in_map = False + rows.append(line.split("\t")) + return curie_map, rows + + +@dataclass(frozen=True) +class Document: + """A self-contained output: how to generate it, read its prefixes, and compare its meaning.""" + + produce: Callable[[str, bool, Path], str] + prefixes: Callable[[str], dict[str, str]] + meaning: Callable[[str], Any] + same: Callable[[Any, Any], bool] = lambda a, b: a == b + + +def _rdf_document(generator: type, rdf_format: str, parse_format: str, **flags: Any) -> Document: + return Document( + produce=lambda path, normalize, _: generator( + path, normalize_prefixes=normalize, format=rdf_format, **flags + ).serialize(), + prefixes=lambda text: _rdf_prefixes(text, parse_format), + meaning=lambda text: Graph().parse(data=text, format=parse_format), + same=isomorphic, + ) + + +def _sssom_produce(path: str, normalize: bool, tmp_path: Path) -> str: + output = tmp_path / f"out-{normalize}.sssom.tsv" + SSSOMGenerator(path, output=str(output), normalize_prefixes=normalize).serialize() + return output.read_text(encoding="utf-8") + + +DOCUMENTS = { + "owl-ttl": _rdf_document(OwlSchemaGenerator, "ttl", "turtle"), + "owl-xml": _rdf_document(OwlSchemaGenerator, "xml", "xml"), + "shacl-ttl": _rdf_document(ShaclGenerator, "ttl", "turtle"), + "shex-rdf": _rdf_document(ShExGenerator, "rdf", "turtle"), + "shexc": Document( + produce=lambda path, normalize, _: ShExGenerator(path, normalize_prefixes=normalize).serialize(), + prefixes=_prefix_lines, + meaning=lambda text: json.loads(as_json(parse_shexc(text))), + ), + "sparql": Document( + produce=lambda path, normalize, _: SparqlGenerator(path, normalize_prefixes=normalize).serialize(), + prefixes=_prefix_lines, + meaning=_sparql_meaning, + ), + "yarrrml": Document( + produce=lambda path, normalize, _: YarrrmlGenerator(path, normalize_prefixes=normalize).serialize(), + prefixes=lambda text: yaml.safe_load(text)["prefixes"], + meaning=lambda text: _expand_curies(yaml.safe_load(text)["mappings"], yaml.safe_load(text)["prefixes"]), + ), + "sssom": Document( + produce=_sssom_produce, + prefixes=lambda text: _sssom(text)[0], + meaning=lambda text: _expand_curies(_sssom(text)[1], _sssom(text)[0]), + ), +} + +# Formats and options that change which prefixes an RDF output binds or prints. +RDF_VARIANTS = { + "owl-n3": _rdf_document(OwlSchemaGenerator, "n3", "n3"), + "owl-json-ld": _rdf_document(OwlSchemaGenerator, "json-ld", "json-ld"), + "owl-nt": _rdf_document(OwlSchemaGenerator, "nt", "nt"), + "owl-diff-stable": _rdf_document(OwlSchemaGenerator, "ttl", "turtle", diff_stable=True), + "owl-mapped-uris": _rdf_document(OwlSchemaGenerator, "ttl", "turtle", use_native_uris=False), + "owl-no-metaclasses": _rdf_document(OwlSchemaGenerator, "ttl", "turtle", metaclasses=False, type_objects=False), + "owl-metadata-profile-rdfs": _rdf_document(OwlSchemaGenerator, "ttl", "turtle", metadata_profile="rdfs"), + "shacl-diff-stable": _rdf_document(ShaclGenerator, "ttl", "turtle", diff_stable=True), + "shacl-annotations": _rdf_document(ShaclGenerator, "ttl", "turtle", include_annotations=True), +} + + +# gen-shex's RDF output reads the ShEx JSON-LD context, which the network marker serves from local files. +DOCUMENT_NAMES = [pytest.param(d, marks=pytest.mark.network) if d == "shex-rdf" else d for d in sorted(DOCUMENTS)] + + +@pytest.mark.parametrize("case", sorted(CASES)) +@pytest.mark.parametrize("document", DOCUMENT_NAMES) +def test_document_means_the_same(tmp_path, document, case): + """With the option a self-contained document denotes the same IRIs as without it.""" + doc = DOCUMENTS[document] + path = _write(tmp_path, case) + off, on = (doc.produce(path, normalize, tmp_path) for normalize in (False, True)) + assert doc.same(doc.meaning(off), doc.meaning(on)) + + +@pytest.mark.parametrize("case", sorted(CASES)) +@pytest.mark.parametrize("document", DOCUMENT_NAMES) +def test_document_writes_the_planned_names(tmp_path, document, case): + """Every generator writes the same name for a namespace, and no other name for it.""" + doc = DOCUMENTS[document] + path = _write(tmp_path, case) + planned = CASES[case][1] + off = doc.prefixes(doc.produce(path, False, tmp_path)) + on = doc.prefixes(doc.produce(path, True, tmp_path)) + for namespace, name in planned.items(): + names = {prefix for prefix, iri in on.items() if iri == namespace} + assert names <= {name}, f"{namespace} is written as {names}" + if namespace in off.values(): + assert on.get(name) == namespace + + +@pytest.mark.parametrize("variant", sorted(RDF_VARIANTS)) +@pytest.mark.parametrize("case", ["prov_alias", "conventional_http_schema_org", "prefixes_declared_in_an_import"]) +def test_rdf_variants(tmp_path, variant, case): + """Other formats and options of gen-owl and gen-shacl keep the graph and use the planned names.""" + doc = RDF_VARIANTS[variant] + path = _write(tmp_path, case) + off, on = (doc.produce(path, normalize, tmp_path) for normalize in (False, True)) + assert doc.same(doc.meaning(off), doc.meaning(on)) + for namespace, name in CASES[case][1].items(): + assert {p for p, iri in doc.prefixes(on).items() if iri == namespace} <= {name} + + +@pytest.mark.parametrize("generator", [OwlSchemaGenerator, ShaclGenerator]) +def test_no_rdflib_generated_names(tmp_path, generator): + """A declared conventional name wins over the name rdflib binds by default, so no ``schema1`` appears.""" + path = _write(tmp_path, "conventional_http_schema_org") + prefixes = _rdf_prefixes(generator(path, normalize_prefixes=True).serialize(), "turtle") + assert prefixes["schema"] == SCHEMA_ORG_HTTP + assert not [p for p in prefixes if re.fullmatch(r"schema\d+", p)] + + +# ── JSON-LD contexts and prefix maps ────────────────────────────────── + + +def _context(path: str, normalize: bool, **flags: Any) -> dict: + return json.loads(ContextGenerator(path, normalize_prefixes=normalize, **flags).serialize())["@context"] + + +def _context_prefixes(context: dict) -> dict[str, str]: + """The prefix definitions of a context: IRIs as strings or ``@prefix`` term definitions.""" + prefixes = {} + for key, value in context.items(): + if key.startswith("@") or ":" in key: + continue + if isinstance(value, str): + prefixes[key] = value + elif isinstance(value, dict) and value.get("@prefix") is True: + prefixes[key] = value["@id"] + return prefixes + + +def _expanded_terms(context: dict, parent: Context | None = None) -> set: + """The IRIs each term of a JSON-LD context expands to, without the term names. + + Uses rdflib's JSON-LD context processing; scoped contexts are expanded within their parent. + """ + ctx = Context(context) if parent is None else parent.subcontext(context) + expanded = {("@vocab", ctx.vocab)} + for name in context: + term = ctx.terms.get(name) + if term is not None: + scoped = frozenset(_expanded_terms(term.context, ctx)) if isinstance(term.context, dict) else None + expanded.add((term.id, term.type, str(term.container), term.reverse, scoped)) + return expanded + + +def _terms(context: dict) -> dict: + """The term definitions of a context, without its prefix definitions.""" + return { + key: value + for key, value in context.items() + if not key.startswith("@") and isinstance(value, dict) and value.get("@prefix") is not True + } + + +def _data_graph(context: Any, terms: dict, prefixes: list[str], vocabulary: list[str]) -> Graph: + """Parse, with *context*, a document that uses every term of *terms* (term definitions). + + It has one node per prefix of *prefixes*, identified by a compact IRI with that prefix. + IRI-valued terms get a compact IRI and a relative IRI, vocabulary-valued ones every name of + *vocabulary* (the permissible values, the only names valid data uses there), and the others a + string. Every term is also a type of every node. + """ + + def value(definition: dict, prefix: str) -> Any: + coercion = definition.get("@type") + if coercion == "@id": + return [f"{prefix}:object", "relative"] + if coercion == "@vocab": + return vocabulary + return "text" + + nodes = [ + {"@id": f"{prefix}:node", "@type": sorted(terms), **{t: value(d, prefix) for t, d in terms.items()}} + for prefix in prefixes + ] + document = {"@context": context, "@graph": nodes} + return Graph().parse(data=json.dumps(document), format="json-ld", base="http://base.test/") + + +def _schema_namespaces(path: str) -> dict[str, str]: + """The prefixes of a schema and its imports, with which LinkML expands the CURIEs of data.""" + sv = SchemaView(path) + sv.imports_closure() + return {str(prefix): str(namespace) for prefix, namespace in sv.namespaces().items()} + + +def _vocabulary(path: str) -> list[str]: + sv = SchemaView(path) + return sorted(text for enum in sv.all_enums().values() for text in enum.permissible_values) + + +CONTEXT_FLAGS = { + "default": {}, + "use-curies": {"use_curies": True}, + "flatprefixes": {"flatprefixes": True}, + "fix-multivalue-containers": {"fix_multivalue_containers": True}, + "no-mergeimports": {"mergeimports": False}, +} + + +@pytest.mark.parametrize("case", sorted(CASES)) +@pytest.mark.parametrize("flags", sorted(CONTEXT_FLAGS)) +def test_context_terms_expand_the_same(tmp_path, flags, case): + """Every term of the context expands to the IRIs it expands to without the option.""" + path = _write(tmp_path, case) + off, on = (_context(path, normalize, **CONTEXT_FLAGS[flags]) for normalize in (False, True)) + assert _expanded_terms(on) == _expanded_terms(off) + assert _terms(on).keys() == _terms(off).keys() + + +@pytest.mark.parametrize("case", sorted(CASES)) +@pytest.mark.parametrize("flags", sorted(CONTEXT_FLAGS)) +def test_context_keeps_declared_names_and_adds_planned_ones(tmp_path, flags, case): + path = _write(tmp_path, case) + off, on = (_context_prefixes(_context(path, normalize, **CONTEXT_FLAGS[flags])) for normalize in (False, True)) + assert off.items() <= on.items() + for namespace, name in CASES[case][1].items(): + if namespace in off.values(): + assert on.get(name) == namespace + + +@pytest.mark.parametrize("case", sorted(CASES)) +@pytest.mark.parametrize("use_curies", [False, True]) +def test_data_expands_the_same(tmp_path, case, use_curies): + """Data written with a prefix that the default context defines means the same under both contexts, + and so does data written with a prefix of the schema or its imports that neither defines. A name + only the normalized context defines is the conventional name of a namespace that the default + context defines under another name. Where the schema's documents write that name, it denotes the + namespace LinkML expands it to; where only an import declares it for a namespace nothing uses, the + context and LinkML differ. Data written with the planned names means the same as with the declared + ones.""" + path = _write(tmp_path, case) + off, on = (_context(path, normalize, use_curies=use_curies) for normalize in (False, True)) + off_prefixes, on_prefixes = _context_prefixes(off), _context_prefixes(on) + schema_prefixes = _schema_namespaces(path) + terms, vocabulary = _terms(off), _vocabulary(path) + sv = SchemaView(path) + plan, written = preferred_prefixes(sv), written_prefixes(sv) + for prefix in sorted(off_prefixes.keys() | schema_prefixes.keys()): + expected_context = off + if prefix not in off_prefixes and prefix in on_prefixes: + namespace = on_prefixes[prefix] + assert namespace in off_prefixes.values() + assert well_known_prefix_map()[namespace] == prefix + if prefix in written: + assert namespace == schema_prefixes[prefix] + else: + # the accepted difference: only an import declares the name, for a namespace nothing uses + assert schema_prefixes.get(prefix) != namespace + expected_context = [{prefix: namespace}, off] + expected = _data_graph(expected_context, terms, [prefix], vocabulary) + assert isomorphic(_data_graph(on, terms, [prefix], vocabulary), expected), prefix + planned = plan.standardize_prefix(prefix) or prefix + assert isomorphic(_data_graph(on, terms, [planned], vocabulary), expected), planned + + +@pytest.mark.parametrize("case", ["prov_alias", "prefix_name_is_a_slot", "prefixes_declared_in_an_import"]) +@pytest.mark.parametrize("prefixes_normalized", [False, True]) +@pytest.mark.parametrize("model_normalized", [False, True]) +def test_prefix_and_model_contexts_combine(tmp_path, case, prefixes_normalized, model_normalized): + """A prefix-only context (``--no-model``) and a model-only context (``--no-prefixes``) combine to the + same meaning, whichever of them was normalized.""" + path = _write(tmp_path, case) + full = _context(path, False) + combined = [ + _context(path, prefixes_normalized, model=False), + _context(path, model_normalized, prefixes=False), + ] + declared = sorted(_context_prefixes(full)) + vocabulary = _vocabulary(path) + expected = _data_graph(full, _terms(full), declared, vocabulary) + assert isomorphic(_data_graph(combined, _terms(full), declared, vocabulary), expected) + + +@pytest.mark.parametrize("case", ["prov_alias", "prefixes_declared_in_an_import"]) +def test_prefix_map_combines_with_a_model_only_context(tmp_path, case): + """The prefixes of ``--no-prefixes`` come from gen-prefix-map, whose names the context then keeps.""" + path = _write(tmp_path, case) + full = _context(path, False) + prefix_map = json.loads(PrefixGenerator(path).serialize()) + combined = [prefix_map, _context(path, True, prefixes=False)] + declared = sorted(_context_prefixes(full)) + expected = _data_graph(full, _terms(full), declared, _vocabulary(path)) + assert isomorphic(_data_graph(combined, _terms(full), declared, _vocabulary(path)), expected) + + +def test_no_prefixes_context_is_unchanged(tmp_path): + """Without prefix definitions a context writes compact IRIs only with the names other contexts define.""" + path = _write(tmp_path, "prov_alias") + assert _context(path, True, prefixes=False) == _context(path, False, prefixes=False) + + +@pytest.mark.parametrize("base_normalized", [False, True]) +@pytest.mark.parametrize("extension_normalized", [False, True]) +@pytest.mark.parametrize("exclude", ["exclude_imports", "exclude_external_imports"]) +def test_contexts_of_an_import_and_its_importer_combine(tmp_path, base_normalized, extension_normalized, exclude): + """The context of an importing schema, without the imported elements, combines with the imported + schema's context, whichever was normalized; the importer's ``time`` slot shares the name ``time``.""" + (tmp_path / "base.yaml").write_text( + _schema( + "base", + "owltime: http://www.w3.org/2006/time#\n", + """\ + classes: + Interval: + class_uri: owltime:Interval + attributes: + begin: {slot_uri: owltime:hasBeginning} + """, + ), + encoding="utf-8", + ) + (tmp_path / "schema.yaml").write_text( + _schema( + "extension", + "owltime: http://www.w3.org/2006/time#\n", + """\ + classes: + Meeting: + is_a: Interval + attributes: + time: {range: string} + """, + imports="linkml:types base", + ), + encoding="utf-8", + ) + base, extension = str(tmp_path / "base.yaml"), str(tmp_path / "schema.yaml") + expected_contexts = [_context(base, False), _context(extension, False, **{exclude: True})] + contexts = [_context(base, base_normalized), _context(extension, extension_normalized, **{exclude: True})] + terms = _terms(expected_contexts[0]) | _terms(expected_contexts[1]) + declared = sorted(_context_prefixes(expected_contexts[0]) | _context_prefixes(expected_contexts[1])) + expected = _data_graph(expected_contexts, terms, declared, []) + assert isomorphic(_data_graph(contexts, terms, declared, []), expected) + + +@pytest.mark.parametrize("embed", [False, True]) +def test_frame_type_expands_the_same(tmp_path, embed): + """The frame's ``@type`` is written with the planned name and denotes the same class.""" + (tmp_path / "schema.yaml").write_text( + _schema( + "frame", + "w3prov: http://www.w3.org/ns/prov#\n", + """\ + classes: + Activity: + tree_root: true + class_uri: w3prov:Activity + attributes: + association: {range: Association, inlined: true, slot_uri: w3prov:qualifiedAssociation} + Association: + class_uri: w3prov:Association + attributes: + role: {slot_uri: w3prov:hadRole} + """, + ), + encoding="utf-8", + ) + path = str(tmp_path / "schema.yaml") + frames = [] + for normalize in (False, True): + output = tmp_path / f"out-{normalize}.context.jsonld" + gen = ContextGenerator(path, normalize_prefixes=normalize, emit_frame=True, embed_context_in_frame=embed) + gen.serialize(output=str(output)) + frame = json.loads(output.with_suffix(".frame.jsonld").read_text(encoding="utf-8")) + context = frame["@context"] if embed else json.loads(output.read_text(encoding="utf-8"))["@context"] + frames.append((frame, Context(context))) + (off, off_ctx), (on, on_ctx) = frames + assert on["@type"] == "prov:Activity" + assert on_ctx.expand(on["@type"]) == off_ctx.expand(off["@type"]) == PROV + "Activity" + assert on.keys() == off.keys() + + +@pytest.mark.parametrize("generator", [ContextGenerator, ShExGenerator]) +def test_yaml_text_input_with_local_imports(tmp_path, generator): + """A schema given as YAML text resolves its local imports against ``base_dir``, as without the option.""" + path = Path(_write(tmp_path, "prefixes_declared_in_an_import")) + from_text = generator(path.read_text(encoding="utf-8"), base_dir=str(tmp_path), normalize_prefixes=True) + assert from_text.serialize() == generator(str(path), normalize_prefixes=True).serialize() + + +def test_sssom_keeps_a_name_it_writes_undeclared(tmp_path): + """gen-sssom writes ``skos:exactMatch`` without declaring ``skos``, so a declared ``skosx`` for SKOS keeps + its name there, while documents that declare what they write use ``skos``.""" + (tmp_path / "schema.yaml").write_text( + _schema( + "skos", + "skosx: http://www.w3.org/2004/02/skos/core#\n", + """\ + classes: + Topic: + exact_mappings: [skosx:Concept] + """, + ), + encoding="utf-8", + ) + path = str(tmp_path / "schema.yaml") + sssom = DOCUMENTS["sssom"] + off, on = (sssom.produce(path, normalize, tmp_path) for normalize in (False, True)) + assert sssom.same(sssom.meaning(off), sssom.meaning(on)) + assert sssom.prefixes(on)["skosx"] == SKOS + assert _rdf_prefixes(OwlSchemaGenerator(path, normalize_prefixes=True).serialize(), "turtle")["skos"] == SKOS + + +def test_sssom_rewrites_only_curie_columns(tmp_path): + (tmp_path / "schema.yaml").write_text( + _schema( + "label", + "w3prov: http://www.w3.org/ns/prov#\n", + """\ + classes: + Agent: + title: "w3prov:Agent as used in the old feed" + class_uri: w3prov:Agent + exact_mappings: [w3prov:Person] + """, + ), + encoding="utf-8", + ) + _, rows = _sssom(_sssom_produce(str(tmp_path / "schema.yaml"), True, tmp_path)) + assert ["prov:Agent", "w3prov:Agent as used in the old feed", "skos:exactMatch", "", "prov:Person"] == rows[1][:5] + + +# ── The plan ────────────────────────────────────────────────────────── + + +@pytest.mark.parametrize("case", sorted(CASES)) +def test_plan(tmp_path, case): + """One plan per schema gives each namespace its name.""" + plan = preferred_prefixes(SchemaView(_write(tmp_path, case))) + assert {ns: plan.reverse_prefix_map[ns] for ns in CASES[case][1]} == CASES[case][1] + + +def test_plan_warns_when_a_prefix_keeps_its_name(tmp_path, caplog): + with caplog.at_level(logging.WARNING): + preferred_prefixes(SchemaView(_write(tmp_path, "http_and_https_schema_org"))) + assert "Prefix 'sdo' is not renamed to 'schema'" in caplog.text + assert "Prefix 'sdohttp' is not renamed" not in caplog.text + + +def test_plan_warns_when_a_name_is_taken_from_an_unused_import_prefix(tmp_path, caplog): + """LinkML keeps expanding the name in data as the import declares it, so the plan says so.""" + with caplog.at_level(logging.WARNING): + preferred_prefixes(SchemaView(_write(tmp_path, "schema_org_alias"))) + assert ( + f"Prefix 'schema' is written for <{SCHEMA_ORG}>. An import declares it for <{SCHEMA_ORG_HTTP}>" in caplog.text + ) + assert "is not renamed" not in caplog.text + + +@pytest.mark.parametrize("case", ["prov_alias", "imported_name_in_use", "import_uses_a_name"]) +def test_plan_is_silent_when_no_import_prefix_is_taken(tmp_path, caplog, case): + with caplog.at_level(logging.WARNING): + preferred_prefixes(SchemaView(_write(tmp_path, case))) + assert "An import declares it" not in caplog.text + + +def test_plan_warning_names_only_the_other_namespaces_of_the_name(tmp_path, caplog): + """One import declares ``schema`` for https://schema.org/ and linkml:types for http://schema.org/; + the name is written for the former, so the warning is about the latter alone.""" + (tmp_path / "module.yaml").write_text( + _schema("module", f"schema: {SCHEMA_ORG}\n", "", imports=""), encoding="utf-8" + ) + (tmp_path / "schema.yaml").write_text( + _schema("main", f"sdo: {SCHEMA_ORG}\n", "classes:\n Person: {}\n", imports="linkml:types module"), + encoding="utf-8", + ) + with caplog.at_level(logging.WARNING): + plan = preferred_prefixes(SchemaView(str(tmp_path / "schema.yaml"))) + assert plan.reverse_prefix_map[SCHEMA_ORG] == "schema" + assert f"An import declares it for <{SCHEMA_ORG_HTTP}>, which nothing uses" in caplog.text + + +def test_plan_is_silent_when_the_import_declares_the_name_for_the_same_namespace(tmp_path, caplog): + (tmp_path / "module.yaml").write_text(_schema("module", f"schema: {SCHEMA_ORG}\n", ""), encoding="utf-8") + (tmp_path / "schema.yaml").write_text( + _schema("main", f"sdo: {SCHEMA_ORG}\n", "classes:\n Person:\n class_uri: sdo:Person\n", imports="module"), + encoding="utf-8", + ) + with caplog.at_level(logging.WARNING): + plan = preferred_prefixes(SchemaView(str(tmp_path / "schema.yaml"))) + assert plan.reverse_prefix_map[SCHEMA_ORG] == "schema" + assert "An import declares it" not in caplog.text + + +UNDECLARED_DC = { + # nothing declares ``dc``: the import's slot gets its namespace from a default CURIE map + "module.yaml": textwrap.dedent( + """\ + id: https://example.org/module + name: module + prefixes: + ex: https://example.org/ + default_prefix: ex + default_curi_maps: [semweb_context] + types: + string: {uri: xsd:string, base: str} + uriorcurie: {uri: xsd:anyURI, base: URIorCURIE, repr: str} + slots: + title: {slot_uri: "dc:title", range: string} + classes: + Document: + slots: [title] + """ + ), + "schema.yaml": textwrap.dedent( + """\ + id: https://example.org/main + name: main + prefixes: + ex: https://example.org/ + dce: http://purl.org/dc/elements/1.1/ + default_prefix: ex + default_curi_maps: [semweb_context] + imports: [module] + classes: + Book: + is_a: Document + attributes: + id: {identifier: true, range: uriorcurie} + creator: {slot_uri: dce:creator, range: string} + """ + ), +} + + +@pytest.mark.parametrize("flags", [{}, {"exclude_imports": True}, {"exclude_imports": True, "mergeimports": False}]) +def test_a_prefix_written_undeclared_is_given_to_no_namespace(tmp_path, flags): + """A CURIE written with a prefix that nothing declares is read with a default CURIE map, here ``dc`` + as DCMI terms. The name is not given to DCMI elements, whose conventional name it is: the contexts + of the schema define it for one namespace at most, whichever flags they are generated with.""" + for name, text in UNDECLARED_DC.items(): + (tmp_path / name).write_text(text, encoding="utf-8") + path = str(tmp_path / "schema.yaml") + assert preferred_prefixes(SchemaView(path)).reverse_prefix_map[DC_ELEMENTS] == "dce" + defined = { + _context_prefixes(_context(path, normalize, **f)).get("dc") for normalize in (False, True) for f in ({}, flags) + } + assert defined - {None} == {DC_TERMS} + + +DIRECT_SCHEMA_ORG = _schema( + "direct", + "schema: https://schema.org/\n", + """\ + classes: + Person: + class_uri: schema:Person + attributes: + id: {identifier: true, range: uriorcurie} + """, +) + + +@pytest.mark.parametrize("generator", [ContextGenerator, ShExGenerator]) +def test_a_schema_that_declares_schema_for_https_does_not_load(tmp_path, generator): + """Why a schema writes ``sdo``: declaring ``schema`` for https://schema.org/ next to linkml:types, + which declares it for http://schema.org/, is refused by the generators built on SchemaLoader.""" + (tmp_path / "schema.yaml").write_text(DIRECT_SCHEMA_ORG, encoding="utf-8") + with pytest.raises(ValueError, match="Prefix: schema mismatch"): + generator(str(tmp_path / "schema.yaml")).serialize() + + +@pytest.mark.parametrize("generator", [OwlSchemaGenerator, ShaclGenerator]) +def test_a_schema_that_declares_schema_for_https_gets_the_imports_namespace(tmp_path, generator): + """... and gen-owl and gen-shacl read the name as the import declares it, so they write + http://schema.org/ IRIs for a class the schema puts in https://schema.org/.""" + (tmp_path / "schema.yaml").write_text(DIRECT_SCHEMA_ORG, encoding="utf-8") + graph = Graph().parse(data=generator(str(tmp_path / "schema.yaml")).serialize(), format="turtle") + iris = {str(term) for triple in graph for term in triple} + assert f"{SCHEMA_ORG_HTTP}Person" in iris + assert f"{SCHEMA_ORG}Person" not in iris + + +# How an import can use a prefix that only it declares, and what is no use of it. +IMPORT_USES = { + "class_uri": ("classes:\n Thing:\n class_uri: other:Thing\n", True), + "slot_uri": ("slots:\n part:\n slot_uri: other:part\n", True), + "attribute_slot_uri": ("classes:\n Thing:\n attributes:\n part: {slot_uri: other:part}\n", True), + "slot_usage_slot_uri": ( + "slots:\n part: {}\nclasses:\n Thing:\n slots: [part]\n" + " slot_usage:\n part: {slot_uri: other:part}\n", + True, + ), + "class_mapping": ("classes:\n Thing:\n exact_mappings: [other:Thing]\n", True), + "slot_mapping": ("slots:\n part:\n close_mappings: [other:part]\n", True), + "enum_uri": ("enums:\n Kind:\n enum_uri: other:Kind\n permissible_values:\n a: {}\n", True), + "permissible_value_meaning": ("enums:\n Kind:\n permissible_values:\n a: {meaning: other:a}\n", True), + "permissible_value_mapping": ( + "enums:\n Kind:\n permissible_values:\n a: {exact_mappings: [other:a]}\n", + True, + ), + "type_uri": ("types:\n Code:\n typeof: string\n uri: other:Code\n", True), + "id_prefixes": ("classes:\n Thing:\n id_prefixes: [other]\n", True), + "emit_prefixes": ("emit_prefixes: [other]\n", True), + "default_prefix": ("classes:\n Thing: {}\n", True), + "name_derived_iri": ("classes:\n Thing:\n class_uri: ex:Thing\n", True), + "attribute_mapping": ( + "classes:\n Thing:\n attributes:\n part: {exact_mappings: [other:part]}\n", + True, + ), + "enum_mapping": ("enums:\n Kind:\n exact_mappings: [other:Kind]\n permissible_values:\n a: {}\n", True), + "iri_instead_of_a_curie": ("classes:\n Thing:\n class_uri: http://example.org/other/Thing\n", True), + "curie_with_a_colon_in_its_local_part": ("slots:\n part:\n slot_uri: other:part:one\n", True), + "type_mapping": ( + "types:\n Code:\n typeof: string\n uri: xsd:string\n exact_mappings: [other:Code]\n", + False, + ), + "description": ("classes:\n Thing:\n description: see other:Thing\n", False), + "nothing": ("", False), +} + + +OTHER = "http://example.org/other/" + + +def _write_import_use(tmp_path: Path, use: str) -> str: + """Write a schema that imports a module using its prefix ``other`` as *use* says; return its path.""" + body, _ = IMPORT_USES[use] + default_prefix = "other" if use in {"default_prefix", "name_derived_iri"} else "ex" + (tmp_path / "module.yaml").write_text( + _schema("module", f"other: {OTHER}\nxsd: http://www.w3.org/2001/XMLSchema#\n", body, default_prefix), + encoding="utf-8", + ) + (tmp_path / "schema.yaml").write_text(_schema("main", "", "", imports="linkml:types module"), encoding="utf-8") + return str(tmp_path / "schema.yaml") + + +@pytest.mark.parametrize("use", sorted(IMPORT_USES)) +def test_a_prefix_only_an_import_declares_is_written_when_in_use(tmp_path, use): + """A prefix that only an import declares holds its name when an element is identified by, or + mapped to, an IRI of its namespace, or names the prefix; the mappings of a type are no use.""" + written = written_prefixes(SchemaView(_write_import_use(tmp_path, use))) + assert ("other" in written) is IMPORT_USES[use][1] + assert "schema" not in written + + +# The generators that write each kind of use; the others leave the namespace out. +IMPORT_USE_DOCUMENTS = { + "owl-ttl": lambda path: _iris( + Graph().parse(data=OwlSchemaGenerator(path, use_native_uris=False).serialize(), format="turtle") + ), + "owl-native": lambda path: _iris(Graph().parse(data=OwlSchemaGenerator(path).serialize(), format="turtle")), + "shacl-ttl": lambda path: _iris(Graph().parse(data=ShaclGenerator(path).serialize(), format="turtle")), + "shexc": lambda path: _iris(json.loads(as_json(parse_shexc(ShExGenerator(path).serialize())))), + "context": lambda path: set(_context_prefixes(_context(path, False)).values()) + | {str(iri) for iri in _expanded_terms(_context(path, False))}, +} + + +# Such a CURIE is no IRI of the namespace to these generators; gen-sparql writes it as it stands +# (test_sparql_keeps_what_the_prefixes_of_its_queries_denote). +@pytest.mark.parametrize("use", sorted(IMPORT_USES.keys() - {"curie_with_a_colon_in_its_local_part"})) +def test_the_generators_write_what_the_plan_counts_as_use(tmp_path, use): + """What the plan counts as a use of an import's prefix is what a generator writes: where it holds + the name some document has an IRI of the namespace, and where it frees the name none has.""" + path = _write_import_use(tmp_path, use) + writers = {name for name, iris in IMPORT_USE_DOCUMENTS.items() if any(i.startswith(OTHER) for i in iris(path))} + assert bool(writers) is IMPORT_USES[use][1], writers + + +@pytest.mark.parametrize("class_uri", ["prov:Provision", f"{NOT_PROV}Provision"], ids=["curie", "iri"]) +@pytest.mark.parametrize("imports", ["linkml:types module leaf", "linkml:types leaf module"]) +@pytest.mark.parametrize("using", ["module", "leaf"]) +def test_a_name_two_imports_declare_is_held_when_either_namespace_is_in_use(tmp_path, using, imports, class_uri): + """Imports may declare one name for two namespaces; which one LinkML reads depends on the import + order. The name is held whichever import uses its namespace and whichever is read first, by the + prefix of a CURIE or by an IRI of either namespace.""" + for name in ("module", "leaf"): + namespace = NOT_PROV if name == using else "http://example.org/unused/" + body = f"classes:\n Provision:\n class_uri: {class_uri}\n" if name == using else "" + module_imports = "linkml:types leaf" if name == "module" else "linkml:types" + (tmp_path / f"{name}.yaml").write_text( + _schema(name, f"prov: {namespace}\n", body, imports=module_imports), encoding="utf-8" + ) + (tmp_path / "schema.yaml").write_text( + _schema("holder", "w3prov: http://www.w3.org/ns/prov#\n", "", imports=imports), encoding="utf-8" + ) + sv = SchemaView(str(tmp_path / "schema.yaml")) + written = written_prefixes(sv) + assert "prov" in written + if class_uri.startswith(NOT_PROV): + assert written["prov"] == NOT_PROV + assert preferred_prefixes(sv).reverse_prefix_map[PROV] == "w3prov" + + +def test_plan_does_not_depend_on_what_the_view_expanded_before(tmp_path): + """SchemaView caches its namespaces, also when asked before the imports are loaded; the plan reads + the prefixes of the CURIEs as written and holds the import's name all the same.""" + path = _write(tmp_path, "import_uses_a_name") + fresh, used = SchemaView(path), SchemaView(path) + used.namespaces() + used.expand_curie("ex:anything") + assert written_prefixes(used) == written_prefixes(fresh) + assert preferred_prefixes(used).reverse_prefix_map[PROV] == "w3prov" + + +# Schemas whose SPARQL uses a prefix that the plan gives to another namespace, or that nothing declares. +SPARQL_KEEPS = { + # the local part has a colon, so the slot's IRI is not in the namespace the prefix is checked against + "colon_in_the_local_part": _schema( + "colon", + "sdo: https://schema.org/\n", + """\ + slots: + legacy: {slot_uri: "schema:legacy:name", range: string} + classes: + Person: + class_uri: sdo:Person + slots: [legacy] + """, + ), + # the schema declares ``schema`` itself, which the generator reads as the import declares it + "name_declared_for_two_namespaces": DIRECT_SCHEMA_ORG, + # nothing declares ``schema`` + "undeclared_prefix": textwrap.dedent( + """\ + id: https://example.org/undeclared + name: undeclared + default_prefix: ex + prefixes: + ex: https://example.org/ + sdo: https://schema.org/ + default_curi_maps: [semweb_context] + types: + string: {uri: xsd:string, base: str} + slots: + legacy: {slot_uri: "schema:legacy", range: string} + classes: + Person: + class_uri: sdo:Person + slots: [legacy] + """ + ), +} + + +def _sparql_names(text: str) -> dict[str, str | None]: + """Each prefix that the queries of a SPARQL file use, with the namespace the file declares for it.""" + declared = _prefix_lines(text) + used = set(re.findall(r"(? dict[str, list[str]]: + return {name: sorted(cls.attributes) for name, cls in view.all_classes().items()} + + inputs = [] + monkeypatch.setattr(sparqlgen, "deepcopy", lambda view: inputs.append(view) or copy.deepcopy(view)) + off, on = (SparqlGenerator(input_path(schema), normalize_prefixes=n) for n in (False, True)) + assert on.queries.keys() == off.queries.keys() + assert _sparql_names(on.serialize()) == _sparql_names(off.serialize()) + assert any(attributes(SchemaView(input_path(schema))).values()) + for view in inputs: + assert attributes(view) == attributes(SchemaView(input_path(schema))) + + +def _iris(meaning: Any) -> set[str]: + """Every IRI or expanded string in the meaning of a document.""" + if isinstance(meaning, Graph): + return {str(term) for triple in meaning for term in triple} + if isinstance(meaning, dict): + return set().union(*(_iris(key) | _iris(value) for key, value in meaning.items())) + if isinstance(meaning, list | tuple): + return set().union(*(_iris(value) for value in meaning)) + return {meaning} if isinstance(meaning, str) else set() + + +def _unwritten_namespaces(path: str) -> set[str]: + """The namespaces of the prefixes that only an import declares and the plan leaves out as unused.""" + sv = SchemaView(path) + written = set(written_prefixes(sv).values()) + return set(_schema_namespaces(path).values()) - written + + +ALL_DOCUMENTS = ( + DOCUMENTS + | RDF_VARIANTS + | { + "owl-type-objects": _rdf_document(OwlSchemaGenerator, "ttl", "turtle", type_objects=True), + "owl-no-mergeimports": _rdf_document(OwlSchemaGenerator, "ttl", "turtle", mergeimports=False), + } +) + + +@pytest.mark.parametrize("case", sorted(CASES)) +@pytest.mark.parametrize( + "document", [pytest.param(d, marks=pytest.mark.network) if d == "shex-rdf" else d for d in sorted(ALL_DOCUMENTS)] +) +def test_no_document_writes_a_namespace_the_plan_leaves_out(tmp_path, document, case): + """The plan treats a prefix that only an import declares as unused when no element is written with + its namespace; no generator then writes an IRI of that namespace, with or without the option.""" + doc = ALL_DOCUMENTS[document] + path = _write(tmp_path, case) + unwritten = _unwritten_namespaces(path) + assert SCHEMA_ORG_HTTP in unwritten or case in { + "http_and_https_schema_org", + "imported_name_in_use", + "conventional_http_schema_org", + } + for normalize in (False, True): + iris = _iris(doc.meaning(doc.produce(path, normalize, tmp_path))) + assert not {iri for iri in iris for namespace in unwritten if iri.startswith(namespace)} + + +@pytest.mark.parametrize("case", sorted(CASES)) +@pytest.mark.parametrize("flags", sorted(CONTEXT_FLAGS)) +def test_no_context_defines_a_namespace_the_plan_leaves_out(tmp_path, flags, case): + path = _write(tmp_path, case) + unwritten = _unwritten_namespaces(path) + for normalize in (False, True): + context = _context(path, normalize, **CONTEXT_FLAGS[flags]) + iris = set(_context_prefixes(context).values()) | {str(iri) for iri in _expanded_terms(context)} + assert not {iri for iri in iris for namespace in unwritten if iri.startswith(namespace)} + + +@pytest.mark.parametrize("base_normalized", [False, True]) +@pytest.mark.parametrize("exclude", ["exclude_imports", "exclude_external_imports"]) +def test_context_of_an_importer_leaves_a_name_its_import_uses(tmp_path, base_normalized, exclude): + """An import's context defines ``prov`` for the namespace its class is in. The importer's context, + without the imported elements, does not redefine it, so data that names both contexts and writes + ``prov:`` keeps its meaning.""" + module, schema = (str(tmp_path / name) for name in ("module.yaml", "schema.yaml")) + _write(tmp_path, "import_uses_a_name") + expected_contexts = [_context(module, False), _context(schema, False, **{exclude: True})] + contexts = [_context(module, base_normalized), _context(schema, True, **{exclude: True})] + assert _context_prefixes(contexts[0])["prov"] == NOT_PROV + assert _context_prefixes(contexts[1]).get("prov", NOT_PROV) == NOT_PROV + terms = _terms(expected_contexts[0]) | _terms(expected_contexts[1]) + declared = sorted(_context_prefixes(expected_contexts[0]) | _context_prefixes(expected_contexts[1])) + expected = _data_graph(expected_contexts, terms, declared, []) + assert isomorphic(_data_graph(contexts, terms, declared, []), expected) + + +@pytest.mark.parametrize( + "prefixes,taken,expected", + [ + ({"sdo": SCHEMA_ORG}, set(), {"sdo": "schema"}), + ({"schema": SCHEMA_ORG}, set(), {}), + ({"ex": "http://example.org/"}, set(), {}), + ({"sdo": SCHEMA_ORG, "schema": SCHEMA_ORG}, set(), {"sdo": "schema"}), + ({"sdo": SCHEMA_ORG, "sdohttp": SCHEMA_ORG_HTTP}, set(), {}), + ({"sdo": SCHEMA_ORG, "schema": "http://example.org/s/"}, set(), {}), + ({"sdo": SCHEMA_ORG}, {"schema"}, {}), + ({"dc": DC_TERMS, "dce": DC_ELEMENTS}, set(), {"dc": "dcterms"}), + ({"FOAF": FOAF}, set(), {"FOAF": "foaf"}), + ], + ids=[ + "alias", + "conventional", + "unknown", + "alias_and_conventional", + "two_namespaces", + "name_declared_elsewhere", + "name_taken", + "declared_name_stays", + "case_only", + ], +) +def test_prefix_renames(prefixes, taken, expected): + assert prefix_renames(prefixes, taken) == expected + + +def test_document_prefixes_rename_only_declared_names(): + """A document renames the prefixes it declares, not one that only the plan knows.""" + plan = Converter( + [ + Record(prefix="schema", uri_prefix=SCHEMA_ORG, prefix_synonyms=["sdo"]), + Record(prefix="bibo", uri_prefix=BIBO, prefix_synonyms=["mybibo"]), + ] + ) + document = document_prefixes({"sdo": SCHEMA_ORG, "ex": "http://example.org/"}, plan) + assert {r.prefix: r.uri_prefix for r in document.records} == {"schema": SCHEMA_ORG, "ex": "http://example.org/"} + assert document.standardize_prefix("mybibo") is None + + +@pytest.mark.parametrize( + "declared,used,expected", + [ + # the alias is in use and still renamed: the name it gets is free + ({"sdo": SCHEMA_ORG}, {"sdo"}, {"schema": SCHEMA_ORG}), + # the document writes ``schema`` without declaring it: no namespace gets that name + ({"sdo": SCHEMA_ORG}, {"sdo", "schema"}, {"sdo": SCHEMA_ORG}), + # the document declares and writes ``schema`` for another namespace: both keep their names + ( + {"sdo": SCHEMA_ORG, "schema": SCHEMA_ORG_HTTP}, + {"sdo", "schema"}, + {"sdo": SCHEMA_ORG, "schema": SCHEMA_ORG_HTTP}, + ), + # it declares ``schema`` for another namespace without writing it: the declaration goes + ({"sdo": SCHEMA_ORG, "schema": SCHEMA_ORG_HTTP}, {"sdo"}, {"schema": SCHEMA_ORG}), + ], + ids=["alias_in_use", "name_written_undeclared", "name_in_use_for_another_namespace", "name_declared_unused"], +) +def test_document_prefixes_keep_what_the_used_prefixes_denote(declared, used, expected): + plan = Converter([Record(prefix="schema", uri_prefix=SCHEMA_ORG, prefix_synonyms=["sdo"])]) + document = document_prefixes(declared, plan, used) + assert {r.prefix: r.uri_prefix for r in document.records} == expected + for prefix in used & declared.keys(): + assert document.expand(f"{prefix}:x") == declared[prefix] + "x" + + +def test_standardize_prefix_leaves_absolute_iris_alone(): + """A suffix starting with ``//`` makes a value an absolute IRI, not a compact one (JSON-LD 1.1 §4.1.5).""" + converter = Converter([Record(prefix="web", uri_prefix="http://example.org/", prefix_synonyms=["http"])]) + assert standardize_prefix("http://schema.org/Person", converter) == "http://schema.org/Person" + assert standardize_prefix("http:Person", converter) == "web:Person" + + +def test_added_preferred_prefixes_skip_terms_and_other_namespaces(): + plan = Converter([Record(prefix="schema", uri_prefix=SCHEMA_ORG, prefix_synonyms=["sdo"])]) + assert not added_preferred_prefixes(plan, {"sdo": SCHEMA_ORG}, terms={"schema"}).records + assert not added_preferred_prefixes(plan, {"sdo": SCHEMA_ORG, "schema": SCHEMA_ORG_HTTP}).records + + +# ── Where the names come from ───────────────────────────────────────── + + +@pytest.mark.parametrize( + "namespace,expected", + [ + ("http://www.opengis.net/ont/geosparql#", "geo"), + (SCHEMA_ORG, "schema"), + ("https://www.w3.org/2003/01/geo/wgs84_pos#", "wgs"), + (SCHEMA_ORG_HTTP, "schema"), + ("http://www.w3.org/ns/shacl#", "sh"), + (DC_ELEMENTS, "dc"), + ], +) +def test_well_known_prefix_map_names(namespace, expected): + assert well_known_prefix_map()[namespace] == expected + + +def test_well_known_prefix_map_uses_curated_sources_only(): + """The names are those of prefixmaps' curated ``linked_data`` context and of rdflib; no name of + another registry, such as prefix.cc's ``ex`` for http://example.org/.""" + curated = {rec.namespace: rec.prefix for rec in load_context("linked_data").prefix_expansions} + rdflib_map = {str(ns): str(pfx) for pfx, ns in Graph(bind_namespaces="rdflib").namespaces() if str(pfx)} + wk = well_known_prefix_map() + assert wk == {**curated, **rdflib_map} + assert "http://example.org/" not in wk + + +def test_well_known_prefix_map_returns_a_copy(): + well_known_prefix_map()["http://never-in-any-real-prefix-map.test/"] = "test" + assert "http://never-in-any-real-prefix-map.test/" not in well_known_prefix_map() + + +# ── Command line ────────────────────────────────────────────────────── + +OFFERING = ["owl", "shacl", "shex", "sparql", "yarrrml", "sssom", "jsonld-context"] + + +@pytest.mark.parametrize("command", OFFERING) +def test_cli_applies_the_option(command, tmp_path): + path = _write(tmp_path, "prov_alias") + out = tmp_path / "out" + extra = {"sssom": ["-o", str(out)], "sparql": ["-d", str(out)]}.get(command, []) + runner = CliRunner() + outputs = {} + for flag in ("--normalize-prefixes", "--no-normalize-prefixes", None): + args = ["generate", command, *extra, *([flag] if flag else []), path] + result = runner.invoke(linkml_cli, args) + assert result.exit_code == 0, result.output + if command == "sssom": + outputs[flag] = out.read_text(encoding="utf-8") + elif command == "sparql": + outputs[flag] = "".join(f.read_text(encoding="utf-8") for f in sorted(out.glob("*.rq"))) + else: + outputs[flag] = result.output + assert outputs["--no-normalize-prefixes"] == outputs[None] + assert re.search(r"\bprov:\w", outputs["--normalize-prefixes"]) + assert not re.search(r"\bprov:\w", outputs[None]) + + +@pytest.mark.parametrize("command", ["prefix-map", "rdf", "jsonld", "python", "pydantic", "json-schema"]) +def test_cli_rejects_the_option_where_names_are_data_or_api(command, tmp_path): + """A prefix map gives each namespace one name, the schema's; gen-rdf and gen-jsonld write CURIEs as + literal data; gen-python and gen-pydantic make prefix names part of the generated code; gen-json-schema + writes none.""" + path = _write(tmp_path, "prov_alias") + result = CliRunner().invoke(linkml_cli, ["generate", command, "--normalize-prefixes", path]) + assert result.exit_code == 2 and "No such option" in result.output