Add nice examples

This commit is contained in:
Glenn Y. Rolland 2019-12-27 20:42:56 +01:00
parent f08fb17b41
commit 239115de71
3 changed files with 54 additions and 50 deletions

View file

@ -21,70 +21,76 @@ gem 'namarara'
And then execute: And then execute:
$ bundle ```shell-session
$ bundle
```
Or install it yourself as: Or install it yourself as:
$ gem install namarara ```shell-session
$ gem install namarara
```
## Usage ## Usage
### Evaluate a single expression ### Evaluate a single expression
``` ```ruby
require 'namarara'
# Initialize Namarara # Initialize Namarara
namarara = Namarara::Parser.new(Namarara::Lexer.new) namarara = Namarara::Parser.new(Namarara::Lexer.new)
# Build the binary expression tree (BET)
namarara_bet = namarara.parse('this AND (that OR other) AND something_else')
# Prepare variables # Prepare variables
variables = { namarara.names = {
this: true, this: 'true',
that: false, that: 'false',
other: false, other: 'false',
something_else: true something_else: 'true'
} }
# Build a binary expression tree (aka BET) from string
# and inject values
exp_tree = namarara.parse('this AND (that OR other) AND something_else')
# Compute tree with variables # Compute tree with variables
result = namarara_bet.compute(variables) result = exp_tree.compute
puts result # = false
``` ```
### Evaluating a set of rules ### Evaluating a set of rules
```ruby ```ruby
require 'namarara'
# Initialize Namarara # Initialize Namarara
namarara = Namarara::Parser.new(Namarara::Lexer.new) namarara = Namarara::Parser.new(Namarara::Lexer.new)
# A set of rules i want to check # A set of rules i want to check
rules = [ # (in this example we are looking for sensitive personnal data)
{name: 'vulnerable_person', expr: 'is_adult AND is_subordinate'}, rules = {
{name: 'has_constraints', expr: 'is_adult AND has_children' }, vulnerable_person: 'is_adult AND is_subordinate',
{name: 'is_child', expr: 'NOT is_adult'} has_constraints: 'is_adult AND has_children',
is_child: 'NOT is_adult'
# ... # ...
] }
# A set of values i want to inject (values can come from HTTP or from database # A set of values i want to inject (values must be expressed as strings)
# as long as they are expressed as strings)
namarara.names = { namarara.names = {
"is_adult" => 'false', "is_adult" => 'false',
"is_subordinate" => 'true', "is_subordinate" => 'true',
"has_children" => 'true' "has_children" => 'true'
} }
rules.map do |rule| results = rules.map { |rule, expr| [rule, namarara.parse(expr).compute] }
namarara_bet = namarara.parse(rule)
result = namarara_bet.compute
if result then
warnings << "Rule #{rule} is true"
end
end
if not warnings.empty? if results.select{ |rule, value| value }.empty?
puts "Attention: vous collectez des DCP de personnes vulnerables" puts "Perfect! Nothing to say ;-)"
puts warnings.join("\n")
else else
puts "Rien à dire :-)" puts "Warning: you are collectif sensitive personnal data !"
results.each do |rule, value|
puts "#{value ? '>>':' '} #{rule}: #{value}"
end
end end
``` ```

View file

@ -4,7 +4,8 @@ require 'namarara'
# Initialize Namarara # Initialize Namarara
namarara = Namarara::Parser.new(Namarara::Lexer.new) namarara = Namarara::Parser.new(Namarara::Lexer.new)
# A set of rules i want to check # A set of rules i want to check
# (in this example we are looking for sensitive personnal data)
rules = { rules = {
vulnerable_person: 'is_adult AND is_subordinate', vulnerable_person: 'is_adult AND is_subordinate',
has_constraints: 'is_adult AND has_children', has_constraints: 'is_adult AND has_children',
@ -19,17 +20,13 @@ namarara.names = {
"has_children" => 'true' "has_children" => 'true'
} }
rules.map do |rule| results = rules.map { |rule, expr| [rule, namarara.parse(expr).compute] }
namarara_bet = namarara.parse(rule.expr)
result = namarara_bet.compute if results.select{ |rule, value| value }.empty?
if result then puts "Perfect! Nothing to say ;-)"
warnings << "Rule #{rule} is true" else
puts "Warning: you are collectif sensitive personnal data !"
results.each do |rule, value|
puts "#{value ? '>>':' '} #{rule}: #{value}"
end end
end end
if not warnings.empty?
puts "Warning: you are collectif sensitive personnal data !"
puts warnings.join("\n")
else
puts "Perfect! Nothing to say ;-)"
end

View file

@ -4,18 +4,19 @@ require 'namarara'
# Initialize Namarara # Initialize Namarara
namarara = Namarara::Parser.new(Namarara::Lexer.new) namarara = Namarara::Parser.new(Namarara::Lexer.new)
# Build the binary expression tree (aka BET)
exp_tree = namarara.parse('this AND (that OR other) AND something_else')
puts p
# Prepare variables # Prepare variables
exp_tree.names = { namarara.names = {
this: 'true', this: 'true',
that: 'false', that: 'false',
other: 'false', other: 'false',
something_else: 'true' something_else: 'true'
} }
# Build a binary expression tree (aka BET) from string
# and inject values
exp_tree = namarara.parse('this AND (that OR other) AND something_else')
# Compute tree with variables # Compute tree with variables
result = exp_tree.compute result = exp_tree.compute
puts "#{result} == " puts result # = false