Skip to content

Commit e7c5398

Browse files
committed
add remove_transition method for pda, update tests
1 parent 74edb89 commit e7c5398

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

pyformlang/fcfg/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
1616
"""
1717

18-
from .fcfg import FCFG, CFGObject, Variable, Terminal, Epsilon, ParseTree
18+
from .fcfg import FCFG, CFGObject, \
19+
Variable, Terminal, Epsilon, ParseTree, NotParsableException
1920
from .feature_production import FeatureProduction
2021
from .feature_structure import FeatureStructure, \
2122
ContentAlreadyExistsException, \
@@ -31,6 +32,7 @@
3132
"Terminal",
3233
"Epsilon",
3334
"ParseTree",
35+
"NotParsableException",
3436
"ContentAlreadyExistsException",
3537
"FeatureStructuresNotCompatibleException",
3638
"PathDoesNotExistsException"]

pyformlang/pda/pda.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,24 @@ def add_transitions(self, transitions: Iterable[InputTransition]) -> None:
253253
self.add_transition(s_from, input_symbol, stack_from,
254254
s_to, stack_to)
255255

256+
def remove_transition(self,
257+
s_from: Hashable,
258+
input_symbol: Hashable,
259+
stack_from: Hashable,
260+
s_to: Hashable,
261+
stack_to: Iterable[Hashable]) -> None:
262+
""" Remove the given transition from the PDA """
263+
s_from = to_state(s_from)
264+
input_symbol = to_symbol(input_symbol)
265+
stack_from = to_stack_symbol(stack_from)
266+
s_to = to_state(s_to)
267+
stack_to = tuple(to_stack_symbol(x) for x in stack_to)
268+
self._transition_function.remove_transition(s_from,
269+
input_symbol,
270+
stack_from,
271+
s_to,
272+
stack_to)
273+
256274
def __call__(self,
257275
s_from: Hashable,
258276
input_symbol: Hashable,

pyformlang/pda/tests/test_pda.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,3 +388,32 @@ def test_object_eq(self):
388388
assert StackSymbol("ABC") != Symbol("ABC")
389389
assert State("ABC") != FAState("ABC")
390390
assert Symbol("s") != Terminal("s")
391+
392+
def test_contains(self, pda_example: PDA):
393+
""" Tests the transition containment checks """
394+
pda = pda_example
395+
assert ("q1", "1", "Z1", "q2", []) in pda
396+
assert ("q0", "epsilon", "Z1", "q2", tuple()) in pda
397+
assert ("a", "b", "c", "d", ["e"]) not in pda
398+
pda.add_transition("q1", "1", "Z1", "q5", ["a"])
399+
assert ("q1", "1", "Z1", "q5", ["a"]) in pda
400+
401+
def test_remove_transition(self, pda_example: PDA):
402+
""" Tests the pda transition removal """
403+
pda = pda_example
404+
assert ("q0", "0", "Z0", "q1", ("Z1", "Z0")) in pda
405+
pda.remove_transition("q0", "0", "Z0", "q1", ("Z1", "Z0"))
406+
assert ("q0", "0", "Z0", "q1", ("Z1", "Z0")) not in pda
407+
pda.remove_transition("q0", "0", "Z0", "q1", ("Z1", "Z0"))
408+
assert ("q0", "0", "Z0", "q1", ("Z1", "Z0")) not in pda
409+
pda.remove_transition("a", "b", "c", "d", ["e"])
410+
assert pda.get_number_transitions() == 2
411+
412+
def test_iteration(self, pda_example: PDA):
413+
""" Tests the iteration of pda transitions """
414+
pda = pda_example
415+
transitions = list(iter(pda))
416+
assert (("q0", "0", "Z0"), ("q1", ("Z1", "Z0"))) in transitions
417+
assert (("q1", "1", "Z1"), ("q2", tuple())) in transitions
418+
assert (("q0", "epsilon", "Z1"), ("q2", tuple())) in transitions
419+
assert len(transitions) == 3

pyformlang/pda/transition_function.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ def add_transition(self,
5656
else:
5757
self._transitions[temp_in] = {temp_out}
5858

59+
def remove_transition(self,
60+
s_from: State,
61+
input_symbol: Symbol,
62+
stack_from: StackSymbol,
63+
s_to: State,
64+
stack_to: Tuple[StackSymbol, ...]) -> None:
65+
""" Remove the given transition from the function """
66+
key = (s_from, input_symbol, stack_from)
67+
if key in self._transitions:
68+
self._transitions[key].discard((s_to, stack_to))
69+
5970
def copy(self) -> "TransitionFunction":
6071
""" Copy the current transition function
6172

0 commit comments

Comments
 (0)