Showing posts with label en. Show all posts
Showing posts with label en. Show all posts

Saturday, July 9, 2016

State machines in JS

You can read in previous blog posts the reasoning behind state machines: they simplify and unify the way we define states and transitions in complex systems.

I ported the JAVA implementation into a simplified JS one.

https://bitbucket.org/xferro/sicoris-statemachine-js

I hope you enjoy it

Optionals in JS

If you had used Optional in other languages, I implemented a very simple version for JS. Check tests for using it properly.

Take a look at it https://bitbucket.org/xferro/sicoris-optional-js

Any comments are more than welcome.

Monday, September 7, 2015

State machines for Java

Managing object states for multi-threaded applications is hard. Especially, when this state can transition to another state depending on multiple events. 

I started thinking about this problem when implementing SIP protocol solutions. A SIP INVITE server transaction would look like:

                               |INVITE
                               |pass INV to TU
            INVITE             V send 100 if TU won't in 200ms
            send response+-----------+
                +--------|           |--------+101-199 from TU
                |        | Proceeding|        |send response
                +------->|           |<-------+
                         |           |          Transport Err.
                         |           |          Inform TU
                         |           |--------------->+
                         +-----------+                |
            300-699 from TU |     |2xx from TU        |
            send response   |     |send response      |
                            |     +------------------>+
                            |                         |
            INVITE          V          Timer G fires  |
            send response+-----------+ send response  |
                +--------|           |--------+       |
                |        | Completed |        |       |
                +------->|           |<-------+       |
                         +-----------+                |
                            |     |                   |
                        ACK |     |                   |
                        -   |     +------------------>+
                            |        Timer H fires    |
                            V        or Transport Err.|
                         +-----------+  Inform TU     |
                         |           |                |
                         | Confirmed |                |
                         |           |                |
                         +-----------+                |
                               |                      |
                               |Timer I fires         |
                               |-                     |
                               |                      |
                               V                      |
                         +-----------+                |
                         |           |                |
                         | Terminated|<---------------+
                         |           |
                         +-----------+

If you don’t think of an easy approach to representing/coding these state machines, you will end up having something: difficult to write, error prone, difficult to test and impossible to trace. The main sign that you are doing it wrong is when your code is full of if/else, synchronized blocks and you had to debug for 2d to find out how the hell you ended up in such an unexpected state.

So, if you happen to have to manage complex state objects, you should take a look to my library. I just finished a new iteration to the code and. Much happier than ever with it: https://bitbucket.org/xferro/sicoris-statemachine . There's already a SNAPSHOT version available, so you can use it already.


Any feedback is more than welcome!

Sunday, September 6, 2015

Better JDBC Prepared Statements?

After using PreparedStatements for a while, you understand how painful it is maintaining SQL queries. Setting values by position is error-prone and tough.
Let's imagine a query that you want to check multiple columns against the same value:
SELECT * FROM Table WHERE a > ? AND b < ? AND c == ?
You will need to have the following code in place:
int THE_VALUE = 12345;
stmt.setInt(1, THE_VALUE);
stmt.setInt(2, THE_VALUE);
stmt.setInt(3, THE_VALUE);
Just imagine that you need to refactor the query such as the new values don't have nothing to do with THE_VALUE. The query could look like this:
SELECT * FROM Table WHERE (a > ? OR ? ) AND (b < ? OR ?) AND c == ?
You'd need to refactor your previous query such as:
int THE_VALUE = 12345;
boolean ANOTHER_VALUE = false;
stmt.setInt(1, THE_VALUE);
stmt.setInt(3, THE_VALUE);
stmt.setInt(5, THE_VALUE);
stmt.setBoolean(2, ANOTHER_VALUE);
stmt.setBoolean(4, ANOTHER_VALUE);
Not sure what you think about, but having to count question marks in a query to make things work never looked very attractive to me. I think we could be smarter than that

So what?

Why not having something like:
SELECT * FROM Table WHERE a > @{startDate} AND b < @{startDate} AND C = @{startDate}
and code like:
int THE_VALUE = 12345;
smartStatement.setInt("startDate", THE_VALUE);
The second approach is not impacted by any refactoring and it's less error prone. So, this small library tackles this problem with a very simplistic approach.

The approach

The library is very compact and simple. It is responsible for parsing the sql query and populating prepared statement values, but nothing more. So, creation of the prepared statement itself is not part of this library. I wanted to avoid over-engineered wrappers for such a small problem

What is not supported?

Do not use parametrized values in the queries. It might break the parsing logic. So something like this will break:
String sql = "SELECT * FROM MY_STRINGS WHERE str = \"a string containing weird stuff: ${else}\" AND id = ${id}";
Use the following instead:
String weird = "a string containing weird stuff: ${else}";
String sql = "SELECT * FROM MY_STRINGS WHERE str = ${weird} AND id = ${id}";
smartStatement.setString(stmt, "weird", weird);
smartStatement.setString(stmt, "id", "WTF");

That's brilliant. Where can I find it?

Check my sicoris-jdbc bickbucket repo . Comment it, fork it, use it. Any feedback is more than welcome.

Friday, July 18, 2014

Wednesday, September 4, 2013

Finite State Machine for Java... and 2

I just needed to refactor the finite state machine for java project that is available in https://github.com/xaviferro/xiron-statemachine

Why? I was not happy with it. Now I think it's much more consistent. I just used the commuting time again for this.


I copy below the README file available in github, in case anyone might be interested. Any feedback is more than welcome.


--------------------------

A state machine is a well-known way of defining and managing complex instance state
for a finite state machine in a high concurrent event-driven environment. 
The state changes quickly and we need to avoid repetitive manual work for managing
the state, which ends up being quite error prone.

The goal of this library is to provide an easy and simple implementation that covers 
most of the cases in a concurrent system. On top of that, the library provides
some nice decoration annotations that allow developers to create state machines
easily.

So, what is a state machine? You might find quite some nice explanations in wikipedia
about Finite State Machine. In my words, a state machine is defined as a set of states, 
events that might be triggered and transitions between states, which happen given an 
specified event. A transition is a tuple of state-event-state. Each tuple defines an 
allowed transition between the source state and target state based on a triggered event.
Transitions that have not been declared will throw an exception, as they are not
allowed at all.

In order to provide fully control on the execution of the transition, each transition
is divided in 3 phases:

- First phase, when exiting the source state. It's the only phase that allows cancelling
  the transaction. See ExitStateController.
  
- Second phase, the transition itself. It's the phase that developers are going to use
  the most. Take a look at TransitionController.
  
- And last but not least, when entering the target state. In this phase we might want
  to process a new event without releasing the lock. This is very useful for intermediate
  conditional states that evaluate a condition and take a decision. See
  EnterStateController.

Some other properties of the state machine are:
- A state machine has one starting state.
- A state machine has multiple intermediate states.
- A state machine has multiple finish states.
- We cannot define transitions sourced in a final state unless they are reflexive ones.
- When created, the state machine state is the starting state.
- Transitions are executed atomically.

The basic model of the state machine, separates the definition -StateMachiheDefinition-,
from the execution state -StateMachine- from the execution strategy -StateMachineStrategy-.

You can see in the model that I have separated the definition of the state machine 
(see StateMachineDefinition for further details) from the execution (see StateMachine)

Apart from the manual state machine definition, the library enriches the model with
some annotation utilities. For example, the following class would define a state machine:

@AStateMachine
public class LegalStateMachineTest {
    @State(isStart=true) public static final String STATE_A = "STATE_A";
    @State public static final String STATE_B = "STATE_B";
    @State public static final String STATE_COND = "STATE_COND";
    @State public static final String STATE_D = "STATE_D";
    
    @Event public static final String EVENT_AB = "EVENT_AB";
    @Event public static final String EVENT_BB = "EVENT_BB";
    @Event public static final String EVENT_BC = "EVENT_BC";
    @Event public static final String EVENT_CD = "EVENT_CD";
    
    @Transitions({@Transition(source=STATE_A, target=STATE_B, event=EVENT_AB),
                  @Transition(source=STATE_B, target=STATE_COND, event=EVENT_BC),
                  @Transition(source=STATE_COND, target=STATE_D, event=EVENT_CD)})
    public void noop(TransitionInfo info) {
        System.out.println("#tx: " + info);
    }
    
    @ExitState(STATE_A)
    public Boolean exitA(TransitionInfo info) {
        System.out.println("#exit: " + info);
        return true;
    }
    
    @EnterState(STATE_COND)
    public EventInfo transitionBC(TransitionInfo info) {
        System.out.println("#enter: " + info);
        return new EventInfo(EVENT_CD, null);
    }
    
    @Test
    public void test() throws StateMachineException {
        StateMachine sm = StateMachines.newNonReentrant(this);
        sm.processEvent(EVENT_AB, null);
        sm.processEvent(EVENT_BC, null);
        
        Assert.assertEquals(sm.getCurrentState(), STATE_D);
    }
}

If we execute the previous test, we would get the following output:
#exit: [STATE_A + EVENT_AB -> STATE_B]
#tx: [STATE_A + EVENT_AB -> STATE_B]
#tx: [STATE_B + EVENT_BC -> STATE_COND]
#enter: [STATE_B + EVENT_BC -> STATE_COND]
#tx: [STATE_COND + EVENT_CD -> STATE_D]

In order to instantiate this class we need to invoke StateMachines.newReentrant(the_object),
that will return a state machine with:
- 4 states: STATE_A, STATE_B, STATE_COND and STATE_D
- 4 events: EVENT_AB, EVENT_BB, EVENT_BC and EVENT_CD
- 1 enter state definition. When entering the state STATE_COND, we decide to trigger a new
  event before releasing the lock
- 3 transitions: 
        STATE_A + EVENT_AB -> STATE_B 
        STATE_B + EVENT_BC -> STATE_COND
        STATE_COND + EVENT_CD -> STATE_D

Hopefully, the example is clear enough.

The last point to mention is about choosing the right strategy when creating a state machine.
There are only two at the moment:
- Reentrant. Means that we can trigger an event when executing a transaction from that thread
  (outside the allowed step during the enter state). Be careful on that.
- Non reentrant. Means that the state machine won't allow transitions during a transition.
  This enforces to think more about the concurrency model and it's, normally, a safer approach.

  

Sunday, March 4, 2012

state machine for java

Not bad. I started the project after my last post. That means less than 3 weeks. Done in my spare time, when commuting to work. In tram 4. Motivation changes the dimension time/productivity.

The result: just a basic concurrent state machine api that allows easily managing complex states and transitions. Besides, I have enriched the controllers with some fancy annotations for simplifying the state machine definition.

Very happy with the result. Still pending some improvements and functionalities.

My first open source contribution after several years thinking about it, under Apache 2.0 license.

Any feedback would be great. I hope you enjoy it: https://github.com/xaviferro/xiron-statemachine

Thursday, February 9, 2012

the Singleton pattern reviewed

Interesting approach to the singleton pattern in Joshua Bloch's book 'Effective Java' 2nd edition.


public enum Singleton {
    INSTANCE;
}

This simple idiom solves a lot of non-trivial problems, as synchronization or serialization. I do really like this approach -I had never thought about this option. 


What about you?



First and.. ¿last?

This is the first post. Let's hope it's not the last one :-)

State machines in JS

You can read in previous blog posts the reasoning behind state machines: they simplify and unify the way we define states and transitions i...