Giter Site home page Giter Site logo

rules_engine's Introduction

RulesEngine

A port of the EasyRules (rules engine) to Elixir http://www.easyrules.org/

Please read the EasyRules link for a quick intro to the concepts and workflows.

  • Implements default rules engine
  • Implements inference rules engine (rules continue to fire while any of the conditions are true)
  • Implements all rules engine parameters
  • Implements all rule groups (UnitRuleGroup, ActivationRuleGroup, ConditionalRuleGroup)
  • DOES NOT implement rules listener or rules engine listener

Authors/Contributors

Bob Sollish - Author/maintainer

Installation

If available in Hex, the package can be installed by adding rules_engine to your list of dependencies in mix.exs:

def deps do
  [
    {:rules_engine, "~> 0.1.0"}
  ]
end

Fizz-Buzz Example

(Look at test/rules_engine_test.exs for more examples)

defmodule RulesEngineLibrary.FizzBuzzWithRulesEngine do
  use RulesEngine

  alias RulesEngine.{Rule, RuleGroup, RulesEngineParameters}

  def main() do
    # RulesEngineParameters
    params = RulesEngineParameters.create(%{skip_on_first_applied_rule: true})

    # --------------------------------------------
    # define our individual rules
    fizz_rule = %Rule{
      name: "FizzRule",
      priority: 1,
      condition: fn(facts) -> rem(facts.number, 5) == 0 end,
      actions: [fn(_facts) -> IO.write("fizz") end]}

    buzz_rule = %Rule{
      name: "BuzzRule",
      priority: 2,
      condition: fn(facts) -> rem(facts.number, 7) == 0 end,
      actions: [fn(_facts) -> IO.write("buzz") end]}

    fizz_buzz_rule = RuleGroup.create(%{
      name: "FizzBuzzRule",
      type: :unit_rule_group,
      rules: [fizz_rule, buzz_rule],
      priority: 0})

    non_fizz_buzz_rule = %Rule{
      name: "NonFizzBuzzRule",
      priority: 3,
      condition: fn(facts) -> rem(facts.number, 5) != 0 || rem(facts.number, 7) != 0 end,
      actions: [fn(facts) -> IO.write(Kernel.inspect(facts.number)) end]}
    # --------------------------------------------

    # create set of rules
    rules = Rule.add_rules([fizz_rule, buzz_rule, fizz_buzz_rule, non_fizz_buzz_rule])

    # run our rules engine 100 times - with values (number facts) from 1 to 100 (inclusive)
    for n <- 1..100 do
      facts = %{number: n}
      fire(params, rules, facts)
      IO.puts("")
    end
  end
end

Running FizzBuzzWithRulesEngine.main() will output:

1
2
3
4
fizz
6
buzz
8
9
fizz
11
12
13
buzz
fizz
16
17
18
19
fizz
buzz
22
23
24
fizz
26
27
buzz
29
fizz
31
32
33
34
fizzbuzz
36
37
38
39
fizz
41
buzz
43
44
fizz
46
47
48
buzz
fizz
51
52
53
54
fizz
buzz
57
58
59
fizz
61
62
buzz
64
fizz
66
67
68
69
fizzbuzz
71
72
73
74
fizz
76
buzz
78
79
fizz
81
82
83
buzz
fizz
86
87
88
89
fizz
buzz
92
93
94
fizz
96
97
buzz
99
fizz

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/rules_engine.

rules_engine's People

Contributors

bsollish-terakeet 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.