Giter Club home page Giter Club logo

kenkenpa's Introduction

Ken-Ken-Pa

Build Status Software License Join the chat at https://gitter.im/shiraji/kenkenpa

  • Download : kenkenpa
  • Download : kenkenpa-compiler

Yet, another light weight Java FSM library. This library follows the idea from Google AutoValue. It generates a subclass that handles states.

How to install?

Use gradle.

buildscript {
    repositories {
      jcenter()
      mavenCentral()
    }
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.7'
    }
}

apply plugin: 'com.neenbedankt.android-apt'

android {
    packagingOptions {
        exclude 'META-INF/services/javax.annotation.processing.Processor'
    }
}

dependencies {
    compile 'com.github.shiraji:kenkenpa:1.0.3'
    apt 'com.github.shiraji:kenkenpa-compiler:1.0.3'
}

packagingOptions block is for developers who use other apt libraries.

If you cannot use gradle, then download jar from Download button on top of the documentation.

How to install develop version?

Please refer jitpack.io

If you do not find right version, please leave a message on gitter.

What is Ken-Ken-Pa?

Ken-Ken-Pa is a Japanese style of Hop Scotch. The difference between Hop Scotch and Ken-Ken-Pa is whether they use squares or circles.

What does this library do?

When you call a method that has @Hop(from = "CIRCLE1", to = "CIRCLE2") annotation, this is an image of execution steps of Ken-Ken-Pa.

execution_image

  1. Call @TakeOff("CIRCLE1") method if exist
  2. Call the method that has @Hop(from = "CIRCLE1", to = "CIRCLE2")
  3. Call @Land("CIRCLE2") method if exist
  4. Change the current state to "CIRCLE2"

How to use?

There are a few steps to use this library.

First, add @KenKenPa annotation to the abstract class. This abstract class will be a state machine class. @KenKenPa annotation needs to have a default state. Currently, states are represented by only String values.

@KenKenPa("CIRCLE1")
public abstract class SimpleFSM {
}

Secondly, create new instance method. The method must return an instance of subclass. The subclass name format is KenKenPa_XXX where XXX is the abstract class name.

@KenKenPa("CIRCLE1")
public abstract class SimpleFSM {
    public static SimpleFSM create() {
        return new KenKenPa_SimpleFSM();
    }
}

Thirdly, add @Hop to define state changes.

@KenKenPa("CIRCLE1")
public abstract class SimpleFSM {
    public static SimpleFSM create() {
        return new KenKenPa_SimpleFSM();
    }

    @Hop(from = "CIRCLE1", to = "CIRCLE2")
    public void fire() {
      System.out.println("fire!");
    }
}

Now, you are set. You can use this class

SimpleFSM simpleFSM = SimpleFSM.create();
simpleFSM.fire(); // => fire! and change current state to CIRCLE2

How to create multiple @Hops?

Sadly, there is limitation on Java (less than Java8). You cannot set same annotation on the same method. Instead of using @Hop, use @Hops which take multiple @Hop as parameters.

@KenKenPa("CIRCLE1")
public abstract class SimpleFSM {
    public static SimpleFSM create() {
        return new KenKenPa_SimpleFSM();
    }

    @Hops({@Hop(from = "CIRCLE1", to = "CIRCLE2"),
            @Hop(from = "CIRCLE2", to = "CIRCLE3")})
    public void fire() {
      System.out.println("fire!");
    }
}
SimpleFSM simpleFSM = SimpleFSM.create();
simpleFSM.fire(); // => fire!
simpleFSM.fire(); // => fire! and change current state to CIRCLE3

How to get current state?

To get current state, you can add GetCurrentState interface to the abstract class.

@KenKenPa("CIRCLE1")
public abstract class SimpleFSM implements GetCurrentState

this interface offers String getCurrentState() method.

SimpleFSM simpleFSM = SimpleFSM.create();
simpleFSM.getCurrentState() // => CIRCLE1

What is @TakeOff?

When children hop to another circle, they "take off" the current circle. @TakeOff is an annotation that represents "Run this method when the current state changed from this state." This annocation is useful when the state require clean up. Annotated method must not have parameters. A state of @TakeOff should be at least one state that defines at @Hop's from parameter. Also, void should be return type. (You can still set return type but you have no way to get the return value)

@KenKenPa("CIRCLE1")
public abstract class MainFSM implements GetCurrentState {
    public static MainFSM newInstance() {
        return new KenKenPa_MainFSM();
    }

    @Hop(from = "CIRCLE1", to = "CIRCLE2")
    public void circle1ToCircle2() {
    }

    @Hop(from = "CIRCLE1", to = "CIRCLE3")
    public void circle1ToCircle3() {
    }

    @Hop(from = "CIRCLE2", to = "CIRCLE1")
    public void circle2ToCircle1() {
    }

    @TakeOff("CIRCLE1")
    void endCircle1() {
      System.out.println("Exit from CIRCLE1");
    }
}
SimpleFSM simpleFSM = SimpleFSM.create();
simpleFSM.circle1ToCircle2(); // => display 'Exit from CIRCLE1'
simpleFSM.circle2ToCircle1(); // => change current state to CIRCLE1
simpleFSM.circle1ToCircle3(); // => display 'Exit from CIRCLE1' and change current state to CIRCLE3

Add description for string parameter.

What is @Land?

When children hop to another circle, they 'land' the next circle. @Land is an annotation that represents "Run this method when the current state became this state." This annotation is useful when the state have the same initialization steps. Annotated method must not have parameters. A state of @Land should be the default state or at least one state that defines at @Hop's to parameter. Also, void should be return type. (You can still set return type but you have no way to get the return value)

@KenKenPa("CIRCLE1")
public abstract class MainFSM implements GetCurrentState {
    public static MainFSM newInstance() {
        return new KenKenPa_MainFSM();
    }

    @Hop(from = "CIRCLE1", to = "CIRCLE2")
    public void circle1ToCircle2() {
    }

    @Hop(from = "CIRCLE1", to = "CIRCLE3")
    public void circle1ToCircle3() {
    }

    @Hop(from = "CIRCLE2", to = "CIRCLE1")
    public void circle2ToCircle1() {
    }

    @Land("CIRCLE2")
    void startCircle2() {
      System.out.println("Now CIRCLE2");
    }
}
SimpleFSM simpleFSM = SimpleFSM.create();
simpleFSM.circle1ToCircle2(); // => display 'Now CIRCLE2'
simpleFSM.circle2ToCircle1(); // => change current state to CIRCLE1

Add description for string parameter.

How actually works?

If the developer creates following KenKenPa annotation class

@KenKenPa("CIRCLE1")
public abstract class TestSM implements GetCurrentState {

    private String mText;

    TestSM(String text) {
        mText = text;
    }

    public static TestSM create(String text) {
        return new KenKenPa_TestSM(text);
    }

    @Hops({@Hop(from = "CIRCLE1", to = "CIRCLE2"), @Hop(from = "CIRCLE2", to = "CIRCLE1")})
    public void fire() {
        System.out.println("Fire!");
    }

    @Hop(from = "CIRCLE1", to = "CIRCLE2")
    public int fire2() {
        return 1;
    }

    @Land("CIRCLE1")
    public void land() {
        System.out.println("land");
    }

    @TakeOff("CIRCLE2")
    public void takeOff() {
        System.out.println("takeoff");
    }
}

KenKenPa_TestSM is generated at compile time.

public final class KenKenPa_TestSM extends TestSM {
  private String $$mCurrentState$$;

  KenKenPa_TestSM(String text) {
    super(text);
    this.$$mCurrentState$$ = "CIRCLE1";
    land();
  }

  @Override
  @Hops({
      @Hop(from = "CIRCLE1", to = "CIRCLE2"),
      @Hop(from = "CIRCLE2", to = "CIRCLE1")
  })
  public final void fire() {
    String newState = takeOff$$fire();
    super.fire();
    land$$fire(newState);
    $$mCurrentState$$ = newState;
  }

  @Override
  @Hop(
      from = "CIRCLE1",
      to = "CIRCLE2"
  )
  public final int fire2() {
    String newState = takeOff$$fire2();
    int returnValue = super.fire2();
    land$$fire2($$mCurrentState$$);
    $$mCurrentState$$ = newState;
    return returnValue;
  }

  @Override
  public final String getCurrentState() {
    return $$mCurrentState$$;
  }

  private final String takeOff$$fire2() {
    switch($$mCurrentState$$) {
      case "CIRCLE1":
      return "CIRCLE2";
    }
    // No definition! Return the default state
    return "CIRCLE1";
  }

  private final void land$$fire2(String newState) {
    switch(newState) {
      case "CIRCLE1":
      land();
      break;
    }
  }

  private final String takeOff$$fire() {
    switch($$mCurrentState$$) {
      case "CIRCLE1":
      return "CIRCLE2";
      case "CIRCLE2":
      takeOff();
      return "CIRCLE1";
    }
    // No definition! Return the default state
    return "CIRCLE1";
  }

  private final void land$$fire(String newState) {
    switch(newState) {
      case "CIRCLE1":
      land();
      break;
      case "CIRCLE2":
      break;
    }
  }
}

License

Copyright 2016 Yoshinori Isogai

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

kenkenpa's People

Contributors

gitter-badger avatar imort avatar shiraji avatar vbauer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

kenkenpa's Issues

Refactor KenKenPaProcessor

KenKenPaProcessor seems too big to implement new feature.

Let's create model that handle each java class's annotations.

Method order matter for validation

From what I see this code, method order is matter for validation. This code expected Hop method fist. I don't think this is good idea as a Java library.

Move to Object state

I think it's time to move String state to Object state.

Since this library uses switch for changing state. I might redesign how to change state. Creating custom enum/class for each state might be one possible idea.

This change definitely are huge. So if I can merge this change I will bump version up to 2.0.0.

Add version number in build.gradle

Currently, if I run ./gradlew install, it generates kenkenpa-unspecified.jar in local maven repository. This make hard to develop/test in local environment. Add version to kenkenpa/kenkenpa-compiler's build.gradle. This version should be related to version inside publish block

Default state's behavior

It now always run @Land of default state. However, I think there should be an option that calls the method or not.

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.