Rules-DSL: Difference between revisions

Line 4: Line 4:


===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 is:
<pre>
<pre>
RULES_MACHINE machine name
RULES_MACHINE machine name
Line 11: Line 11:
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====
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.
Rules can be defined in the <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:
In the B Rules-DSL a rule has the following structure:
<pre>
<pre>
RULE rule name // will be the name of the operation and variable storing the result
RULE rule name // will be the name of the operation and variable storing the result
Line 22: Line 22:
ERROR_TYPES positive number of error types
ERROR_TYPES positive number of error types
BODY
BODY
     RULE_FAIL
     arbitrarily many rule bodys (see below)
END
</pre>
 
Within the body you can use as many conditions for the outer rule as you like. There are two constructs for specifiying the expectation of the rule.
The following is formulated in a positive way, i.e. the execution of the rule leads to <code>SUCCESS</code> if the conditions in the <code>EXPECT</code>-part are fulfilled.
<pre>
    RULE_FORALL
         list of identifiers
         list of identifiers
     WHEN
     WHERE
         conditions on identifiers for a failing rule
         conditions on identifiers
    EXPECT
      conditions that must be fulfilled for this rule
     ERROR_TYPE
     ERROR_TYPE
         number encoding error type, must be in range of error types
         number encoding error type, must be in range of error types
     COUNTEREXAMPLE  
     COUNTEREXAMPLE  
         STRING_FORMAT("errorMessage ~w", identifier from list)
         STRING_FORMAT("errorMessage ~w", identifier from list)
     END;
     END
     RULE_FORALL
</pre>
Alternatively, you can also formulate the rule negatively, i.e. the execution of the rule leads to <code>FAIL</code> if the conditions in the <code>WHEN</code>-part are fulfilled.
<pre>
     RULE_FAIL
         list of identifiers
         list of identifiers
     WHERE
     WHEN
         conditions on identifiers
         conditions on identifiers for a failing rule
    EXPECT
      conditions that must be fulfilled for this rule
     ERROR_TYPE
     ERROR_TYPE
         number encoding error type, must be in range of error types
         number encoding error type, must be in range of error types
Line 42: Line 52:
         STRING_FORMAT("errorMessage ~w", identifier from list)
         STRING_FORMAT("errorMessage ~w", identifier from list)
     END
     END
END
</pre>
</pre>


Line 72: Line 81:
END
END
</pre>
</pre>
===Use Rules for Data Validation===

Revision as of 11:10, 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 is:

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.

Rules

Rules can be defined in the 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 has the following structure:

RULE rule name // will be the name of the operation and variable storing the result
RULE_ID id
DEPENDS_ON_RULE list of rules
DEPENDS_ON_COMPUTATION list of computations
ACTIVATION predicate
ERROR_TYPES positive number of error types
BODY
    arbitrarily many rule bodys (see below)
END

Within the body you can use as many conditions for the outer rule as you like. There are two constructs for specifiying the expectation of the rule. The following is formulated in a positive way, i.e. the execution of the rule leads to SUCCESS if the conditions in the EXPECT-part are fulfilled.

    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

Alternatively, you can also formulate the rule negatively, i.e. the execution of the rule leads to FAIL if the conditions in the WHEN-part are fulfilled.

    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

Computations

COMPUTATION computation name
DEPENDS_ON_RULE list of rules
DEPENDS_ON_COMPUTATION list of computations
ACTIVATION predicate
BODY
    DEFINE variable name
        TYPE type of variable
        DUMMY_VALUE value of variable before execution (initialisation)
        VALUE value of variable after execution
     END
END

Functions

FUNCTION output <-- function_name(list of input parameters)
PRECONDITION
    predicate
POSTCONDITION
    predicate
BODY
   some B statements
   output := ...
END

Use Rules for Data Validation