Coder Social home page Coder Social logo

glenux / namarara Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 121 KB

A library that can parses boolean expressions, builds an binary expression tree and evalue given a set of values

License: MIT License

Ruby 98.70% Shell 0.51% Dockerfile 0.79%
ruby ast bet binary-expression-tree abstract-syntax-tree parser parser-library evaluate-expressions evaluate grammar

namarara's Introduction

๐Ÿšจ The project has moved to a self-hosted git instance!
๐Ÿšจ Please use the new URL for an up-to-date version: https://code.apps.glenux.net/glenux/namarara

Namarara

Build Gem Version GitHub license Donate on patreon

Namarara is a library that can parses boolean expressions, builds an binary expression tree and evalutes a result given a set of values associated to the variables used within the boolean expression.

Installation

Add this line to your application's Gemfile:

gem 'namarara'

And then execute:

$ bundle

Or install it yourself as:

$ gem install namarara

Usage

Evaluate a single expression

require 'namarara'

# Initialize Namarara
namarara = Namarara::Parser.new(Namarara::Lexer.new)

# Prepare variables 
namarara.names = {
  this: 'true',
  that: 'false',
  other: 'false',
  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
result = exp_tree.compute
puts result # = false

Evaluating a set of rules

require 'namarara'

# Initialize Namarara
namarara = Namarara::Parser.new(Namarara::Lexer.new)

# A set of rules i want to check 
# (in this example we are looking for sensitive personnal data)
rules = {
    vulnerable_person: 'is_adult AND is_subordinate',
    has_constraints: 'is_adult AND has_children',
    is_child: 'NOT is_adult'
    # ...
}

# A set of values i want to inject (values must be expressed as strings)
namarara.names = {
    "is_adult" => 'false', 
    "is_subordinate" => 'true',
    "has_children" => 'true'
}

results = rules.map { |rule, expr| [rule, namarara.parse(expr).compute] }

if results.select{ |rule, value| value }.empty?
    puts "Perfect! Nothing to say ;-)"
else
    puts "Warning: you are collecting sensitive personnal data !"
    results.each do |rule, value|
      puts "#{value ? '>>':'  '} #{rule}: #{value}" 
    end
end

Development

After checking out the repo, run bin/setup to install dependencies.

Then, run rake test to run the tests.

You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install.

To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/glenux/namarara

License and copyright

Namarara is an open source project under the terms of the MIT License.

Original author : Brendon Torre

Current developer & maintainer : Glenn Y. Rolland (@glenux)

Sponsors and funding

Namarara is an independent project whose development and maintenance is made possible thanks to the support of its patrons.

If you wish to join them and support the work of its author, just participate with this link :

>>>ย Become a patron or sponsor on Patreonย <<<

namarara's People

Contributors

glenux avatar roguelearg avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.