Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate JSON-LD context from a DefinedNamespace #1706

Merged
merged 8 commits into from Apr 4, 2022

Conversation

nicholascar
Copy link
Member

@nicholascar nicholascar commented Feb 7, 2022

Closes #1705

Proposed Changes

  • adds a jsonld_context() function to the DefinedNamespace class
  • consolidates DefinedNamespace testing files
  • addes a test for jsonld_context() function

Notes for reviewers: I don't really get what the purpose of the DefinedNamespaceMeta class is v. the DefinedNamespace class. There is some funky Python going on there that is beyond me. Is it best to have added this function to DefinedNamespaceMeta, rather than DefinedNamespace? It still appears for use with DefinedNamespace and that's where all the 'DefinedNamespace functions seem to be...

@nicholascar nicholascar changed the title Add jsonld_context() function to DefinedNamespace Generate JSON-LD context from a DefinedNamespace Feb 7, 2022
@niklasl
Copy link
Member

niklasl commented Feb 7, 2022

I'm curious about the use case here (especially why just using @vocab doesn't cover it)?

@nicholascar
Copy link
Member Author

the use case

See the profile resources for GeoSPARQL 1.1: https://github.com/opengeospatial/ogc-geosparql/tree/master/1.1. It includes context/geo-context.json which is a JSON-LD context file for all the GeoSAPRQL elements.

I want this function to enable DefinedNamespace instance to be able to produce context listing of all their content not purely for use in JSON-LD data - as you say, @vocab can do that but also to allow communication of a namespace's content via a context object. We can point programmers at the GeoSPARQL context (and the Simple Features context there too) and say "use all the terms there".

I find that for new ontologies, I'm making DefinedNamespace instances for simple Python use of the ontology content and I use the defined_namespace_creator.py script for that. With this addition, I'll be able to crank out JSON-LD contexts too.

@niklasl
Copy link
Member

niklasl commented Feb 7, 2022

I can see that this means of providing a hint of what properties are available may be useful. It is somewhat limited (as it provides no hint about what values to expect for instance), and I'd suspect users to either need the full ontology, or some (ideally generated) JSON schema du jour, depending on further implementation needs.

I'm not sure that it fits as a method on a core RDFLib class though? I wouldn't expect users of these instances to need a method for it? Also, it does in principle collide with a vocabulary property of the same name (quite unlikely of course, but conceivable).

I believe this function may fit better in some utility or tool. Perhaps in rdflib/tools alongside the script you referenced. On that topic, you may want to check out this old vocabulary to JSON-LD context generator made for schema.org quite some time ago.

Copy link
Member

@aucampia aucampia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good to me.

@aucampia
Copy link
Member

aucampia commented Feb 7, 2022

To me this seems like a useful thing to be able to do from a namespace, I think there is of course more that can be done from an ontology but I still think this does not really go way outside of purpose of RDFLib. It is maybe not that difficult to implement on top of RDFLib but it is also not a significant liability to maintain in RDFLib either.

Some utilities to take any RDF graph and/or OWL graph and generate a context for a prefix may also be useful, but I think even so this makes sense.

@niklasl
Copy link
Member

niklasl commented Feb 7, 2022

If you intend to keep it as a method rather than a utility function, I'd probably recommend making it "protected" with a leading underscore to lesser the collision risk, and possibly just returning an unserialized dict for the user to serialize in any preferred form. A bit like namedtuple._asdict.

@aucampia
Copy link
Member

aucampia commented Feb 7, 2022

Also, it does in principle collide with a vocabulary property of the same name (quite unlikely of course, but conceivable).

Actually, maybe better as a free function inside rdflib.namespace yes.

nicholascar and others added 3 commits February 11, 2022 10:50
Co-authored-by: Iwan Aucamp <aucampia@gmail.com>
Co-authored-by: Iwan Aucamp <aucampia@gmail.com>
Co-authored-by: Iwan Aucamp <aucampia@gmail.com>
@@ -239,6 +239,16 @@ def __dir__(cls) -> Iterable[str]:
values = {cls[str(x)] for x in cls.__annotations__}
return values

def jsonld_context(self, pfx: str) -> str:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still would collide with any jsonld_context term in a namespace. It should thus at least be renamed or moved to a utility function. It also makes sense to let it return a dict, to allow the user of the function to decide serialization form (indent, sorting, etc.).

@nicholascar
Copy link
Member Author

I've change the function name to as_jsonld_context but left it part of DefinedNamespace and not made it protected as to move it out or to leading underscore it will make it less visible as an option on the class in IDEs, which is where I want it to appear.

@aucampia aucampia self-requested a review April 2, 2022 12:26
@aucampia
Copy link
Member

aucampia commented Apr 2, 2022

Will try do a review this weekend and fix tests if they still fail.

Method name changed and it now returns a dict instead of a JSON string.
Copy link
Member

@aucampia aucampia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@nicholascar nicholascar merged commit adca592 into master Apr 4, 2022
@nicholascar nicholascar deleted the definednamespace_jsonld branch April 4, 2022 22:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide a function to auto-generate a JSON-LD context from a Closed/DefinedNamespace
4 participants