Skip to content

Commit

Permalink
fix bug due to combination of parse_args and as_pyasp + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Aluriak committed Nov 15, 2019
1 parent fe41075 commit 6efdb6a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
6 changes: 3 additions & 3 deletions clyngor/answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def _format(self, answer_set) -> dict or frozenset:
if self._group_atoms:
return {pred: frozenset() for pred in answer_set}
if self._as_pyasp:
return as_pyasp.TermSet(as_pyasp.Atom(pred, ()) for pred in answer_set)
return as_pyasp.TermSet(as_pyasp.Atom.from_tuple_repr((pred, ())) for pred in answer_set)
return builder(answer_set)
elif self._first_arg_only:
answer_set = builder((pred, args[0] if args else ())
Expand All @@ -286,7 +286,7 @@ def _format(self, answer_set) -> dict or frozenset:
mapping = defaultdict(set)
for pred, args in answer_set:
if self._as_pyasp:
args = as_pyasp.Atom(pred, args)
args = as_pyasp.Atom.from_tuple_repr((pred, args))
mapping[pred].add(args)
if self._group_atoms_with_arity:
mapping[pred, len(args)].add(args)
Expand All @@ -296,7 +296,7 @@ def _format(self, answer_set) -> dict or frozenset:
else:
return {pred: builder(args) for pred, args in mapping.items()}
elif self._as_pyasp:
return as_pyasp.TermSet(as_pyasp.Atom(*atom) for atom in answer_set)
return as_pyasp.TermSet(as_pyasp.Atom.from_tuple_repr(atom) for atom in answer_set)
return answer_set


Expand Down
13 changes: 13 additions & 0 deletions clyngor/as_pyasp.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,25 @@ def __str__(self):
return '{}({})'.format(self.predicate, ','.join(map(str, self.arguments)))
else:
return self.predicate

def __repr__(self): return str(self)

def __iter__(self):
"""Conserve the same API as regular representation of atoms"""
return iter((self.predicate, self.arguments))

def from_tuple_repr(atom:tuple) -> object:
"Expects something like ('a', (('b', ('c', 'd')))), encoding a(b(c,d))"
def from_args(args:tuple) -> object:
for arg in args:
if isinstance(arg, (tuple, list)):
yield Atom.from_tuple_repr(arg)
else:
yield arg
pred, args = atom
return Atom(pred, from_args(args))



# aliases used by pyasp
Term = Atom
Expand Down
48 changes: 48 additions & 0 deletions clyngor/test/test_answers.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,51 @@ def test_int_not_parsed():
for model in next(clyngor.ASP(asp).int_not_parsed):
t = type(model[1][0])
assert t is str, t

def test_atoms_with_leading_underscore():
"""needed to test leading underscores in naive parsing ;
next test will fall under the case where careful parsing is automatically activated"""
get_answers = lambda: Answers(('_a(_b,_c,d) _e',)).parse_args
# naive parsing
answers = get_answers()
answer_a = next(answers)
assert next(answers, None) is None, "there is more than one model"
expected_a = frozenset((('_a', ('_b', '_c', 'd')), ('_e', ())))
assert answer_a == expected_a, (answer_a, type(answer_a))
# careful parsing
answers = get_answers().careful_parsing
answer_a = next(answers)
assert next(answers, None) is None, "there is more than one model"
expected_a = frozenset((('_a', ('_b', '_c', 'd')), ('_e', ())))
assert answer_a == expected_a, answer_a

def test_atoms_with_leading_underscore_with_nested():
get_answers = lambda: Answers(('_a("b",_b(c,_d)) _c',)).parse_args
# naive parsing (NB: careful parsing should be automatically activated)
answers = get_answers()
answer_a = next(answers)
assert next(answers, None) is None, "there is more than one model"
expected_a = frozenset((('_a', ('"b"', ('_b', ('c', '_d')))), ('_c', ())))
assert answer_a == expected_a, (answer_a, type(answer_a))
# careful parsing
answers = get_answers().careful_parsing
answer_a = next(answers)
assert next(answers, None) is None, "there is more than one model"
expected_a = frozenset((('_a', ('"b"', ('_b', ('c', '_d')))), ('_c', ())))
assert answer_a == expected_a, answer_a

def test_pyasp_and_nested_atoms():
get_answers = lambda: Answers(('a(b(c,d))',)).parse_args
# without as_pyasp
answers = get_answers()
answer_a = next(answers)
assert next(answers, None) is None, "there is more than one model"
expected_a = frozenset((('a', (('b', ('c', 'd')),)),))
assert answer_a == expected_a, (answer_a, type(answer_a))
# with as_pyasp
answers = get_answers().as_pyasp
answer_a = next(answers)
assert next(answers, None) is None, "there is more than one model"
expected_a = frozenset((Atom('a', (Atom('b', ('c', 'd')),)),))
print('LBTL:', type(answer_a), answer_a)
assert answer_a == expected_a, (answer_a, type(answer_a))

0 comments on commit 6efdb6a

Please sign in to comment.