2
votes

I'm working on multi user web application in Flask. Till yet I used MySQL as data source. Now, I'm adding some functionality that would require Prolog as inference engine. My question is related to way, how to handle fact and rules data. I don't think it is good option to create on each web request Prolog file from MySQL data a consult this new created file on server. Prolog data should be somehow persisted on server and Prolog engine update after addition of some fact or rule. Has anybody encountered this before? What is correct way of implementation of this kind of functionality?

So please, how to persist Prolog data in multiuser web application, where each user can add some facts or even rules. And other users should immediately see changed data. How to to it robust and proper way?

1
This is not a duplicate of the linked question, which is a solution in the Python domain. This problem is in the Prolog domain of your program, you need a way to persist changes in the Prolog. As you're in SWI-Prolog, library persistency is your best bet. If you need help and they won't remove the duplicate tag, ask again but only tag with Prolog... - Paul Brown
I'd consider keeping it in the database and access it from Prolog using library(odbc). - Daniel Lyons

1 Answers

3
votes

This is a big and complicated question. Let's start breaking it down.

In the SWI-Prolog side you need a dynamic but persistent database. Here library(persistency) will handle this for you.

But, and it's a big but, Flask is a multi-threaded application, meaning you need some way to communicate with SWI-Prolog in a multi-threaded way and your data updates also need to be thread safe. PySWIP can't handle this.

For the SWI-Prolog you'll need to wrap all of your library(persistency) predicates in a with_mutex/2 for thread safety.

The best bet for multi-threaded communication with SWI-Prolog is via pengines. In this way you run your SWI-Prolog in its own server and communicate with it via HTTP requests. The library(persistency) predicates use a mutex, so are considered unsafe, which means you'll have to mark them as safe.

There is a pip install pengines, but the docs are out of sync with the development. You don't need to call pengine.create(), but you will need to call pengine.doAsk(query) after query = pengine.ask() and finish a set of queries with pengine.iAmFinished(query). It's not a bad idea to read the source code on this one.

It can all be clobbered together and it does work!