FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log inLog in 


Prolog backtracking when using a SQL Database instead of Prolog Facts

Post new topic   Reply to topic    discuss.visual-prolog.com Forum Index -> Visual Prolog
View previous topic :: View next topic  
Author Message
larryhoughton



Foothill Ranch
Joined: 04 Oct 2016
Posts: 9

PostPosted: 31 Aug 2017 19:45    Post subject: Prolog backtracking when using a SQL Database instead of Prolog Facts Reply with quote

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
Back to top
View user's profile Send private message
Thomas Linder Puls



Copenhagen, Denmark
Joined: 28 Feb 2000
Posts: 3124

PostPosted: 1 Sep 2017 8:36    Post subject: Reply with quote

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

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
Prolog Development Center
Back to top
View user's profile Send private message
larryhoughton



Foothill Ranch
Joined: 04 Oct 2016
Posts: 9

PostPosted: 2 Sep 2017 2:20    Post subject: Reply with quote

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).
Quote:
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):
Quote:
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
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    discuss.visual-prolog.com Forum Index -> Visual Prolog All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum