Skip to content

Commit

Permalink
Better read EDS without a TOP
Browse files Browse the repository at this point in the history
Part of #316
  • Loading branch information
goodmami committed Dec 17, 2020
1 parent 0aef275 commit 9e21148
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 18 deletions.
56 changes: 38 additions & 18 deletions delphin/codecs/eds.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,28 @@ def _decode(lineiter):


def _decode_eds(lexer):
_, top, _ = lexer.expect_type(LBRACE, SYMBOL, COLON)
lexer.accept_type(GRAPHSTATUS)
lexer.expect_type(LBRACE)

# after the LBRACE, the following patterns determine the top:
# : 1st is COLON -> None
# (fragmented) 1st is GRAPHSTATUS -> None
# } 1st is RBRACE -> None
# | 1st is NODESTATUS -> None
# <sym1> : (fragmented) 3rd is GRAPHSTATUS -> <sym1>
# <sym1> : | 3rd is NODESTATUS -> <sym1>
# <sym1> : <sym2> : 4th is COLON -> <sym1>
# <sym1> : <sym2> ... otherwise -> None
if lexer.peek()[0] in (COLON, GRAPHSTATUS, RBRACE, NODESTATUS):
top = None
lexer.accept_type(COLON)
lexer.accept_type(GRAPHSTATUS)
elif (lexer.peek(2)[0] in (GRAPHSTATUS, NODESTATUS)
or lexer.peek(3)[0] == COLON):
top, _ = lexer.expect_type(SYMBOL, COLON)
lexer.accept_type(GRAPHSTATUS)
else:
top = None

nodes = []
while lexer.peek()[0] != RBRACE:
lexer.accept_type(NODESTATUS)
Expand Down Expand Up @@ -231,7 +251,11 @@ def _decode_edges(start, lexer):
def _encode_eds(e, properties, lnk, show_status, indent):
# do something predictable for empty EDS
if len(e.nodes) == 0:
return '{:\n}' if indent else '{:}'
return '{\n}' if indent else '{}'

delim = '\n' if indent else ' '
connected = ' ' if indent else ''
disconnected = '|' if show_status else ' '

# determine if graph is connected
g = {node.id: set() for node in e.nodes}
Expand All @@ -241,25 +265,21 @@ def _encode_eds(e, properties, lnk, show_status, indent):
g[target].add(node.id)
nidgrp = _bfs(g, start=e.top)

status = ''
top_parts = []
if e.top is not None:
top_parts.append(e.top + ':')
if show_status and nidgrp != set(g):
status = ' (fragmented)'
delim = '\n' if indent else ' '
connected = ' ' if indent else ''
disconnected = '|' if show_status else ' '
top_parts.append('(fragmented)')

parts = []
if top_parts or indent:
parts.append(' '.join(top_parts))

ed_list = []
for node in e.nodes:
membership = connected if node.id in nidgrp else disconnected
ed_list.append(membership + _encode_node(node, properties, lnk))

return '{{{top}{status}{delim}{ed_list}{enddelim}}}'.format(
top=e.top + ':' if e.top is not None else ':',
status=status,
delim=delim,
ed_list=delim.join(ed_list),
enddelim='\n' if indent else ''
)
parts.append(membership + _encode_node(node, properties, lnk))

return '{' + delim.join(parts) + ('\n}' if indent else '}')


def _encode_node(node, properties, lnk):
Expand Down
41 changes: 41 additions & 0 deletions tests/codecs/edsnative_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,34 @@ def test_decode():
assert e.nodes[3].predicate == '_bark_v_1'


def test_decode_no_top():
e = edsnative.decode(
'{:\n'
' e2:_bark_v_1{e}[ARG1 x4]\n'
' _1:udef_q[BV x4]\n'
' x4:_dog_n_1{x}[]\n'
'}'
)
assert e.top is None
assert len(e.nodes) == 3
# without initial colon
e = edsnative.decode(
'{\n'
' e2:_bark_v_1{e}[ARG1 x4]\n'
' _1:udef_q[BV x4]\n'
' x4:_dog_n_1{x}[]\n'
'}'
)
assert e.top is None
assert len(e.nodes) == 3
# without newlines
e = edsnative.decode(
'{:e2:_bark_v_1{e}[ARG1 x4] _1:udef_q[BV x4] x4:_dog_n_1{x}[]}'
)
assert e.top is None
assert len(e.nodes) == 3


def test_encode(dogs_bark_from_mrs):
d = EDS(**dogs_bark_from_mrs)
assert edsnative.encode(d) == (
Expand All @@ -50,3 +78,16 @@ def test_encode(dogs_bark_from_mrs):
' _1:udef_q[BV x4]\n'
' x4:_dog_n_1{x}[]\n'
'}')


def test_encode_no_top(dogs_bark_from_mrs):
d = EDS(**dogs_bark_from_mrs)
d.top = None
assert edsnative.encode(d) == (
'{e2:_bark_v_1{e}[ARG1 x4] _1:udef_q[BV x4] x4:_dog_n_1{x}[]}')
assert edsnative.encode(d, indent=True) == (
'{\n'
' e2:_bark_v_1{e}[ARG1 x4]\n'
' _1:udef_q[BV x4]\n'
' x4:_dog_n_1{x}[]\n'
'}')

0 comments on commit 9e21148

Please sign in to comment.