Skip to content

Commit

Permalink
Merge pull request #8 from fogfish/simplify-datalog-syntax
Browse files Browse the repository at this point in the history
Simplify datalog syntax
  • Loading branch information
fogfish committed Jan 18, 2019
2 parents 183108f + 3862b7f commit 03d5051
Show file tree
Hide file tree
Showing 21 changed files with 858 additions and 1,145 deletions.
18 changes: 10 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
script: make && make test
language: erlang
otp_release:
- 19.2
dist: trusty

before_install:
- curl -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.2.1.deb && sudo dpkg -i --force-confnew elasticsearch-5.2.1.deb && sudo service elasticsearch restart
- curl -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.4.deb && sudo dpkg -i --force-confnew elasticsearch-6.5.4.deb && sudo service elasticsearch restart

addons:
apt:
packages:
- oracle-java8-set-default
script:
- make
- make test
- ./rebar3 coveralls send

otp_release:
- 20.1
- 20.0
- 19.2

115 changes: 63 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ If you use `rebar3` you can include the library in your project with
The library requires Elastic search as a storage back-end. Use the docker container for development purpose.

```bash
docker run -it -p 9200:9200 fogfish/elasticsearch:6.2.3
docker run -p 9200:9200 -d fogfish/elasticsearch:6.2.3
```

Build library and run the development console
Expand All @@ -52,8 +52,8 @@ elasticlog:start().
%% deploy a schema
elasticlog:schema(Sock, #{
<<"schema:name">> => <<"xsd:string">>,
<<"schema:born">> => <<"xsd:string">>,
<<"schema:death">> => <<"xsd:string">>,
<<"schema:born">> => <<"xsd:date">>,
<<"schema:death">> => <<"xsd:date">>,
<<"schema:title">> => <<"xsd:string">>,
<<"schema:year">> => <<"xsd:integer">>,
<<"schema:director">> => <<"xsd:anyURI">>,
Expand All @@ -80,18 +80,21 @@ stream:foreach(
```

Querying and joining semantical data requires translation of facts into n-ary relation.
The library implements a σ function (`.stream`) that produces a stream of tuple from any
bucket. It takes a name of bucket as first argument followed by predicate names.
The library implements a σ function that produces a stream of tuple from any bucket.
The datalog notation support compact IRI as predicate name. The library uses IRI notation to
project ground-truth predicate on table.


**Basic queries**

```erlang
%%
%% define a query goal to match a person with `name` equal to `Ridley Scott`.
Q = "?- person(_, \"Ridley Scott\").
person(id, name) :-
.stream(\"imdb\", \"rdf:id\", \"schema:name\").".
Q = "
?- imdb:person(_, \"Ridley Scott\").
imdb:person(\"rdf:id\", \"schema:name\").
".

%%
%% parse and compile a query into executable function
Expand All @@ -110,12 +113,14 @@ stream:list(elasticlog:q(F, Sock)).
```erlang
%%
%% define a query to discover all movies produces in 1987
Q = "?- h(_, _).
movie(id, title, year) :-
.stream(\"imdb\", \"rdf:id\", \"schema:title\", \"schema:year\").
Q = "
?- movie(_, _).
imdb:movie(\"rdf:id\", \"schema:title\", \"schema:year\").
h(id, title) :-
movie(id, title, 1987).".
movie(id, title) :-
imdb:movie(id, title, 1987).
".

%%
%% parse and compile a query into executable function
Expand All @@ -131,17 +136,19 @@ F = datalog:c(elasticlog, datalog:p(Q)).
stream:list(elasticlog:q(F, Sock)).
```

**Predicates**
**Infix Predicates**

```erlang
%%
%% define a query to discover all movies produces before 1984
Q = "?- h(_, _).
movie(title, year) :-
.stream(\"imdb\", \"schema:title\", \"schema:year\").
Q = "
?- movie(_, _).
imdb:movie(\"rdf:id\", \"schema:title\", \"schema:year\").
h(title, year) :-
movie(title, year), year < 1984.".
movie(title, year) :-
imdb:movie(id, title, year), year < 1984.
".

%%
%% parse and compile a query into executable function
Expand All @@ -164,18 +171,18 @@ stream:list(elasticlog:q(F, Sock)).
```erlang
%%
%% define a query to discover actors of all movies produced before 1984
Q = "?- h(_, _).
movie(title, year) :-
.stream(\"imdb\", \"schema:title\", \"schema:year\", \"schema:cast\").
Q = "
?- actors(_, _).
person(id, name) :-
.stream(\"imdb\", \"rdf:id\", \"schema:name\").
imdb:movie(\"schema:title\", \"schema:year\", \"schema:cast\").
imdb:person(\"rdf:id\", \"schema:name\").
h(title, name) :-
movie(title, year, cast),
.flat(cast),
person(cast, name),
year < 1984.".
actors(title, name) :-
imdb:movie(title, year, cast),
.flat(cast),
imdb:person(cast, name),
year < 1984.
".

%%
%% parse and compile a query into executable function
Expand Down Expand Up @@ -204,17 +211,17 @@ stream:list(elasticlog:q(F, Sock)).
```erlang
%%
%% define a query to discover all movies with Sylvester Stallone
Q = "?- h(_).
movie(title, cast) :-
.stream(\"imdb\", \"schema:title\", \"schema:cast\").
Q = "
?- movie(_).
person(id, name) :-
.stream(\"imdb\", \"rdf:id\", \"schema:name\").
imdb:movie(\"schema:title\", \"schema:cast\").
imdb:person(\"rdf:id\", \"schema:name\").
h(title) :-
person(id, name),
movie(title, id),
name = \"Sylvester Stallone\".".
movie(title) :-
imdb:person(id, name),
imdb:movie(title, id),
name = \"Sylvester Stallone\".
".

%%
%% parse and compile a query into executable function
Expand Down Expand Up @@ -247,9 +254,11 @@ x(_) :-
```erlang
%%
%% define a query to count release per decade
Q = "?- movie(_).
movie(year) :-
.select(\"imdb\", \"schema:year\"), histogram(10).".
Q = "
?- imdb:year(_).
imdb:year(histogram 10 \"schema:year\").
".

%%
%% parse and compile a query into executable function
Expand All @@ -270,16 +279,16 @@ stream:list(elasticlog:q(F, Sock)).
```erlang
%%
%% define a query to count releases by 5 top directors
Q = "?- h(_, _).
movie(id) :-
.select(\"imdb\", \"schema:director\"), category(5).
Q = "
?- top(_, _).
person(id, name) :-
.stream(\"imdb\", \"rdf:id\", \"schema:name\").
imdb:director(category 5 \"schema:director\").
imdb:person(\"rdf:id\", \"schema:name\").
h(name, id) :-
movie(id),
person(id, name).".
top(name, id) :-
imdb:director(id),
imdb:persion(id, name).
".

%%
%% parse and compile a query into executable function
Expand All @@ -305,9 +314,11 @@ The library support an implicit query constrains. It supports a use-case where c
```erlang
%%
%% define a query goal to match a person with `name` equal to `Ridley Scott`.
Q = "?- person(_, \"Ridley Scott\").
person(id, name) :-
.stream(\"imdb\", \"rdf:id\", \"schema:name\").".
Q = "
?- imdb:person(_, \"Ridley Scott\").
imdb:person(\"rdf:id\", \"schema:name\").
s".

%%
%% parse and compile a query into executable function
Expand Down
14 changes: 13 additions & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

{deps, [
datum,
datalog,
% datalog,

{feta, ".*",
{git, "https://github.com/fogfish/feta", {branch, master}}
Expand All @@ -16,7 +16,19 @@
{git, "https://github.com/fogfish/semantic", {branch, master}}
},

{datalog, ".*",
{git, "https://github.com/fogfish/datalog", {branch, master}}
},

{esio, ".*",
{git, "https://github.com/fogfish/esio", {branch, master}}
}
]}.

%%
%%
{plugins , [coveralls]}.
{cover_enabled , true}.
{cover_export_enabled , true}.
{coveralls_coverdata , "/tmp/test/elasticlog/ct.coverdata"}.
{coveralls_service_name , "travis-ci"}.
8 changes: 8 additions & 0 deletions rebar.config.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
case os:getenv("TRAVIS") of
"true" ->
JobId = os:getenv("TRAVIS_JOB_ID"),
lists:keystore(coveralls_service_job_id, 1, CONFIG, {coveralls_service_job_id, JobId});
_ ->
CONFIG
end.

16 changes: 8 additions & 8 deletions rebar.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{"1.1.0",
[{<<"datalog">>,{pkg,<<"datalog">>,<<"1.2.2">>},0},
[{<<"datalog">>,
{git,"https://github.com/fogfish/datalog",
{ref,"e61951aca0b115048e59a25bef5edbcbe36d0710"}},
0},
{<<"datum">>,{pkg,<<"datum">>,<<"4.5.0">>},0},
{<<"esio">>,
{git,"https://github.com/fogfish/esio",
Expand Down Expand Up @@ -30,17 +33,14 @@
{<<"pipe">>,{pkg,<<"pipes">>,<<"2.0.0">>},2},
{<<"semantic">>,
{git,"https://github.com/fogfish/semantic",
{ref,"6db551b5bbf9785bc6a168b5726ecc65638e8b06"}},
{ref,"d8e2a8c712b13595ae7fda0c0b4ab68c24ea946e"}},
0},
{<<"uid">>,
{git,"https://github.com/fogfish/uid",
{ref,"698ebcb27b273a2f879d8bd24be5f5ea54c98850"}},
1}]}.
{<<"uid">>,{pkg,<<"uid">>,<<"1.3.3">>},1}]}.
[
{pkg_hash,[
{<<"datalog">>, <<"F9F0434CDFD722304DAFF7BAA036221949968754B9B35109C85304D4A2496D41">>},
{<<"datum">>, <<"0F7F064A509632C70448F9BC7E7295BD5F9CD6E3207863B176416EF14FD19E0C">>},
{<<"jsx">>, <<"D2F6E5F069C00266CAD52FB15D87C428579EA4D7D73A33669E12679E203329DD">>},
{<<"meck">>, <<"1F7B1A9F5D12C511848FEC26BBEFD09A21E1432EADB8982D9A8ACEB9891A3CF2">>},
{<<"pipe">>, <<"A10DF54F1EC80EAA491EE7BD689C794886E6E668A9F05436514695E9CF5370AE">>}]}
{<<"pipe">>, <<"A10DF54F1EC80EAA491EE7BD689C794886E6E668A9F05436514695E9CF5370AE">>},
{<<"uid">>, <<"31CA7E3C9D53716A2FF425E18A33E32C141B763D47B174F757D7CAF7E4228A67">>}]}
].
2 changes: 1 addition & 1 deletion src/elasticlog.app.src
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{application, elasticlog,
[
{description, "elastic search datalog"},
{vsn, "0.7.12"},
{vsn, "1.0.0"},
{modules, []},
{registered, []},
{applications,[
Expand Down

0 comments on commit 03d5051

Please sign in to comment.