Giter Site home page Giter Site logo

codacy-badger / aptacode.statenet Goto Github PK

View Code? Open in Web Editor NEW

This project forked from aptacode/statenet

0.0 0.0 0.0 8.98 MB

A small .Net Standard library used to model simple State Machines

Home Page: https://aptacode.com/opensource/statenet/

License: MIT License

C# 100.00%

aptacode.statenet's Introduction

A .Net Standard library used to model complicated State Machines

Discord Group for development / help

https://discord.gg/D8MSXJB

NuGet Codacy Badge

Overview

The original goal of StateNet was to create a simple way to define and control the flow through pages of an application. Since its inception the library has grown versatile with many usecases.

Usage

StateNet works by defining a network of states interlinked by the inputs that can be applied to them.

How to Configure the Network

List all of the states your application needs and consider the relationship between them to determine the inputs for your system. Create a connection by assigning a weight to each input that can be applied to a state. There are three approaches to configure the network:

1) Network Creation Tool

Using the built in network creation tool you can graphically create / modify networks saved as Json files.

2) Object oriented

  • Create a class which derives from 'Network'
  • Define each State as a property on the class
  • Use attributes on the State properties to define the relationships between them
  public class CustomNetwork : Network
  {
	[StartState("Start")]
	[Connection("Left", "D1")]
	[Connection("Right", "D2")]
	public State StartTestState { get; set; }

	[StateName("D1")]
	[Connection("Next", "D1", "StateVisitCount(\"D2\") < 2 ? 1 : 0")]
	[Connection("Next", "End", "StateVisitCount(\"D2\") >= 2 ? 1 : 0")]
	public State Decision1TestState { get; set; }

	[StateName("D2")]
	[Connection("Next", "D1", "StateVisitCount(\"D2\") > 2 ? 1 : 0")]
	[Connection("Next", "D2", "StateVisitCount(\"D2\") <= 2 ? 1 : 0")]

	public State Decision2TestState { get; set; }

	[StateName("End")]
	public State EndTestState { get; set; }
  }

3) Programmatic

	IStateNetwork stateNetwork = new StateNetwork();
	var networkEditor = new StateNetworkEditor(stateNetwork);

	networkEditor.SetStart("ready");

	networkEditor.Always("ready", "Play", "playing");
	networkEditor.Always("ready", "Stop", "stopped");
	networkEditor.Always("playing", "Pause", "paused");
	networkEditor.Always("playing", "Stop", "stopped");
	networkEditor.Always("paused", "Play", "playing");
	networkEditor.Always("paused", "Stop", "stopped");

Running the engine

Pass the configured network to the engine through its constructor. Optionally subscribe to the engines events such as OnStarted, OnFinished, OnTransition or subscribe to specific State transitions. Control the flow through the network by calling Start(), Stop() and Apply(action)

//Create and configure the Network using your prefered method
var network = ...

//Create the Engine
var engine = new Engine(network);

//Subscribe to the engines events
engine.OnFinished += (sender) => { ... }
engine.OnTransition += (sender) => { ... }
engine.Subscribe(network["Playing"], () => { ... });

//Start the Engine
engine.Start();

//Apply actions to move through the states
engine.Apply("Play");
...
engine.Apply("Stop");

License

MIT License

aptacode.statenet's People

Contributors

timmoth avatar matthewzar avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.