Giter Club home page Giter Club logo

rule-engine's Introduction

Rule Engine

The intent for Rule Engine is to separate the definition of rules from the application code. This repository contains only the code for the Rule Engine in Scala. And you can build the UI and storage of the rules, so that rules can be added, modified, and deleted, without the need to change the application code.

Usage

Please look at our unit tests in RuleEngineUnitTests.scala for more usage examples.

val rule1 = TestConcreteRule(
  id = "rule1",
  name = "rule 1",
  definition = RuleDefinition(
    schemaVersion = "1.0",
    logic = RuleTerm(
      and = Seq(
        RuleTerm(condition = Some(Condition(variable = REFERRER_URL, operator = Operator.ENDS_WITH, value = "google.com/"))),
        RuleTerm(or = Seq(
          RuleTerm(condition = Some(Condition(variable = REQUEST_URL, operator = Operator.CONTAINS, value = "/cp"))),
          RuleTerm(condition = Some(Condition(variable = REQUEST_URL, operator = Operator.CONTAINS, value = "/cs")))
        ))
      )
    )
  ),
  executionOrder = 1)

val rule2 = TestConcreteRule(
  id = "rule2",
  name = "rule 2",
  definition = RuleDefinition(
    schemaVersion = "1.0",
    logic = RuleTerm(
      and = Seq(
        RuleTerm(condition = Some(Condition(variable = REFERRER_URL, operator = Operator.ENDS_WITH, value = "facebook.com/"))),
        RuleTerm(or = Seq(
          RuleTerm(condition = Some(Condition(variable = REQUEST_URL, operator = Operator.CONTAINS, value = "/project"))),
          RuleTerm(condition = Some(Condition(variable = REQUEST_URL, operator = Operator.CONTAINS, value = "utm_campaign=christmas")))
        ))
      )
    )
  ),
  executionOrder = 2)
    
  val rules = List(rule1, rule2)
  val ruleEngine = new RuleEngine(rules)
  val variables = Map(REFERRER_URL -> "http://www.google.com/", REQUEST_URL -> "https://porch.com/pro?utm_campaign=october_fest")
  val matchingRule = ruleEngine.getFirstMatchingRule(variables)

If you build a RESTful APIs to create, read, update, and delete build rules, you can define the rules in JSON and transform them into Scala on the server side. Here is a JSON example of a rule

  {
     "schemaVersion": "1.0",
     "logic": {
         "and": [
             {
                "condition": {
                    "variable": "REFERRER_URL",
                    "operator": "ENDS_WITH",
                    "value": "google.com/”
                 }
             },
             {
              or: [
                  {
                    "condition": {
                        "variable": "REQUEST_URL",
                        "operator": "CONTAINS",
                        "value": "/cp”
                     }
                  },
                  {
                    "condition": {
                        "variable": "REQUEST_URL",
                        "operator": "CONTAINS",
                        "value": "/cs”
                     }
                  }
              ]
             }
         ]
     }
  }

Make Changes and Build

You will need to instal Maven (https://maven.apache.org/), and run this in command line:

mvn clean install    

Or you can just build the Scala code into a .jar file using Scala compiler scalac.

Unit Test

Run this in command line:

mvn test 

Licensing

BSD 3-Clause License: http://opensource.org/licenses/BSD-3-Clause

Contributing

Create fork and create a pull request for merging back to the main repo.

Support

File an issue: https://github.com/porchdotcom/rule-engine/issues

Contact

David Long [email protected]

rule-engine's People

Contributors

davidlong-porch avatar

Watchers

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