A rule checker library for Java. Checks sets of rules for completeness, overlap, and constraint satisfaction.
Switch branches/tags
Nothing to show
Clone or download
sergey-grebenshchikov Fixed consistency checking bug
Latest commit a18a698 Feb 8, 2017


rc - a rule checker

rc is a completeness, overlap, and constraint satisfaction checker library for sets of rules. For a command-line interface to this library see rc-cli.

rc is implemented using Z3, the automatic theorem prover built by Microsoft Research.

Table of Contents


Maven dependency



rc is used as follows:

  • Construct a set of rules as an input to rc (either via a builder API or via YAML files)
  • Use rc to check the rules for completeness, consistency and constraint satisfaction
  • Use the counterexamples generated by rc to improve the rules

The input to rc has the following structure:

  • Module
    • Type definitions
    • Rule sets
      • Variable definitions
      • Rules
        • Precondition (when)
        • Postcondition (then)

There are two ways of constructing these objects:

  • Using the builders in io.github.sgreben.rc.Context and io.github.sgreben.rc.buildersto construct the objects directly.
  • Building (or loading from YAML) the declaration classes in io.github.sgreben.rc.declartions and then compiling them.

As an example, we will express the following rules for a fictional IoT device:

  • Given an enumeration type STATE with the values ON, OFF, STANDBY,
  • Given the sensor inputs temperature (integer), temperatureGoal (integer), brightness (real), motion (real),
  • Given the state input variable state and output variable stateOut of type STATE,
  • We apply the rules
    • When temperature > 23 && (motion < 0.3 || brightness < 0.1)
      • Then stateOut = OFF
    • When temperature < temperatureGoal && motion >= 0.3
      • Then stateOut = ON
    • When temperature >= temperatureGoal && motion < 0.1
      • Then stateOut = state
    • When temperature >= temperatureGoal && motion > 0.1
      • Then stateOut = STANDBY

Using the builder API

To start, we grab an instance of the Context class, which we will use to access the builders:

import io.github.sgreben.rc.Context;

Context context = new Context();

Next, we construct the enumeration type:

EnumType stateType = context.buildType().enumeration()

Using the type definition we can construct our variables:

Variable temperature = context.buildExpression()
Variable temperatureGoal = context.buildExpression()
Variable brightness = context.buildExpression()
Variable motion = context.buildExpression()
Variable state = context.buildExpression()
Variable stateOut = context.buildExpression()

We are now ready to construct our rules:

ExpressionBuilder eb = context.buildExpression();

// When `temperature > 23 && (motion < 0.3 || brightness < 0.1)`
// Then `stateOut = OFF`
Rule rule1 = context.buildRule()
    .withName("rule 1")
        eb.Greater(temperature, context.buildValue().integer(23)),
            eb.Less(motion, context.buildValue().real(0.3)),
            eb.Less(brightness, context.buildValue().real(0.1))
        stateOut, context.buildValue().enumeration("OFF"))

// When `temperature < temperatureGoal && motion >= 0.3`
// Then `stateOut = ON`
Rule rule2 = context.buildRule()
    .withName("rule 2")
        eb.Less(temperature, temperatureGoal),
        eb.GreaterOrEqual(motion, context.buildValue().real(0.3))
        stateOut, context.buildValue().enumeration("ON"))

// When `temperature >= temperatureGoal && motion < 0.1`
// Then `stateOut = state`
Rule rule3 = context.buildRule()
    .withName("rule 3")
        eb.GreaterOrEqual(temperature, temperatureGoal),
        eb.Less(motion, context.buildValue().real(0.1))
    .withPostcondition(eb.Equal(stateOut, state))

// When `temperature >= temperatureGoal && motion > 0.1`
// Then `stateOut = STANDBY`
Rule rule4 = context.buildRule()
    .withName("rule 4")
        eb.GreaterOrEqual(temperature, temperatureGoal),
        eb.Greater(motion, context.buildValue().real(0.1))
        stateOut, context.buildValue().enumeration("STANDBY"))

To check properties of a set of rules, we construct an instance of the RuleSet class:

RuleSet myRuleSet = context.buildRuleSet()
    .withName("my rule set")

Done! You can now skip ahead to Performing analysis to learn how to check the rule set for completeness, overlap, and constraint satisfaction.

Using the declaration API

Save the following as a file myModule.yaml.

name: myModule
        - ON
        - OFF
        - STANDBY
    - name: my rule set
        temperature: int 
        temperatureGoal: int 
        motion: real
        brightness: real
        state: STATE
        stateOut: STATE
        - name: rule 1
          when: temperature > 23 && (motion < 0.3 || brightness < 0.1)
            stateOut: OFF
        - name: rule 2
          when: temperature < temperatureGoal && motion >= 0.3
              stateOut: ON
        - name: rule 3
          when: temperature >= temperatureGoal && motion < 0.1
              stateOut: state
        - name: rule 4
          when: temperature >= temperatureGoal && motion > 0.1
              stateOut: STANDBY

We can now load this module using the ModuleDeclaration class:

import io.github.sgreben.rc.declarations.ModuleDeclaration;

ModuleDeclaration moduleDeclaration = ModuleDeclaration.load("myModule.yaml")

To perform analysis on the module, we have to compile it using a Context:

Context context = new Context();
Module module = moduleDeclaration.compile(context);

We can now obtain the rule set we defined above:

RuleSet myRuleSet = module.ruleSet("my rule set");

Done! You can now continue to Performing analysis to learn how to check the rule set for completeness, overlap, and constraint satisfaction.

Performing analysis

We are now ready to check our rules for completeness, overlap, and constraint satisfaction:

// Checks completeness, prints example unmatched values if incomplete
if (!myRuleSet.isComplete()) {
// Checks for overlaps, prints example values matched by multiple rules
if (myRuleSet.isOverlapping()) {
// Checks if the given constraint is satisfied in all cases
// Constraint: motion > 0.1 => state != OFF
Expression constraint = eb.Implies(
    eb.Greater(motion, context.buildValue.real(0.1)),
    eb.NotEqual(state, context.buildValue.enumeration("OFF"))
if (!myRuleSet.satisfiesConstraint(constraint)) {