sebhoss/reguloj


Lightweight business rule engine

License: WTFPL

Language: Java

Keywords: android, gwt, java, rule-engine


reguloj

reguloj is a small and lightweight Java rule engine.

Usage

Creating rule engines

A rule engine evaluates a set of rules in a specific context. The RuleEngines utility class offers to build 3 different rule engines:

// All rules will be evaluated indefinitely until no further rule fires.
final RuleEngine<CONTEXT> chained = RuleEngines.chained();

// All rules will be evaluated, but only a maximum number of 5 times.
final RuleEngine<CONTEXT> limited = RuleEngines.limited(5);

// Evaluates all rules, stops after the first one that fires.
final RuleEngine<CONTEXT> firstWins = RuleEngines.firstWins();

If custom inference behavior is required, subclass AbstractRuleEngine and implement the infer() method. The following code example shows how to work with rule engines:

// setup - more details later
RuleEngine<CONTEXT> engine = ...;
Collection<Rule<CONTEXT>> rules = ...;
CONTEXT context = ...;

// true if at least one rule can fired.
engine.analyze(rules, context);

// perform conclusions of those rules that fired.
engine.infer(rules, context);

Creating rules

A rule has a (unique) name and runs in a given context. Additionally it can be checked whether a rule fires in a given context.

Either implement the Rule interface yourself and or use the supplied rule implementation and builder. A standard rule is composed of a predicate and a consumer. Both interfaces require you to implement only a single method and do not restrict you in any way. Complex rules can be created by grouping or chaining predicates/consumers together with the help of several utility methods. The following example creates a rule composed of 2 predicates and 2 consumers:


final Rule<CONTEXT> rule = Rules.rule().called(name)
                        .when(predicate1.and(predicate2))
                        .then(consumer1.andThen(consumer2));

// true if the rule would fire in the given context, e.g. the above predicate is true.
rule.fires(context);

// runs (applies) the rule in the given context
rule.run(context);

Using Java 8 lambdas is possible as well:


final Rule<CONTEXT> rule = Rules.rule().called(name)
                        .when(context -> context.check())
                        .then(context -> context.action())

Creating an inference context

An inference context contains information needed by predicates and/or consumers. This project supplies a simple implementation of the Context interface called BaseContext. Subclass BaseContext in case your rules need extra information. The API acknowledges this by using <CONTEXT extends Context<?>> as type parameter for all methods which expect an Context. See item 28 in Effective Java for more details.

Integration

To use reguloj just declare the following dependency inside your POM:


<dependency>
  <groupId>com.github.sebhoss</groupId>
  <artifactId>reguloj</artifactId>
  <version>${version.reguloj}</version>
</dependency>

Replace ${version.reguloj} with the latest release

Requirements

Java Version 1.X.Y 2.X.Y 3.X.Y
Java 8
Java 7
Java 6

Development

License

This program is free software. It comes without any warranty, to the extent permitted by applicable law. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. See http://sam.zoy.org/wtfpl/COPYING for more details.

Setup

reguloj is using Maven, Eclipse and Git as the main development tools. To build the project yourself just download & install at least Maven 3.0 and call mvn install inside the project folder. Maven should then proceed to clean, test, build, package and install the business rules engine.

Versioning

reguloj follows the semantic versioning guidelines.

Project Statistics

Sourcerank 4
Repository Size 242 KB
Stars 14
Forks 2
Watchers 3
Open issues 15
Dependencies 5
Contributors 1
Tags 4
Created
Last updated
Last pushed

Top Contributors See all

Sebastian Hoß

Packages Referencing this Repo

com.github.sebhoss:reguloj
Lightweight business rule engine
Latest release 3.0.0 - Updated - 14 stars

Recent Tags See all

reguloj-3.0.0 November 30, 2014
reguloj-2.0.1 January 03, 2014
reguloj-2.0.0 September 30, 2013
reguloj-1.0.0 September 28, 2013

Something wrong with this page? Make a suggestion

Last synced: 2018-01-17 14:30:35 UTC

Login to resync this repository