I search some way to translate the output of swiplserver queries into some result string SWI-Prolog can handle with
E.g. the query:
Example 1
from swiplserver import *
with PrologMQI() as mqi:
with mqi.create_thread() as prolog_thread:
result = prolog_thread.query("A = point{x:1, y:2}.put([x=3,z=0]).")
print(result)
results in:
Python output
[{‘A’: {‘args’: [{‘x’: 1, ‘y’: 2}, {‘args’: [[{‘args’: [‘x’, 3], ‘functor’: ‘=’}, {‘args’: [‘z’, 0], ‘functor’: ‘=’}]], ‘functor’: ‘put’}], ‘functor’: ‘.’}}]
Output in SWI-Prolog
A = point{x:3, y:2, z:0}.
If I now want to change the result (maybe with A=point{x:3, y:2, z:0}.put([z=4]).
), my first task is, to translate the result in some string prolog is able to accept as parameter (in an easy way).
The easiest way to do this is maybe to store the result string in some .txt-file and read that out with python.
This can be done for an arbitrary predicate in SWI-Prolog by
Output in SWI-Prolog
?- protocol('text.txt').
true.
?- A = point{x:1, y:2}.put([x=3,z=0]).
A = point{x:3, y:2, z:0}.
?- noprotocol.
true.
But for some reason the simulation in swiplserver doesn’t generate some file, means if I try to simulate it with code:
Example 2
from swiplserver import *
with PrologMQI() as mqi:
with mqi.create_thread() as prolog_thread:
prolog_thread.query("protocol('text.txt').")
result = prolog_thread.query("A = point{x:1, y:2}.put([x=3,z=0]).")
prolog_thread.query("noprotocol.")
print(result)
The server generates some file “test.txt” but doesn’t store the result of my query inside of it. In summary my two questions are:
- Is their some simple way to manage the outputs swiplserver gives me for further inputs in prolog predicates (see result of example 1)?
- What is the reason the result of my query in example 2 is not stored in the file “text.txt”?