rdf

package module
v0.0.0-...-93fe882 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2021 License: MIT Imports: 15 Imported by: 2

README

rdf

This package introduces data structures for representing RDF resources, and includes functions for parsing and serialization of RDF data.

For complete documentation see godoc.

This fork

The source of this fork, https://github.com/knakk/rdf, has a number of characterstics which make it unattractive to us (and perhaps others).

  1. The licence is non-OCI, is untested in any way. Aleister Crowley is not a great source for legal policy
    1. This repo, and other repos we fork to use, have been granted the MIT Licence. Relicencing seems to be within the spirit of the original licence but frankly: who even knows?

Documentation

Overview

Package rdf provides functionality for working with RDF resources, including parsing and serialization of the various RDF formats.

Data model

The package adhers to the RDF data model as described in http://www.w3.org/TR/rdf11-concepts/.

Data structures

TODO.

Encoding and decoding

The package aims to support all the RDF serialization formats standardized by W3C. Currently the following are implemented:

Format     | Decode | Encode
-----------|--------|--------
RDF/XML    | x      | -
N-Triples  | x      | x
N-Quads    | x      | x
Turtle     | x      | x
TriG       | -      | -
JSON-LD    | -      | -

The parsers are implemented as streaming decoders, consuming an io.Reader and emitting triples/quads as soon as they are available. Simply call Decode() until the reader is exhausted and emits io.EOF:

f, err := os.Open("mytriples.ttl")
if err != nil {
    // handle error
}
dec := rdf.NewTripleDecoder(f, rdf.Turtle)
for triple, err := dec.Decode(); err != io.EOF; triple, err = dec.Decode() {
    // do something with triple ..
}

The encoders work similarily. For a complete working example, see the rdf2rdf application, which converts between different serialization formats using the decoders and encoders of the rdf package: https://github.com/knakk/rdf2rdf.

Index

Constants

This section is empty.

Variables

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 ErrEncoderClosed = errors.New("Encoder is closed and cannot encode anymore")

ErrEncoderClosed is the error returned from Encode() when the Triple/Quad-Encoder is closed

Functions

func QuadsEqual

func QuadsEqual(a, b Quad) bool

QuadsEqual tests if two Quads are identical.

func TermsEqual

func TermsEqual(a, b Term) bool

TermsEqual returns true if two Terms are equal, or false if they are not.

func TriplesEqual

func TriplesEqual(a, b Triple) bool

TriplesEqual tests if two Triples are identical.

Types

type Blank

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

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

func NewBlank

func NewBlank(id string) (Blank, error)

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

func (Blank) Serialize

func (b Blank) Serialize(f Format) string

Serialize returns a string representation of a Blank node.

func (Blank) String

func (b Blank) String() string

String returns the Blank node label

func (Blank) Type

func (b Blank) Type() TermType

Type returns the TermType of a blank node.

type Context

type Context interface {
	Term
	// contains filtered or unexported methods
}

Context interface distiguishes which Terms are valid as a Quad's Context. Incidently, this is the same as Terms valid as a Subject of a Triple.

type Format

type Format int

Format represents a RDF serialization format.

const (
	NTriples Format = iota
	Turtle
	RDFXML

	NQuads // N-Quads

)

Supported parser/serialization formats for Triples and Quads.

type IRI

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

IRI represents a RDF IRI resource.

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) Serialize

func (u IRI) Serialize(f Format) string

Serialize returns a string representation of an IRI.

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.

func (IRI) String

func (u IRI) String() string

String returns the IRI string.

func (IRI) Type

func (u IRI) Type() TermType

Type returns the TermType of a 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 NewLangLiteral

func NewLangLiteral(v, lang string) (Literal, error)

NewLangLiteral 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 NewLiteral

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

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

func NewTypedLiteral

func NewTypedLiteral(v string, dt IRI) Literal

NewTypedLiteral returns a literal with the given datatype.

func (Literal) Lang

func (l Literal) Lang() string

Lang returns the language of a language-tagged string.

func (Literal) Serialize

func (l Literal) Serialize(f Format) string

Serialize returns a string representation of a Literal.

func (Literal) String

func (l Literal) String() string

String returns the 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, acordig to the the DataType.

type Object

type Object interface {
	Term
	// contains filtered or unexported methods
}

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

type ParseOption

type ParseOption int

A ParseOption allows to customize the behaviour of a decoder.

const (
	// Base IRI to resolve relative IRIs against (for formats that support
	// relative IRIs: Turtle, RDF/XML, TriG, JSON-LD)
	Base ParseOption = iota
)

Options which can configure a decoder.

type Predicate

type Predicate interface {
	Term
	// contains filtered or unexported methods
}

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

type Quad

type Quad struct {
	Triple
	Ctx Context
}

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

func (Quad) Serialize

func (q Quad) Serialize(f Format) string

Serialize serializes the Quad in the given format (assumed to be NQuads atm).

type QuadDecoder

type QuadDecoder struct {
	DefaultGraph Context // default graph
	// contains filtered or unexported fields
}

QuadDecoder parses RDF quads in one of the following formats: N-Quads.

For streaming parsing, use the Decode() method to decode a single Quad at a time. Or, if you want to read the whole source in one go, DecodeAll().

func NewQuadDecoder

func NewQuadDecoder(r io.Reader, f Format) *QuadDecoder

NewQuadDecoder returns a new QuadDecoder capable of parsing quads from the given io.Reader in the given serialization format.

func (*QuadDecoder) Decode

func (d *QuadDecoder) Decode() (Quad, error)

Decode returns the next valid Quad, or an error

func (*QuadDecoder) DecodeAll

func (d *QuadDecoder) DecodeAll() ([]Quad, error)

DecodeAll decodes and returns all Quads from source, or an error

type QuadEncoder

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

QuadEncoder serializes RDF Quads. Currently only supports N-Quads.

func NewQuadEncoder

func NewQuadEncoder(w io.Writer, f Format) *QuadEncoder

NewQuadEncoder returns a new QuadEncoder on the given writer. The only supported format is NQuads.

func (*QuadEncoder) Close

func (e *QuadEncoder) Close() error

Close closes the encoder and flushes the underlying buffering writer.

func (*QuadEncoder) Encode

func (e *QuadEncoder) Encode(q Quad) error

Encode encodes a Quad.

func (*QuadEncoder) EncodeAll

func (e *QuadEncoder) EncodeAll(qs []Quad) error

EncodeAll encodes all quads.

type Subject

type Subject interface {
	Term
	// contains filtered or unexported methods
}

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

type Term

type Term interface {
	// Serialize returns a string representation of the Term in the specified serialization format.
	Serialize(Format) string

	// String returns the term as it is stored, without any modifications.
	String() string

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

Term represents an RDF term. There are 3 term types: Blank node, Literal and IRI.

type TermType

type TermType int

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

const (
	TermBlank TermType = iota
	TermIRI
	TermLiteral
)

Exported RDF term types.

type Triple

type Triple struct {
	Subj Subject
	Pred Predicate
	Obj  Object
}

Triple represents a RDF triple.

func (Triple) Serialize

func (t Triple) Serialize(f Format) string

Serialize returns a string representation of a Triple in the specified format.

However, it will only serialize the triple itself, and not include the prefix directives. For a full serialization including directives, use the TripleEncoder.

type TripleDecoder

type TripleDecoder interface {
	// Decode parses a RDF document and return the next valid triple.
	// It returns io.EOF when the whole document is parsed.
	Decode() (Triple, error)

	// DecodeAll parses the entire RDF document and return all valid
	// triples, or an error.
	DecodeAll() ([]Triple, error)

	// SetOption sets a parsing option to the given value. Not all options
	// are supported by all serialization formats.
	SetOption(ParseOption, interface{}) error
}

TripleDecoder parses RDF documents (serializations of an RDF graph).

For streaming parsing, use the Decode() method to decode a single Triple at a time. Or, if you want to read the whole document in one go, use DecodeAll().

The decoder can be instructed with numerous options. Note that not all options are supported by all formats. Consult the following table:

Option      Description        Value      (default)       Format support
------------------------------------------------------------------------------
Base        Base IRI           IRI        (empty IRI)     Turtle, RDF/XML
Strict      Strict mode        true/false (true)          TODO
ErrOut      Error output       io.Writer  (nil)           TODO

func NewTripleDecoder

func NewTripleDecoder(r io.Reader, f Format) TripleDecoder

NewTripleDecoder returns a new TripleDecoder capable of parsing triples from the given io.Reader in the given serialization format.

type TripleEncoder

type TripleEncoder struct {
	Namespaces map[string]string // IRI->prefix custom mappings.

	OpenStatement      bool // True when triple statement hasn't been closed (i.e. in a predicate/object list)
	GenerateNamespaces bool // True to auto generate namespaces, false if you give it some custom namespaces and do not want generated ones
	// contains filtered or unexported fields
}

TripleEncoder serializes RDF Triples into one of the following formats: N-Triples, Turtle, RDF/XML.

For streaming serialization, use the Encode() method to encode a single Triple at a time. Or, if you want to encode multiple triples in one batch, use EncodeAll(). In either case; when done serializing, Close() must be called, to ensure that all writes are persisted, since the Encoder uses buffered IO.

func NewTripleEncoder

func NewTripleEncoder(w io.Writer, f Format) *TripleEncoder

NewTripleEncoder returns a new TripleEncoder capable of serializing into the given io.Writer in the given serialization format.

func (*TripleEncoder) Close

func (e *TripleEncoder) Close() error

Close finalizes an encoding session, ensuring that any concluding tokens are written should it be needed (eg.g close the root tag for RDF/XML) and flushes the underlying buffered writer of the encoder.

The encoder cannot encode anymore when Close() has been called.

func (*TripleEncoder) Encode

func (e *TripleEncoder) Encode(t Triple) error

Encode serializes a single Triple to the io.Writer of the TripleEncoder.

func (*TripleEncoder) EncodeAll

func (e *TripleEncoder) EncodeAll(ts []Triple) error

EncodeAll serializes a slice of Triples to the io.Writer of the TripleEncoder. It will ignore duplicate triples.

Note that this function will modify the given slice of triples by sorting it in-place.

Jump to

Keyboard shortcuts

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