IF Commons GitHub

rdf-schema

Written to: 1 requirement: 1 proved, 0 partly, 0 failing, 0 with no test recorded. 0001-rdf FR-003

Class schemas and the individuals they make (commons 0012-ontology FR-003 to FR-005). A schema names a class and its fields, each a property, a range and a cardinality; the type of an individual's values is derived from it, so a required field left out, a value of the wrong range or a reference to an individual of another class does not compile. An individual is a named node of its class, used wherever a reference to that class is asked for, and its triples are its type and then its fields in the schema's order. The schemas an ontology states about itself (OWL's ontology, class and properties), code lists keyed by local name and SKOS concept schemes keyed by notation are here too, and validate names what a type cannot state.

import { classSchema, many, one, range } from "./rdf-schema.ts";
import { en, namespace, term, vocabulary } from "./rdf-term.ts";
import { rdfs } from "./rdf-vocabularies.ts";
const EX = namespace("ex", "https://example.org/terms#");
const ex = vocabulary(EX, { Shelf: "owl:Class", Book: "owl:Class", holds: "rdf:Property" });
const Book = classSchema(ex.Book, { label: one(rdfs.label, range.langString) });
const Shelf = classSchema(ex.Shelf, { holds: many(ex.holds, range.ref<"ex:Book">()) });
const iliad = Book.make(term(EX, "iliad"), { label: en("The Iliad") });
Shelf.make(term(EX, "top"), { holds: [iliad] }); // a Shelf given where a Book is asked for does not compile

Source: commons/rdf/src/rdf-schema.ts

17 of 33 exports are imported by a test of the run. A test that imports an export may not call it or check what it returns.

RangeKindtypeno test imports it
type RangeKind = "string" | "lang" | "text" | "boolean" | "integer" | "ref";

What a field's values may be: a plain string, a string in a language, either, a boolean, an integer, or a reference.

Rangetypeno test imports it
type Range<V> = { readonly kind: RangeKind; readonly words?: readonly string[]; readonly "@ifc/rdf-value": V };

A field's range, with the TypeScript type V of its values for the compiler.

rangeconst
const range: { readonly string: Range<string>; readonly langString: Range<LangString>; readonly text: Range<string | LangString>; readonly boolean: Range<boolean>; readonly integer: Range<number>; readonly ref: <C extends string>() => Range<NamedOf<C>>; readonly oneOf: <const W extends readonly [string, ...string[]]>(words: W) => Range<W[number]>; }

The ranges of a field. ref<"a" | "b">() accepts an individual of either class.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

Cardinalitytypeno test imports it
type Cardinality = "one" | "optional" | "many";

How many values a field holds: exactly one, at most one, or any number in order.

Fieldtypeno test imports it
type Field<V, K extends Cardinality> = { readonly property: Named; readonly range: Range<V>; readonly cardinality: K };

A field of a schema: its property, its range and its cardinality.

onefunction
function one<V>(property: Named, r: Range<V>): Field<V, "one">

A field holding exactly one value.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

optionalfunction
function optional<V>(property: Named, r: Range<V>): Field<V, "optional">

A field holding at most one value.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

manyfunction
function many<V>(property: Named, r: Range<V>): Field<V, "many">

A field holding any number of values, in the order given.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

Fieldstypeno test imports it
type Fields = Readonly<Record<string, Field<unknown, Cardinality>>>;

The fields of a schema, by the names its individuals' values are written with.

ValuesOftypeno test imports it
type ValuesOf<S extends Fields> = { readonly [N in Keys<S, "one">]: ValueOf<S[N]> } & {
  readonly [N in Keys<S, "optional">]?: ValueOf<S[N]>;
} & { readonly [N in Keys<S, "many">]?: readonly ValueOf<S[N]>[] };

The values of an individual of a schema with the fields S: a required field's value, an optional one's, a list for many.

Statementtypeno test imports it
type Statement = { readonly predicate: Named; readonly objects: readonly Value[]; readonly words?: readonly string[] };

One predicate of an individual and its values in order: what its triples are made from.

Individualtype
type Individual<C extends string, V> = Named<C> & {
  readonly class: Named;
  readonly values: V;
  readonly statements: readonly Statement[];
};

An individual: a named node of its class C, with the class, its values V and the statements they make (its type first).

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

AnyIndividualtypeno test imports it
type AnyIndividual = Named & { readonly class: Named; readonly statements: readonly Statement[] };

An individual of any class, as a writer or a check takes it.

ClassSchematypeno test imports it
type ClassSchema<C extends string, S extends Fields> = { /** The class. */ readonly class: Named; /** The class's … (417 characters)
type ClassSchema<C extends string, S extends Fields> = {
  /** The class. */
  readonly class: Named;
  /** The class's CURIE, the brand of its individuals. */
  readonly curie: C;
  /** The fields, in the order their triples are made. */
  readonly fields: S;
  /** An individual of the class: the subject and its values. */
  readonly make: (subject: Named<C>, values: ValuesOf<S>) => Individual<C, ValuesOf<S>>;
};

A class schema: the class, its CURIE, its fields, and how an individual of it is made.

classSchemafunction
function classSchema<const C extends string, const S extends Fields>(cls: VocabularyTerm<never, C>, fields: S): ClassSchema<C, S>

The schema of a class (a term of a vocabulary, whose CURIE brands the individuals) with its fields.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

OwlOntologyconst
const OwlOntology: ClassSchema<"owl:Ontology", { readonly label: Field<LangString, "optional">; readonly comment: Field<LangString, "optional">; readonly description: Field<LangString, "optional">; readonly versionInfo: Field<string, "optional">; readonly versionIri: Field<Named<"owl:Ontology">, "optional">; }>

The schema of an ontology's own description: owl:Ontology with its label, description and version.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

OwlClassconst
const OwlClass: ClassSchema<"owl:Class", { readonly label: Field<LangString, "optional">; readonly comment: Field<LangString, "optional">; readonly subClassOf: Field<Named<"owl:Class"> | Named<"rdfs:Class">, "many">; readonly definition: Field<LangString, "optional">; }>

The schema of a class: its label, comment, SKOS definition and superclasses.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

ObjectPropertyconst
const ObjectProperty: ClassSchema<"owl:ObjectProperty", { readonly label: Field<LangString, "optional">; readonly commen… (413 characters)
const ObjectProperty: ClassSchema<"owl:ObjectProperty", { readonly label: Field<LangString, "optional">; readonly comment: Field<LangString, "optional">; readonly domain: Field<Named<"owl:Class"> | Named<"rdfs:Class"> | Named<"rdfs:Datatype">, "optional">; readonly range: Field<Named<"owl:Class"> | Named<"rdfs:Class"> | Named<"rdfs:Datatype">, "optional">; readonly definition: Field<LangString, "optional">; }>

The schema of an object property: its label, comment, domain, range and SKOS definition.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

DatatypePropertyconst
const DatatypeProperty: ClassSchema<"owl:DatatypeProperty", { readonly label: Field<LangString, "optional">; readonly co… (417 characters)
const DatatypeProperty: ClassSchema<"owl:DatatypeProperty", { readonly label: Field<LangString, "optional">; readonly comment: Field<LangString, "optional">; readonly domain: Field<Named<"owl:Class"> | Named<"rdfs:Class"> | Named<"rdfs:Datatype">, "optional">; readonly range: Field<Named<"owl:Class"> | Named<"rdfs:Class"> | Named<"rdfs:Datatype">, "optional">; readonly definition: Field<LangString, "optional">; }>

The schema of a datatype property: its label, comment, domain, range and SKOS definition.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

ontologyOffunction
function ontologyOf(ns: Namespace): Named<"owl:Ontology">

The named node of the ontology a namespace's terms belong to (https://example.org/terms for https://example.org/terms#).

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

CodeListtypeno test imports it
type CodeList<C extends string, V, K extends string> = { readonly [N in K]: Individual<C, V> };

A code list: individuals of one class keyed by their local names, whose keys' union is the type code branches on.

codesfunction
function codes<C extends string, S extends Fields, const E extends Readonly<Record<string, ValuesOf<S>>>>(schema: ClassSchema<C, S>, ns: Namespace, entries: E): CodeList<C, ValuesOf<S>, keyof E & string>

The code list of a schema's class in a namespace: each key is a local name, each value the individual's values.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

codeOffunction
function codeOf<K extends string>(list: { readonly [N in K]: Named; }, node: Named): K | undefined

The key of a code list that names an individual, or nothing where none does.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

ConceptSchemeSchemaconstno test imports it
const ConceptSchemeSchema: ClassSchema<"skos:ConceptScheme", { readonly prefLabel: Field<LangString, "one">; readonly definition: Field<LangString, "optional">; }>

The schema of a SKOS concept scheme: its label and definition.

ConceptSchemaconstno test imports it
const ConceptSchema: ClassSchema<"skos:Concept", { readonly inScheme: Field<Named<"skos:ConceptScheme">, "one">; readonly notation: Field<string, "one">; readonly prefLabel: Field<LangString, "one">; readonly definition: Field<LangString, "optional">; }>

The schema of a SKOS concept: its scheme, notation, label and definition.

ConceptEntrytypeno test imports it
type ConceptEntry = { readonly local: string; readonly label: LangString; readonly definition?: LangString };

What one concept of a scheme says beside its notation: its local name in the namespace, its label and its definition.

ConceptSchemetypeno test imports it
type ConceptScheme<N extends string> = { /** The scheme itself. */ readonly scheme: Individual<"skos:ConceptScheme",… (414 characters)
type ConceptScheme<N extends string> = {
  /** The scheme itself. */
  readonly scheme: Individual<"skos:ConceptScheme", ValuesOf<typeof ConceptSchemeSchema.fields>>;
  /** Its concepts, by notation. */
  readonly concepts: { readonly [K in N]: Individual<"skos:Concept", ValuesOf<typeof ConceptSchema.fields>> };
  /** The scheme and its concepts, in order. */
  readonly individuals: readonly AnyIndividual[];
};

A SKOS concept scheme: the scheme, its concepts by notation, and every individual it states.

conceptSchemefunction
function conceptScheme<const E extends Readonly<Record<string, ConceptEntry>>>(ns: Namespace, local: string, head: { readonly label: LangString; readonly definition?: LangString | undefined; }, entries: E): ConceptScheme<keyof E & string>

A SKOS concept scheme in a namespace, its concepts keyed by their notations, whose union is the type of a notation.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

OntologyFiletypeno test imports it
type OntologyFile = {
  readonly name: string;
  readonly prefixes: readonly Namespace[];
  readonly individuals: readonly AnyIndividual[];
};

One file of an ontology: its name, the prefixes its Turtle may write, and the individuals it states, in order.

triplesOffunction
function triplesOf(individuals: readonly AnyIndividual[]): readonly Triple[]

The triples of individuals: each individual's statements in order, each triple once, where first stated.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

RdfFindingtypeno test imports it
type RdfFinding =
  | { readonly kind: "not-an-integer"; readonly subject: Iri; readonly predicate: Iri; readonly value: number }
  | {
      readonly kind: "not-one-of";
      readonly subject: Iri;
      readonly predicate: Iri;
      readonly value: string;
      readonly words: readonly string[];
    }
  | { readonly kind: "stated-twice"; readonly subject: Iri };

What a type cannot state about individuals (or what a run without the type checker lets through): an integer that is not a safe integer, a word not of its field's list, or a subject stated twice.

formatRdfFindingfunction
function formatRdfFinding(f: RdfFinding): string

The sentence a finding is told with.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

validatefunction
function validate(individuals: readonly AnyIndividual[]): readonly RdfFinding[]

Every finding about the individuals, in the order they are stated.

Imported by 10 tests of the run (10 passed, 0 failed): the library's results

Titles, headings, exports, tables, columns and requirements. Esc closes.