Skip to content
Forward chaining rule engine with an extensible DSL
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
docs/images Adding a bit of documentation and an example graph to the May 12, 2016
engine-core/src Pulled the trait apart into a NoChangesStep and a ChangesStep Nov 8, 2016
engine/src fix for some code-quality issues in the tests Jan 13, 2017
project #6 Release using SBT. Can now release to maven central from local mac… Apr 21, 2016
.gitignore First commit. Working version of the rule engine is now open source, … Apr 13, 2016
.plan Updated .plan May 1, 2016
LICENSE First commit. Working version of the rule engine is now open source, … Apr 13, 2016
build.sbt Bumping to development Jan 16, 2017


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



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 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)


Executing the above code will yield the following:

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


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)


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

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

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

You can’t perform that action at this time.