1

I am modelling a process using an UML state diagram. Here is some pseudo-code that determines the current state:

function getAccountState(customer) {

    if (authorizationRequired(customer)) {
        return State.AUTHORIZATION_REQUIRED
    }

    if (updateRequired(customer)) {
        return State.UPDATE_REQUIRED
    }

    return State.DRAFT
}

The closest I got was this diagram: State diagram

However, I think it is somewhat strange that each transition is contained twice. The order matters though which means, the authorization-check should always come first.

How would one model this process?

EDIT:

The background behind this process is a REST service. The account is modeled as a resource and can go through various states. Any time the resource is requested, the service performs the checks in the order described by the pseudo code above to generate an according representation. Depending on the answer, it includes either:

  • a link to authorize the account if the account requires authorization
  • a link to update the profile if an update is needed (this however can only happen once the account is authorized or does not have to be authorized)
  • a link to finalize the account if the profile is up-to-date (either because it had to be updated and was updated by the client or it never had to be updated in the first place)

The code above is just an example though. The service could also utilize a database field storing the "state", although this is an anti-pattern isn't? It is more feasible to "derive" the current state by applying the business rules on the stored data instead of (redundantly) storing the state in a separate field. That is what the pseudo code should indicate.

Thomas Eizinger
  • 1,404
  • 14
  • 25
  • Your code does not really represent a state machine. It is not clear how the auth-needed/upd.-req. operation influence the whole system. Also you can not transfer two times from the start. That will take an undetermined/random path, – qwerty_so Feb 21 '17 at 20:26
  • The code is supposed to represent the rules of the "active" state. E.g. When creating an account, the method is called and may return that the account needs to be authorized before continuing. After this action (for example authorization) the method is called again and may return that a profile update is needed. If not, the account is in the draft state. The code is only supposed to represent this logic, this code does not really exist in some system. I just wanted to illustrate the rules. – Thomas Eizinger Feb 21 '17 at 21:37
  • Yes, I know. But it just returns the conditions returned from the operations. This is not a state machine at all. It's just a simple operation returning a conditional value accidentally name "state". – qwerty_so Feb 21 '17 at 21:51
  • I added some background information that should clarify the example. Maybe the pseudo-code example was a bit misleading. The problem can be modeled as state machine in my opinion the question is, how ... – Thomas Eizinger Feb 22 '17 at 08:16

2 Answers2

1

According to your edit, I'd come up with the following approach:

enter image description here

You will reach the Draft state through (optional) authorization and updating. If they fail, the state machine is reset.

qwerty_so
  • 35,448
  • 8
  • 62
  • 86
0

I would suggest one remark about the point "it is somewhat strange that each transition is contained twice", I understood that from one state you can have several transitions triggered by the same event but in this case, transitions have different guards. As I remember, the notation is evt[guard]. Hope this help.

granier
  • 1,739
  • 13
  • 25
  • That is an interesting approach. The question is whether it is better to express the condition primarily (using the UML notation) or as a guard on the transition. Any advice on when to use what? – Thomas Eizinger Feb 22 '17 at 14:05
  • According to this question (http://stackoverflow.com/questions/5724259/state-transition-with-different-guard-condition) using guards would actually be the way to go. Also the linked example in the accepted answer talks about choices being used for two or more states. A boolean choice would of course qualify for this but guards being explicitly design to handle boolean choices seem to be more appropriate here. – Thomas Eizinger Feb 22 '17 at 14:35
  • I just tried to model that using guards: http://imgur.com/hFlOazC I think I will be going for the choicepoint approach. Seems to be easier to understand and what if not simplicity is the idea behind visualizing something with a diagram :) – Thomas Eizinger Feb 22 '17 at 14:44
  • The problem with this is, that it's undetermined which guard is evaluated first. You should use decisions if the order is important (like here). – qwerty_so Feb 22 '17 at 15:38