The application tests threaded code generated by the editor of hierarchical state machines. The original scheme can be seen in the attached movie. It's another model of a switch affected by two events: TURN and RESET. The first switches two states ON and OFF, the second resets the state machine to the OFF state regardless of what state it was in before.
The editor's Planner module was supplemented with Dart code generator, which automatically generates the switch_reset_helper.dart file with the class of the same name. A core has also been added to the application, which services the launch of threaded code and the impact of events on it. This is a set of several very simple classes: EventWrapper, which describes and keep an event, QHsmHelper with the IQHsmStateMachineHelper interface, which contains a container of threaded codes and ensures its execution under the influence of events, ThreadedCodeExecutor - a class that ensures the launch of threaded code for a specific state and event.
The generated wwitch_reset_helper.dart file is a skeleton for the logical part of the application, namely the list and bodies of empty transfer functions that can and should be filled with some content. For example, with trace elements in the simplest case. Some functions may not be used and should be deleted or commented out:
// Class Switch_resetHelper automatically generated at 2024-12-11 15:28:59
import '../core/q_hsm_helper.dart';
import '../core/threaded_code_executor.dart';
class Switch_resetHelper {
final QHsmHelper helper_ = QHsmHelper('switch');
Switch_resetHelper() {
createHelper();
}
// void switchEntry([Object? data]) {
// }
//
// void switchInit([Object? data]) {
// }
void offEntry([Object? data]) {
print ('OFF');
}
void offReset([Object? data]) {
print ('@RESET');
}
// void offExit([Object? data]) {
// }
void offTurn([Object? data]) {
print ('OFF: TURN');
}
void onEntry([Object? data]) {
print ('ON ');
}
// void onExit([Object? data]) {
// }
void onTurn([Object? data]) {
print ('ON : TURN');
}
void init() {
helper_.post('init');
}
void run(final String eventName) {
helper_.post(eventName);
}
String state() {
return helper_.getState();
}
void createHelper() {
helper_.insert('switch', 'init', ThreadedCodeExecutor(helper_, 'off', [
// switchEntry,
// switchInit,
offEntry,
]));
helper_.insert('off', 'RESET', ThreadedCodeExecutor(helper_, 'off', [
offReset,
// offExit,
// switchInit,
offEntry,
]));
helper_.insert('off', 'TURN', ThreadedCodeExecutor(helper_, 'on', [
offTurn,
onEntry,
]));
helper_.insert('on', 'RESET', ThreadedCodeExecutor(helper_, 'off', [
offReset,
// onExit,
// offExit,
// switchInit,
offEntry,
]));
helper_.insert('on', 'TURN', ThreadedCodeExecutor(helper_, 'off', [
onTurn,
// onExit,
// offExit,
// switchInit,
offEntry,
]));
}
}
The application is created as a dart console application: switch_tc.dart and can be launched in terminal mode:
import 'switch_reset_helper.dart';
String run() {
testSwitch();
return 'Was done';
}
void testSwitch() {
Switch_resetHelper hsmHelper = Switch_resetHelper();
hsmHelper.init();
hsmHelper.run('TURN');
hsmHelper.run('RESET');
hsmHelper.run('TURN');
hsmHelper.run('TURN');
hsmHelper.run('RESET');
}
In addition, a unit test is written:
import 'package:switch_tc/switch_reset_helper.dart';
import 'package:switch_tc/switch_tc.dart';
import 'package:test/test.dart';
void main() {
test('run', () {
expect(run(), 'Was done');
});
test('run', () {
Switch_resetHelper hsmHelper = Switch_resetHelper();
expect(hsmHelper.state(), 'switch');
hsmHelper.init();
expect(hsmHelper.state(), 'off');
hsmHelper.run('TURN');
expect(hsmHelper.state(), 'on');
hsmHelper.run('RESET');
expect(hsmHelper.state(), 'off');
hsmHelper.run('TURN');
expect(hsmHelper.state(), 'on');
hsmHelper.run('TURN');
expect(hsmHelper.state(), 'off');
hsmHelper.run('RESET');
expect(hsmHelper.state(), 'off');
});
}
Result
/home/michael-k/snap/flutter/common/flutter/bin/cache/dart-sdk/bin/dart --enable-asserts --no-serve-devtools /home/michael-k/AndroidStudioProjects/Flutter/switch_tc/bin/switch_tc.dart
OFF
OFF: TURN
ON
@RESET
OFF
OFF: TURN
ON
ON : TURN
OFF
@RESET
OFF
App switch_tc: Was done!
Process finished with exit code 0