RangeKindtypeno test imports ittype 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.
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 compileRangeKindtypeno test imports ittype 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 ittype 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.
rangeconstconst 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.
Cardinalitytypeno test imports ittype Cardinality = "one" | "optional" | "many";How many values a field holds: exactly one, at most one, or any number in order.
Fieldtypeno test imports ittype 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.
onefunctionfunction one<V>(property: Named, r: Range<V>): Field<V, "one">A field holding exactly one value.
optionalfunctionfunction optional<V>(property: Named, r: Range<V>): Field<V, "optional">A field holding at most one value.
manyfunctionfunction many<V>(property: Named, r: Range<V>): Field<V, "many">A field holding any number of values, in the order given.
Fieldstypeno test imports ittype Fields = Readonly<Record<string, Field<unknown, Cardinality>>>;The fields of a schema, by the names its individuals' values are written with.
ValuesOftypeno test imports ittype 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 ittype 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.
Individualtypetype 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).
AnyIndividualtypeno test imports ittype 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 ittype 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.
classSchemafunctionfunction 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.
OwlOntologyconstconst 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.
OwlClassconstconst 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.
ObjectPropertyconstconst 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.
DatatypePropertyconstconst 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.
ontologyOffunctionfunction 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#).
CodeListtypeno test imports ittype 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.
codesfunctionfunction 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.
codeOffunctionfunction codeOf<K extends string>(list: { readonly [N in K]: Named; }, node: Named): K | undefinedThe key of a code list that names an individual, or nothing where none does.
ConceptSchemeSchemaconstno test imports itconst 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 itconst 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 ittype 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 ittype 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.
conceptSchemefunctionfunction 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.
OntologyFiletypeno test imports ittype 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.
triplesOffunctionfunction triplesOf(individuals: readonly AnyIndividual[]): readonly Triple[]The triples of individuals: each individual's statements in order, each triple once, where first stated.
RdfFindingtypeno test imports ittype 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.
formatRdfFindingfunctionfunction formatRdfFinding(f: RdfFinding): stringThe sentence a finding is told with.
validatefunctionfunction validate(individuals: readonly AnyIndividual[]): readonly RdfFinding[]Every finding about the individuals, in the order they are stated.