Make use of DSL Grammar
Using DSL grammar, you can combine different behaviours in intuitive ways to bring out a functionality.
The two dummy behaviours available in this framework are as follows:
falseIdentity
- Always evaluates to false, regardless of the input.trueIdentity
- Always evaluates to true, regardless of the input.
These two behaviors are used in the following examples and descriptions.
Binary AND operator
Keyword: and
And works the same way it does it other languages. Hence following observations.
falseIdentity and trueIdentity == falseIdentity
falseIdentity and falseIdentity == falseIdentity
trueIdentity and trueIdentity == trueIdentity
trueIdentity and falseIdentity == falseIdentity
Here we are using == to show their equality.
Value declaration operator
Keywords: val
Symbols: =
Value declaration lets you modularize things to make your DSL. A DSL only using the and operator and val operator appears as follows:
val rule1= falseIdentity and trueIdentity and trueIdentity
val rule2= trueIdentity and trueIdentity and trueIdentity
rule1 and rule2
The above expression evaluates to false.
Binary OR operator
The or operator works the same way it does it other languages.
falseIdentity or trueIdentity == trueIdentity
falseIdentity or falseIdentity == falseIdentity
trueIdentity or trueIdentity == trueIdentity
trueIdentity or falseIdentity == trueIdentity
Let's expand our DSL to use or as follows.
val rule1= falseIdentity and trueIdentity and trueIdentity
val rule2= trueIdentity and trueIdentity and trueIdentity
val rule3=rule1 and rule2
rule3 or trueIdentity
The above expression evaluates to true.
Unary Not operator
not negates value of a behaviour ( from true to false and vice versa). Hence following observations.
not(falseIdentity) == trueIdentity
not(trueIdentity) == falseIdentity
Use the not operator as follows.
val rule1= falseIdentity and trueIdentity and trueIdentity
val rule2= trueIdentity and trueIdentity and trueIdentity
val rule3=rule1 and rule2
val rule4=rule3 or trueIdentity
rule4 and not(falseIdentity)
The above expression evaluates to true.
All operator
all takes a list of behaviours and evaluates to true only if all of the behaviours evaluate to true.
all(falseIdentity) == falseIdentity
all(falseIdentity,falseIdentity) == falseIdentity
all(trueIdentity) == trueIdentity
all(trueIdentity,trueIdentity) == trueIdentity
all(trueIdentity,falseIdentity) == falseIdentity
all(falseIdentity,........) == falseIdentity
Use the all operator as follows.
val rule1= falseIdentity and trueIdentity and trueIdentity
val rule2= trueIdentity and trueIdentity and trueIdentity
val rule3=rule1 and rule2
val rule4=rule3 or trueIdentity
val rule5=rule4 and not(falseIdentity)
all(rule5,trueIdentity)
The above expression evaluates to true.
Brackets
Use brackets to either remove ambiguity or group expressions to improve readability. For example, consider following expression:
trueIdentity or trueIdentity and falseIdentity
What does above expression evaluates to? If or executes first, this will result in false. Otherwise in true. To remove this ambiguity you can be explicit by grouping sub-expressions using brackets as follows.Hence following observations.
trueIdentity or (trueIdentity and falseIdentity) == trueIdentity
(trueIdentity or trueIdentity) and falseIdentity == falseIdentity
Use Brackets as follows.
val rule1= falseIdentity and trueIdentity and trueIdentity
val rule2= trueIdentity and trueIdentity and trueIdentity
val rule3=rule1 and rule2
val rule4=rule3 or trueIdentity
val rule5=rule4 and not(falseIdentity)
(all(rule5,trueIdentity) and falseIdentity) or trueIdentity
The above expression evaluates to true.
Any Operator
Keywords: any
The any operator takes a list of behaviours and evaluates to true if any one of the behaviours evaluate to true.
any(falseIdentity) == falseIdentity
any(falseIdentity,falseIdentity) == falseIdentity
any(trueIdentity) == trueIdentity
any(trueIdentity,trueIdentity) == trueIdentity
any(trueIdentity,falseIdentity) == trueIdentity
any(trueIdentity,........) == trueIdentity
Use the any operator as follows:
val rule1= falseIdentity and trueIdentity and trueIdentity
val rule2= trueIdentity and trueIdentity and trueIdentity
val rule3=rule1 and rule2
val rule4=rule3 or trueIdentity
val rule5=rule4 and not(falseIdentity)
val rule6=(all(rule5,trueIdentity) and falseIdentity) or trueIdentity
any(rule6,falseIdentity,falseIdentity)
The above expression evaluates to true.
Given-Choose-Otherwise Operation
Keywords: given,choose,otherwise
Given-Choose-Otherwise works like if conditions in languages like Java and Scala. If given behaviour evaluates to true, behaviour specified for choose is evaluated as a result else behaviour specified for otherwise is evaluated as a result( or falseIdentity if otherwise clause is missing)
given(falseIdentity) choose falseIdentity == falseIdentity
given(falseIdentity) choose trueIdentity == falseIdentity
given(trueIdentity) choose falseIdentity == falseIdentity
given(trueIdentity) choose trueIdentity == trueIdentity
given(falseIdentity) choose falseIdentity otherwise trueIdentity == trueIdentity
given(falseIdentity) choose falseIdentity otherwise falseIdentity == falseIdentity
given(falseIdentity) choose trueIdentity otherwise trueIdentity == trueIdentity
given(falseIdentity) choose trueIdentity otherwise falseIdentity == falseIdentity
given(trueIdentity) choose falseIdentity otherwise falseIdentity == falseIdentity
given(trueIdentity) choose falseIdentity otherwise trueIdentity == falseIdentity
given(trueIdentity) choose trueIdentity otherwise falseIdentity == trueIdentity
given(trueIdentity) choose trueIdentity otherwise trueIdentity == trueIdentity
Use given, choose, otherwise operator as follows.
val rule1= falseIdentity and trueIdentity and trueIdentity
val rule2= trueIdentity and trueIdentity and trueIdentity
val rule3=rule1 and rule2
val rule4=rule3 or trueIdentity
val rule5=rule4 and not(falseIdentity)
val rule6=(all(rule5,trueIdentity) and falseIdentity) or trueIdentity
val rule7=any(rule6,falseIdentity,falseIdentity)
given(rule7) choose falseIdentity otherwise trueIdentity
The above expression evaluates to false.