Skip to content

The repository contains app demonstrates the use of threaded code for hierarchical state machine on JS (JavaScript)

Notifications You must be signed in to change notification settings

mk590901/Switch-TC-JS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 

Repository files navigation

Switch-TC-JS

The application tests threaded code generated by the editor of hierarchical state machines. The original scheme can be seen on the switch_reset.svg attached to the project. It's 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.

Precondition

The editor's Planner module was supplemented with JS code generator, which automatically create the switch_reset_helper.mjs file with the transfer functions. This file also contain function createHelper builds QHsmHelper class for processing these functions. 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 placed to the tc_core.mjs file: EventWrapper, which describes and keep an event, QHsmHelper which contains a container of threaded codes and ensures its execution under the influence of events, ThreadedCodeExecutor - a class ensures the launch of threaded code for a specific state and event.

The generated switch_reset_helper.mjs 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:

//	File switch_reset_helper.mjs automatically generated at 2025-01-02 10:12:32

import {QHsmHelper,ThreadedCodeExecutor} from './tc_core.mjs';

class Switch_resetHelper {
    constructor() {
      this.helper_ = new QHsmHelper('switch');
      this.createHelper();
    }

    //	Transfer functions

    // switchEntry(data) {
    // }

    // switchInit(data) {
    // }

    offEntry(data) {
      console.log('OFF');
    }
  
    offReset(data) {
      console.log('@RESET');
    }
  
    // offExit(data) {
    // }
  
    offTurn(data) {
      console.log('OFF: TURN');
    }
  
    onEntry(data) {
      console.log('ON ');
    }

    // onExit(data) {
    // }
    
    onTurn(data) {
      console.log('ON : TURN');
    }
  
    init() {
      this.helper_.post('init');
    }
  
    run(eventName) {
      this.helper_.post(eventName);
    }
  
    createHelper() {
      this.helper_.insert('switch', 'init', new ThreadedCodeExecutor(this.helper_, 'off', [
        // this.switchEntry,
        // this.switchInit,
        this.offEntry,
      ]));
      this.helper_.insert('off', 'RESET', new ThreadedCodeExecutor(this.helper_, 'off', [
        this.offReset,
        // this.offExit,
        // this.switchInit,
        this.offEntry,
      ]));
      this.helper_.insert('off', 'TURN', new ThreadedCodeExecutor(this.helper_, 'on', [
        this.offTurn,
        this.onEntry,
      ]));
      this.helper_.insert('on', 'RESET', new ThreadedCodeExecutor(this.helper_, 'off', [
        this.offReset,
        // this.onExit,
        // this.offExit,
        // this.switchInit,
        this.offEntry,
      ]));
      this.helper_.insert('on', 'TURN', new ThreadedCodeExecutor(this.helper_, 'off', [
        this.onTurn,
        // this.onExit,
        // this.offExit,
        // this.switchInit,
        this.offEntry,
      ]));
    }
  }
  
  export default Switch_resetHelper;

Additional modules

To test the threaded code for hierarchical state machine, need to manually create small module that ensure the launch of the application:

test_switch.mjs

function testSwitch() {
    const hsmHelper = new Switch_resetHelper();
    hsmHelper.init();
    hsmHelper.run('TURN');
    hsmHelper.run('RESET');
    hsmHelper.run('TURN');
    hsmHelper.run('TURN');
    hsmHelper.run('RESET');
  }
  
  testSwitch();
  

Description of the application

The application is created as a ubuntu console application and can be launched via node app in terminal mode as shown below:

micrcx@micrcx-desktop:~/JS/switch_tc$ node test_switch.mjs
OFF
OFF: TURN
ON
@RESET
OFF
OFF: TURN
ON
ON : TURN
OFF
@RESET
OFF
micrcx@micrcx-desktop:~/JS/switch_tc$

Movie

nodejs.webm

About

The repository contains app demonstrates the use of threaded code for hierarchical state machine on JS (JavaScript)

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published