Rules-DSL: Difference between revisions

Line 3: Line 3:
The B Rules domain-specific language mainly provides operations for data validation. <em>Rules</em> allow checking for expected properties, while <em>computations</em> can be used to define and compute variables based on the successful execution of certain rules. Furthermore you can use <em>functions</em> to compute values multiple times depending on different inputs.
The B Rules domain-specific language mainly provides operations for data validation. <em>Rules</em> allow checking for expected properties, while <em>computations</em> can be used to define and compute variables based on the successful execution of certain rules. Furthermore you can use <em>functions</em> to compute values multiple times depending on different inputs.


====Setting up a Rules Machine====
===Setting up a Rules Machine===
Rules machines are stored in <code>.rmch</code>-files. The general setup for the machine header looks like this:
Rules machines are stored in <code>.rmch</code>-files. The general setup for the machine header looks like this:
<pre>
<pre>
Line 10: Line 10:
</pre>
</pre>
Below, you can use <code>SETS</code>, <code>DEFINITIONS</code>, <code>PROPERTIES</code> or <code>CONSTANTS</code> as in a normal B machine. Note that <code>VARIABLES</code> are not supported as they are set by rule based computations.
Below, you can use <code>SETS</code>, <code>DEFINITIONS</code>, <code>PROPERTIES</code> or <code>CONSTANTS</code> as in a normal B machine. Note that <code>VARIABLES</code> are not supported as they are set by rule based computations.
====Use Rules for Data Validation====
Rules can be defined <code>OPERATIONS</code>-section of a rules machine. Depending on whether the expectations are met, a rule returns <code>SUCCESS</code> or <code>FAIL</code>. If a rule fails, additionally provided string messages are returned as counterexamples.
In the B Rules-DSL a rule can look like this:
<pre>
RULE rule name // will be the name of the operation and variable storing the result
DEPENDS_ON_RULE list of rules
DEPENS_ON_COMPUTATION list of computations
ACTIVATION predicate
ERROR_TYPES positive number of error types
BODY
    RULE_FAIL
        list of identifiers
    WHEN
        conditions on identifiers for a failing rule
    ERROR_TYPE
        number encoding error type, must be in range of error types
    COUNTEREXAMPLE
        STRING_FORMAT("errorMessage ~w", identifier from list)
    END;
    RULE_FORALL
        list of identifiers
    WHERE
        conditions on identifiers
    EXPECT
      conditions that must be fulfilled for this rule
    ERROR_TYPE
        number encoding error type, must be in range of error types
    COUNTEREXAMPLE
        STRING_FORMAT("errorMessage ~w", identifier from list)
    END
END
</pre>

Revision as of 09:41, 3 May 2023

Rules DSL

The B Rules domain-specific language mainly provides operations for data validation. Rules allow checking for expected properties, while computations can be used to define and compute variables based on the successful execution of certain rules. Furthermore you can use functions to compute values multiple times depending on different inputs.

Setting up a Rules Machine

Rules machines are stored in .rmch-files. The general setup for the machine header looks like this:

RULES_MACHINE machine name
REFERENCES list of rules machines

Below, you can use SETS, DEFINITIONS, PROPERTIES or CONSTANTS as in a normal B machine. Note that VARIABLES are not supported as they are set by rule based computations.

Use Rules for Data Validation

Rules can be defined OPERATIONS-section of a rules machine. Depending on whether the expectations are met, a rule returns SUCCESS or FAIL. If a rule fails, additionally provided string messages are returned as counterexamples. In the B Rules-DSL a rule can look like this:

RULE rule name // will be the name of the operation and variable storing the result
DEPENDS_ON_RULE list of rules
DEPENS_ON_COMPUTATION list of computations
ACTIVATION predicate
ERROR_TYPES positive number of error types
BODY
    RULE_FAIL 
        list of identifiers
    WHEN 
        conditions on identifiers for a failing rule
    ERROR_TYPE
        number encoding error type, must be in range of error types
    COUNTEREXAMPLE 
        STRING_FORMAT("errorMessage ~w", identifier from list)
    END;
    RULE_FORALL
        list of identifiers
    WHERE 
        conditions on identifiers
    EXPECT
       conditions that must be fulfilled for this rule
    ERROR_TYPE
        number encoding error type, must be in range of error types
    COUNTEREXAMPLE 
        STRING_FORMAT("errorMessage ~w", identifier from list)
    END
END