Thursday, December 20, 2012

Logical Model Part III - Card Game Results Tracking System

Previous

Logical Model - Part III

Part III of the logical model discusses how some of the business rules stated in the requirements will be enforced.

Even before completing specifications for all relvars, we should try to define all database constraints for all business rules that we already can:

Business Rule 5

The rule: One player can only play one match in each round of any tournament, is enforced by the key {tournament id, round id, player id} of the relvar match draw.

Business Rule 3

The rule Count of players for each match (identified by Match ID) has to be either 3 or 4, can be specified as the following relational algebra (RA) expression (finds the matches that break the rule):

restrict(summarizeby(matchdraw,(matchid),(noofplayers(count))), noofplayers < 3 or noofplayers > 4)

Business Rule 6

The rule: Match table has to exists for each match of a round or for none of them, can be specified as the following RA expression (finds the matches that break the rule):

semiminus(
  project(
    join(
      semijoin(
        tournamentround
        ,join(
          project(
            matchdraw
            ,(matchid,roundid,tournamentid))
          ,matchtable))
      ,project(matchdraw,(matchid,roundid,tournamentid)))
    ,(matchid))
  ,matchtable)

Hm, that easy?! Here is the explanation.

We don’t need to be concerned about tournament rounds with no matches in match table. They fall into the second part of the rule (none of the matches has match table defined). So, the first step is to find the rounds with at least one match with match table.

Matches with match tables are:

join(project(matchdraw,(matchid,roundid,tournamentid)),matchtable); let’s call this expression T.

Rounds with at least one match with match table are:

semijoin(tournamentround , T);  let’s call this expression R.

The next step is finding all matches of the selected rounds:

join(R, matchdraw); let’s call this expression A.

Finally, find any matches for rounds with at least one match with match table that are not in match table:

semiminus(A, matchtable)

By substituting back we have:

semiminus(join(semijoin(tournamentround , T), matchdraw), matchtable)

Looks a lot like algebra we’re all familiar with, doesn't it?

No comments: