HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

How should I model a worflow of status/activities ? (rules like "if is 3 go to 4, if is 4 go to 5" etc)

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
howactivitieslikestatusworflowshouldrulesmodeletc

Problem

I'm trying to model a workflow (status and activities) to create "rules".

Let me give you an example.

The main "object" let's say are "vehicle requirements", I will have this table:

idRequirement, idVehicle,DateOfRequirement, idStatus, LastUpdate
1                2       2012-01-02 10:20:00, 1     ,2012-01-02 10:20:00


In the example above as the register is just created, the DateOfRequirement and LastUpdate is the same, LastUpdate it will be updated after every change of status.

Status TAble

idStatus,Status
 1         New
 2         Vehicle Assigned
 3         Closed


Activities Table

idActivitie, Activitie
 1            Create Vehicle Requirement
 2            Assign Vehicle
 3            Close requirement.
 4            ReAssign Vehicle.
 5            Confirm


Until the tables above I have no problems, but my problem is that
for example:

I know I cannot close a requirement (update to idStatus=3 on "vehicle requirements")
if the current status is not "Vehicle Assigned" (idStatus=2).

I can do that by code (or I think so), but is there anyway to model this kind of rules to tables ? I mean a table where I retrieve the next idStatus and the activitie to perform (to save a "kardex/historical" of each requirement and it's activities), checking for example the Activitie or the current status ?

The rules also will have 3 "alerts" accordin the time and status. For example the limit time "green" to assign vehicle is 30 minutes or less the yellow "alert" it will be between 31 and 45 after the requirement has been created (status new) and the red alert it will show it after 45 of being in status "new"

idRequirement,Vehicle,CurrentStatus,NextStatus,       Alert
       1         N/A      New          Vehicle Assigned  Green


After 39 minutues it will something like this:

idRequirement,Vehicle,CurrentStatus,NextStatus,       Alert
       1         N/A      New          Vehicle Assigned  Yellow


And after 45 minutes:

```
idRequi

Solution

What you have can work, given a few constraints:

  • You move between states in a linear fashion



  • You have multiple states with multiple activities.



  • You have only one consideration for what "color" the alert indicator should be.



  • You have three colors and only three colors (no more and no fewer).



Based on what you mentioned as requirements, that solution fits most of the constraints, but not #2. There are a couple of things I would look at, though.

One thing I would question is whether you need that Activity table at all. If it really is a 1:1 correspondence between states and activities, you could wrap it up into one table without loss: your activity is just the action which pushes you to the next state. If, however, there are multiple activities per state, this can make sense. If you have an overarching status category (e.g., when the row is in "Vehicle Assigned" status, there are 5 separate activities which need to occur to get it to "Closed" status), then this general idea is alright.

So if you have one activity per state, the Status table would have the following attributes:

  • Name



  • Activity required to get to this state (if you, for example, have a web application and you want to populate the button text with a verb instead of a noun)



  • The next status ID (assuming this is a linear workflow)



  • GreenMax (the maximum number of minutes during which the row can be "green")



  • YellowMax (the maximum number of minutes during which the row can be "yellow")



RedMax is implied: it's red whenever the difference between the current time and whichever time you want to use (DateOfRequirement, LastUpdate, etc.) is greater than YellowLimit.

Alternatively, if you have different categories for display such as "step 1 has red-yellow-green, but step 2 is just red-green and step 3 is red-pink-orange" (or if you believe that you will), I would move the colors out to their own table and create a Status-Color bridge table with status ID, color ID, and maximum time. That makes retrieval a bit more complicated, though.

Now, if you do have multiple activities per status, then I would turn Status into a parent-child relationship with Activity, instead of a many-to-many relationship like you have now with Rules. So Status would have Name, and Activity would look like:

  • Name



  • Status ID (to get the current status)



  • The next Activity ID (assuming this is a linear workflow)



  • GreenMax



  • YellowMax



And on your main object, you would actually hold the activity ID instead of the status ID, because you can derive the status from the activity.

The other thing I would do is drop the Rules table. The only time that you would need a Rules table is if you have multiple statuses, multiple activities, and there are multiple relationships between these (for example, that both the New and Closed statuses both be able to perform the Assign Vehicle activity). Based on your example, this does not appear to be the case. It is possible that you actually do that many-to-many relationship between activities and statuses, if the relationship is not like your example. In that event, your Rules table looks okay, with the caveat that you don't need the Red limit.

Incidentally, I highly recommend that you put the actual workflow logic in a stored procedure, whether you keep the linear flow (like you have now) or move to a non-linear workflow model. You can use a combination of triggers and check constraints to force a workflow to operate in a particular manner, but it's a lot easier to understand if you just do it in a stored procedure: all of the code is in one place not in an easy-to-forget area like a trigger. Putting this in a stored procedure also abstracts it away from the application and business object layers, so they just need to call a method which says "go to the next workflow step" (and maybe pass in a detail to help you figure out which workflow step, in the event that this is not a linear process).

EDIT (post question update)

Based on your question update and comment, I'm okay with a three-table approach. I'm not positive that you need it, but this could simplify things at the application level.

At this point, what you have is a state machine, with a State (status) table and a Transition (activity) table. Your rule table then defines which transitions are valid for a particular state.

I would change the Rule table slightly, to look like this:

  • Surrogate key



  • Rule Name



  • Starting State ID



  • Transition (Activity ID)



  • Ending State ID



  • Green Limit



  • Yellow Limit



There is one minor change: the position of the different keys. Your rule table now reads like a book: starting from one state, perform a transition and end at a different state (or the same one--you have an example in which the final state is the same as the initial state).

You would have a unique key constraint on starting state ID and activity ID, to guarantee that a particular transition from a particular state always ends i

Context

StackExchange Database Administrators Q#29277, answer score: 5

Revisions (0)

No revisions yet.