Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vipul-Cariappa committed Oct 19, 2023
1 parent 08b176e commit 8fa9839
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 27 deletions.
10 changes: 3 additions & 7 deletions automata/base/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3
"""Miscellaneous utility functions and classes."""
from __future__ import annotations

import os
import pathlib
Expand All @@ -17,7 +18,6 @@
Literal,
Set,
Tuple,
TypeAlias,
TypeVar,
Union,
)
Expand All @@ -31,10 +31,6 @@
_visual_imports = False
else:
_visual_imports = True
finally:
# create a type for type checker
# irrespective of whether the imports were successful
GraphT: TypeAlias = "pgv.AGraph"


LayoutMethod = Literal["neato", "dot", "twopi", "circo", "fdp", "nop"]
Expand Down Expand Up @@ -86,7 +82,7 @@ def create_graph(
reverse_orientation: bool = False,
fig_size: Union[Tuple[float, float], Tuple[float], None] = None,
state_separation: float = 0.5,
) -> GraphT:
) -> pgv.AGraph:
"""Creates and returns a graph object
Args:
- horizontal (bool, optional): Direction of node layout. Defaults
Expand Down Expand Up @@ -123,7 +119,7 @@ def create_graph(


def save_graph(
graph: GraphT,
graph: pgv.AGraph,
path: Union[str, os.PathLike],
) -> None:
"""Write `graph` to file given by `path`. PNG, SVG, etc.
Expand Down
8 changes: 4 additions & 4 deletions automata/pda/npda.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@ def _get_input_path(

accepted = path[0] in self.final_states

for i in reversed(steps):
if len(i) == 1:
path.append(i.pop())
for step in reversed(steps):
if len(step) == 1:
path.append(step.pop())
continue

for curr_step in i:
for curr_step in step:
for next_step in self._get_next_configurations(curr_step):
if next_step == path[-1]:
path.append(curr_step)
Expand Down
27 changes: 11 additions & 16 deletions automata/pda/pda.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python3
"""Classes and methods for working with all pushdown automata."""
from __future__ import annotations

import abc
import os
Expand Down Expand Up @@ -167,11 +168,11 @@ def show_diagram(

if with_machine:
# initialize diagram with all states
graph = self._add_states_diagram(graph, arrow_size_str, font_size_str)
self._add_states_diagram(graph, arrow_size_str, font_size_str)

# add required transitions to show execution of the
# PDA for the given input string
graph = self._create_transitions_for_input_diagram(
self._create_transitions_for_input_diagram(
graph,
input_path,
is_edge_drawn,
Expand All @@ -182,13 +183,13 @@ def show_diagram(
)

# add all the necessary transitions between states
graph = self._add_transitions_diagram(
self._add_transitions_diagram(
graph, is_edge_drawn, arrow_size_str, font_size_str
)

if with_stack:
# add the stack transitions
graph = self._create_stack_diagram(
self._create_stack_diagram(
input_path,
graph,
start_color,
Expand All @@ -198,10 +199,10 @@ def show_diagram(
)
else:
# initialize diagram with all states
graph = self._add_states_diagram(graph, arrow_size_str, font_size_str)
self._add_states_diagram(graph, arrow_size_str, font_size_str)

# add all the necessary transitions between states
graph = self._add_transitions_diagram(
self._add_transitions_diagram(
graph, is_edge_drawn, arrow_size_str, font_size_str
)

Expand All @@ -222,7 +223,7 @@ def _create_stack_diagram(
end_color: coloraide.Color,
font_size: str,
arrow_size: str,
) -> pgv.AGraph:
) -> None:
"""
Constructs stack for all the transitions in the `input_path` and
adds the constructed stacks into `graph`. Returns the same `graph`
Expand Down Expand Up @@ -275,7 +276,7 @@ def _create_transitions_for_input_diagram(
end_color: coloraide.Color,
arrow_size: str,
font_size: str,
) -> pgv.AGraph:
) -> None:
"""
Add transitions to show execution of the PDA for the given input string
"""
Expand All @@ -302,15 +303,13 @@ def _create_transitions_for_input_diagram(
penwidth="2.5",
)

return graph

def _add_transitions_diagram(
self,
graph: pgv.AGraph,
is_edge_drawn: EdgeDrawnDictT,
arrow_size: str,
font_size: str,
) -> pgv.AGraph:
) -> None:
"""
Add transitions to between states
"""
Expand All @@ -334,14 +333,12 @@ def _add_transitions_diagram(
fontsize=font_size,
)

return graph

def _add_states_diagram(
self,
graph: pgv.AGraph,
arrow_size: str,
font_size: str,
) -> pgv.AGraph:
) -> None:
"""
Add all the states of the PDA
"""
Expand Down Expand Up @@ -369,8 +366,6 @@ def _add_states_diagram(
graph.add_nodes_from(nonfinal_states, shape="circle", fontsize=font_size)
graph.add_nodes_from(final_states, shape="doublecircle", fontsize=font_size)

return graph

def _validate_transition_invalid_input_symbols(
self, start_state: PDAStateT, input_symbol: str
) -> None:
Expand Down

0 comments on commit 8fa9839

Please sign in to comment.