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

Added string model parsing in clingo wrapper #110

Merged
merged 6 commits into from
Jun 4, 2021

Conversation

camcunningham
Copy link
Contributor

A sort of addition onto my last commit - same theme. Needed to be able to create a model based on a string instead of a temp file. If there should be a better naming convention, feel free to change it up.

Again, if this functionality exists elsewhere, let me know!

@gfrances
Copy link
Member

Thanks, @camcunningham ! The functionality looks good, but we try to avoid duplicating so much code whenever possible.

In this case, looks like the PR could be easily refactored e.g. with one single method with signature

def parse_model(*, symbol_mapping, filename=None, content=None)

then some check to make user the user provides filename XOR content, and then refactor all common code
into a method _parse_model(...) that takes a Python generator providing one line of the model at a time.

Do you think you'd have the time to refactor the PR along those lines?
Cheers,

@camcunningham
Copy link
Contributor Author

@gfrances Absolutely! Makes a lot of sense - I'll make those changes tonight

@camcunningham
Copy link
Contributor Author

@gfrances Made some of those changes. Ended up iterating through the lines without a generator - didn't know if I'd need to use one. If you still want that I'd be happy to refactor

@aig-upf aig-upf deleted a comment from codecov-commenter Jun 2, 2021
@gfrances
Copy link
Member

gfrances commented Jun 2, 2021

I suggested the use of a generator because it would avoid the need for loading the entire file in memory, which is what the call to readlines is now doing. Usually wouldn't be such a big deal, but here we should assume that the ASP models can get pretty large on problems where the grounding is huge. Let me make some closer suggestions in the code of the PR itself.

@camcunningham
Copy link
Contributor Author

Makes sense - I'll make those changes. Thanks for the info on generators!

@camcunningham
Copy link
Contributor Author

Hm. That's strange. It passed the checks before the swap to generators - did I do it wrong or is there potentially an issue with using them there?

@haz
Copy link

haz commented Jun 3, 2021

Seems to be issues with the grounding on the docs with the LP solver. E.g., https://travis-ci.com/github/aig-upf/tarski/jobs/510458691

@gfrances -- any chance there's issues with gringo installing on travis?

@gfrances
Copy link
Member

gfrances commented Jun 3, 2021

Nope, it's just that this line is iterating over the characters of the filename, not over the contents of the filename. Should be something like:

    with open(filename, "r") as f:
        return _parse_model((l for l in f), symbol_mapping)

@codecov-commenter
Copy link

codecov-commenter commented Jun 3, 2021

Codecov Report

Merging #110 (612f63d) into devel (afc61a9) will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##            devel     #110   +/-   ##
=======================================
  Coverage   98.19%   98.19%           
=======================================
  Files          47       47           
  Lines        3046     3046           
  Branches      114      114           
=======================================
  Hits         2991     2991           
  Misses         45       45           
  Partials       10       10           

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update afc61a9...612f63d. Read the comment docs.

@camcunningham
Copy link
Contributor Author

Ah - that was dumb of me! Took your suggestion way too literally. Should be good to go now.

def parse_model(*, filename=None, content=None, symbol_mapping):
if filename and not content:
with open(filename, "r") as f:
return _parse_model((l for l in f), symbol_mapping)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this make it an iterator, or build the full list before calling the method? Might seem weird, but would just passing in f work? It's treated as lines in _parse_model, and may well be just the iterator you're looking for...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The (...) syntax creates a generator, but you're totally right that passing in f should work as well;
likewise, passing in content.splitlines() below should work too.

@camcunningham
Copy link
Contributor Author

@gfrances Any other changes you think need to be made?

@gfrances
Copy link
Member

gfrances commented Jun 4, 2021

I would maybe just adopt the suggestion by Chris, passing f and content.splitlines(), but otherwise I think we're good to go?

@gfrances gfrances merged commit ffc7e53 into aig-upf:devel Jun 4, 2021
@gfrances
Copy link
Member

gfrances commented Jun 4, 2021

Thanks @camcunningham, @haz !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants