Skip to content

Commit

Permalink
LinkedQL: Collect Step (#920)
Browse files Browse the repository at this point in the history
* Add Collect Step

* Update description

* Correct path construct by collect

* Test collect and make it work like properties
  • Loading branch information
iddan authored Apr 4, 2020
1 parent 89bd614 commit e88e947
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
58 changes: 58 additions & 0 deletions query/linkedql/steps/collect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package steps

import (
"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/query"
"github.com/cayleygraph/cayley/query/linkedql"
"github.com/cayleygraph/cayley/query/path"
"github.com/cayleygraph/quad"
"github.com/cayleygraph/quad/voc"
)

func init() {
linkedql.Register(&Collect{})
}

var _ linkedql.IteratorStep = (*Collect)(nil)
var _ linkedql.PathStep = (*Collect)(nil)

// Collect corresponds to .view().
type Collect struct {
From linkedql.PathStep `json:"from"`
Name quad.IRI `json:"name"`
}

// Description implements Step.
func (s *Collect) Description() string {
return "Recursively resolves values of a list (also known as RDF collection)"
}

// BuildIterator implements linkedql.IteratorStep.
func (s *Collect) BuildIterator(qs graph.QuadStore, ns *voc.Namespaces) (query.Iterator, error) {
return linkedql.NewValueIteratorFromPathStep(s, qs, ns)
}

var (
first = quad.IRI("rdf:first").Full()
rest = quad.IRI("rdf:rest").Full()
rdfNil = quad.IRI("rdf:nil").Full()
)

// BuildPath implements linkedql.PathStep.
func (s *Collect) BuildPath(qs graph.QuadStore, ns *voc.Namespaces) (*path.Path, error) {
fromPath, err := s.From.BuildPath(qs, ns)
if err != nil {
return nil, err
}
p := fromPath.
Out(s.Name).
Save(first, string(first)).
Save(rest, string(rest)).
Or(
fromPath.Out(s.Name).FollowRecursive(rest, -1, nil).
Save(first, string(first)).
Save(rest, string(rest)),
).
Or(fromPath.Save(s.Name, string(s.Name)))
return p, nil
}
34 changes: 34 additions & 0 deletions query/linkedql/steps/test-cases/collect.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"data": {
"http://example.com/friends": {
"@list": [
{ "@id": "http://example.com/alice" },
{ "@id": "http://example.com/bob" }
]
}
},
"query": {
"@context": {
"linkedql": "http://cayley.io/linkedql"
},
"@type": "linkedql:Documents",
"linkedql:from": {
"@type": "linkedql:Collect",
"linkedql:name": "http://example.com/friends",
"linkedql:from": {
"@type": "linkedql:Vertex",
"linkedql:values": []
}
}
},
"results": [
{
"http://example.com/friends": {
"@list": [
{ "@id": "http://example.com/alice" },
{ "@id": "http://example.com/bob" }
]
}
}
]
}

0 comments on commit e88e947

Please sign in to comment.