Skip to content

FrancescoBonizzi/Boolli

Repository files navigation

Logo Boolli

Build status Quality Gate Status

Boolli is a boolean expressions interpreter.

What is Boolli able to do? Saying true or false given a boolean expression!

To Boolli, a boolean expression is something generated by this EBNF grammar:

expr: factor((and | or) factor)*
factor: (not)* factor | boolean | LPAR expr RPAR
boolean: true | false | 0 | 1

Bolli is composed of:

  • A lexer
  • A parser
  • An interpreter

The base data structure is Abstract Syntax Tree

How can it be helpful?

  • To understand how to parse a simple grammar
  • When you substitute boolean tokens with Func<bool> (even Func<Task<bool>>) tokens and define a simple rule system

Installation

PM> Install-Package Boolli

NuGet

Getting started

1. Evaluate a simple boolean expression

var boolli = new Evaluator();
string booleanExpression = "not true and false or (true and false)";
bool result = boolli.EvaluateBooleanExpression(booleanExpression);

You can also use 0 as false and 1 as true:

var boolli = new Evaluator();
string booleanExpression = "not 1 and 0 or (true and false)";
bool result = boolli.EvaluateBooleanExpression(booleanExpression);

2. Evaluate a simple Func<bool> expression

var boolli = new Evaluator();
bool result = boolli.EvaluateFuncOfBoolExpression(
    "f1 and f2",
    new NamedBooleanFunction[]
    {
        new NamedBooleanFunction("f1", () => true),
        new NamedBooleanFunction("f2", () => false),
    });

3. Evaluate a simple Func<Task<bool>> expression

var boolli = new Evaluator();
bool result = await boolli.EvaluateFuncOfBoolExpressionAsync(
    "f3 and f4",
    new NamedAsyncBooleanFunction[]
    {
        new NamedAsyncBooleanFunction("f3", async () => { await Task.Delay(100); return true; }),
        new NamedAsyncBooleanFunction("f4", async () => { await Task.Delay(100); return true; }),
    });

4. A real case scenario

I made a very very very simple scenario (if you want you can improve it, thanks 😁) to make a basic rule based alerting system. This is the source code. Run it and tell me what you think!

Building

Simply clone this repository and build the Boolli.sln solution.

How to contribute

  • Report any issues
  • Propose new features / improvements
  • Just telling your opinion :-)

About

Booli is a boolean expressions interpreter.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages