Documentation
¶
Overview ¶
Package ontology is the type system a graph is read through: classes, properties, the domains and ranges that connect them, and the subclass hierarchy over both. It infers a schema from observed data, checks data against a schema, and reads and writes the interchange formats — JSON-LD, JSON Schema, Turtle and N-Triples.
Index ¶
- Constants
- Variables
- func Camel(name string) string
- func CheckClass(name string) (suggestion string, ok bool)
- func CheckProperty(name string, kind Kind) (suggestion string, ok bool)
- func CheckSchema(name string) (suggestion string, ok bool)
- func ClassName(name string) string
- func Count(n int) *int
- func Cycles(classes []Class) []string
- func Pascal(name string) string
- func PropertyName(name string) string
- func SchemaName(name string) string
- func Singular(name string) string
- func Title(name string) string
- func XSD(v any) string
- type Class
- type Entity
- type Graph
- type Inference
- type Kind
- type Link
- type Namespace
- func (n Namespace) All() map[string]string
- func (n *Namespace) Bind(prefix, iri string)
- func (n Namespace) Class(name string) string
- func (n Namespace) Individual(name string) string
- func (n Namespace) Property(name string) string
- func (n Namespace) Resolve(prefix string) (string, bool)
- func (n Namespace) Root() string
- type Property
- type Schema
- func (s Schema) Ancestors(name string) []string
- func (s Schema) Class(name string) (Class, bool)
- func (s Schema) JSONLD() ([]byte, error)
- func (s Schema) NTriples() string
- func (s Schema) Normalize() Schema
- func (s Schema) Property(name string) (Property, bool)
- func (s Schema) TermIRI(name string) string
- func (s Schema) Turtle() string
- type Statement
Constants ¶
const DefaultBase = "https://semantica.dev/ontology/"
DefaultBase is the namespace terms are minted in when none is given.
Variables ¶
var ErrCollision = errors.New("name collision")
ErrCollision reports two source names that normalize to one term, which would silently merge two different things into one.
var Standard = map[string]string{
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"owl": "http://www.w3.org/2002/07/owl#",
"xsd": "http://www.w3.org/2001/XMLSchema#",
"skos": "http://www.w3.org/2004/02/skos/core#",
"sh": "http://www.w3.org/ns/shacl#",
"dc": "http://purl.org/dc/elements/1.1/",
"dcterms": "http://purl.org/dc/terms/",
}
Standard are the prefixes every RDF document may assume.
Functions ¶
func Camel ¶
Camel is Pascal with the first word left lower: "has name" is "hasName". A name already in camelCase is left alone, so normalizing is idempotent and "worksFor" does not become "worksfor".
func CheckClass ¶
CheckClass reports whether a class name follows the conventions, and what it would be if it did not.
func CheckProperty ¶
CheckProperty reports whether a property name follows the conventions for its kind, and what it would be if it did not. An object property reads as a verb phrase; a data property only has to be lower or camel case.
func CheckSchema ¶
CheckSchema reports whether an ontology name follows the conventions, and what it would be if it did not.
func Count ¶
Count returns a pointer to n, for setting Min and Max inline. A nil Min or Max leaves that end of the count open, which is not the same as a count of zero.
func Cycles ¶
Cycles lists the classes that reach themselves through the hierarchy. A cycle makes the subclass relation meaningless, so it is reported rather than followed.
func Pascal ¶
Pascal joins the words of name with each capitalised: "my class" is "MyClass", and so is "MY_CLASS".
func PropertyName ¶
PropertyName is name written the way a property is written: camelCase.
func SchemaName ¶
SchemaName is name written the way an ontology is written: title case with an "Ontology" suffix.
func Singular ¶
Singular strips a regular English plural ending. It knows nothing of irregular plurals, and leaves words ending in a double s alone.
Types ¶
type Class ¶
type Class struct {
Name string
IRI string
Label string
Comment string
Parent string // the class this one specialises, by name
Attrs []string // attribute names shared by the instances observed
Seen int // instances observed
From string // the source type this class was inferred from
}
Class is one type in the schema.
type Entity ¶
Entity is one observed instance: what it is, what it is called, and the attributes carried on it. Framework bookkeeping has no place in Attrs, so it cannot be mistaken for a datatype property.
type Inference ¶
type Inference struct {
Min int // observations a type needs before it becomes a class; zero means two
Base string // namespace for minted IRIs; empty means DefaultBase
Flat bool // leave the class hierarchy alone
}
Inference derives a schema from data that has already been observed. It is the way the package is mostly used: entities and links come out of extraction, and the schema that describes them is read back off the data rather than written by hand.
The zero value infers a class from any type seen twice, mints IRIs in DefaultBase, and looks for a hierarchy.
func (Inference) Classes ¶
Classes groups the entities by type and returns one class per type seen at least Min times. Two source types that normalize to the same class name are a collision, not a merge: silently folding them would lose the distinction the data drew.
func (Inference) Properties ¶
Properties infers object properties from the links and datatype properties from the entity attributes, then merges the ones that normalize to the same name. A name claimed by both an object and a datatype property is a collision: the two cannot share an IRI and mean different things.
type Kind ¶
type Kind string
Kind separates the two sorts of property: one whose values are other entities, one whose values are literals.
const ( // Object is a property whose values are entities. Object Kind = "object" // Data is a property whose values are literals. Data Kind = "data" // Annotation is a property that describes a term rather than constraining // its instances, such as a label or an editorial note. Annotation Kind = "annotation" )
type Link ¶
Link is one observed relation between two entities. From and To name their endpoints by ID or by Name; FromType and ToType carry the endpoint classes when the source states them.
type Namespace ¶
type Namespace struct {
Base string
Version string
Bound map[string]string // bindings beyond the standard ones
}
Namespace mints IRIs for terms and resolves prefixes. The zero value mints in DefaultBase and knows the standard prefixes.
func (Namespace) Individual ¶
Individual returns the IRI for a named instance.
type Property ¶
type Property struct {
Name string
IRI string
Kind Kind
Label string
Comment string
Domain []string // classes the property applies to
Range []string // classes for Object, datatypes for Data
Min *int // fewest values allowed; nil leaves the count open
Max *int // most values allowed; nil leaves the count open
One []string // the only values allowed
Pattern string // regular expression every value must match
Seen int // observations behind the inference
}
Property is one relation in the schema. An Object property ranges over classes, a Data property over datatypes.
type Schema ¶
type Schema struct {
IRI string
Name string
Comment string
Version string
Base string // namespace the terms live in
Prefixes map[string]string
Classes []Class
Properties []Property
}
Schema is a whole ontology: the terms and the namespace they live in.
func FromStatements ¶
FromStatements builds a schema from triples already parsed.
func ParseJSONLD ¶
ParseJSONLD reads a schema from a JSON-LD document. The document may hold a graph or a single node, and terms may be written with any prefix the context binds.
func ParseTurtle ¶
ParseTurtle reads an OWL ontology out of a Turtle or N-Triples document: the classes, the properties, their labels, comments, domains, ranges and the subclass hierarchy. Terms are named by the local part of their IRI, which is the identity the rest of the package indexes by.
func (Schema) Ancestors ¶
Ancestors lists the classes a class specialises, nearest parent first. A cycle in the hierarchy stops the walk rather than hanging it.
func (Schema) JSONLD ¶
JSONLD writes the schema as a JSON-LD document: a context binding the prefixes and a graph of one node per term.
func (Schema) Normalize ¶
Normalize returns the schema with duplicate terms dropped and the IRIs, labels and endpoints left blank filled in. Terms keep their first occurrence and their order.
func (Schema) Property ¶
Property returns the property of that name, and whether the schema has one.
type Statement ¶
type Statement struct {
Subject string
Predicate string
Object string
Literal bool
Datatype string
Lang string
}
Statement is one RDF triple in lexical form. Subject and Predicate are IRIs or blank node labels; Object is one of those unless Literal is set, in which case it is the lexical value and Datatype or Lang describes it.
func Statements ¶
Statements parses Turtle into triples. N-Triples is a subset of the same grammar, so it parses too and there is no second entry point for it. The parser is deliberately tolerant: it reads the constructs a serializer emits — prefixes, predicate and object lists, blank node property lists, collections, typed and tagged literals — and reports where it gives up.