Discussions related to Visual Prolog
larryhoughton
Posts: 11
Joined: 4 Oct 2016 21:16

Prolog backtracking when using a SQL Database instead of Prolog Facts

Unread post by larryhoughton »

Hello,
Prolog automatically and sequentially steps to the next Prolog Fact during backtracking (while trying to find a successful solution).

Conceptually how should the SQL retrieval of data be done in a way that might mimic the Prolog Fact DB backtracking selection of the next Fact "DB record"?

To accomplish the same "Next Fact/DB record" functionality with a SQL DB (for Backtracking);
do I need to create some kind of SQL Select "routine" that maintains a "DB cursor/pointer" to sequentially retrieve the next SQL DB record for backtracking or ???

Any code examples?

Thanks,
Larry
User avatar
Thomas Linder Puls
VIP Member
Posts: 1398
Joined: 28 Feb 2000 0:01

Unread post by Thomas Linder Puls »

You can use the fetch_nd predicate. This is from the simpleOdbc demo project:

Code: Select all

class predicates     selectDemo_string : (odbcConnection Connection). clauses     selectDemo_string(Connection) :-         stdio::write("\nSelect persons older than 35 from PersonStr table \n(Table contains long string values)\n"),         Stmt = Connection:execDirect("select name, age, birthday, longfield from PersonStr where Age > ?", [integer(35)]),         NumColumns = Stmt:numResultCols(),         foreach Stmt:fetch_nd() do             stdio::nl(),             foreach Column = std::fromTo(1, NumColumns) do                 ColName = Stmt:colAttribute_string(Column, odbc_native::sql_desc_name),                 ColValue = Stmt:getColumnValue(Column),                 stdio::writef("%s = %s, \n", ColName, printImage(ColValue))             end foreach         end foreach,         Stmt:free().
But be aware about free-ing the statement.

Normally when using backtracking you can create a nondeterm predicate that returns the result one by one, but that is not advisable here, because if you don't backtrack to the end then you will not reach the free.
Regards Thomas Linder Puls
PDC
larryhoughton
Posts: 11
Joined: 4 Oct 2016 21:16

Unread post by larryhoughton »

Hi Thomas,
As part of a previous discussion topic (http://discuss.visual-prolog.com/viewtopic.php?t=15668) it was recommended that I not retrieve the DB Facts and Assert them to Prolog Facts for processing (e.g. Backtracking).
However, I think it you should avoid/minimize data caching in your application. I.e. instead of loading data into facts and then serve them from the facts, you should rather serve them directly from the database.
I know SQL very well and would like to follow the advice above, but it's not clear to me from the last comment (in this thread):
Normally when using backtracking you can create a nondeterm predicate that returns the result one by one, but that is not advisable here, because if you don't backtrack to the end then you will not reach the free.how
How would I "serve <Facts> directly from the database"?

I can always fall back to my original plan, which was to: Retrieve from the DB, Assert to Facts and Backtrack against the "retrieved Facts". I just got the impression that there is a better way and I'm not understanding how to go about the better way.

Thanks in advance,
Larry
Post Reply