Previous Section Next Section

5.4 Soft Constraints

Often constraints are specified as a default preference. These constraints need to be overridden later. These are called soft constraints and are specified using the keywords keep soft. Soft value constraints on a data item are considered only at the time the data item is generated, after the hard value constraints on the data item are applied. Soft constraints are evaluated in reverse order of definition. If a soft constraint conflicts with the hard constraints that have already been applied, it is skipped. If there is a contradiction with other soft constraints, the last loaded soft constraint prevails.

In Example 5-4 below, the constraints will be evaluated in the following order:

  1. The hard constraint is applied, so the range is [1..10].

  2. The last soft constraint in the code order, x < 6, is considered. It does not conflict with the current range, so it is applied. The range is now [1..5].

  3. The next to last soft constraint, x == 8, conflicts with the current range, so it is skipped. The range is still [1..5].

  4. The first soft constraint in the code order, x > 3, does not conflict with the current range, so it is applied. The final range of legal values is [4, 5].

Example 5-4 Evaluation Order of Soft Constraints
Example shows the order in which the constraints are evaluated
<'
struct cons {
   x: uint;
   keep x in [1..10]; //First eval, range of x is [1..10]
   keep soft x > 3;   //Fourth eval, no conflict range of x is [4, 5]
   keep soft x==8;    //Third eval, conflict with [1..5], skipped
   keep soft x < 6;   //Second eval, no conflict, range of x is [1..5]
};
'>

Example 5-5 shows more examples of soft constraints.

Example 5-5 Soft Constraints
Example shows different combinations of soft constraints
and hard constraints and the corresponding results.
<'
struct example {
   len1: uint;
     keep soft len1 == 64; //No contradiction with hard constraint
     keep len1 <= 100; //Result is len1 equal to 64

   len2: uint;
     keep soft len2 == 64; //Contradiction with hard constraint
     keep len2 >= 100; //Result is len2 value >= 100

   len3: uint;
     keep soft len3 > 64;
     keep soft len3 < 64; //Contradiction with previous soft
                          //constraint. Last loaded constraint
                          //prevails i.e. len3 > 64
};
'>

5.4.1 Resetting Soft Constraints

A soft constraint usually indicates the default preferences for a certain field. In order to override the default and change the value with a hard constraint, the verification engineer must also reset the soft constraints for that field using the reset_soft() constraint. It is advisable to do this explicitly to make sure that there are no residual soft constraints acting on that field. Example 5-6 shows how to reset soft constraints.

Example 5-6 Resetting Soft Constraints
[View full width]
Example shows a soft constraint is reset before another hard constraint is applied in the
graphics/ccc.gif extension of the struct.
<'
struct packet { //Original struct definition with soft constraint.
   len: uint;
   keep soft len in [64..1500]; //Default range
};

extend packet { //Struct extension, typically in separate file
                //but shown here in the same file for convenience.
     keep len.reset_soft(); //Reset all previously loaded soft
                            //constraints on the len field.
                            //reset_soft() is on a per field basis.
     keep len > 2000;       //Apply a hard constraint.

};
'>
Previous Section Next Section