All Projects → scala-rules → rule-engine

scala-rules / rule-engine

Licence: MIT License
Forward chaining rule engine with an extensible DSL

Programming Languages

scala
5932 projects

Projects that are alternatives of or similar to rule-engine

cs-expert-system-shell
C# implementation of an expert system shell
Stars: ✭ 34 (+21.43%)
Mutual labels:  rule-engine
TabInOut
Framework for information extraction from tables
Stars: ✭ 37 (+32.14%)
Mutual labels:  rule-engine
devs
Devs是一款轻量级的规则引擎
Stars: ✭ 15 (-46.43%)
Mutual labels:  rule-engine
rools
A small rule engine for Node.
Stars: ✭ 118 (+321.43%)
Mutual labels:  rule-engine
mmqtt
An Open-Source, Distributed MQTT Broker for IoT.
Stars: ✭ 58 (+107.14%)
Mutual labels:  rule-engine
SimpleRules
Yet another rules engine, but simpler to use!!
Stars: ✭ 44 (+57.14%)
Mutual labels:  rule-engine
rule-engine-front-v2
🔥🔥🔥📌 规则引擎前端 📌 RuleEngine 基于web可视化配置,简单高效快捷。
Stars: ✭ 58 (+107.14%)
Mutual labels:  rule-engine
ruledesigner
Rule Designer is the Eclipse-based development environment for ODM developers.
Stars: ✭ 14 (-50%)
Mutual labels:  rule-engine
rule-engine
基于流程,事件驱动,可拓展,响应式,轻量级的规则引擎。
Stars: ✭ 165 (+489.29%)
Mutual labels:  rule-engine
EngineX
Engine X - 实时AI智能决策引擎、规则引擎、风控引擎、数据流引擎。 通过可视化界面进行规则配置,无需繁琐开发,节约人力,提升效率,实时监控,减少错误率,随时调整; 支持规则集、评分卡、决策树,名单库管理、机器学习模型、三方数据接入、定制化开发等;
Stars: ✭ 369 (+1217.86%)
Mutual labels:  rule-engine
RulerZBundle
Symfony Bundle for RulerZ
Stars: ✭ 38 (+35.71%)
Mutual labels:  rule-engine
Rulette
A pragmatic business rule management system
Stars: ✭ 91 (+225%)
Mutual labels:  rule-engine
claire
Continuously Learning Artificial Intelligence Rules Engine (Claire) for Smart Homes
Stars: ✭ 18 (-35.71%)
Mutual labels:  rule-engine
liteflow
Small but powerful rules engine,轻量强大优雅的规则引擎
Stars: ✭ 1,119 (+3896.43%)
Mutual labels:  rule-engine
evrete
Evrete is a lightweight and intuitive Java rule engine.
Stars: ✭ 37 (+32.14%)
Mutual labels:  rule-engine
powerflows-dmn
Power Flows DMN - Powerful decisions and rules engine
Stars: ✭ 46 (+64.29%)
Mutual labels:  rule-engine
arete
Fast, Clojure-based rule engine
Stars: ✭ 25 (-10.71%)
Mutual labels:  rule-engine
IoT-Technical-Guide
🐝 IoT Technical Guide --- 从零搭建高性能物联网平台及物联网解决方案和Thingsboard源码分析 ✨ ✨ ✨ (IoT Platform, SaaS, MQTT, CoAP, HTTP, Modbus, OPC, WebSocket, 物模型,Protobuf, PostgreSQL, MongoDB, Spring Security, OAuth2, RuleEngine, Kafka, Docker)
Stars: ✭ 2,565 (+9060.71%)
Mutual labels:  rule-engine
hmrb
Python Rule Processing Engine 🏺
Stars: ✭ 65 (+132.14%)
Mutual labels:  rule-engine
naive-rete
Python RETE algorithm
Stars: ✭ 51 (+82.14%)
Mutual labels:  rule-engine

scala-rules

Scala-Rules is a forward chaining rule engine, built in Scala. The engine comes with a DSL with which derivations can be modelled as small steps that combine into large derived networks of facts.

The project started with a Dutch DSL to facilitate bankers writing mortgage-related calculations. The development of an English DSL is currently in progress (see Future Work).

Codeship Status for scala-rules/scala-rules

Getting Started

The minimal requirement to get started is to add the artifact containing the rule engine to your project. Below are snippets for SBT and Maven. Be sure to check if any newer versions are available and adjust the snippets accordingly.

SBT - build.sbt

libraryDependencies += "org.scala-rules" %% "rule-engine" % "0.2.6"

Maven - pom.xml

<dependency>
	<groupId>org.scala-rules</groupId>
	<artifactId>rule-engine_2.11</artifactId>
	<version>0.2.6</version>
</dependency>

Concepts

The rule engine uses a Fact as the base for all of its derivations. A Fact defines its name and type so it can be referenced in evaluations down the road. Facts are best defined inside a Glossary:

object MyGlossary extends Glossary {
	val factA = defineFact[Int]
	val factB = defineFact[Int]
	val factC = defineFact[Int]
}

Note: the name of the fact is automatically deduced from the variable it is assigned to. defineFact has an optional parameter for a description of the fact.

Using this glossary, it is now possible to define derivations. The Scala Rules DSL provides an easy way to express how facts interact and come together to form your logic. To enable the DSL, create a class that extends Berekening:

import org.scalarules.dsl.nl.grammar._
import MyGlossary._

class MyArithmetics extends Berekening (
	Gegeven (factA > 0) Bereken factC is factB - factA 
)

Note the parenthesis behind Berekening, using braces won’t work. The Berekening constructor requires a series of DslDerivations, using braces causes this argument to be an empty list and yields you no executable derivations.

Note 2: for more information about the possibilities of the DSL, see the Wiki page about it

The two listings above are actually all you need to define your calculations, validations or evaluations. The engine will have enough information to start working for you. Only one thing is still missing for the scenario to make sense to you: values.

The engine requires you to construct a Context mapping a set of initial Facts to their values. When you have that, you can let the engine do the rest:

val initialContext: Context = Map(
  factA -> 4,
  factB -> 10
)
val derivations: List[Derivation] = new MyArithmetics().berekeningen

val resultContext: Context = FactEngine.runNormalDerivations(initialContext, derivations)

println(PrettyPrinter.printContext(resultContext))

Executing the above code will yield the following:

Values in context:
  factA = 4
  factB = 10
  factC = 6
Done.

Debugging

If you want to see exactly what the engine is doing, you can replace the runNormalDerivations with runDebugDerivations. The return type of that function is a tuple containing the resulting Context and a list of Step objects. The latter describe exactly what actions the engine performed and why:

val initialContext: Context = Context(
  factA -> 4,
  factB -> 10
)
val derivations: List[Derivation] = new MyArithmetics().berekeningen

val (resultContext, steps) = FactEngine.runDebugDerivations(initialContext, derivations)

println(PrettyPrinter.printSteps(steps))

Executing this snippet will yield an overview of the steps taken by the engine:

Steps taken:
 * Evaluate: factC
   * Result: Evaluated
   * Change: Map(factC -> 6)
Done.

For each step there is the Fact that was about to be evaluated (factC). Next is the status of the evaluation. The engine might indicate that the condition was false and thus the evaluation was skipped. Finally the change field shows what is added to the Context as a result of this evaluation.

Going Hardcore

If you have complicated tasks to perform, or our DSL simply does not fit your needs, you can write custom evaluations.

The DSL in the Berekening class yields a list of Derivation-objects. Constructing one yourself is not difficult, but requires some explanation:

case class DefaultDerivation(
  input: List[Fact[Any]], 
  output: Fact[Any], 
  condition: Context => Boolean, 
  operation: Evaluation[Any]) extends Derivation

For a derivation to work, the engine must know all the Facts it uses as input. The input parameter requires you to provide these Facts as a list.

The output allows the engine to store the result of the derivation in the Context. The output fact is the key for this.

The condition is a function to decide whether the operation should be executed. The function is provided with the current Context containing all values known at the point of execution. Returning true causes the operation to be executed, false will cause it to be skipped.

Finally, the operation is the function which will result in the value assigned to the output fact. You need to instantiate or extend any of the available Evaluation classes.

# Future Work

The DSL is currently being translated into English and we expect to release this in July 2016.

In the project for which Scala Rules was originally developed, we have incorporated a utility project to visualise the dependency graphs generated by the engine. These graphs provide great insights in the execution order of the engine and allow for easy review of derivations by non-programmers. This project cannot as of yet be released as an open source module, because it is too strongly tied to the original project. We are making the graph visualisation more generally applicable for Scala Rules and expect to release it in the summer of 2016. Here’s an example visualisation of a derivation graph:

Example Dependency Graph showing Execution Order of derivations

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].