rdf: github.com/knakk/rdf Index | Files

package rdf

import "github.com/knakk/rdf"

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

Package Files

decoder.go encoder.go lex.go nq.go nt.go rdf.go rdfxml.go rune.go ttl.go

Variables

var DateFormat = time.RFC3339

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

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

func QuadsEqual Uses

func QuadsEqual(a, b Quad) bool

QuadsEqual tests if two Quads are identical.

func TermsEqual Uses

func TermsEqual(a, b Term) bool

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

func TriplesEqual Uses

func TriplesEqual(a, b Triple) bool

TriplesEqual tests if two Triples are identical.

type Blank Uses

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

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

func NewBlank Uses

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 Uses

func (b Blank) Serialize(f Format) string

Serialize returns a string representation of a Blank node.

func (Blank) String Uses

func (b Blank) String() string

String returns the Blank node label

func (Blank) Type Uses

func (b Blank) Type() TermType

Type returns the TermType of a blank node.

type Context Uses

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 Uses

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 Uses

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

IRI represents a RDF IRI resource.

func NewIRI Uses

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 Uses

func (u IRI) Serialize(f Format) string

Serialize returns a string representation of an IRI.

func (IRI) Split Uses

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 Uses

func (u IRI) String() string

String returns the IRI string.

func (IRI) Type Uses

func (u IRI) Type() TermType

Type returns the TermType of a IRI.

type Literal Uses

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 Uses

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 Uses

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 Uses

func NewTypedLiteral(v string, dt IRI) Literal

NewTypedLiteral returns a literal with the given datatype.

func (Literal) Lang Uses

func (l Literal) Lang() string

Lang returns the language of a language-tagged string.

func (Literal) Serialize Uses

func (l Literal) Serialize(f Format) string

Serialize returns a string representation of a Literal.

func (Literal) String Uses

func (l Literal) String() string

String returns the literal string.

func (Literal) Type Uses

func (l Literal) Type() TermType

Type returns the TermType of a Literal.

func (Literal) Typed Uses

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 Uses

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

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

type ParseOption Uses

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 Uses

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

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

type Quad Uses

type Quad struct {
    Triple
    Ctx Context
}

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

func (Quad) Serialize Uses

func (q Quad) Serialize(f Format) string

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

type QuadDecoder Uses

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 Uses

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 Uses

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

Decode returns the next valid Quad, or an error

func (*QuadDecoder) DecodeAll Uses

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

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

type QuadEncoder Uses

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

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

func NewQuadEncoder Uses

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 Uses

func (e *QuadEncoder) Close() error

Close closes the encoder and flushes the underlying buffering writer.

func (*QuadEncoder) Encode Uses

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

Encode encodes a Quad.

func (*QuadEncoder) EncodeAll Uses

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

EncodeAll encodes all quads.

type Subject Uses

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

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

type Term Uses

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 Uses

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 Uses

type Triple struct {
    Subj Subject
    Pred Predicate
    Obj  Object
}

Triple represents a RDF triple.

func (Triple) Serialize Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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

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

func (*TripleEncoder) EncodeAll Uses

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.

Package rdf imports 15 packages (graph) and is imported by 15 packages. Updated 2020-05-16. Refresh now. Tools for package owners.