Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jeevanjames/policyevaluation
Library for evaluating policies in a logical expression
https://github.com/jeevanjames/policyevaluation
Last synced: about 1 month ago
JSON representation
Library for evaluating policies in a logical expression
- Host: GitHub
- URL: https://github.com/jeevanjames/policyevaluation
- Owner: JeevanJames
- License: mit
- Created: 2023-08-26T13:23:13.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-01-10T18:45:17.000Z (10 months ago)
- Last Synced: 2024-10-09T16:32:59.871Z (about 1 month ago)
- Language: C#
- Size: 96.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
![Banner](Images/Banner.png)
# PolicyEvaluation
[![Package NuGet Package](https://img.shields.io/nuget/v/Jeevan.PolicyEvaluation.svg)](https://www.nuget.org/packages/Jeevan.PolicyEvaluation/) [![Package NuGet Package Downloads](https://img.shields.io/nuget/dt/Jeevan.PolicyEvaluation)](https://www.nuget.org/packages/Jeevan.PolicyEvaluation) [![GitHub Actions Status](https://github.com/JeevanJames/PolicyEvaluation/workflows/Build/badge.svg?branch=main)](https://github.com/JeevanJames/PolicyEvaluation/actions)
[![GitHub Actions Build History](https://buildstats.info/github/chart/JeevanJames/PolicyEvaluation?branch=main&includeBuildsFromPullRequest=false)](https://github.com/JeevanJames/PolicyEvaluation/actions)
PolicyEvaluation is a small library for parsing logical expressions containing policy names, to evaluate if the expression passes or fails. The policy evaluation itself is provided externally (through a delegate); the main job of this library is to evaluate the policies in the context of a logical expression.
A few sample expressions (where `Policy1`, `Policy2`, `Policy3` and `Policy4` are policy names):
* `Policy1 AND Policy2`
* `Policy1 OR Policy2`
* `Policy1 AND (Policy2 OR Policy3)`
* `(Policy1 AND Policy2) OR Policy3`
* `(Policy1 OR Policy2) AND (Policy3 OR Policy4)`Rules for expressions:
* Policy names should start with a letter, followed by alphanumeric characters and underscores.
* Only AND and OR logical operators are allowed, and they are case-sensitive.
* Nesting is allowed using parentheses. Only one level of nesting is allowed.## Installation
Via Package Manager:
```
PM> Install-Package Jeevan.PolicyEvaluation
```Via `dotnet` CLI:
```
dotnet add package Jeevan.PolicyEvaluationdotnet add package Jeevan.PolicyEvaluation
```## Usage
```cs
// Import the namespace Jeevan.PolicyEvaluation
using Jeevan.PolicyEvaluation;// Declare a function to evaluate a policy, given its name.
Func policyEvaluatorLogic = policyName =>
{
// Perform policy evaluation here.
// Use methods from PolicyOutcome to generate the result.
};// Instantiate the PolicyEvaluator class
PolicyEvaluator evaluator = new(policyEvaluatorLogic);// Call the EvaluateExpression method to evaluate an expression.
const string expression = "Policy1 AND (Policy2 OR Policy3)";
var result = evaluator.EvaluateExpression(expression);// You can check whether the expression was satisfied.
if (result.IsSatisfied)
Console.WriteLine("Expression was satisfied.");// OR you can check whether the expression was not satisfied and get the reason and which policy
// caused the expression to fail.
if (result.IsNotSatisfied(out string? policyName, out string? failureMessage))
Console.WriteLine($"Policy {policyName} failed with error {failureMessage}.");
```