Documentation
¶
Overview ¶
Package graphml reads and writes graphs in the GraphML XML dialect (http://graphml.graphdrawing.org/). v1 supports the commonly-encountered shape: <node id="...">, <edge source="..." target="..."> with an optional <data key="..."> carrying an int64 weight under a <key id="..." attr.name="weight" .../> declaration.
Index ¶
- func ReadInto(r io.Reader) (*adjlist.AdjList[string, int64], int, error)
- func ReadIntoCtx(ctx context.Context, r io.Reader) (*adjlist.AdjList[string, int64], int, error)
- func ReadWithProps(r io.Reader) (*lpg.Graph[string, int64], int, error)
- func ReadWithPropsCtx(ctx context.Context, r io.Reader) (*lpg.Graph[string, int64], int, error)
- func Write(w io.Writer, a *adjlist.AdjList[string, int64]) error
- func WriteCtx(ctx context.Context, w io.Writer, a *adjlist.AdjList[string, int64]) error
- func WriteWithProps(w io.Writer, g *lpg.Graph[string, int64]) error
- func WriteWithPropsCtx(ctx context.Context, w io.Writer, g *lpg.Graph[string, int64]) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ReadInto ¶
ReadInto parses a GraphML document from r into an adjacency list. Returns the loaded list, the number of edges added, and an error on parse failure.
func ReadIntoCtx ¶
ReadIntoCtx is the context-aware variant of ReadInto. ctx.Err() is checked every 4096 edges; on cancellation returns (partialAdj, edgesAdded, wrapped ctx.Err()).
func ReadWithProps ¶
ReadWithProps parses a GraphML document from r and returns an lpg.Graph with typed node properties derived from <key> declarations and <data> elements. Edge weights are read via the standard "weight" key. The second return value is the number of edges added.
func ReadWithPropsCtx ¶
ReadWithPropsCtx is the context-aware variant of ReadWithProps.
func Write ¶
Write streams a GraphML document representing a to w. The output includes a single <graph> with directed or undirected edgedefault inferred from a, a <key for=edge attr.name=weight attr.type=long> declaration, and one <node>/<edge> per node and edge.
Example ¶
ExampleWrite shows a GraphML round-trip: marshal a directed, weighted graph to XML with Write, then unmarshal it back with ReadInto and confirm the structure survived.
package main
import (
"bytes"
"fmt"
"github.com/FlavioCFOliveira/GoGraph/graph/adjlist"
"github.com/FlavioCFOliveira/GoGraph/graph/io/graphml"
)
func main() {
src := adjlist.New[string, int64](adjlist.Config{Directed: true})
_ = src.AddEdge("a", "b", 7)
_ = src.AddEdge("b", "c", 9)
var buf bytes.Buffer
if err := graphml.Write(&buf, src); err != nil {
panic(err)
}
dst, edges, err := graphml.ReadInto(&buf)
if err != nil {
panic(err)
}
fmt.Println("edges read:", edges)
fmt.Println("order:", dst.Order())
fmt.Println("size:", dst.Size())
fmt.Println("a->b:", dst.HasEdge("a", "b"))
}
Output: edges read: 2 order: 3 size: 2 a->b: true
func WriteCtx ¶
WriteCtx is the context-aware variant of Write. ctx.Err() is checked at the start of node and edge encoding; on cancellation returns the wrapped ctx.Err.
func WriteWithProps ¶
WriteWithProps writes a GraphML document to w for the LPG g. A <key> declaration is emitted for every property key encountered across all nodes, with attr.type set to the GraphML equivalent of the first value seen for that key. Properties are serialised as <data> child elements of their respective <node> elements. Edge weights are written with the standard id="w" key.
Example ¶
ExampleWriteWithProps shows the labelled-property-graph round-trip: WriteWithProps serialises node properties as <data> elements and ReadWithProps restores them, so a typed property recovers its value.
package main
import (
"bytes"
"fmt"
"github.com/FlavioCFOliveira/GoGraph/graph/adjlist"
"github.com/FlavioCFOliveira/GoGraph/graph/io/graphml"
"github.com/FlavioCFOliveira/GoGraph/graph/lpg"
)
func main() {
src := lpg.New[string, int64](adjlist.Config{Directed: true})
_ = src.AddEdge("alice", "bob", 1)
_ = src.SetNodeProperty("alice", "age", lpg.Int64Value(30))
var buf bytes.Buffer
if err := graphml.WriteWithProps(&buf, src); err != nil {
panic(err)
}
dst, _, err := graphml.ReadWithProps(&buf)
if err != nil {
panic(err)
}
got, ok := dst.GetNodeProperty("alice", "age")
age, _ := got.Int64()
fmt.Println("alice has age:", ok)
fmt.Println("alice.age:", age)
}
Output: alice has age: true alice.age: 30
Types ¶
This section is empty.