Notification on condition failure

55 views
Skip to first unread message

Gaurav Arora

unread,
Oct 29, 2014, 4:08:31 AM10/29/14
to squirrel-st...@googlegroups.com, shilpi...@gmail.com
Hi,

We're trying to figure out if there is anyway that we can be notified of condition failures. Our use case is that because we're exposing our FSM as a REST service there might be cases where the user does not POST/PUT the correct data. In such a case we'd like to know when a condition fails so we can return an appropriate response.

Right now we're using the context as a DTO. We set the error in the context within the condition and then inspect it after the event is fired. Is there a better way to implement this?

Regards,
Gaurav

He Henry

unread,
Oct 29, 2014, 5:05:07 AM10/29/14
to squirrel-st...@googlegroups.com, shilpi...@gmail.com
Hi Gaurav,

If condition fail cause no state transition happened, a TransitionDeclinedEvent will be fired and also AbstractStateMachine.afterTransitionDeclined() method will be invoked. Basically you can either add a declined event listener(see StateMachineLogger) to state machine or override afterTransitionDeclined method in your state machine implementation class to handle this case.
For example "StateMachine.getRawStateFrom(fromStateId).getTransitions(event)" means the condition of these transition fails.

I hope this answers your question.

Henry

在 2014年10月29日星期三UTC+8下午4时08分31秒,Gaurav Arora写道:

Gaurav Arora

unread,
Oct 29, 2014, 5:20:25 AM10/29/14
to squirrel-st...@googlegroups.com, shilpi...@gmail.com
Hi Henry,

That makes sense and I can see that TransitionDeclinedEvent is indeed fired. However consider a use case where I have multiple conditions. In such a case I would never know which of the conditions fails. I guess what I am asking is, is there a mechanism to propagate failure reason up the stack?

Thanks,
Gaurav

He Henry

unread,
Oct 29, 2014, 6:24:31 AM10/29/14
to squirrel-st...@googlegroups.com, shilpi...@gmail.com
I think you can. See the sample code.

// this will return all the transition that can be triggered by that event. Since no transition happened which means conditions of returned 
// transition instances were not pass.
ImmutableTransition<~> transitionsFailOnConditionGuard = someStateMachine.getRawStateFrom(fromStateId).getTransitions(event); 
// Thus you can iterate on this transition list and get its condition which is fail condition
for(ImmutableTransition<~> transition : transitionsFailOnConditionGuard) {
    Condition<~> failedCondition = transition.getCondition();
}

Is this what you want?

在 2014年10月29日星期三UTC+8下午5时20分25秒,Gaurav Arora写道:

Gaurav Arora

unread,
Oct 29, 2014, 6:44:01 AM10/29/14
to He Henry, squirrel-st...@googlegroups.com, shilpi...@gmail.com
If I have two or more conditions which I have and'd together I will only know that the overall condition failed, not which of the three conditions failed.

Let's say I have condition A and B. I use Conditions.and(A, B) and use that as a condition for my transition. When the transition failed, I will get a single condition that failed, not one of A or B.

I am not sure if I am explaining this correctly but I basically want that when a condition fails I should be able to attach a 'reason' to it.

--
You received this message because you are subscribed to a topic in the Google Groups "squirrel state machine" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/squirrel-state-machine/7wgEOM_95x4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to squirrel-state-ma...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

He Henry

unread,
Oct 29, 2014, 7:01:15 AM10/29/14
to squirrel-st...@googlegroups.com, hekai...@gmail.com, shilpi...@gmail.com
OK. I understand your question now. I think this may be a solution but I haven't tried yet.

Basically you can add a rejectReason field in your extended state machine, and when some condition fails you set the reject reason by retrieve current state machine instance from thread local, like "StateMachineContext.currentInstance().setRejectReason("some reason")". 

So when you handle the transition decline event you can know what cause failure. Does this solves your problem?

在 2014年10月29日星期三UTC+8下午6时44分01秒,Gaurav Arora写道:
To unsubscribe from this group and all its topics, send an email to squirrel-state-machine+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages