How do I use output from SPARC solver as input to a python file?

*爱你&永不变心* 提交于 2019-12-13 07:18:27

问题


My question: I need to save the output from a SPARC solver (which is currently appearing as text in the terminal) as a variable in my Python code. How can I do this?

Quick note: SPARC is a solver used for ASP (answer-set-programming) files - just mentioning this so that people don't get confused and think I am referring to asp.net.

I am running a Python file and an ASP file (in query mode) simultaneously, in the same terminal, using the command python pythonfile.py | java -jar sparc.jar aspfile.sp. Output from the python file in the form of sys.stdout.write() is being redirected as the input to the SPARC solver; i.e. the text I output becomes the query that is solved using my ASP code. This is working, and generating the output I want, but I can't figure out how to use that output in my Python code.

This is a follow-up to another question I have asked, found here. In that question I was trying to find out how to run an ASP file from my C++ code. I went with option 2 in the answer I was given, and am using redirected stdout with the two files running as separate processes. Please note one major change since the original question: I am now using Python instead of C++.

Further details if required: My Python version is 2.7 and my operating system is Ubuntu 14.04. I don't think it's relevant but, in case it is, you should know that my Python code is also being used to control a Gazebo Turtlebot simulation, and I am using ROS Indigo to run that simulation. I won't post my code unless someone requests it, as I just want an idea of what method I could use (I can't find anything that works on the internet), rather than needing my code debugged / assistance writing it. However I will post below an example of what is output to the terminal when my code runs, as this is the information I am trying to 'capture'.

SPARC  V2.52
program translated
?- yes
?- no

It is the answers 'yes' and 'no' that I want to save as variables in my Python file.

SOLUTION:
For anyone wanting to do the same thing, I followed the answer provided by CaptainTrunky.
First I run the command python pythonfile.py | java -jar sparc.jar aspfile.sp > sparc.out, saving the SPARC output to the text file sparc.out.
Then I run python outputParser.py to run a script that prints the contents of the text file, allowing me to check that I'm manipulating the data correctly. The script is very simple:

lines = [line.strip('?- ') for line in open('sparc.out')]
lines = [line.strip('\n') for line in lines]
print lines

回答1:


You can use python to read from sys.stdin with a script similar to the following (filter.py):

import sys
for line in sys.stdin.readlines():
  if line.startswith('?- '):
    print line.strip()

Then invoke your pipeline like this:

python pythonfile.py | java -jar sparc.jar aspfile.sp | python filter.py



回答2:


I would suggest you do dump SPARK output to text file and then to parse it with you tool.

Write a shell script that does it for you:

python pythonfile.py | java -jar sparc.jar aspfile.sp > spark.out
python parse_out.py spark.out


来源:https://stackoverflow.com/questions/43554833/how-do-i-use-output-from-sparc-solver-as-input-to-a-python-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!