Skip to content

Commit 33d0895

Browse files
committed
add from_networkx abstract method to fa, use update instead of union in enfa
1 parent e4bf660 commit 33d0895

File tree

5 files changed

+24
-18
lines changed

5 files changed

+24
-18
lines changed

pyformlang/finite_automaton/deterministic_finite_automaton.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ def _from_epsilon_nfa_internal(cls, enfa: EpsilonNFA, eclose: bool) \
368368
all_trans = [enfa(x, symbol) for x in current]
369369
state = set()
370370
for trans in all_trans:
371-
state = state.union(trans)
371+
state.update(trans)
372372
if not state:
373373
continue
374374
# Eclose added

pyformlang/finite_automaton/epsilon_nfa.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,7 @@ def _get_next_states_iterable(
107107
"""
108108
next_states = set()
109109
for current_state in current_states:
110-
next_states_temp = self._transition_function(current_state,
111-
symbol)
112-
next_states = next_states.union(next_states_temp)
110+
next_states.update(self(current_state, symbol))
113111
return next_states
114112

115113
def accepts(self, word: Iterable[Hashable]) -> bool:
@@ -177,7 +175,7 @@ def eclose_iterable(self, states: Iterable[Hashable]) -> Set[State]:
177175
states = [to_state(x) for x in states]
178176
res = set()
179177
for state in states:
180-
res = res.union(self.eclose(state))
178+
res.update(self.eclose(state))
181179
return res
182180

183181
def eclose(self, state: Hashable) -> Set[State]:
@@ -599,7 +597,7 @@ def __invert__(self) -> "EpsilonNFA":
599597
return self.reverse()
600598

601599
def kleene_star(self) -> "EpsilonNFA":
602-
""" Compute the kleene closure of current EpsilonNFA"""
600+
""" Compute the kleene closure of current EpsilonNFA """
603601
new_start = self.__get_new_state("Start")
604602
kleene_closure = EpsilonNFA(start_states={new_start},
605603
final_states={new_start})

pyformlang/finite_automaton/finite_automaton.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,16 @@ def to_networkx(self) -> MultiDiGraph:
533533
graph.add_edge(s_from.value, s_to.value, label=label_)
534534
return graph
535535

536+
@classmethod
537+
@abstractmethod
538+
def from_networkx(cls, graph: MultiDiGraph) -> "FiniteAutomaton":
539+
"""
540+
Import a networkx graph into an finite state automaton. \
541+
The imported graph requires to have the good format, i.e. to come \
542+
from the function to_networkx
543+
"""
544+
raise NotImplementedError
545+
536546
def write_as_dot(self, filename: str) -> None:
537547
"""
538548
Write the automaton in dot format into a file
@@ -573,8 +583,7 @@ def get_accepted_words(self, max_length: Optional[int] = None) \
573583
word_to_add = tuple(current_word)
574584
if not self.__try_add(words_by_state[current_state], word_to_add):
575585
continue
576-
transitions = self._transition_function.get_transitions_from(
577-
current_state)
586+
transitions = self.get_transitions_from(current_state)
578587
for symbol, next_state in transitions:
579588
if next_state in states_leading_to_final:
580589
temp_word = current_word.copy()
@@ -593,7 +602,7 @@ def _get_states_leading_to_final(self) -> Set[State]:
593602
leading_to_final = self.final_states.copy()
594603
visited = set()
595604
states_to_process: deque[Any] = \
596-
deque((None, start_state) for start_state in self.start_states)
605+
deque((None, start_state) for start_state in self.start_states)
597606
while states_to_process:
598607
previous_state, current_state = states_to_process.pop()
599608
if previous_state and current_state in leading_to_final:

pyformlang/finite_automaton/nondeterministic_finite_automaton.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,4 @@ def from_epsilon_nfa(cls, enfa: EpsilonNFA) \
157157

158158
class InvalidEpsilonTransition(Exception):
159159
"""Exception raised when an epsilon transition is created in
160-
nondeterministic automaton"""
160+
non-epsilon NFA"""

pyformlang/regular_expression/regex.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def get_number_operators(self) -> int:
136136
return 1 + sum(son.get_number_operators() for son in self.sons)
137137
return 0
138138

139-
def to_minimal_dfa(self) -> "DeterministicFiniteAutomaton":
139+
def to_minimal_dfa(self) -> DeterministicFiniteAutomaton:
140140
""" Builds minimal dfa from current regex """
141141
enfa = self.to_epsilon_nfa()
142142
dfa = DeterministicFiniteAutomaton.from_epsilon_nfa(enfa)
@@ -164,13 +164,12 @@ def _to_epsilon_nfa_internal(self, copy: bool) -> EpsilonNFA:
164164
Transforms the regular expression into an epsilon NFA.
165165
Copy enfa in case of external usage.
166166
"""
167-
if self._enfa is not None:
168-
return self._enfa.copy() if copy else self._enfa
169-
self._enfa = EpsilonNFA()
170-
s_initial = self._set_and_get_initial_state_in_enfa(self._enfa)
171-
s_final = self._set_and_get_final_state_in_enfa(self._enfa)
172-
self._process_to_enfa(self._enfa, s_initial, s_final)
173-
return self._to_epsilon_nfa_internal(copy)
167+
if self._enfa is None:
168+
self._enfa = EpsilonNFA()
169+
s_initial = self._set_and_get_initial_state_in_enfa(self._enfa)
170+
s_final = self._set_and_get_final_state_in_enfa(self._enfa)
171+
self._process_to_enfa(self._enfa, s_initial, s_final)
172+
return self._enfa.copy() if copy else self._enfa
174173

175174
def _set_and_get_final_state_in_enfa(self, enfa: EpsilonNFA) -> State:
176175
s_final = self._get_next_state_enfa()

0 commit comments

Comments
 (0)