Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delay in file creation with Python implementation in ASP #5

Open
ArnaudBelcour opened this issue Apr 5, 2018 · 4 comments
Open

Delay in file creation with Python implementation in ASP #5

ArnaudBelcour opened this issue Apr 5, 2018 · 4 comments
Assignees
Labels

Comments

@ArnaudBelcour
Copy link
Contributor

ArnaudBelcour commented Apr 5, 2018

Hello,
I have an issue while using Clyngor to encapsulate ASP files (which also use python implementation). The python implementation in ASP is used to select some atoms in the answer and to write these atoms in a file. The ASP encoding and fact have been tested with Clingo (version 5.2.2 compiled with Python) and it works, the file is created.

Clyngor achieves to create the file but there is a delay in the creation of the file. Indeed the file is not accessible right after the solver call. If I try to use the file, I have an error saying that the file doesn't exist. But when the script ends, the file exists.

I write a simple example, with encoding and fact files (which are working with my Clingo) and a python script with Clyngor which creates the file but runs into an error when trying to access the file with the print(file.read()).

The ASP encoding extracts a number from a predicate with two numbers, assign it to another predicate and then write the new predicate in a file.

Example:

Python script:

import clyngor

clyngor.solve(('test.lp', 'data.lp'))

print(open('resultfile.lp').read())

Encoding file:

#script (python)

import clingo

def on_model(answer, output_file):
    atoms=answer.symbols(atoms=True)
    for atom in atoms:
        if atom.name == "count" and len(atom.arguments) == 1:
            output_file.write( "count("+str(atom.arguments[0])+").\n")



def main(prg):
    output_file = open("resultfile.lp", "w")
    prg.ground([("base", [])])
    for answer_number, answer in enumerate(prg.solve(yield_=True, async=True), start=1):
        print(answer)
        on_model(answer, output_file)
    output_file.close()

#end.

count(X) :- query(X, A).

data.lp:

query(1,2).
query(2,3).
query(4,5).
@Aluriak Aluriak self-assigned this Apr 5, 2018
@ArnaudBelcour
Copy link
Contributor Author

This is my clingo --version :

clingo version 5.2.2
Address model: 64-bit

libgringo version 5.2.2
Configuration: with Python 2.7.14, without Lua

libclasp version 3.3.3 (libpotassco version 1.0.1)
Configuration: WITH_THREADS=1
Copyright (C) Benjamin Kaufmann

License: The MIT License <https://opensource.org/licenses/MIT>

@Aluriak
Copy link
Owner

Aluriak commented Apr 5, 2018

The problem is due to clyngor.solve being a generator.

When, in the python file, you run solely clyngor.solve(('test.lp', 'data.lp')), nothing is done. You have to get at least the first element to run the solving and therefore the side effects of the ASP source code.

One fix may be : next(clyngor.solve(('test.lp', 'data.lp')), None).

Do this fixes the problem for your real code ?

@ArnaudBelcour
Copy link
Contributor Author

Yes, I have tested this fix on my real code and it solves the problem.
Thank you!

@Aluriak Aluriak reopened this Apr 2, 2019
@Aluriak
Copy link
Owner

Aluriak commented Apr 2, 2019

Problem is not solved, and is probably a big one.

As shown in the following example, removing async=True in clingo API and use next on clyngor.solve will provide the proper behavior. Every other combination is dubious.

encoding.lp is:

#script (python)

import clingo

def on_model(answer, output_file):
    atoms=answer.symbols(atoms=True)
    for atom in atoms:
        if atom.name == "count" and len(atom.arguments) == 1:
            output_file.write( "count("+str(atom.arguments[0])+").\n")



def main(prg):
    output_file = open("resultfile.lp", "w")
    prg.ground([("base", [])])
    # NB: removing async=True will remove the error
    with prg.solve(yield_=True, async=True) as handle:
        for answer_number, answer in enumerate(handle, start=1):
            print(answer)
            on_model(answer, output_file)
    output_file.close()

#end.

count(X) :- query(X, A).
query(1,2).
query(2,3).
query(4,5).

test.py is:

import clyngor

next(clyngor.solve(('test.lp',)), None)  # we need this to work
# clyngor.solve(('test.lp', 'testt.lp'))  # but this should work too

print(open('resultfile.lp').read())

@Aluriak Aluriak added the bug label Apr 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants