项目作者: Swirrl

项目描述 :
:tea: SPARQL-like DSL for querying in memory Linked Data Models
高级语言: Clojure
项目地址: git://github.com/Swirrl/matcha.git
创建时间: 2018-09-04T23:55:30Z
项目社区:https://github.com/Swirrl/matcha

开源协议:Eclipse Public License 1.0

下载


Matcha

Clojars Project cljdoc badge

A Clojure DSL to query in memory triple models with a SPARQL like
language. Matcha provides simple BGP (Basic Graph Pattern) style
queries on in memory graphs of linked data triples.

Matcha

Whilst Matcha is intended to query RDF models it can also be used to
query arbitrary clojure data, so long as it consists of Clojure values
stored in 3/tuple vectors, each entity of the triple is assumed to
follow Clojure value equality semantics.

The primary use cases for Matcha are to make handling graphs of RDF
data easy by querying data with SPARQL-like queries. A typical
workflow is to CONSTRUCT data from a backend SPARQL query, and then
use Matcha to query this graph locally.

Features

  • SPARQL-like BGP queries across multiple triple patterns.
  • Parameterised queries using just clojure let.
  • Ability to index your database, with index-triples. In order to
    be queried Matcha needs to have indexed the data; if your data is
    unindexed it will index it before running the query, and then
    dispose of the index. This can lead to poor performance when you
    want to query the same set of data multiple times.
  • Construct graph query results directly into clojure datastructures.
  • Support for VALUES clauses (unlike in SPARQL we do not yet support
    binding arbitrary tuples/tables). So we only support the
    VALUES ?x { ... } form.
  • Support for OPTIONALs with SPARQL-like semantics.

Limitations

The initial implementation is macro heavy. This means use cases where
you want to dynamically create in memory queries may be more awkward.

Currently there is no support for the following SPARQL-like features:

  1. Reasoning on in memory vocabularies with RDFS (maybe OWL)
  2. Clojurescript support (planned)

Usage

Matcha defines some primary query functions select, select-1,
build, build-1, construct, construct-1 and ask.

First lets define an in memory database of triples, in reality this
could come from a SPARQL query CONSTRUCT, but here we’ll just define
some RDF-like data inline.

Triples can be vectors of clojure values or any datastructure that
supports positional destructuring via clojure.lang.Indexed, this
allows Matcha to work grafter.rdf.protocols.Statement records.
Matcha works with any clojure values in the triples, be they java
URI’s, or clojure keywords.

  1. (def friends-db [[:rick :rdfs/label "Rick"]
  2. [:martin :rdfs/label "Martin"]
  3. [:katie :rdfs/label "Katie"]
  4. [:julie :rdfs/label "Julie"]
  5. [:rick :foaf/knows :martin]
  6. [:rick :foaf/knows :katie]
  7. [:katie :foaf/knows :julie]
  8. [:rick :a :foaf/Person]
  9. [:katie :a :foaf/Person]
  10. [:martin :a :foaf/Person]])

Now we can build our query functions:

General Query Semantics

There are two main concepts to Matcha queries. They typically define:

  1. a projection, which states what variables to return to your Clojure
    program, and the datastructure they should be returned in.
  2. a Basic Graph Pattern (BGP), that defines the pattern of the graph
    traversal.

BGPs have some semantics you need to be aware of:

  • Clojure symbols beginning with a ? are treated specially as query
    variables.
  • Other symbols are resolved to their values.

build

build always groups returned solutions into a sequence of clojure
maps, where the subjects are grouped into maps, and the maps are
grouped by their properties. If a property has multiple values they
will be rolled up into a set, otherwise they will be a scalar value.

Each map returned by build typically represents a resource in the
built graph, which is projected into a sequence of maps, with
potentially multi-valued keys.

It takes a binding for ?subject of the map, a map form specifying
the projection of other property/value bindings a bgp and a
database.

  1. (build ?person
  2. {:foaf/knows ?friends}
  3. [[?person :foaf/knows ?friends]]
  4. friends-db)
  5. ;; => ({:grafter.rdf/uri :rick, :foaf/knows #{:martin :katie}}
  6. ;; {:grafter.rdf/uri :katie, :foaf/knows :julie}

NOTE: :foaf/knows is projected into a set of values for :rick, but
a single scalar value for :katie.

The ?subject is by default associated with the key
:grafter.rdf/uri. If you wish to specify this key yourself you can
by providing a key/value pair as the subject: e.g. substituting
?person for [:id ?person] changes the return values like so:

  1. (build [:id ?person]
  2. {:foaf/knows ?friends}
  3. [[?person :foaf/knows ?friends]]
  4. friends-db)
  5. ;; => ({:id :rick, :foaf/knows #{:martin :katie}}
  6. ;; {:id :katie, :foaf/knows :julie}

Because build knows it is always returning a sequence of maps, it
will remove any keys corresponding to unbound variables introduced
through optionals. This is unlike construct.

select

select compiles a query from your arguments, that returns results as a
sequence of tuples. It is directly analagous to SPARQL’s SELECT query.

The bgp argument is analagous to a SPARQL WHERE clause and should be
a BGP.

When called with one argument, select projects all ?qvars used in the
query. This is analagous to SELECT * in SPARQL:

  1. (def rick-knows
  2. (select
  3. [[:rick :foaf/knows ?p2]
  4. [?p2 :rdfs/label ?name]]))
  5. (rick-knows friends-db)
  6. ;; => ["Martin" "Katie"]

When called with two arguments select expects the first argument to be a
vector of variables to project into the solution sequence.

  1. (def rick-knows (select [?name]
  2. [[:rick :foaf/knows ?p2]
  3. [?p2 :rdfs/label ?name]]))
  4. (rick-knows friends-db)
  5. ;; => ["Martin" "Katie"]

There is also select-1 which is just like select but returns just
the first solution.

construct

NOTE: if you’re using you construct to return maps, you should first
consider using build which fixes some issues present in common
construct usage.

CONSTRUCTs allow you to construct arbitrary clojure data structures
directly from your query results, and position the projected query
variables where ever you want within the projected datastructure
template.

Args:

  • construct-pattern: an arbitrary clojure data structure. Results
    will be projected into the ?qvar “holes”.
  • bgps: this argument is analagous to a SPARQL WHERE clause and should be
    a BGPs.
  • db-or-idx: A matcha “database”.

When called with two arguments construct returns a query function
that accepts a db-or-idx as its only argument. When called, the
function returns a sequence of matching tuples in the form of the
construct-pattern.

  1. (construct {:grafter.rdf/uri :rick
  2. :foaf/knows {:grafter.rdf/uri ?p
  3. :rdfs/label ?name}}
  4. [[:rick :foaf/knows ?p]
  5. [?p :rdfs/label ?name]])
  6. ;; => (fn [db-or-idx] ...)

When called with 3 arguments, queries the db-or-idx directly, returning a
sequence of results in the form of the construct-pattern.

  1. (construct {:grafter.rdf/uri :rick
  2. :foaf/knows {:grafter.rdf/uri ?p
  3. :rdfs/label ?name}}
  4. [[:rick :foaf/knows ?p]
  5. [?p :rdfs/label ?name]]
  6. friends-db)
  7. ;; => {:grafter.rdf/uri :rick
  8. ;; :foaf/knows #{{:grafter.rdf/uri :martin, :rdfs/label "Martin"}
  9. ;; {:grafter.rdf/uri :katie, :rdfs/label "Katie"}}}

Maps in a projection that contain the special key of
:grafter.rdf/uri trigger extra behaviour, and cause the query
engine to group solutions by subject, and merge values into clojure
sets. For example in the above query you’ll notice that foaf:knows
groups its solutions. If you don’t want these maps to be grouped,
don’t include the magic key :grafter.rdf/uri in the top level
projection.

There is also construct-1 which is just like construct but returns
only the first solution.

See the unit
tests

for more examples, including examples that use Matcha with Grafter
Statements and vocabularies.

ask

ask is the only query that doesn’t specify an explicit projection.
It accepts a BGP, like the other query types and returns a boolean
result if there were any matches found.

  1. (def any-triples? (ask [[?s ?p ?o]])
  2. (any-triples? friends-db) ;; => true

Parameterising queries

You can parameterise Matcha queries simply by adding a lexical binding or wrapping a function call over your Matcha query. For example

  1. (defn lookup-friends [person-id database]
  2. (->> database
  3. (construct {:grafter.rdf/uri ?friend
  4. :name ?name}
  5. [[person-id :foaf/knows ?friend]
  6. [?friend :rdfs/label ?name]]))
  7. (lookup-friends :rick friends-db)
  8. ;; => [{:grafter.rdf/uri :martin, :name "Martin"}
  9. ;; {:grafter.rdf/uri :katie, :name "Katie"}]

OPTIONALs

We support SPARQL-like OPTIONALs in all query types with the following syntax:

  1. (defn lookup-name [person-id database]
  2. (select [?name]
  3. [[person-id :a :foaf/Person]
  4. (optional [[person :rdfs/label ?name]])
  5. (optional [[person :foaf/name ?name]])]))

VALUEs

We support dynamic VALUEs clauses in all query types like so:

  1. (defn lookup-names [person-ids database]
  2. (select [?name]
  3. [(values ?person-id person-ids)
  4. [?person-id :rdfs/label ?name]]))
  5. (lookup-names [:rick :katie] friends-db) ;; => ["Rick", "Katie"]

You can also hardcode the values into the query:

  1. (defn lookup-names [person-ids database]
  2. (select [?name]
  3. [(values ?person-id [:rick :katie])
  4. [?person-id :rdfs/label ?name]]))

Any “flat collection” (i.e. a sequential? or a set?) is valid
on the right hand side of a values binding.

Performance

Matcha is intended to be used on modest sizes of data, typically
thousands of triples, and usually no more than a few hundred thousand
triples. Proper benchmarking hasn’t yet been done but finding all
solutions on a database of a million triples can be done on a laptop
in less than 10 seconds. Query time scaling seems to be roughly
linear with the database size.

Avoiding clj-kondo lint errors with matcha macros

Matcha exports some clj-kondo configuration which prevents clj-kondo
warning about unbound variables when using the matcha query macros.

You can import these configs into your project with the following
command:

  1. $ clj-kondo --copy-configs --dependencies --lint "$(clojure -Spath)"
  2. Imported config to .clj-kondo/grafter/matcha.alpha. To activate, add "grafter/matcha.alpha" to :config-paths in .clj-kondo/config.edn.

Then simply add the following to .clj-kondo/config.edn:

  1. {:config-paths ["grafter/matcha.alpha"]}

Developing Matcha

Matcha uses tools.build and
tools.deps for builds,
development and testing.

The command:

  1. $ clojure -T:build test

Will run the tests, whilst

  1. $ clojure -T:build build
  2. $ clojure -T:build install

can be used to build and install a jar into your local mvn repository.

However for consuming local Matcha changes in local projects you are
usually better using tools.deps :classpath-overrides, or creating
a branch and consuming via a :git/url.

Deploying to Clojars

For deployments CircleCI is setup
to automatically deploy tags of the form vX.Y.Z where X.Y.Z are
major.minor.patch numbers. If you have permissions (i.e. you are
a Swirrl developer) the recommended workflow is to create a new
release of the main branch in github with a tag that bumps the
version number appropriately.

NOTE: For this step to work you will need appropriate deployment
privileges on clojars.org.

License

Copyright © Swirrl IT Ltd 2018

Distributed under the Eclipse Public License either version 1.0 or (at
your option) any later version.