Setting up swiplserver for the first time. Help needed

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.

The exact line that gets the error is below.

prolog.consult(’./graph_traversal.pl’)

The error is :

OSError: exception: access violation reading 0x0000000000000050

I would appreciate any help (preferable a quick one) cause I have a demo tomorrow.

Kind Regards,
Ferda

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.

1 Like

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.

2 Likes

Ok, Thank you I will modify the code to try this library.

Hello Again,
I couldn’t install this swipl. What is the correct repository link? I would appreciate if you can share…

Kind Regards,
Ferda

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.

Hi,
I do not want to deal with building etc. now. The latest windows installer has the version 8.4.1.1. Is it ok?

Kind Regards,
Ferda

To be more specific, that is the stable version. https://www.swi-prolog.org/download/stable

The installation, not build, for the Windows development version can be found at https://www.swi-prolog.org/download/devel which is currently at 8.5.3-1.

For an even more recent Windows version, is the nightly build found at https://www.swi-prolog.org/download/daily/bin/


This is also documented at

Install SWI-Prolog development version on Windows 10


HTH

Hello,
How do we consult with swiplserver. I couldn’t see a sample?

Thanks in advance.
Ferda

Maybe it is just me but this statement has me confused.

To me it could mean 1 of 2 different things.


To consult a Prolog source code file from the top level, see Getting started quickly


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.

Kind Regards,
Ferda

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?

Kind Regards,
Ferda

Did you read the documentation here: swiplserver · PyPI and here: swiplserver.prologmqi API documentation?

It seems rather simple python code:

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(...).

After seeing links by SWI it now makes sense.

mqi is only a few months old and I have not used it yet, thus my confustion. :frowning_face:

To make the terms mqi and swiplserver easier to understand when used in a topic here have added them as linkify terms. (ref)

If you look at the use of the term in earlier replies you will see that they have all magickly turned into links. :wink:

1 Like

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.

Please tell me what I am doing wrong?

Run the folowing

$ swipl --quiet -g mqi_start -t halt -- --write_connection_values=true   

You should see some output like:

38515
149542373205988535753366437482310558445

The first number is the port.

If you don’t see the numbers, there is your problem.

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.

> os.system("swipl -s ./mqi.pl -g \"mqi:install_to_library('./mqi.pl')\" ")

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

I get the below error :frowning:

ERROR: -g mqi_start: catch/3: Unknown procedure: mqi_start/0

What does

swipl --version    

return?

SWI-Prolog version 8.3.27 for x64-win64