document path context, pipe it through Reversal() as well, and update the godoc
This commit is contained in:
parent
f74051a520
commit
3d286f5245
2 changed files with 64 additions and 35 deletions
|
|
@ -20,13 +20,35 @@ type applyMorphism func(graph.QuadStore, graph.Iterator, *context) (graph.Iterat
|
|||
|
||||
type morphism struct {
|
||||
Name string
|
||||
Reversal func() morphism
|
||||
Reversal func(*context) (morphism, *context)
|
||||
Apply applyMorphism
|
||||
tags []string
|
||||
context context
|
||||
}
|
||||
|
||||
// context allows a high-level change to the way paths are constructed. Some
|
||||
// functions may change the context, causing following chained calls to act
|
||||
// cdifferently.
|
||||
//
|
||||
// In a sense, this is a global state which can be changed as the path
|
||||
// continues. And as with dealing with any global state, care should be taken:
|
||||
//
|
||||
// When modifying the context in Apply(), please copy the passed struct,
|
||||
// modifying the relevant fields if need be (or pass the given context onward).
|
||||
//
|
||||
// Under Reversal(), any functions that wish to change the context should
|
||||
// appropriately change the passed context (that is, the context that came after
|
||||
// them will now be what the application of the function would have been) and
|
||||
// then yield a pointer to their own member context as the return value.
|
||||
//
|
||||
// For more examples, look at the morphisms which claim the individual fields.
|
||||
type context struct {
|
||||
labelSet Path
|
||||
// Represents the path to the limiting set of labels that should be considered under traversal.
|
||||
// inMorphism, outMorphism, et al should constrain edges by this set.
|
||||
// A nil in this field represents all labels.
|
||||
//
|
||||
// Claimed by the withLabel morphism
|
||||
labelSet *Path
|
||||
}
|
||||
|
||||
// Path represents either a morphism (a pre-defined path stored for later use),
|
||||
|
|
@ -34,7 +56,7 @@ type context struct {
|
|||
type Path struct {
|
||||
stack []morphism
|
||||
qs graph.QuadStore // Optionally. A nil qs is equivalent to a morphism.
|
||||
baseContext *context
|
||||
baseContext context
|
||||
}
|
||||
|
||||
// IsMorphism returns whether this Path is a morphism.
|
||||
|
|
@ -74,8 +96,11 @@ func NewPath(qs graph.QuadStore) *Path {
|
|||
// Reverse returns a new Path that is the reverse of the current one.
|
||||
func (p *Path) Reverse() *Path {
|
||||
newPath := NewPath(p.qs)
|
||||
ctx := &newPath.baseContext
|
||||
for i := len(p.stack) - 1; i >= 0; i-- {
|
||||
newPath.stack = append(newPath.stack, p.stack[i].Reversal())
|
||||
var revMorphism morphism
|
||||
revMorphism, ctx = p.stack[i].Reversal(ctx)
|
||||
newPath.stack = append(newPath.stack, revMorphism)
|
||||
}
|
||||
return newPath
|
||||
}
|
||||
|
|
@ -153,10 +178,10 @@ func (p *Path) Both(via ...interface{}) *Path {
|
|||
// predicates from the current nodes.
|
||||
//
|
||||
// For example:
|
||||
// // Returns a list of predicates valid from "bob"
|
||||
// //
|
||||
// // Will return []string{"follows"} if there are any things that "follow" Bob
|
||||
// StartPath(qs, "bob").InPredicates()
|
||||
// // Returns a list of predicates valid from "bob"
|
||||
// //
|
||||
// // Will return []string{"follows"} if there are any things that "follow" Bob
|
||||
// StartPath(qs, "bob").InPredicates()
|
||||
func (p *Path) InPredicates() *Path {
|
||||
p.stack = append(p.stack, predicatesMorphism(true))
|
||||
return p
|
||||
|
|
@ -166,11 +191,11 @@ func (p *Path) InPredicates() *Path {
|
|||
// predicates from the current nodes.
|
||||
//
|
||||
// For example:
|
||||
// // Returns a list of predicates valid from "bob"
|
||||
// //
|
||||
// // Will return []string{"follows", "status"} if there are edges from "bob"
|
||||
// // labelled "follows", and edges from "bob" that describe his "status".
|
||||
// StartPath(qs, "bob").OutPredicates()
|
||||
// // Returns a list of predicates valid from "bob"
|
||||
// //
|
||||
// // Will return []string{"follows", "status"} if there are edges from "bob"
|
||||
// // labelled "follows", and edges from "bob" that describe his "status".
|
||||
// StartPath(qs, "bob").OutPredicates()
|
||||
func (p *Path) OutPredicates() *Path {
|
||||
p.stack = append(p.stack, predicatesMorphism(false))
|
||||
return p
|
||||
|
|
@ -220,8 +245,8 @@ func (p *Path) FollowReverse(path *Path) *Path {
|
|||
// tag, and propagate that to the result set.
|
||||
//
|
||||
// For example:
|
||||
// // Will return []map[string]string{{"social_status: "cool"}}
|
||||
// StartPath(qs, "B").Save("status", "social_status"
|
||||
// // Will return []map[string]string{{"social_status: "cool"}}
|
||||
// StartPath(qs, "B").Save("status", "social_status"
|
||||
func (p *Path) Save(via interface{}, tag string) *Path {
|
||||
p.stack = append(p.stack, saveMorphism(via, tag))
|
||||
return p
|
||||
|
|
@ -244,12 +269,12 @@ func (p *Path) Has(via interface{}, nodes ...string) *Path {
|
|||
// 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")
|
||||
// // 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
|
||||
|
||||
ctx := &newPath.baseContext
|
||||
for {
|
||||
if i < 0 {
|
||||
return p.Reverse()
|
||||
|
|
@ -263,7 +288,9 @@ func (p *Path) Back(tag string) *Path {
|
|||
}
|
||||
}
|
||||
}
|
||||
newPath.stack = append(newPath.stack, p.stack[i].Reversal())
|
||||
var revMorphism morphism
|
||||
revMorphism, ctx = p.stack[i].Reversal(ctx)
|
||||
newPath.stack = append(newPath.stack, revMorphism)
|
||||
i--
|
||||
}
|
||||
}
|
||||
|
|
@ -291,7 +318,7 @@ func (p *Path) BuildIteratorOn(qs graph.QuadStore) graph.Iterator {
|
|||
func (p *Path) Morphism() graph.ApplyMorphism {
|
||||
return func(qs graph.QuadStore, it graph.Iterator) graph.Iterator {
|
||||
i := it.Clone()
|
||||
ctx := p.baseContext
|
||||
ctx := &p.baseContext
|
||||
for _, m := range p.stack {
|
||||
i, ctx = m.Apply(qs, i, ctx)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue