Playing the Game of life with HANA using decision tables and Calculation views
I stumbled upon this interesting blog written by Wenjun Zhou , in which he explains the implementation of the game using SQL script and window functions ( I would recommend reading Wenjun’s blog to understand the Game of life before reading this blog). This inspired me to try the game of life using decision table and graphical calculation views.
1. Create the initial pattern as a table in HANA.
2. Find the number of neighboring live cells for each cell using a calculation view.
3. Implement the rules of the game in a decision table to get the next generation cells.
We will start with the below pattern ,
Pattern is created as a table in HANA ( lets call it CELLGAME ),
If a cell is alive , value in column S will be 1 otherwise the value will be 0.
Building the calculation view
Number of live neighbors could be derived from the below SQL ( from Wenjun’s Blog ). Let us try to implement this SQL in a Calculation view.
SELECT A.X, A.Y, A.S, SUM(B.S) N FROM LIFE A INNER JOIN LIFE B ON ABS(A.X - B.X) < 2 AND ABS(A.Y - B.Y) < 2 AND (A.X <> B.X OR A.Y <> B.Y) GROUP BY A.X, A.Y, A.S;
To create an inner join to the table, add two projection nodes and add the CELLGAME table to it. A dummy calculated column with a default value as 1 is created in both the projections.
Join both the projections using a join node and Rename one of the S column to N ( Number of neighbors ) as shown below,
With this set up all the rows of the tables will be joined . To apply the join condition, create two calculated columns ABS_X and ABS_Y to calculate ABS(“X”-“X_1”) and ABS(“Y”-“Y_1”)
Apply filter to the projection using the calculated columns created in the previous step.
To implement group by clause and SUM function from SQL to calculation view, Link the projection node to the calculation view’s aggregation node and add X,Y,S and N columns to the output. N as a measure with aggregation type as SUM.
Result of this calculation view will give the number of live neighbors for each cells
Below are the rules of game of life,
“1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
2. Any live cell with two or three live neighbours lives on to the next generation.
3. Any live cell with more than three live neighbours dies, as if by overcrowding.
4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.”
These rules can be converted to decision table conditions,
1. Any live cell with fewer than two live neighbours dies, as if caused by under-population –> if S = 1 and N < =1 , then Result = 0
2. Any live cell with two or three live neighbours lives on to the next generation. –> if S =1 and ( N = 2 or N = 3 ) , then Result = 1
3. Any live cell with more than three live neighbours dies, as if by overcrowding. –> if S = 1 and N > 3 , then Result = 0
4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. –> if S = 0 and N = 3 , then Result = 0.
Using the above conditions, create a decision table with S , N columns as conditions and a new parameter S_NEW ( Result of the game ) as action.
Result of this decision table will give the next generation cells.
Update : The next part of the blog which explains how to create an interactive game using the views that we have built is available here