A simple hierarchical state machine compiler that generates C.

Overview

Makina

Makina is a work in progress. Use at your own risk.

Makina is a hierarchical state machine source-to-source translator. It takes state machine descriptions as input and produces C language implementations of those state machines.

Demo

Syntax

Each file given as input to the Makina compiler represents a single state machine. At the top of the file the name of the machine is specified with a machine statement:

Machines

machine Oven;

Here the machine is called Oven.

States

States are defined below that with state definitions:

machine Oven;
state closed {
    
}

Now the state machine Oven has one state called closed.

Of course state machines usually have more than one state. As many states as are required can be added to a single machine:

machine Oven;
state closed {
    
}
state open {
    
}

Now Oven has two states: closed and open.

Event Handlers

In addition to states, state machines also define events. Events are triggers that cause the state machine to take action. In order to assign an action to an event a state must include an event handler that could look like this:

machine Oven;
state closed {
    on open -> open;
}
state open {
    
}

The state closed now has an handler for the event named open. The handler causes the machine Oven to transition from closed to open. The full syntax for an event handler looks like this:

on  [()] [] [->|--> ];

The things in angle brackets can be arbitrary identifiers that conform to the C language concept of an identifier. Things in square brackets are optional. In the case above the handler includes an optional transition target, but not a guard or action.

Guards and actions can be used to create complex behavior. If a state includes a guarded handler then the user defined function that implements the guard must return a true value for the handler to be triggered. For example:

on foo (some_guard) guarded_action;
on foo default_action;

If a state has the previous two handlers defined and is processing the event foo first the user defined (C language) function some_guard will be called. If its return value is true then the function guarded_action will be called. If some_guard returned 0 then the machine will continue checking for handlers for event foo. The next defined handler is not guarded, so the function default_action will be called.

All actions and guards are implemented by the user as C language functions with the following prototype:

int (struct  *, struct _event *);

Transition targets can be default (-> ) or external (--> ). Default transitions behave like a local transition (they don't exit the source state) when the target state is a descendant of the source state and like an external transition otherwise. External transitions always cause the source state to exit.

Hierarchical States

To avoid duplicating event handlers for similar states Makina allows you to define sub states that defer unhandled events to their parent states. The closed state of the oven machine might have a few sub states:

machine Oven;
initial state closed {
    on open -> open;
    initial state idle {
        on start -> cooking;
    }
    state cooking {
        entry enable_heater;
        on timeout -> idle;
        exit disable_heater; 
    }
}
state open {
    on close -> closed;
    on start error;
}

Now state closed has two sub states: idle and cooking. Additionally the states closed and closed.idle have been designated as initial. This isn't strictly necessary in this case, because default initial states are those appearing first in document order, but has been added to show the syntax. Initial states are the states that are first entered when the machine is initialized.

Since neither sub state of closed defines a handler for the open event if the Oven machine is in either of those states when an open event is processed the parent state's handler, and any associated actions or transitions, will be triggered. In this case the open event will cause a transition to the open state regardless of which sub state was active.

Entry and Exit Actions

Entry and exit actions are similar to event handlers except they are triggered when a state is entered or exited. For example: the entry handler entry enable_heater; in state closed.cooking causes the function enable_heater to be called when that state is entered. Similarly, exit disable_heater; causes disable_heater to be called when state closed.cooking is exited. Exit handlers will be triggered event if the event that causes the transition is defined in a parent state as is the case with the open event in the example above.

Invoking Makina

Get the jar

Clone the repo and build the makina-compiler project using Intellij. Or download the latest jar from here.

Run the compiler

java -jar //makina-compiler.jar  [] []

Output

Two files, .h and .c will be created (or overwritten!) in the same directory as or in the specified output directory.

Command line options

-o or --output : Specify the output path for generated files.

-m or --machine-data : Specify the type of the ctx member of the generated machine type. The default type is void *. May occur zero or one times.

-e or --event-data : Specify the type of the ctx member of the generated event type. The default type is void *. May occur zero or one times.

-i or --include : Specify files to include in the generated C header. This can be used to provide definitions of custom types specified with -e or -m. May occur zero or more times.

You might also like...

Buried Barrels is a simple vanilla+ mod that generates hidden barrels throughout the world.

Buried Barrels is a simple vanilla+ mod that generates hidden barrels throughout the world. Special maps leading to these treasures can be found on mo

Apr 7, 2022

Simple, server side api for drawing on maps with runtime only state and no id collisions

Simple, server side api for drawing on maps with runtime only state and no id collisions! It can be used in non-main/server threads for better performance/more fps.

Sep 2, 2022

💾 Simple persisted state in react-native

React Native Use Persisted State Preview Introduce 💾 Simple persisted state in react-native 🌏 Globally accessable like redux, recoil... 🚀 No loadin

Aug 28, 2022

State-of-the-art cryptography to protect your world seed against seed cracking tools

SecureSeed State-of-the-art cryptography to protect your world seed against seed cracking tools. This mod is written for the Fabric Mod Loader. If you

Dec 28, 2022

The state-of-the-art Dashboard of Apache RoccketMQ provides excellent monitoring capability. Various graphs and statistics of events, performance and system information of clients and application is evidently made available to the user.

RocketMQ Dashboard How To Install With Docker get docker image mvn clean package -Dmaven.test.skip=true docker:build or docker pull apacherocketmq/ro

Dec 30, 2022

It generates a circular Image from users display names with animation support

DisplayNameView generates an avatar from users name. Setup allprojects { repositories { ... maven { url 'https://jitpack.io' }

Sep 24, 2021

Generates and keeps up-to-date your Spring Boot applications' Let's Encrypt or other ACME compliant SSL certificates.

Generates and keeps up-to-date your Spring Boot applications' Let's Encrypt or other ACME compliant SSL certificates.

Generates and keeps up-to-date your Spring Boot applications' Let's Encrypt or other ACME compliant SSL certificates. Pure Java in a single file of library code. An automated embedded alternative to Certbot and docker-sidecars. No JVM restart is needed on certificate update.

Nov 18, 2022

Representational State Transfer + Structured Query Language(RSQL): Demo application using RSQL parser to filter records based on provided condition(s)

Representational State Transfer + Structured Query Language: RSQL Demo application using RSQL parser to filter records based on provided condition(s)

Nov 23, 2022

A server-state reactive Java web framework for building real-time user interfaces and UI components.

RSP About Maven Code examples HTTP requests routing HTML markup Java DSL Page state model Single-page application Navigation bar URL path UI Component

Jul 13, 2022

Restler is a library that automatically generates a client for a web service at run time, by analyzing the respective annotated Spring controller interface

Restler Overview Restler is a library that automatically generates a client for a web service at run time, by analyzing the respective annotated Sprin

Oct 24, 2022

State of the art mutation testing system for the JVM

Pitest (aka PIT) is a state of the art mutation testing system for Java and the JVM. Read all about it at http://pitest.org Releases 1.7.3 #952 Mutate

Dec 26, 2022

Generates a Proguard mapping file for use in obfuscating your Java projects.

Reaper Generates a Proguard mapping file for use in obfuscating your Java projects. Features Automatically checks for duplicate names. Interactive, in

Dec 29, 2022

A gradle plugin generates resConfig & languages array from project res folder.

For Android application projects that accept user-submitted translations, the number of supported languages may be large and growing. When new languages are added, developers need to manually update resConfig (1) and language array xml/class (2). Manual means there could be human error.

Nov 12, 2022

A Parser That parses OpenAPI definitions in JSON or YAML format and Generates Pact files That contain HTTP request/response interactions by parsing Given Open API specifications

This is a Parser That parses OpenAPI definitions in JSON or YAML format and Generates Pact files That contain HTTP request/response interactions by parsing Given Open API specifications.

Mar 19, 2022

jextract is a tool which mechanically generates Java bindings from a native library headers.

Jextract jextract is a tool which mechanically generates Java bindings from a native library headers. This tools leverages the clang C API in order to

Dec 30, 2022

ReleaseFab is a Java client application which automatically generates Release Notes for any project.

ReleaseFab is a Java client application which automatically generates Release Notes for any project.

ReleaseFab is a Java client application which atomatically generates Release Notes for any project. The information can be gathered from multiple different sources including the source code itself, the Git repository and an Application Lifecycle Management System of your choice.

Jun 10, 2022

A library that automatically generates and manages configuration files based on classes.

sc-cfg SC-CFG is a simple, yet powerful library that automatically generate configuration files based on your classes. Compatible with Java 8+ and Kot

Nov 28, 2022

Executable state charts, workflows and more.

Executable state charts, workflows and more.

Act Getting started If you are using Maven, include the following in your POM: dependency groupIdorg.requirementsascode.act/groupId a

Dec 30, 2022

Sync DND state between Android phone and watch

Sync DND state between Android phone and watch

DNDSync This App was developed to enable Do Not Disturb (DND) synchronization between my Pixel phone and the Galaxy Watch 4 since this option was only

Dec 19, 2022
Comments
  • Is generating unreachable code correct?

    Is generating unreachable code correct?

    The generated C for this has unreachable code:

    machine unreach;
    
    initial state s {
        on TERMINATE -> .done;
        on I (guard_foo) clear_foo -> done;
    
        state done {}
    
        state s1 {
            on I -> done;
    
            state done {}
        }
    }
    
    state done {
    
    }
    

    like so:

    static int unreach_s_s1_done(struct unreach *self, struct unreach_event *event) {
            if (!self || !event) return -1;
            switch (event->id) {
            case unreach_event_I:
                    if (1) {
                            self->state = NULL;
                            self->state = unreach_s_s1_done;
                            break;
                    }
                    if (guard_foo(self, event)) {
                            self->state = NULL;
                            clear_foo(self, event);
                            self->state = unreach_s_done;
                            break;
                    }
                    break;
    

    The if (guard_foo) is unreachable and the C compiler complains about it.

    opened by David-Gould 2
  • Add support for user specified context types

    Add support for user specified context types

    In the generated struct <machine> { ... } and struct <machine>_event { ... } there is a void *ctx. The user should be able to specify the type of ctx. Maybe it could be passed on the command line (java -jar makina-compiler --machine-context struct my_type input.mkna) or the generated files could use a macro for that variable's type which would have to be defined by the user.

    opened by clnhlzmn 1
  • Please suppress redundant state = NULL" assignments.">

    Please suppress redundant "self->state = NULL" assignments.

    I'd like to use this for very small 8 bit MCUs like STM8 and Pauduk with the SDCC compiler. Unfortunately SDCC does not optimize away the dead assignments, which bloats the generated code considerably.

    If I were greedy, I'd also ask for an option to declare the type of the state variable so I could use uint8_t instead of int.

    BTW, I really like the description language and the generated code, so clear and clean.

    opened by David-Gould 8
Owner
Colin Holzman
Colin Holzman
Lightweight Java State Machine

Maven <dependency> <groupId>com.github.stateless4j</groupId> <artifactId>stateless4j</artifactId> <version>2.6.0</version>

Stateless 4 Java 772 Dec 22, 2022
jMonkeyEngine Animation State Machine

jme-anim-state-machine jMonkeyEngine Animation State Machine State machine to make animation states consume from a character controller state. This wa

Niels Drost 3 Oct 20, 2021
Hierarchical Temporal Memory implementation in Java - an official Community-Driven Java port of the Numenta Platform for Intelligent Computing (NuPIC).

htm.java Official Java™ version of... Hierarchical Temporal Memory (HTM) Community-supported & ported from the Numenta Platform for Intelligent Comput

Numenta 301 Dec 1, 2022
HMFF - A Hierarchical Mapping File Format

HMFF - A Hierarchical Mapping File Format This library provides a recursive key-value interpretation for your configuration files, with comment suppor

Justis R 3 Oct 23, 2021
SLUGGER: Lossless Hierarchical Summarization of Massive Graphs

SLUGGER: Lossless Hierarchical Summarization of Massive Graphs Source code for the paper SLUGGER: Lossless Hierarchical Summarization of Massive Graph

null 2 Dec 14, 2021
Datumbox is an open-source Machine Learning framework written in Java which allows the rapid development of Machine Learning and Statistical applications.

Datumbox Machine Learning Framework The Datumbox Machine Learning Framework is an open-source framework written in Java which allows the rapid develop

Vasilis Vryniotis 1.1k Dec 9, 2022
A Java Virtual Machine - running on a Java Virtual Machine - running on a (jk).

Javaception A Java Virtual Machine - running on a Java Virtual Machine - running on a (jk). Goals JVMS compliant Java Virtual Machine Somewhat fast Re

null 33 Oct 10, 2022