rdf

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 31, 2022 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

package rdf provides tools for the Resource Description Framework (RDF), see https://www.w3.org/RDF/.

Index

Constants

View Source
const (
	RDFType = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
)

Variables

View Source
var (
	// ErrEmptyIRI is returned when an IRI is empty
	ErrEmptyIRI = errors.New("empty IRI is not allowed")

	// ErrInvalidNamespaceLabel is returned when invalid characters are
	// part of the namespace label.
	ErrInvalidNamespaceLabel = errors.New("invalid namespace label")

	// ErrDisallowedCharacterInIRI is returned when an IRI contains
	// any of the disallowed characters: [\x00-\x20<>"{}|^`\]
	// This error is ofter wrapped in another error showing the offending character
	ErrDisallowedCharacterInIRI = errors.New("disallowed character")

	// ErrEmptyBlankNode is returned when a BlankNode is empty
	ErrEmptyBlankNode = errors.New("empty BlankNode is not allowed")

	// ErrInvalidLanguageTag is returned when a RDF Literal.lang tag is invalid
	ErrInvalidLanguageTag = errors.New("invalid language tag")

	// ErrInvalidDataType is returned when the RDF Literal.DataType IRI is malformed
	ErrInvalidDataType = errors.New("invalid Literal.DataType IRI")

	// ErrUnsupportedDataType is returned when the RDF Literal.DataType is not part
	// of SupportDataTypes.
	ErrUnsupportedDataType = errors.New("unsupported Literal.DataType IRI")

	// ErrInvalidLiteral is returned when the RDF Literal value is invalid
	ErrInvalidLiteral = errors.New("invalid literal value")
)
View Source
var (
	DC      = &IRIBuilder{baseIRI: IRI{str: "http://purl.org/dc/elements/1.1/"}}
	DCAT    = &IRIBuilder{baseIRI: IRI{str: "http://www.w3.org/ns/dcat#"}}
	DCTERMS = &IRIBuilder{baseIRI: IRI{str: "http://purl.org/dc/terms/"}}
	EDM     = &IRIBuilder{baseIRI: IRI{str: "http://www.europeana.eu/schemas/edm/"}}
	FOAF    = &IRIBuilder{baseIRI: IRI{str: "http://xmlns.com/foaf/0.1/"}}
	IIIF    = &IRIBuilder{baseIRI: IRI{str: "http://iiif.io/api/image/2#"}}
	NAVE    = &IRIBuilder{baseIRI: IRI{str: "http://schemas.delving.eu/nave/terms/"}}
	ODRL    = &IRIBuilder{baseIRI: IRI{str: "http://www.w3.org/ns/odrl/2/"}}
	ORE     = &IRIBuilder{baseIRI: IRI{str: "http://www.openarchives.org/ore/terms/"}}
	OWL     = &IRIBuilder{baseIRI: IRI{str: "http://www.w3.org/2002/07/owl#"}}
	RDF     = &IRIBuilder{baseIRI: IRI{str: "http://www.w3.org/1999/02/22-rdf-syntax-ns#"}}
	RDFS    = &IRIBuilder{baseIRI: IRI{str: "http://www.w3.org/2000/01/rdf-schema#"}}
	SCHEMA  = &IRIBuilder{baseIRI: IRI{str: "http://schema.org/"}}
	SKOS    = &IRIBuilder{baseIRI: IRI{str: "http://www.w3.org/2004/02/skos/core#"}}
	XML     = &IRIBuilder{baseIRI: IRI{str: "http://www.w3.org/XML/1998/namespace"}}
	XSD     = &IRIBuilder{baseIRI: IRI{str: "http://www.w3.org/2001/XMLSchema#"}}
)

Shortcuts for the default support namespaces

View Source
var AnonNodeIDSize = 10

AnonNodeIdSize is the length of the generated identifier. The user can override this setting

View Source
var DateFormat = time.RFC3339

DateFormat defines the string representation of xsd:DateTime values. You can override it if you need another layout.

View Source
var ErrRecordNotFound = errors.New("record not found")
View Source
var IsA = Predicate(IRI{str: RDFType})
View Source
var (

	// SupportDataTypes contains a list of valid DataType IRIs.
	// You can extend or set this list with additional XSD IRIs.
	SupportDataTypes = []IRI{

		xsdString, xsdBoolean, xsdDecimal, xsdInteger,

		xsdDouble, xsdFloat,

		xsdDateTime,

		xsdByte, xsdInt,

		rdfLangString, xmlLiteral,
		admsIdentifier, edtf, wktLiteral,
	}
)

The XML schema built-in datatypes (xsd): https://dvcs.w3.org/hg/rdf/raw-file/default/rdf-concepts/index.html#xsd-datatypes

Functions

This section is empty.

Types

type BlankNode

type BlankNode struct {
	// contains filtered or unexported fields
}

BlankNode represents a RDF blank node; an unqualified IRI with identified by a label.

func NewAnonNode

func NewAnonNode() (bnode BlankNode)

NewAnonNode returns a new blank node with a pseudo-randomly generated ID.

func NewBlankNode

func NewBlankNode(id string) (BlankNode, error)

NewBlank returns a new blank node with a given label. It returns an error only if the supplied label is blank.

Leading and trailing whitespace is trimmed from the id.

func (BlankNode) Equal

func (b BlankNode) Equal(other Term) bool

Equal returns whether this blank node is equivalent to another.

func (BlankNode) RawValue

func (b BlankNode) RawValue() string

RawValue returns the Blank node label

func (BlankNode) String

func (b BlankNode) String() string

String returns the NTriples representation of the blank node.

func (BlankNode) Type

func (b BlankNode) Type() TermType

Type returns the TermType of a blank node.

func (BlankNode) ValidAsObject

func (b BlankNode) ValidAsObject()

ValidAsObject denotes that a Blank node is valid as a Triple's Object.

func (BlankNode) ValidAsSubject

func (b BlankNode) ValidAsSubject()

ValidAsSubject denotes that a Blank node is valid as a Triple's Subject.

func (BlankNode) Validate

func (b BlankNode) Validate() *validator.Validator

type Builder

type Builder struct{}

func (*Builder) IRI

func (b *Builder) IRI(str string) IRI

func (*Builder) Literal

func (b *Builder) Literal(str string) Literal

func (*Builder) LiteralWithDataType

func (b *Builder) LiteralWithDataType(str string, dataType IRI) Literal

func (*Builder) LiteralWithLang

func (b *Builder) LiteralWithLang(str, lang string) Literal

type Context

type Context interface {
	Term
	ValidAsSubject()
}

Context is the IRI namespace for the Quad

type Graph

type Graph struct {
	BaseURI IRI

	UseIndex         bool
	UseResource      bool
	NamespaceManager NamespaceManager
	// contains filtered or unexported fields
}

Graph is a collection of triples where the order of insertion is remembered

func NewGraph

func NewGraph() *Graph

func (*Graph) Add

func (g *Graph) Add(triples ...*Triple)

AddTriple appends triple to the Graph triples Note: there is no deduplication. The same triple can be added multiple times

func (*Graph) AddTriple

func (g *Graph) AddTriple(s Subject, p Predicate, o Object)

AddTriple is used to add a triple made of individual S, P, O objects

func (*Graph) Get

func (g *Graph) Get(s Subject) (rsc *Resource, ok bool)

func (*Graph) Len

func (g *Graph) Len() int

Len returns the number of triples in the Graph

func (*Graph) Namespaces

func (g *Graph) Namespaces() (ns []*domain.Namespace, err error)

func (*Graph) Resources

func (g *Graph) Resources() map[Subject]*Resource

func (*Graph) Stats

func (g *Graph) Stats() *GraphStats

func (*Graph) Triples

func (g *Graph) Triples() []*Triple

Triples returns an list based on insertion order of the triples in Graph.

func (*Graph) TriplesOnce

func (g *Graph) TriplesOnce() ([]*Triple, error)

TriplesOnce returns an list based on insertion order of the triples in Graph, an error is returned when triples have been Added after the previous read.

type GraphIndex

type GraphIndex struct {
	Subjects        map[hasher]uint64
	Predicates      map[hasher]uint64
	RDFTypes        map[hasher]uint64
	ObjectResources map[hasher]uint64
	Languages       map[hasher]uint64
	DataTypes       map[hasher]uint64
	NamespacesURIs  map[string]uint64
}

type GraphStats

type GraphStats struct {
	Languages      int
	ObjectIRIs     int
	ObjectLiterals int
	Predicates     int
	Resources      int
	Namespaces     int
	Triples        uint64
}

GraphStats returns counts for unique values in Graph

type IRI

type IRI struct {
	// contains filtered or unexported fields
}

IRI is an URI / IRI reference.

func NewIRI

func NewIRI(iri string) (IRI, error)

NewIRI returns a new IRI, or an error if it's not valid.

A valid IRI cannot be empty, or contain any of the disallowed characters: [\x00-\x20<>"{}|^`\].

func (IRI) Equal

func (u IRI) Equal(other Term) bool

Equal returns whether this resource is equal to another.

func (IRI) RawValue

func (u IRI) RawValue() (str string)

RawValue returns the string value of the a resource without brackets.

func (IRI) Split

func (u IRI) Split() (prefix, suffix string)

Split returns the prefix and suffix of the IRI string, splitted at the first '/' or '#' character, in reverse order of the string.

When the IRI can't be split, both the prefix and suffix are returned empty

func (IRI) String

func (u IRI) String() (str string)

String returns the NTriples representation of this resource.

func (IRI) Type

func (u IRI) Type() TermType

Type returns the TermType of a IRI.

func (IRI) ValidAsObject

func (u IRI) ValidAsObject()

ValidAsObject is a placeholder to verify that it can be used as an Object .

func (IRI) ValidAsPredicate

func (u IRI) ValidAsPredicate()

ValidAsPredicate is a placeholder to verify that it can be used as a Predicate .

func (IRI) ValidAsSubject

func (u IRI) ValidAsSubject()

ValidAsSubject is a placeholder to verify that it can be used as a Subject.

func (IRI) Validate

func (u IRI) Validate() *validator.Validator

type IRIBuilder

type IRIBuilder struct {
	// contains filtered or unexported fields
}

IRIBuilder is used to easily create namespaces IRIs.

func NewIRIBuilder

func NewIRIBuilder(base IRI) *IRIBuilder

NewIRIBuilder returns an IRIBuilder.

This base IRI is expected to be valid.

func (*IRIBuilder) IRI

func (b *IRIBuilder) IRI(label string) (IRI, error)

IRI returns a namespaces IRI suffixed with the given label.

An error is returned when the label is invalid. Although this is extra error handling for each new IRI, we prefer the guarantees of correctness of the returned IRI.

type Literal

type Literal struct {

	// The datatype of the Literal.
	DataType IRI
	// contains filtered or unexported fields
}

Literal represents a RDF literal; a value with a datatype and (optionally) an associated language tag for strings.

func NewLiteral

func NewLiteral(str string) (Literal, error)

NewLiteral creates a RDF literal, it fails if the value string is not not well-formed.

The literal will have the datatype IRI xsd:String.

func NewLiteralInferred

func NewLiteralInferred(v interface{}) (Literal, error)

NewLiteralInferred returns a new Literal, or an error on invalid input. It tries to map the given Go values to a corresponding xsd datatype.

func NewLiteralWithLang

func NewLiteralWithLang(str, lang string) (Literal, error)

NewLiteralWithLang creates a RDF literal with a given language tag, or fails if the language tag is not well-formed.

The literal will have the datatype IRI xsd:String.

func NewLiteralWithType

func NewLiteralWithType(str string, dt IRI) (Literal, error)

NewLiteralWithType returns a literal with the given datatype, or fails if the DataType IRI is malformed.

func (Literal) Equal

func (l Literal) Equal(other Term) bool

Equal returns whether this literal is equivalent to another.

func (Literal) HasImpliedDataType

func (l Literal) HasImpliedDataType() bool

func (Literal) Lang

func (l Literal) Lang() string

Lang returns the language of a language-tagged string.

func (Literal) RawValue

func (l Literal) RawValue() string

func (Literal) String

func (l Literal) String() string

String returns the NTriples representation of this literal string.

func (Literal) Type

func (l Literal) Type() TermType

Type returns the TermType of a Literal.

func (Literal) Typed

func (l Literal) Typed() (interface{}, error)

Typed tries to parse the Literal's value into a Go type, according to the the DataType.

func (Literal) ValidAsObject

func (l Literal) ValidAsObject()

ValidAsObject denotes that a Literal is valid as a Triple's Object.

func (Literal) Validate

func (l Literal) Validate() *validator.Validator

type NamespaceManager

type NamespaceManager interface {
	GetWithPrefix(prefix string) (ns *domain.Namespace, err error)
	GetWithBase(base string) (ns *domain.Namespace, err error)
}
var DefaultNamespaceManager NamespaceManager

DefaultNamespaceManager can be set at package level to serve as a default when no NamespaceManager is set on a Graph.

The namespace manager is used by Resource and some RDF Encode/Decoder packages.

type Object

type Object interface {
	Term
	ValidAsObject()
}

Object interface distiguishes which Terms are valid as a Object of a Triple.

type Option

type Option func(obj *Literal) error

func WithDataType

func WithDataType(dt IRI) Option

WithDataType is an Option to set a DataType on AddSimpleLiteral()

func WithLanguage

func WithLanguage(lang string) Option

WithLanguage is an Option to set a Language on AddSimpleLiteral()

type Parser

type Parser interface {
	Parse(r io.Reader, g *Graph) (*Graph, error)
}

Parser is an interface for parsing RDF in io.Reader and storing it into the Graph.

This should be implemement by RDF parsing packages

type Predicate

type Predicate interface {
	Term
	ValidAsPredicate()
}

Predicate interface distiguishes which Terms are valid as a Predicate of a Triple.

type Quad

type Quad struct {
	*Triple
	// contains filtered or unexported fields
}

Quad represents a RDF Quad; a Triple plus the context in which it occurs.

func NewQuad

func NewQuad(triple *Triple, ctx Context) (*Quad, error)

func (Quad) Equal

func (q Quad) Equal(other *Quad) bool

Equal tests if other Quad is identical.

type Record

type Record struct {
	ID         domain.HubID
	CreatedAt  time.Time
	ModifiedAt time.Time
	Hash       string
	Deleted    bool
	Version    int32
	GraphData  []byte
	GraphURI   string
	MimeType   string
}

Record is the primary grouping of triples to represent a search record. This is a replacement of the old fragments.FragmentGraph. lod.RecordStore interface interacts as a storage layer for these records

TODO(kiivihal): implement index record

func ReadRecord

func ReadRecord(hubID domain.HubID, basePath string) (*Record, error)

func (*Record) Graph

func (r *Record) Graph() (*Graph, error)

func (*Record) Merge

func (r *Record) Merge(previous *Record) (bool, error)

func (*Record) Path

func (r *Record) Path() string

func (*Record) Write

func (r *Record) Write(path string) error

Write will write to path and append the relative path from r.Path() When the r.Hash is not identical a new record will be written, otherwise only the Version will be updated

func (*Record) WriteTmp

func (r *Record) WriteTmp(path string) error

func (*Record) WriteTo

func (r *Record) WriteTo(w io.Writer) (int64, error)

type Resource

type Resource struct {
	PredicateURIBase *IRIBuilder `json:"-"`
	// contains filtered or unexported fields
}

Resource contains all the predicates linked to a Subject

func NewResource

func NewResource(subject Subject) *Resource

func (*Resource) Add

func (r *Resource) Add(t *Triple)

func (*Resource) AddSimpleLiteral

func (r *Resource) AddSimpleLiteral(predicateLabel, value string, options ...Option)

func (*Resource) HasErrors

func (r *Resource) HasErrors() bool

func (*Resource) Label

func (r *Resource) Label() (Literal, bool)

func (*Resource) Predicates

func (r *Resource) Predicates() map[Predicate]*resourcePredicate

func (*Resource) SortedPredicates

func (r *Resource) SortedPredicates() []*resourcePredicate

func (*Resource) Subject

func (r *Resource) Subject() Subject

func (*Resource) Triples

func (r *Resource) Triples() []*Triple

func (*Resource) Types

func (r *Resource) Types() []IRI

type Subject

type Subject interface {
	Term
	ValidAsSubject()
}

Subject interface distiguishes which Terms are valid as a Subject of a Triple.

type Term

type Term interface {
	// Equal returns whether this term is equal to another.
	Equal(Term) bool

	// RawValue returns the raw value of this term.
	RawValue() string

	// String returns the NTriples representation of this term.
	String() string

	// Type returns the Term type.
	Type() TermType

	// Validate returns is the Term is valid
	Validate() *validator.Validator
}

A Term is the value of a subject, predicate or object, i.e. a IRI reference, BlankNode or Literal.

To work with the underlying concrete type, use a type assertion or a type switch.

t, ok := term.(IRI)

type TermType

type TermType int

TermType describes the type of RDF term: Blank node, IRI or Literal

const (
	TermBlankNode TermType = iota
	TermIRI
	TermLiteral
)

Exported RDF term types.

func (TermType) String

func (i TermType) String() string

type Triple

type Triple struct {
	Subject   Subject
	Predicate Predicate
	Object    Object
}

Triple represents a RDF triple.

func NewTriple

func NewTriple(subject Subject, predicate Predicate, object Object) (triple *Triple)

NewTriple returns a new triple with the given subject, predicate and object.

func (Triple) Equal

func (triple Triple) Equal(other *Triple) bool

Equal returns this triple is equivalent to the argument.

func (Triple) GetRDFType

func (triple Triple) GetRDFType() (string, bool)

needed for refactor remove later

func (Triple) String

func (triple Triple) String() string

String returns the NTriples representation of this triple.

Directories

Path Synopsis
formats
jsonld
Package jsonld provides tools to parse and serialize RDF data in the JSON-LD format.
Package jsonld provides tools to parse and serialize RDF data in the JSON-LD format.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL