diff --git a/data/testdata.nq b/data/testdata.nq index 4d211c0..12fe713 100644 --- a/data/testdata.nq +++ b/data/testdata.nq @@ -8,4 +8,6 @@ "cool_person" . . . - "cool_person" . \ No newline at end of file + "cool_person" . + . + . diff --git a/graph/path/path.go b/graph/path/path.go index 7fb0720..54506ce 100644 --- a/graph/path/path.go +++ b/graph/path/path.go @@ -14,7 +14,11 @@ package path -import "github.com/google/cayley/graph" +import ( + "fmt" + + "github.com/google/cayley/graph" +) type applyMorphism func(graph.QuadStore, graph.Iterator, *context) (graph.Iterator, *context) @@ -94,6 +98,11 @@ func (p *Path) Tag(tags ...string) *Path { return p } +// As is a synonym for Tag. +func (p *Path) As(tags ...string) *Path { + return p.Tag(tags...) +} + // Out updates this Path to represent the nodes that are adjacent to the // current nodes, via the given outbound predicate. // @@ -129,7 +138,7 @@ func (p *Path) And(path *Path) *Path { return p } -// And updates the current Path to represent the nodes that match either the +// Or updates the current Path to represent the nodes that match either the // current Path so far, or the given Path. func (p *Path) Or(path *Path) *Path { p.stack = append(p.stack, orMorphism(path)) @@ -187,6 +196,34 @@ func (p *Path) Has(via interface{}, nodes ...string) *Path { return p } +// Back returns to a previously tagged place in the path. Any constraints applied after the Tag will remain in effect, but traversal continues from the tagged point instead, not from the end of the chain. +// +// For example: +// // Will return "bob" iff "bob" is cool +// StartPath(qs, "bob").Tag("person_tag").Out("status").Is("cool").Back("person_tag") +func (p *Path) Back(tag string) *Path { + newPath := NewPath(p.qs) + i := len(p.stack) - 1 + + for { + if i < 0 { + return p.Reverse() + } + if p.stack[i].Name == "tag" { + for _, x := range p.stack[i].tags { + if x == tag { + // Found what we're looking for. + p.stack = p.stack[:i+1] + return p.And(newPath) + } + } + } + newPath.stack = append(newPath.stack, p.stack[i].Reversal()) + i-- + } + +} + // BuildIterator returns an iterator from this given Path. Note that you must // call this with a full path (not a morphism), since a morphism does not have // the ability to fetch the underlying quads. This function will panic if @@ -217,3 +254,12 @@ func (p *Path) Morphism() graph.ApplyMorphism { return i } } + +func (p *Path) debugPrint() { + var strs []string + for _, x := range p.stack { + strs = append(strs, x.Name) + } + fmt.Println("stack:", strs) + fmt.Println("ctx:", p.baseContext) +} diff --git a/graph/path/path_test.go b/graph/path/path_test.go index 6fb3362..bf22c16 100644 --- a/graph/path/path_test.go +++ b/graph/path/path_test.go @@ -15,12 +15,15 @@ package path import ( + "io" + "os" "reflect" "sort" "testing" "github.com/google/cayley/graph" "github.com/google/cayley/quad" + "github.com/google/cayley/quad/cquads" _ "github.com/google/cayley/graph/memstore" _ "github.com/google/cayley/writer" @@ -28,38 +31,43 @@ import ( // This is a simple test graph. // -// +---+ +---+ -// | A |------- ->| F |<-- -// +---+ \------>+---+-/ +---+ \--+---+ -// ------>|#B#| | | E | -// +---+-------/ >+---+ | +---+ -// | C | / v -// +---+ -/ +---+ -// ---- +---+/ |#G#| -// \-->|#D#|------------->+---+ -// +---+ -// +// +-------+ +------+ +// | alice |----- ->| fred |<-- +// +-------+ \---->+-------+-/ +------+ \-+-------+ +// ----->| #bob# | | | emily | +// +---------+--/ --->+-------+ | +-------+ +// | charlie | / v +// +---------+ / +--------+ +// \--- +--------+ | #greg# | +// \-->| #dani# |------------>+--------+ +// +--------+ -var simpleGraph = []quad.Quad{ - {"A", "follows", "B", ""}, - {"C", "follows", "B", ""}, - {"C", "follows", "D", ""}, - {"D", "follows", "B", ""}, - {"B", "follows", "F", ""}, - {"F", "follows", "G", ""}, - {"D", "follows", "G", ""}, - {"E", "follows", "F", ""}, - {"B", "status", "cool", "status_graph"}, - {"D", "status", "cool", "status_graph"}, - {"G", "status", "cool", "status_graph"}, - {"predicates", "are", "follows", ""}, - {"predicates", "are", "status", ""}, +func loadGraph(path string, t testing.TB) []quad.Quad { + var r io.Reader + var simpleGraph []quad.Quad + f, err := os.Open(path) + if err != nil { + t.Fatalf("Failed to open %q: %v", path, err) + } + defer f.Close() + r = f + + dec := cquads.NewDecoder(r) + q1, err := dec.Unmarshal() + if err != nil { + t.Fatalf("Failed to Unmarshal: %v", err) + } + for ; err == nil; q1, err = dec.Unmarshal() { + simpleGraph = append(simpleGraph, q1) + } + return simpleGraph } -func makeTestStore(data []quad.Quad) graph.QuadStore { +func makeTestStore(t testing.TB) graph.QuadStore { + simpleGraph := loadGraph("../../data/testdata.nq", t) qs, _ := graph.NewQuadStore("memstore", "", nil) w, _ := graph.NewQuadWriter("single", qs, nil) - for _, t := range data { + for _, t := range simpleGraph { w.AddQuad(t) } return qs @@ -104,89 +112,100 @@ func testSet(qs graph.QuadStore) []test { return []test{ { message: "use out", - path: StartPath(qs, "A").Out("follows"), - expect: []string{"B"}, + path: StartPath(qs, "alice").Out("follows"), + expect: []string{"bob"}, }, { message: "use in", - path: StartPath(qs, "B").In("follows"), - expect: []string{"A", "C", "D"}, + path: StartPath(qs, "bob").In("follows"), + expect: []string{"alice", "charlie", "dani"}, }, { message: "use path Out", - path: StartPath(qs, "B").Out(StartPath(qs, "predicates").Out("are")), - expect: []string{"F", "cool"}, + path: StartPath(qs, "bob").Out(StartPath(qs, "predicates").Out("are")), + expect: []string{"fred", "cool_person"}, }, { message: "use And", - path: StartPath(qs, "D").Out("follows").And( - StartPath(qs, "C").Out("follows")), - expect: []string{"B"}, + path: StartPath(qs, "dani").Out("follows").And( + StartPath(qs, "charlie").Out("follows")), + expect: []string{"bob"}, }, { message: "use Or", - path: StartPath(qs, "F").Out("follows").Or( - StartPath(qs, "A").Out("follows")), - expect: []string{"B", "G"}, + path: StartPath(qs, "fred").Out("follows").Or( + StartPath(qs, "alice").Out("follows")), + expect: []string{"bob", "greg"}, }, { message: "implicit All", path: StartPath(qs), - expect: []string{"A", "B", "C", "D", "E", "F", "G", "follows", "status", "cool", "status_graph", "predicates", "are"}, + expect: []string{"alice", "bob", "charlie", "dani", "emily", "fred", "greg", "follows", "status", "cool_person", "predicates", "are"}, }, { message: "follow", - path: StartPath(qs, "C").Follow(StartMorphism().Out("follows").Out("follows")), - expect: []string{"B", "F", "G"}, + path: StartPath(qs, "charlie").Follow(StartMorphism().Out("follows").Out("follows")), + expect: []string{"bob", "fred", "greg"}, }, { message: "followR", - path: StartPath(qs, "F").FollowReverse(StartMorphism().Out("follows").Out("follows")), - expect: []string{"A", "C", "D"}, + path: StartPath(qs, "fred").FollowReverse(StartMorphism().Out("follows").Out("follows")), + expect: []string{"alice", "charlie", "dani"}, }, { message: "is, tag, instead of FollowR", - path: StartPath(qs).Tag("first").Follow(StartMorphism().Out("follows").Out("follows")).Is("F"), - expect: []string{"A", "C", "D"}, + path: StartPath(qs).Tag("first").Follow(StartMorphism().Out("follows").Out("follows")).Is("fred"), + expect: []string{"alice", "charlie", "dani"}, tag: "first", }, { message: "use Except to filter out a single vertex", - path: StartPath(qs, "A", "B").Except(StartPath(qs, "A")), - expect: []string{"B"}, + path: StartPath(qs, "alice", "bob").Except(StartPath(qs, "alice")), + expect: []string{"bob"}, }, { message: "use chained Except", - path: StartPath(qs, "A", "B", "C").Except(StartPath(qs, "B")).Except(StartPath(qs, "A")), - expect: []string{"C"}, + path: StartPath(qs, "alice", "bob", "charlie").Except(StartPath(qs, "bob")).Except(StartPath(qs, "alice")), + expect: []string{"charlie"}, }, { message: "show a simple save", path: StartPath(qs).Save("status", "somecool"), tag: "somecool", - expect: []string{"cool", "cool", "cool"}, + expect: []string{"cool_person", "cool_person", "cool_person"}, }, { message: "show a simple saveR", - path: StartPath(qs, "cool").SaveReverse("status", "who"), + path: StartPath(qs, "cool_person").SaveReverse("status", "who"), tag: "who", - expect: []string{"G", "D", "B"}, + expect: []string{"greg", "dani", "bob"}, }, { message: "show a simple Has", - path: StartPath(qs).Has("status", "cool"), - expect: []string{"G", "D", "B"}, + path: StartPath(qs).Has("status", "cool_person"), + expect: []string{"greg", "dani", "bob"}, }, { message: "show a double Has", - path: StartPath(qs).Has("status", "cool").Has("follows", "F"), - expect: []string{"B"}, + path: StartPath(qs).Has("status", "cool_person").Has("follows", "fred"), + expect: []string{"bob"}, + }, + { + message: "use .Tag()-.Is()-.Back()", + path: StartPath(qs, "bob").In("follows").Tag("foo").Out("status").Is("cool_person").Back("foo"), + expect: []string{"dani"}, + }, + { + message: "do multiple .Back()s", + path: StartPath(qs, "emily").Out("follows").As("f").Out("follows").Out("status").Is("cool_person").Back("f").In("follows").In("follows").As("acd").Out("status").Is("cool_person").Back("f"), + tag: "acd", + expect: []string{"dani"}, }, } } func TestMorphisms(t *testing.T) { - qs := makeTestStore(simpleGraph) + qs := makeTestStore(t) for _, test := range testSet(qs) { var got []string if test.tag == "" { diff --git a/query/gremlin/gremlin_test.go b/query/gremlin/gremlin_test.go index 5c86b8f..55afd6a 100644 --- a/query/gremlin/gremlin_test.go +++ b/query/gremlin/gremlin_test.go @@ -262,14 +262,14 @@ var testQueries = []struct { query: ` g.V().InPredicates().All() `, - expect: []string{"follows", "status"}, + expect: []string{"are", "follows", "status"}, }, { message: "list all out predicates", query: ` g.V().OutPredicates().All() `, - expect: []string{"follows", "status"}, + expect: []string{"are", "follows", "status"}, }, }