Hey folks! Continuing with the Bitwise Operators POC, this is what we’ll do:
What we’ll do
For example, you can have the states:
- Standing (still);
And, combining the states, you can define some (naive) rules:
- You can awake only if your state is sleeping;
- You can seat only when you awake OR Standing (ok, I know, you can also seat when sleeping, bla, bla, bla… but, for this example, this is the rule, OK?);
- You can stand up only when you’re seated;
- You can walk only if you are standing OR running;
- You can run only if you are standing OR walking;
- You can fall if you are sleeping OR awake OR seated OR standing OR walking OR running.
As you could see, you’ll create simple rules with only one condition or combining with the Bitwise OR Operator.
We’ll log some things on the console to help the understanding of how the things work.
Creating the states and rules
To do this, follow the rules. It also will be helpful to look at the logs!
Select ONE state (selecting more than one won’t work, because we don’t live in a quantic world!), a rule and click on ‘Check Rule’.
Testing a simple rule
The first test is pretty simple: Can awake? The rule is: should be sleeping.
When we create the rule, we’ll iterate through the ‘multi-select’ taking the selected ones and, we’ll use the ‘Bitwise OR’ to create the rule.
As this first test is a simple one, the value of the rule will be the same value as the state.
So, we’ll test if ‘a & b’ where a is the status and b is the rule. The binary representation of the number 1 is 00000001, so, when we test using the Bitwise AND we’ll get:
00000001 00000001 -------- 00000001
As 1 is ‘truthy’, yes, you can awake if you’re sleeping!
But, can you run when sleeping (dreaming you’re running is not valid, ok? ahahahah)?
As our log teach us, a is 00000001 and b is 24, or 00011000, so:
00000001 00011000 -------- 00000000
The result of ‘a & b’ is zero, or, ‘falsy’.
Testing a more complex rule
Now, let’s check if you can seat. The rule is: you can seat if you’re awake or standing. Awake, in this case, requires a broad interpretation, when you’re awake, you can be, for example, awake and lying in bed. So…
Status awake testing can-seat rule:
Why? See the logs! Awake is 2, or, 00000010 and can-seat is 10, or 00001010, so:
00000010 00001010 -------- 00000010 (or, 2!)
The result is 2, and 2 is ‘truthy’, so, you can seat if you’re awake.
Status standing testing can-seat rule:
Why? Standing is 8, or 00001000 and can-seat is 10, or 00001010, so:
00001000 00001010 -------- 00001000 (or, 8!)
The result is 8, and 8 is truthy, so, you can seat if you’re standing.
Status running testing can-seat rule:
Why? Running is 32, or 00100000 and can-seat is 10, or 00001010, so:
00100000 00001010 -------- 00000000 (zero)
The result is 0, and 0 is ‘falsy’, so, you can’t seat if you’re running.
This is how it must work. At the next post, we’ll create all the CRUD code, the logic, and some hardcoded tests.
Stay tuned to follow this POC and Happy Coding!