Persistent predicates based on RocksDB

For those wanting to try using rocks-predicates module and need to see just what is needed. These are specific to Ubuntu but should also work with Debian. If you use Windows then Ubuntu can be installed using WSL 2.

Note: This was created as minimal working example for issue 5 but is still useful for others.

Note: Because of Issue 2 the ability to run queries from the top level after rdb_close/1 is run should not occur. In the future if the modules this relies on are changed then the example Prolog code here will probably need to use break/0 or similar.


Installing SWI-Prolog via PPA

Based on: Installing from PPA (Ubuntu Personal Package Archive)

$ sudo apt-get update
$ sudo apt-get upgade
$ sudo apt-get install software-properties-common
$ sudo apt-add-repository ppa:swi-prolog/devel
$ sudo apt-get update
$ sudo apt-get install swi-prolog
$ swipl

After starting SWI-Prolog check the version to make sure it is a recent version.
As of 07/12/2022 it is 8.5.14

?- version.
Welcome to SWI-Prolog (threaded, 64 bits, version 8.5.14)

Installing SWI-Prolog pack RocksDB using Git instead of pack_install/1 so that different commits can be tried.

$ sudo apt install libsnappy-dev liblz4-dev libzstd-dev libgflags-dev
$ mkdir -p  ~/.local/share/swi-prolog/pack
$ cd ~/.local/share/swi-prolog/pack
$ git clone https://github.com/JanWielemaker/rocksdb.git
$ git clone https://github.com/JanWielemaker/rocks-predicates.git
$ cd rocksdb
$ git clone https://github.com/facebook/rocksdb.git

As this only downloaded the source code for the packs they still need to be compiled.

$ swipl
?- pack_rebuild(rocksdb).

To see the checked out commit for a directory

groot@System:~/.local/share/swi-prolog/pack/rocksdb$ git show --oneline -s
e253458 (HEAD -> master, tag: V0.10.0, origin/master, origin/HEAD) ENHANCED: added statistics and logging options

OS: Ubuntu 22.04 LTS
SWI-Prolog: Install via PPA development version 8.5.14
RocksDB pack commit: e253458
RocksDB commit: a9565ccb2
rocks-predicates pack commit: 3071007


Example Prolog code with data (facts) for loading into RocksDB.
Note: This was originally created as a MWE for issue 5 thus the odd name of the module.

:- module('issue_5',
    [
        check/1,
        mars_lander/9
    ]).

% -------------------------------------------------------------------------

% Use the fact dummy and the user:file_search_path/2 to setup the Alias myapp.
dummy.

user:file_search_path(myapp,Dir) :-
    source_file(dummy,File),
    file_directory_name(File,Dir).

user:file_search_path(library, '/home/groot/.local/share/swi-prolog/pack/rocks-predicates').

% ----------------------------------------------------------------------------

:- use_module(library(rocksdb)).
:- use_module(library(rocks_preds)).

% ----------------------------------------------------------------------------

% Source: https://en.wikipedia.org/wiki/List_of_Mars_landers#Mars_landers
fact_01('1','Mars 2MV-3 No.1','04 Nov 1962','25 Nov 1962','890','-','-','Failed','Soviet Union').
fact_01('2','Mars 2','19 May 1971','27 Nov 1971','1210','45°S 47°E','-','Failed','Soviet Union').
fact_01('3','Mars 3','28 May 1971','02 Dec 1971','1210','45°S 202°E','Sirenum Terra','Partial Success','Soviet Union').
fact_01('4','Mars 6','05 Aug 1973','12 Mar 1974','635','23.90°S 19.4°W','Margaritifer Terra','Failed','Soviet Union').
fact_01('5','Mars 7','09 Aug 1973','-','635','-','-','Failed','Soviet Union').
fact_01('6','Viking 1','20 Aug 1975','20 Jul 1976','572','22.27°N 47.95°W','Chryse Planitia','Success','USA').
fact_01('7','Viking 2','09 Sep 1975','03 Sep 1976','572','47.64°N 225.71°W','Utopia Planitia','Success','USA').
fact_01('8','Phobos 1','07 Jul 1988','-','2600','-','-','Failed','Soviet Union').
fact_01('9','Phobos 2','12 Jul 1988','-','2600','-','-','Failed','Soviet Union').
fact_01('10','Mars 96','16 Nov 1996','-','3159','41°31N 153°77 W♦','-','Failed','Russia').
fact_01('11','Mars Pathfinder','04 Dec 1996','04 Jul 1997','361','19°7′48″ N 33°18′12″W','Ares Vallis','Success','USA').
fact_01('12','Mars Polar Lander','03 Jan 1999','03 Dec 1999','583','76°S 195°W','Ultimi Scopuli','Failed','USA').
fact_01('13','Beagle 2','02 Jun 2003','25 Dec 2003','33.2','11.5265°N 90.4295°E','Isidis Planitia','Failed','United Kingdom').
fact_01('14','Spirit rover','10 Jun 2003','4 Jan 2004','174','14.5684°S 175.4726°E','Gusev Crater','Success','USA').
fact_01('15','Opportunity rover','07 Jul 2003','25 Jan 2004','174','1.9462°S 354.4743°E','Meridiani Planum','Success','USA').
fact_01('16','Phoenix lander','04 Aug 2007','5 May 2008','350','68.22°N 125.7°W','Vastitas Borealis','Success','USA').
fact_01('17','Curiosity rover','26 Nov 2011','5 Aug 2012','899','4.5895°S 137.4417°E','Gale Crater','Operational','USA').
fact_01('18','Schiaparelli EDM','14 Mar 2016','19 Oct 2016','577','2.052°S 6.208°W','Meridiani Planum','Failed','European UnionESA/Russia').
fact_01('19','InSight Mars Lander','5 May 2018','26 Nov 2018','727','4.5°N 135.9°E','Elysium Planitia','Operational','USA').
fact_01('20','Perseverance rover','30 Jul 2020','18 Feb 2021','1,025','18.4447°N 77.4508°E','Jezero crater','Operational','USA').
fact_01('21','Tianwen-1','23 July 2020','14 May 2021','240','109.7°E, 25.1°N','Utopia Planitia','Operational','China').

% ----------------------------------------------------------------------------

user:file_search_path(rocksdb,myapp('RocksDB')).

% ----------------------------------------------------------------------------

check(1) :-
    load.

load :-
    absolute_file_name(rocksdb('.'),Rocksdb_directory),
    setup_call_cleanup(
        rdb_open(Rocksdb_directory,RocksDB),
        load_records,
        rdb_close(RocksDB)
    ).

load_records :-
    forall(
        fact_01(A1,A2,A3,A4,A5,A6,A7,A8,A9),
        rdb_assertz(fact_01(A1,A2,A3,A4,A5,A6,A7,A8,A9))
    ).

mars_lander(A1,A2,A3,A4,A5,A6,A7,A8,A9) :-
    rdb_clause(fact_01(A1,A2,A3,A4,A5,A6,A7,A8,A9),true).

Example run.

groot@Galaxy:~$ swipl
Welcome to SWI-Prolog (threaded, 64 bits, version 8.5.14)
...

?- working_directory(_,'/mnt/c/Users/groot/Projects/rocks-predicates_issue_5').
true.

?- [issue_5].
true.

?- check(1).
true.

?- current_blob(Blob,rocksdb).
Blob = <rocksdb>(0x557084c04070) ;
false.

?- rocks_preds:default_db(Dir).
Dir = '/mnt/c/Users/groot/Projects/rocks-predicates_issue_5/RocksDB'.

?- current_table(rocks_preds:H,_),H=rdb_clause_index(_,M:P/I,_).
H = rdb_clause_index(<rocksdb>(0x563e1abac0f0), issue_5:fact_01/9, _),
M = issue_5,
P = fact_01,
I = 9 ;
false.

?- rocks_preds:default_db(Dir),rocks_preds:rdb_predicate_property(Dir,issue_5:fact_01(_,_,_,_,_,_,_,_,_),number_of_clau
ses(N)).
Dir = '/mnt/c/Users/groot/Projects/rocks-predicates_issue_5/RocksDB',
N = 21.

?- issue_5:fact_01(A1,A2,A3,A4,A5,A6,A7,A8,A9).
A1 = '1',
A2 = 'Mars 2MV-3 No.1',
A3 = '04 Nov 1962',
A4 = '25 Nov 1962',
A5 = '890',
A6 = A7, A7 = (-),
A8 = 'Failed',
A9 = 'Soviet Union' ;
A1 = '2',
A2 = 'Mars 2',
A3 = '19 May 1971',
A4 = '27 Nov 1971',
A5 = '1210',
A6 = '45°S 47°E',
A7 = (-),
A8 = 'Failed',
A9 = 'Soviet Union' ;
A1 = '3',
A2 = 'Mars 3',
A3 = '28 May 1971',
A4 = '02 Dec 1971',
A5 = '1210',
A6 = '45°S 202°E',
A7 = 'Sirenum Terra',
A8 = 'Partial Success',
A9 = 'Soviet Union'
...
?-

Note: As this is a MWE for issue 5 this is how to recreate the issue.

?- mars_lander(A1,A2,A3,A4,A5,A6,A6,A8,A9).
A1 = '1',
A2 = 'Mars 2MV-3 No.1',
A3 = '04 Nov 1962',
A4 = '25 Nov 1962',
A5 = '890',
A6 = (-),
A8 = 'Failed',
A9 = 'Soviet Union' ;

Press space bar to see next result.

A1 = '5',
A2 = 'Mars 7',
A3 = '09 Aug 1973',
A4 = A6, A6 = (-),
A5 = '635',
A8 = 'Failed',
A9 = 'Soviet Union' .

Press enter here instead of space bar which causes the error.

ERROR: Arguments are not sufficiently instantiated
ERROR: In:
ERROR:    [5] '$execute_goal2'(user:mars_lander('5','Mars 7','09 Aug 1973',-,'635',-,-,'Failed','Soviet Union'),['A1'='5',...|...],true)
?-

Enjoy.