Skip to content

Code with your vocabulary

Kay Kim edited this page Jul 1, 2023 · 7 revisions

This is an Archived Page: The following content has been ported from solidproject.org


In this part of the tutorial, we will build on the quickstart version of the Obelisk vocabulary to develop a simple JavaScript application that answers the question: "Is this person an obelisk sculptor ?"

The artifact generator

The last thing we want developers to do is memorize and type IRIs all over their code. That's why some libraries (such as rdf-namespaces) define constants giving easy access to the terms from common vocabularies:

import { rdf } from 'rdf-namespaces';

// The value of iri will be http://www.w3.org/1999/02/22-rdf-syntax-ns#type.
const iri = rdf.type;

However, an issue with these libraries is that, by their nature, they are limited to just the common, well-known vocabularies. What about your specific vocabulary, designed for your app? How can you make the terms described in that vocabulary easily reusable in your code (and easily reusable in the code of others who may wish to reuse the terms you've defined in your cool vocabulary)?

The ArtifactGenerator is a tool (currently available as Alpha) that takes any RDF vocabulary as input and can generate multiple source-code bundles (e.g., Java JAR, JavaScript NPM module, etc.) that you can then use in your application. Let's see how it works.

Generate the artifact

To generate a JavaScript artifact with the default options, you can use the following command: npx @inrupt/artifact-generator generate https://solidproject.org/assets/misc/tutorials/quickstart-obelisk.ttl --noprompt.

You can specify a local vocabulary file rather than an IRI if your vocabulary is not yet available online.

Use the artifact

In your application (TODO: provide git repo with demo app), require the generated output and use your vocabulary terms directly:

const OBELISK = require("/path/to/generated/artifact/OBELISK")

// The value of sculptorIri will be http://w3id.org/obelisk/Sculptor
const sculptorIri = OBELISK.Sculptor.value;
// The value of sculptorDefaultLabel will be Sculptor
const sculptorDefaultLabel = OBELISK.Sculptor.labelInLang()

If you use an IDE, at this point you have auto-completion on all the terms of your vocabulary.

From there, we can build a simple app that tells you, based on a person's WebID, if that person is an obelisk sculptor or not. Note that the generated artifact is:

  • imported on line 1,
  • used on line 16 to get an IRI (OBELISK.Sculptor.value),
  • used on line 20 to get a label (OBELISK.Sculptor.labelInLang()).
<iframe src="https://codesandbox.io/embed/lit-using-generated-artifact-8vfyf?fontsize=14" title="lit-using-generated-artifact" allow="geolocation; microphone; camera; midi; vr; accelerometer; gyroscope; payment; ambient-light-sensor; encrypted-media; usb" style="width:100%; height:500px; border:0; border-radius: 4px; overflow:hidden;" sandbox="allow-modals allow-forms allow-popups allow-scripts allow-same-origin"></iframe>

If you want to be recognised as an obelisk sculptor, you can uncomment line 35. You will have to add this app to your authorized apps to be able to write into your Pod.

Congratulations, you are now familiar with what's in a vocabulary, and saw how to code using one.

Next step: find out the well-known vocabularies

Clone this wiki locally