diff --git a/exporter/exporter.go b/exporter/exporter.go index 07e56a9..10fef10 100644 --- a/exporter/exporter.go +++ b/exporter/exporter.go @@ -3,6 +3,7 @@ package exporter import ( "io" "encoding/json" + "strconv" "github.com/google/cayley/graph" ) @@ -61,6 +62,98 @@ func (exp *Exporter) ExportJson() { exp.Write("]\n") } +//experimental +func (exp *Exporter) ExportGml() { + var seen map[string]int32 // todo eliminate this for large dbs + var id int32 + + exp.Write("Creator Cayley\ngraph\n[\n") + + seen = make(map[string]int32) + it := exp.qstore.QuadsAllIterator() + for graph.Next(it) { + cur := exp.qstore.Quad(it.Result()) + if _, ok := seen[cur.Subject]; !ok { + exp.Write(" node\n [\n id ") + seen[cur.Subject] = id + exp.Write(strconv.FormatInt(int64(id), 10)) + exp.Write("\n label ") + exp.WriteEscString(cur.Subject) + exp.Write("\n ]\n") + id++ + } + if _, ok := seen[cur.Object]; !ok { + exp.Write(" node\n [\n id ") + seen[cur.Object] = id + exp.Write(strconv.FormatInt(int64(id), 10)) + exp.Write("\n label ") + exp.WriteEscString(cur.Object) + exp.Write("\n ]\n") + id++ + } + exp.count++ + } + + it.Reset() + for graph.Next(it) { + cur := exp.qstore.Quad(it.Result()) + exp.Write(" edge\n [\n source ") + exp.Write(strconv.FormatInt(int64(seen[cur.Subject]), 10)) + exp.Write("\n target ") + exp.Write(strconv.FormatInt(int64(seen[cur.Object]), 10)) + exp.Write("\n label ") + exp.WriteEscString(cur.Predicate) + exp.Write("\n ]\n") + exp.count++ + } + exp.Write("]\n") +} + +//experimental +func (exp *Exporter) ExportGraphml() { + var seen map[string]bool // eliminate this for large databases + + exp.Write("\n") + exp.Write("\n") + exp.Write(" \n") + + seen = make(map[string]bool) + it := exp.qstore.QuadsAllIterator() + for graph.Next(it) { + cur := exp.qstore.Quad(it.Result()) + if found := seen[cur.Subject]; !found { + seen[cur.Subject] = true + exp.Write(" \n") + } + if found := seen[cur.Object]; !found { + seen[cur.Object] = true + exp.Write(" \n") + } + exp.count++ + } + + it.Reset() + for graph.Next(it) { + cur := exp.qstore.Quad(it.Result()) + exp.Write(" \n") + exp.Write(" ") + exp.Write(cur.Predicate) + exp.Write("\n \n") + exp.count++ + } + exp.Write(" \n\n"); +} + //print out the string quoted, escaped func (exp *Exporter) WriteEscString(str string) { var esc []byte diff --git a/internal/dump.go b/internal/dump.go index e43fe3f..e4674be 100644 --- a/internal/dump.go +++ b/internal/dump.go @@ -34,6 +34,11 @@ func Dump(qs graph.QuadStore, outFile, typ string) error { export.ExportNquad() case "json": export.ExportJson() + // gml/graphml experimental + case "gml": + export.ExportGml() + case "graphml": + export.ExportGraphml() default: return fmt.Errorf("unknown format %q", typ) }