-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add a match step * Prefix use of RDFG in schema * Evaluate JSON LD document in runtime * Fix JSON LD parsing with context * Check for lone @id before counting quads in pattern * Give match from a min-cardinality 0 * Handle nil from * Group consts * Use voc and tag Lookup * Fix small issues regarding syntax * Different syntax for value * Define hasMinCardinality as requested * linkedql: Wrap shared properties of restrictions with owlRestriction * linkedql: Correct name to owlPropertyRestriction and share more code * linkedql: Add fixme comemnt in match.go * linkedql: Add fixme comment to steps_test
- Loading branch information
Showing
5 changed files
with
237 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package linkedql | ||
|
||
// GraphPattern represents a JSON-LD document | ||
type GraphPattern = map[string]interface{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package steps | ||
|
||
import ( | ||
"fmt" | ||
|
||
"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/jsonld" | ||
"github.com/cayleygraph/quad/voc" | ||
"github.com/cayleygraph/quad/voc/rdf" | ||
"github.com/cayleygraph/quad/voc/rdfs" | ||
) | ||
|
||
func init() { | ||
linkedql.Register(&Match{}) | ||
} | ||
|
||
var _ linkedql.IteratorStep = (*Match)(nil) | ||
var _ linkedql.PathStep = (*Match)(nil) | ||
|
||
// Match corresponds to .has(). | ||
type Match struct { | ||
From linkedql.PathStep `json:"from" minCardinality:"0"` | ||
Pattern linkedql.GraphPattern `json:"pattern"` | ||
} | ||
|
||
// Description implements Step. | ||
func (s *Match) Description() string { | ||
return "filters all paths which are, at this point, on the subject for the given predicate and object, but do not follow the path, merely filter the possible paths. Usually useful for starting with all nodes, or limiting to a subset depending on some predicate/value pair." | ||
} | ||
|
||
// BuildIterator implements linkedql.IteratorStep. | ||
func (s *Match) BuildIterator(qs graph.QuadStore, ns *voc.Namespaces) (query.Iterator, error) { | ||
return linkedql.NewValueIteratorFromPathStep(s, qs, ns) | ||
} | ||
|
||
// BuildPath implements linkedql.PathStep. | ||
func (s *Match) BuildPath(qs graph.QuadStore, ns *voc.Namespaces) (*path.Path, error) { | ||
var p *path.Path | ||
if s.From != nil { | ||
fromPath, err := s.From.BuildPath(qs, ns) | ||
if err != nil { | ||
return nil, err | ||
} | ||
p = fromPath | ||
} else { | ||
p = path.StartPath(qs) | ||
} | ||
|
||
// Get quads | ||
quads, err := parsePattern(s.Pattern, ns) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// Group quads to subtrees | ||
entities := make(map[quad.Value]map[quad.Value][]quad.Value) | ||
for _, q := range quads { | ||
entity := linkedql.AbsoluteValue(q.Subject, ns) | ||
property := linkedql.AbsoluteValue(q.Predicate, ns) | ||
value := linkedql.AbsoluteValue(q.Object, ns) | ||
properties, ok := entities[entity] | ||
if !ok { | ||
properties = make(map[quad.Value][]quad.Value) | ||
entities[entity] = properties | ||
} | ||
if isSingleEntityQuad(q) { | ||
continue | ||
} | ||
properties[property] = append(properties[property], value) | ||
} | ||
|
||
for entity, properties := range entities { | ||
if iri, ok := entity.(quad.IRI); ok { | ||
p = p.Is(iri) | ||
} | ||
// FIXME(iddan): this currently flattens all nested objects, which is totally incorrect; recurse or limit allowed json-ld | ||
for property, values := range properties { | ||
p = p.Has(property, values...) | ||
} | ||
} | ||
|
||
return p, nil | ||
} | ||
|
||
func parsePattern(pattern linkedql.GraphPattern, ns *voc.Namespaces) ([]quad.Quad, error) { | ||
context := make(map[string]interface{}) | ||
for _, namespace := range ns.List() { | ||
context[namespace.Prefix] = namespace.Full | ||
} | ||
patternClone := linkedql.GraphPattern{ | ||
"@context": context, | ||
} | ||
for key, value := range pattern { | ||
patternClone[key] = value | ||
} | ||
reader := jsonld.NewReaderFromMap(patternClone) | ||
quads, err := quad.ReadAll(reader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if id, ok := patternClone["@id"]; ok && len(quads) == 0 { | ||
idString, ok := id.(string) | ||
if !ok { | ||
return nil, fmt.Errorf("Unexpected type for @id %T", idString) | ||
} | ||
quads = append(quads, makeSingleEntityQuad(quad.IRI(idString))) | ||
} | ||
if len(quads) == 0 && len(pattern) != 0 { | ||
return nil, fmt.Errorf("Pattern does not parse to any quad. `{}` is the only pattern allowed to not parse to any quad") | ||
} | ||
return quads, nil | ||
} | ||
|
||
func makeSingleEntityQuad(id quad.IRI) quad.Quad { | ||
return quad.Quad{Subject: id, Predicate: quad.IRI(rdf.Type), Object: quad.IRI(rdfs.Resource)} | ||
} | ||
|
||
func isSingleEntityQuad(q quad.Quad) bool { | ||
// rdf:type rdfs:Resource is always true but not expressed in the graph. | ||
// it is used to specify an entity without specifying a property. | ||
return q.Predicate == quad.IRI(rdf.Type) && q.Object == quad.IRI(rdfs.Resource) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters