Skip to content

Commit 5883323

Browse files
committed
add loading and make_input_complete for NonDetMooreMachines
1 parent 2716ca4 commit 5883323

File tree

2 files changed

+20
-28
lines changed

2 files changed

+20
-28
lines changed

aalpy/utils/FileHandler.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from aalpy.automata import Dfa, MooreMachine, Mdp, Onfsm, MealyState, DfaState, MooreState, MealyMachine, \
99
MdpState, StochasticMealyMachine, StochasticMealyState, OnfsmState, MarkovChain, McState, Sevpa, SevpaState, \
10-
SevpaTransition, Vpa, VpaState, VpaTransition, NDMooreMachine
10+
SevpaTransition, Vpa, VpaState, VpaTransition, NDMooreMachine, NDMooreState
1111

1212
file_types = ['dot', 'png', 'svg', 'pdf', 'string']
1313
automaton_types = {Dfa: 'dfa', MealyMachine: 'mealy', MooreMachine: 'moore', Mdp: 'mdp',
@@ -238,6 +238,8 @@ def _process_label(label, source, destination, automaton_type):
238238
inp = int(inp) if inp.isdigit() else inp
239239
out = int(out) if out.isdigit() else out
240240
source.transitions[inp].append((out, destination))
241+
if automaton_type == 'ndmoore':
242+
source.transitions[int(label) if label.isdigit() else label].append(destination)
241243
if automaton_type == 'mc':
242244
prob = label
243245
source.transitions.append((destination, float(prob)))
@@ -317,7 +319,7 @@ def _process_node_label_prime(node_name, label, line, node_label_dict, node_type
317319
if automaton_type == 'mdp' or automaton_type == 'mc':
318320
node_label_dict[node_name] = node_type(node_name, label)
319321
else:
320-
if automaton_type == 'moore' and label != "":
322+
if automaton_type in {'moore', 'ndmoore'} and label != "":
321323
label_output = _strip_label(label)
322324
if "|" in label_output:
323325
label, output = label_output.split("|", maxsplit=1)
@@ -349,7 +351,8 @@ def load_automaton_from_file(path, automaton_type, compute_prefixes=False):
349351
350352
path: pathlike or str to the file
351353
352-
automaton_type: type of the automaton, one of ['dfa', 'mealy', 'moore', 'mdp', 'smm', 'onfsm', 'mc', 'sevpa']
354+
automaton_type: type of the automaton, one of ['dfa', 'mealy', 'moore', 'mdp', 'smm',
355+
'onfsm', 'ndmoore', 'mc', 'sevpa', 'vpa']
353356
354357
compute_prefixes: it True, shortest path to reach every state will be computed and saved in the prefix of
355358
the state. Useful when loading the model to use them as a equivalence oracle. (Default value = False)
@@ -364,7 +367,7 @@ def load_automaton_from_file(path, automaton_type, compute_prefixes=False):
364367
id_node_aut_map = {'dfa': (DfaState, Dfa), 'mealy': (MealyState, MealyMachine), 'moore': (MooreState, MooreMachine),
365368
'onfsm': (OnfsmState, Onfsm), 'mdp': (MdpState, Mdp), 'mc': (McState, MarkovChain),
366369
'smm': (StochasticMealyState, StochasticMealyMachine), 'sevpa': (SevpaState, Sevpa),
367-
'vpa': (VpaState, Vpa)}
370+
'vpa': (VpaState, Vpa), 'ndmoore': (NDMooreState, NDMooreMachine)}
368371

369372
nodeType, aut_type = id_node_aut_map[automaton_type]
370373

aalpy/utils/HelperFunctions.py

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def make_input_complete(automaton, missing_transition_go_to='self_loop'):
235235
"""
236236
from aalpy.base import DeterministicAutomaton
237237
from aalpy.automata import Dfa, MooreState, MealyMachine, Mdp, StochasticMealyMachine, Onfsm, \
238-
DfaState, MealyState, MooreMachine, OnfsmState, MdpState, StochasticMealyState
238+
DfaState, MealyState, MooreMachine, OnfsmState, MdpState, StochasticMealyState, NDMooreMachine, NDMooreState
239239

240240
assert missing_transition_go_to in {'self_loop', 'sink_state'}
241241

@@ -249,6 +249,7 @@ def make_input_complete(automaton, missing_transition_go_to='self_loop'):
249249
MooreMachine: MooreState(state_id='sink', output='sink_state'),
250250
MealyMachine: MealyState(state_id='sink'),
251251
Onfsm: OnfsmState(state_id='sink'),
252+
NDMooreMachine: NDMooreState(state_id='sink'),
252253
Mdp: MdpState(state_id='sink', output='sink_state'),
253254
StochasticMealyMachine: StochasticMealyState(state_id='sink')}
254255

@@ -259,31 +260,19 @@ def make_input_complete(automaton, missing_transition_go_to='self_loop'):
259260
for state in automaton.states:
260261
for i in input_al:
261262
if i not in state.transitions.keys():
262-
if missing_transition_go_to == 'self_loop':
263-
if isinstance(automaton, DeterministicAutomaton):
264-
state.transitions[i] = state
265-
if isinstance(automaton, MealyMachine):
266-
state.output_fun[i] = 'epsilon'
267-
if isinstance(automaton, Onfsm):
268-
state.transitions[i].append(('epsilon', state))
269-
if isinstance(automaton, Mdp):
270-
state.transitions[i].append((state, 1.))
271-
if isinstance(automaton, StochasticMealyMachine):
272-
state.transitions[i].append((state, 'epsilon', 1.))
273-
else:
274-
if isinstance(automaton, Dfa):
275-
state.transitions[i] = sink_state
276-
if isinstance(automaton, MooreMachine):
277-
state.transitions[i] = sink_state
263+
target_state = state if missing_transition_go_to == 'self_loop' else sink_state
264+
265+
if isinstance(automaton, (DeterministicAutomaton, Dfa, MooreMachine, MealyMachine)):
266+
state.transitions[i] = target_state
278267
if isinstance(automaton, MealyMachine):
279-
state.transitions[i] = sink_state
280268
state.output_fun[i] = 'epsilon'
281-
if isinstance(automaton, Onfsm):
282-
state.transitions[i].append(('epsilon', sink_state))
283-
if isinstance(automaton, Mdp):
284-
state.transitions[i].append((sink_state, 1.))
285-
if isinstance(automaton, StochasticMealyMachine):
286-
state.transitions[i].append((sink_state, 'epsilon', 1.))
269+
elif isinstance(automaton, (Onfsm, NDMooreMachine)):
270+
state.transitions[i].append(('epsilon', target_state) if
271+
isinstance(automaton, Onfsm) else target_state)
272+
elif isinstance(automaton, Mdp):
273+
state.transitions[i].append((target_state, 1.))
274+
elif isinstance(automaton, StochasticMealyMachine):
275+
state.transitions[i].append((target_state, 'epsilon', 1.))
287276

288277
return automaton
289278

0 commit comments

Comments
 (0)