Hello Everyone,
I have been using prolog in my python application for a while now. I am very happy using it. Since now, I was providing input to my app from a file. Recently I developed a Flask based web front end for this app. Unfortunetely, when I try to start my application via web I get Access Violation error at the point where Prolog initializes itself and reads the pl file.
This uses pyswip? The first things that codes to mind is threading. Most likely the recent MQI interface provided by @ericzinda is a safer alternative. Well, maybe pyswip users have other clues.
I can’t help with the (presumably) pyswip issue above, but I can validate that the swiplserver Python library (the Python library that uses the MQI interface that @jan mentions) works well in a Flask application. That’s how I’ve been using it.
On the prolog side you don’t have to install anything, it is included in the latest SWI-Prolog version. Make sure to use the development version (8.5.3), which has recent fixes.
To access a page from an SWI-Prolog web server just access the page like any others.
Typically the server will run locally and use a specific port, e.g. 8080, and have an index.html page. So using your favorite web browser the URL would be http://localhost:8080/index.html
Hi Eric,
Thanks for the quick response. I am relatively new to the terminology, so I may be asking in a confusing way. Sorry for that.
What I was asking is the equivalent of prolog.consult (pl_file_name_and_path) (where prolog is type of Prolog class) in this system.
I assume I would do this with exec in python but not sure.
How about the assertz function? Cause part of (static ones) my prolog definitions are in a pl file, but for part of them (dynamic ones depending on user input), I was using the assertz function to extend the initial definitions. I do all of this stuff as a part of initializing prolog in my system. Later, I do the queries…
I would appreciate if you can provide more detailed examples on how to use the pl file and how to do the assertz with this new swiplserver in python? I think the query part is relatively more straightforward.
Hi Eric,
As I described in my initial question, my prolog code and my interaction with it via python works well without the web interface, but has problem (access violation) when accessed via web. So, I need to have a sample working not on the command line but via python and swiplserver. Unfortunately, the getting starting guide does not provide an answer to my query. I have been trying to solve this problem using only the pyswip for a while now with no result.
I understand that swiplserver package solves this problem, but I still can not understand how to use it properly. I can not find the functions that are necessary for my solution, basically assertz and consult. I am assuming query would work in a similar way pyswip. Couldn’t try it yet due to not passing the initialization phase… Hopefully you can help me to learn to use this new package?
from swiplserver import PrologMQI, PrologThread
with PrologMQI() as mqi:
with mqi.create_thread() as prolog_thread:
result = prolog_thread.query("member(X, [color(blue), color(red)])")
print(result)
EDIT: seems to me you just replace "member[X, ...]" with consult(...) or assertz(...).
Thanks for the response. I tried to replace with consult now. Now I am getting the below error which seems to be related to configuration of MQServer.
I saw some
site-packages\swiplserver\prologmqi.py", line 428, in start
raise PrologLaunchError("no port found in stdout")
swiplserver.prologmqi.PrologLaunchError: no port found in stdout
I tried to run the below command with and without the halt hoping to have a running mq server but it didn’t help.
Hi Again,
I was thinking MQI as a self starter. However, it didn’t in my case. Getting the MQI start error port not found, I decided to execute the below command hoping to start it manually.
When I look at the python requirements, there is not much. I added the lib and library folders for both swi-prolog and swipl to the path. Anyway, I am not sure why I am getting this error. I am still at the step where I am trying to switch the member statement with consult (From one of the above messages).
ERROR: d:/my_project_path/python/mqi.pl:109:
ERROR: source_sink library(socket)' does not exist Warning: d:/my_project_path/python/mqi.pl:109: Warning: Goal (directive) failed: mqi:use_module(library(socket)) ERROR: d:/my_project_path/python/mqi.pl:110: ERROR: source_sink library(http/json)’ does not exist
Warning: d:/my_project_path/python/mqi.pl:110:
Warning: Goal (directive) failed: mqi:use_module(library(http/json))
ERROR: d:/my_project_path/python/mqi.pl:111:
ERROR: source_sink library(http/json_convert)' does not exist Warning: d:/my_project_path/python/mqi.pl:111: Warning: Goal (directive) failed: mqi:use_module(library(http/json_convert)) ERROR: d:/my_project_path/python/mqi.pl:113: ERROR: source_sink library(term_to_json)’ does not exist
Warning: d:/my_project_path/python/mqi.pl:113:
Warning: Goal (directive) failed: mqi:use_module(library(term_to_json))
ERROR: d:/my_project_path/python/mqi.pl:115:
ERROR: source_sink library(filesex)' does not exist Warning: d:/my_project_path/python/mqi.pl:115: Warning: Goal (directive) failed: mqi:use_module(library(filesex)) ERROR: d:/my_project_path/python/mqi.pl:121: ERROR: source_sink library(time)’ does not exist
Warning: d:/my_project_path/python/mqi.pl:121:
Warning: Goal (directive) failed: mqi:use_module(library(time))
ERROR: d:/my_project_path/python/mqi.pl:122:
ERROR: source_sink `library(uuid)’ does not exist
Warning: d:/my_project_path/python/mqi.pl:122:
Warning: Goal (directive) failed: mqi:use_module(library(uuid))
ERROR: -g mqi:install_to_library(‘./mqi.pl’): mqi:install_to_library/1: Unknown procedure: mqi:copy_file/2