At least with Perl you can dump your database as a Prolog program:
Let's see what we can get with this from a simple database like this:use strict; use DBIx::Class::Loader; use AI::Prolog; my $loader = DBIx::Class::Loader->new( dsn => $ARGV[0], user => $ARGV[1], password => $ARGV[2], namespace => 'Data', ); my $prog; for my $c ( $loader->classes ) { my $rs = $c->search(); $prog .= '% ' . join ( ',', $c->columns ) . "\n"; while ( my $row = $rs->next ){ $prog .= $c->table . '('; my @quoted = map {AI::Prolog->quote($row->$_)} $c->columns; $prog .= join ',', @quoted; $prog .= ").\n"; } } print $prog;
Run the dumper:create table usr(id integer primary key, name varchar(100), cityid int +eger); create table city (id integer primary key, name varchar(100)); insert into usr values (1, 'John', 1); insert into usr values (2, 'Marry', 1); insert into usr values (3, 'Eva', 2); insert into usr values (4, 'Zby', 3); insert into city values (1, 'New York'); insert into city values (2, 'London'); insert into city values (3, 'Warsaw');
What I get in the mytables file is:perl dump.pl dbi:Pg:dbname=prolog > mytables
The sequence of columns is not retained by DBIx::Class::Loader - that's why I added the column names in the Prolog comments (lines starting with '%'), this will be usefull for creating the queries. That's our logic database. Now let's query it. I'll run the full SWI prolog engine here and load the above program, I do this because I cannot get AI::Prolog to work with compound queries that I need.% name,id city('New York',1). city('London',2). city('Warsaw',3). % cityid,name,id usr(1,'John',1). usr(1,'Marry',2). usr(2,'Eva',3). usr(3,'Zby',4).
Now the database is loaded, and we can query it. How about a table join? Let's look for all users that are from 'New York'. Now I need the comments that tell me the sequence of columns.zby@zby:~/progs/prologdata$ swipl Welcome to SWI-Prolog (Multi-threaded, Version 5.2.13) Copyright (c) 1990-2003 University of Amsterdam. SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. Please visit http://www.swi-prolog.org for details. For help, use ?- help(Topic). or ?- apropos(Word). ?- [mytables]. % mytables compiled 0.00 sec, 1,400 bytes Yes ?-
Great, it found both.?- usr(Cityid, Name, Id), city('New York', Cityid). Cityid = 1 Name = 'John' Id = 1 ; Cityid = 1 Name = 'Marry' Id = 2 ; No ?-
And here is some code you can add to the first program to ask the query at once without saving the intermediate Prolog program. Unfortunately I did not found any way how to do conjunction in the queries (needed for table joins here).
my $prologDB = AI::Prolog->new( $prog ); $prologDB->query( "usr(1,Name,Uid)." ); while (my $results = $prologDB->results) { print "@$results\n"; }
For sure there are other ways how to convert relational databases to logic, I hope other monks here will share their ideas on how to do that and how to use it.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Query database in Prolog
by Ovid (Cardinal) on Dec 29, 2005 at 21:09 UTC | |
by zby (Vicar) on Dec 29, 2005 at 21:31 UTC | |
by Ovid (Cardinal) on Dec 29, 2005 at 21:43 UTC | |
|
Re: Query database in Prolog
by diotalevi (Canon) on Dec 29, 2005 at 19:52 UTC | |
by zby (Vicar) on Dec 29, 2005 at 20:16 UTC | |
by diotalevi (Canon) on Dec 29, 2005 at 20:32 UTC | |
by zby (Vicar) on Dec 29, 2005 at 20:52 UTC | |
by tilly (Archbishop) on Dec 29, 2005 at 21:58 UTC | |
| |
by diotalevi (Canon) on Dec 29, 2005 at 20:55 UTC |