Git Product home page Git Product logo

logicengine's Introduction

LogicEngine

Feature Requests

LogicEngine is designed to run arbitrary rules or bits of logic against a given model. It's written in C# .NET 4.5.1.

This project is born out of a DRY (don't repeat yourself) mentality. In other words, I was using the same code on several projects.

How to use

The source code has a working example (ExampleEngine) which shows how easy the logic engine is to use.

API

The only real requirement is to implement the IRule interface:

public interface IRule<T> where T : class
{
    IEngineResult Execute(T model);
}

In the ExampleEngine, the AddRule class looks like this:

public class AddRule : IRule<ExampleModel> 
{
    public IEngineResult Execute(ExampleModel model)
    {
        EngineResult result = new EngineResult() { TimeStart = DateTime.UtcNow, Name = GetType().ToString() };
        model.AddResult = model.Value1 + model.Value2;
        result.TimeEnd = DateTime.UtcNow;
        return result;
    }
}

The EngineResult class can be implemented, or you can use your own. The Engine returns a list of EngineResults. You can add any information you'd like.

Once the rules are created, you only need to create an engine with them and execute the rules against the model.

Engine<ExampleModel> engine = new Engine<ExampleModel>(
    new RuleCollection<ExampleModel>()
    {
        new AddRule(), 
        new DivisionRule(), 
        new MultiplicationRule(), 
        new SubtractRule()
    });
var retval = engine.Execute(model);
return retval;

Bumper Rules

The engine has some "bumper rules". They're rules that run before/after all your rules. They will give you run start/stop times.

new Engine<SomeModel>(someListOfRules) {RunBumperRules = true;}

Dependency Injection Suppot

You can use dependency injection to add your rules. Simply add them to the RulesCollection.

UnityContainer container = new UnityContainer();
container.RegisterType<IEngine<string>,Engine<string>>();
IRuleCollection<string> coll = new RuleCollection<string>() {rule1, rule2, rule3};
container.RegisterInstance(coll);
var engine = container.Resolve<IEngine<string>>();

logicengine's People

Contributors

wbsimms avatar

Watchers

James Cloos avatar  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.