Documentation
¶
Overview ¶
Package export writes a graph out in the formats other tools read.
Everything here streams. A Source hands assertions to a Format one at a time and the format writes each one as it arrives, so the graph never has to fit in memory. The formats that name nodes — graphml, gexf, dot, mermaid, cypher — hold the set of nodes they have already declared, which is the graph's nodes and not its edges; the rest hold nothing at all.
Two questions separate the formats. What can one carry? json, ndjson, csv and tsv carry every field of an assertion, the span it was read from and the confidence it was given included; the RDF and graph formats carry the assertion alone, because a triple has nowhere to put the rest. What can one be read back from? The four that carry everything, and nt. Registry.Read parses them into the same Source a writer takes, so converting one to another is one call inside another:
err := export.Default.Write(ctx, w, export.Default.Read(f, "nt"), "ttl")
A term that already carries a scheme is an IRI and is written as it stands. Anything else is a name, and the RDF formats mint it into Base.
A format is named by its file extension, so the name of a document is the name of the format that writes it:
json ndjson jsonld csv tsv nt ttl rdfxml graphml gexf dot mermaid cypher
parquet and yaml resolve too, and report ErrLibrary: both need an encoder outside the standard library, and this module takes no dependencies. See Absent.
Index ¶
- Constants
- Variables
- type Absent
- type CSV
- type Cypher
- type DOT
- type Format
- type GEXF
- type GraphML
- type JSON
- type JSONLD
- type Mermaid
- type NDJSON
- type NT
- type RDFXML
- type Reader
- type Registry
- func (r *Registry) Drop(name string)
- func (r *Registry) Formats() []string
- func (r *Registry) Get(name string) (Format, bool)
- func (r *Registry) Read(rd io.Reader, format string) Source
- func (r *Registry) Set(name string, f Format)
- func (r *Registry) Write(ctx context.Context, w io.Writer, src Source, format string) error
- type Source
- type Turtle
Constants ¶
const Base = "https://semantica.dev/ns#"
Base is the namespace a term without a scheme is minted into. It is Semantica's own vocabulary, so a graph written here and one written by the Python this is ported from name the same things.
Variables ¶
var ( // ErrFormat says nothing is registered under that name. ErrFormat = errors.New("unknown format") // ErrTerm says an assertion is missing a subject, predicate or object. // A format that identifies nodes has nothing to call the node, so it // stops rather than write a document with a hole in it. ErrTerm = errors.New("assertion has an empty term") // ErrName says a predicate cannot be written as an XML qualified name, // which is the one thing rdfxml needs that the other formats do not. ErrName = errors.New("predicate has no XML name") // ErrLibrary says the format needs code this module does not carry. // Register a writer for it to fill the gap. ErrLibrary = errors.New("format needs a library this module does not carry") // ErrParse says the format is written here but not read back. ErrParse = errors.New("format cannot be parsed") // ErrDrained says a source over a reader has already been walked and its // bytes are gone. ErrDrained = errors.New("source is already read") )
What this package returns when it cannot write what it was given.
var Default = &Registry{by: map[string]Format{ "json": JSON{}, "ndjson": NDJSON{}, "jsonld": JSONLD{}, "csv": CSV{}, "tsv": CSV{Sep: '\t'}, "nt": NT{}, "ttl": Turtle{}, "rdfxml": RDFXML{}, "graphml": GraphML{}, "gexf": GEXF{}, "dot": DOT{}, "mermaid": Mermaid{}, "cypher": Cypher{}, "parquet": Absent{"parquet"}, "yaml": Absent{"yaml"}, }}
Default resolves the formats this package implements.
Functions ¶
This section is empty.
Types ¶
type Absent ¶
type Absent struct{ Name string }
Absent stands for a format this module knows of and cannot write, so asking for it says what is missing rather than that the name is unknown. parquet and yaml are registered this way: a columnar writer and a YAML encoder are both outside the standard library, and this module takes no dependencies.
Registering a real writer under the name fills the gap:
export.Default.Set("parquet", myParquetWriter)
Until then csv and ndjson carry every field either of them would.
type CSV ¶
CSV writes the graph as a table, a row to an assertion, carrying every field. Sep is the column separator, a comma when unset; the registry has it under csv with a comma and under tsv with a tab.
Bare writes the rows without the header line, for appending to a table that already has one. Reading rows written that way takes the columns in the order above, since there is no header to say otherwise.
type Cypher ¶
type Cypher struct{ Label string }
Cypher writes CREATE statements for Neo4j, Memgraph and the graph databases that speak the same language. A node is created the first time an assertion names it, and each edge is a MATCH on the two ids it joins followed by a CREATE of the relation between them.
Label is the label every node is created with, Node when unset; the graph carries no type of its own to use in its place. A relation keeps the predicate it was asserted with, back-quoted, rather than being upper-cased into the house style: case is part of a name, and losing it cannot be undone.
type DOT ¶
type DOT struct{ Name string }
DOT writes the Graphviz language. Name is the graph's name, G when unset.
type Format ¶
Format writes a stream of assertions. A format is a value with no state of its own, so one may be shared and used concurrently.
type GEXF ¶
type GEXF struct{}
GEXF writes the XML Gephi reads. Its grammar puts every node before every edge, so this is the one format here that walks the source twice: once for the nodes and once for the edges. The alternative is holding the edges, and a graph has more of those than nodes.
type GraphML ¶
type GraphML struct{}
GraphML writes the XML that Cytoscape, yEd and Gephi read. A node is declared the first time an assertion names it, immediately before the edge that needs it, so the document is written in one pass and every edge follows the nodes it joins.
type JSON ¶
type JSON struct{ Indent string }
JSON writes the graph as one array of assertions, one to a line so the output diffs, and carries every field.
Indent pretty-prints each assertion by that much; unset, an assertion is one line. The array brackets are written as the walk starts and ends, so the document streams whichever way it is set.
type JSONLD ¶
type JSONLD struct{ Base string }
JSONLD writes the graph as JSON-LD: a context naming the namespace and a graph of nodes, one per assertion.
One node per assertion rather than one per subject, because grouping by subject means holding the graph. A JSON-LD reader merges the nodes back by @id, so the document says the same thing either way. Being RDF, it carries the assertion and not the confidence or the span it came from.
type Mermaid ¶
type Mermaid struct{ Dir string }
Mermaid writes a flowchart, for a diagram in a document that renders one. Dir is the layout direction — LR, TD, RL, BT — LR when unset.
Mermaid names a node with an identifier rather than a string, so each term gets one, n0 upward in the order the terms are first seen, with the term itself as the node's text.
type NDJSON ¶
type NDJSON struct{}
NDJSON writes one assertion per line, each a complete JSON object. It is the format to reach for when the graph is large or the reader is a pipe: nothing has to be held, and a line is a whole record.
type NT ¶
type NT struct{ Base string }
NT writes N-Triples: one <subject> <predicate> <object> . to a line. It is the format every RDF tool reads, the cheapest to write, and the only RDF format here that reads back, since a line is a whole statement.
A triple has nowhere to put a confidence or the span a claim came from, so those are not written. A graph that has to keep them goes out as ndjson.
type RDFXML ¶
type RDFXML struct{ Base string }
RDFXML writes RDF/XML: an rdf:Description per assertion, carrying the predicate as a property element.
One description per assertion rather than one per subject, for the reason JSON-LD writes one node per assertion: grouping means holding the graph. The namespace of each predicate is declared on the property element itself, which is what lets the document be written in one pass.
RDF/XML is the one format here that cannot write every predicate: a property element needs an XML name, so a predicate whose IRI does not end in one is reported as ErrName rather than dropped.
type Reader ¶
Reader is the other half of a format that can parse its own output. json, ndjson, csv, tsv and nt implement it; the rest are written and not read.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry resolves a name to the format written under it, and is the seam for formats this package does not implement: register a writer and it is reached the same way as the rest.
export.Default.Set("parquet", myParquetWriter)
err := export.Default.Write(ctx, f, src, "parquet")
The zero Registry is empty and usable; Default holds the formats here.
func (*Registry) Read ¶
Read is the source that parses rd as the named format. A format that does not carry every field of an assertion cannot be read back, and the walk reports ErrParse.
The reader is spent by the first walk; a second returns ErrDrained rather than an empty graph, so a format that needs two walks fails loudly instead of writing a graph with no edges.
type Source ¶
Source walks a graph, handing every assertion to yield in order. It returns yield's error unchanged, so a format that cannot write stops the walk instead of being handed the rest of a graph it has no use for.
A source may be walked more than once: Triples, Match and Walk all replay, which is what GEXF needs, since its grammar puts every node before every edge. A source over an io.Reader is the exception and says so.
func Match ¶
Match is the source of what a triple store holds for a pattern. An empty field matches anything, so Match(st, "", "", "") is the whole store.
A store answers with the assertion and not with what was known about it, so the triples this yields carry no confidence and no source span.
func Walk ¶
Walk is the source of the edges reachable from roots in a graph store, breadth first from each root in turn, every edge yielded once.
A graph store reports a node's neighbours without saying which relation leads to them, so the walk cannot recover the predicate and records label in its place. Where the predicates matter, export from the triple store.