Constraint block contains declarative statements which restrict the range of variable or defines the relation between variables. Constraint programming is a powerful method that lets users build generic, resuble objects that can be extended or more constrained later. constraint solver can only support 2 stete values. If a 4 state variable is used, solver treates them as 2 state variable.. Constraint solver fails only if there is no solution which satisfies all the constraints. Constraint block can also have nonrandom variables, but at least one random variable is needed for randomization. Constraints are tied to objects. This allows inheritance, hierarchical constraints, controlling the constraints of specific object.
Inheritance
One of the main advantage of class randomization is Inheritance. Constraints in derived class with the same name in base class overrides the base class constraints just like task and functions.
EXAMPLE: class Base;
randinteger Var;
constraint range { Var < 100 ; Var > 0 ;}
endclass
class Extended extends Base;
constraint range { Var < 100 ; Var > 50 ;} // Overrighting the Base class constraints.
endclass
program inhe_31;
Extended obj;
initial begin obj = new();
for(int i=0 ; i < 100 ; i++)
if(obj.randomize())
$display(" Randomization sucsessfull : Var = %0d ",obj.Var);
else $display("Randomization failed");
end endprogram
RESULTS:
# Randomization sucsessfull : Var = 91 # Randomization sucsessfull : Var = 93 # Randomization sucsessfull : Var = 77 # Randomization sucsessfull : Var = 68 # Randomization sucsessfull : Var = 67 # Randomization sucsessfull : Var = 52 # Randomization sucsessfull : Var = 71 # Randomization sucsessfull : Var = 98 # Randomization sucsessfull : Var = 69
Adding new constraints in the derived class, can change the solution space. Solver has to solve both constraints defined in base class and derived class. In the example given below, Constraint range_1 defines the range that Var is between 0 to 100.Constraint range_2 limits the Var to be greater than 50 and solver has to solve both the constraints and the solution space is between 50 to 100.
EXAMPLE: class Base;
randinteger Var;
constraint range_1 { Var < 100 ; Var > 0 ;}
endclass
class Extended extends Base;
constraint range_2 { Var > 50 ;} // Adding new constraints in the Extended class
endclass
program inhe_32;
Extended obj;
initial begin obj = new();
for(int i=0 ; i < 20 ; i++)
if(obj.randomize())
$write(": Var = %0d :",obj.Var);
else $display("Randomization failed");
end endprogram
RESULTS:
Var = 91 :: Var = 93 :: Var = 77 :: Var = 68 :: Var = 67 :: Var = 52 :: Var = 71 :: Var = 98 :: Var = 69 :: Var = 70 :: Var = 96 :: Var = 88 :: Var = 84 :: Var = 99 :: Var = 68 :: Var = 83 :: Var = 52 :: Var = 72 :: Var = 93 :: Var = 80 :
Overrighting Constraints
The randomize() task is virtual. Accordingly it treats the class constraints in a virtual manner. When a named constraint is redefined in an extended class, the previous definition is overridden and when casting extended class to base class does not change the constraint set.
EXAMPLE: class Base;
randinteger Var;
constraint range { Var < 100 ; Var > 0 ;}
endclass
class Extended extends Base;
constraint range { Var == 100 ;} // Overrighting the Base class constraints.
endclass
program inhe_33;
Extended obj_e;
Base obj_b;
initial begin obj_e = new();
obj_b = obj_e;
for(int i=0 ; i < 7 ; i++)
if(obj_b.randomize())
$display(" Randomization sucsessfull : Var = %0d ",obj_b.Var);
else $display("Randomization failed");
end endprogram
RESULTS:
# Randomization sucsessfull : Var = 100 # Randomization sucsessfull : Var = 100 # Randomization sucsessfull : Var = 100 # Randomization sucsessfull : Var = 100 # Randomization sucsessfull : Var = 100 # Randomization sucsessfull : Var = 100 # Randomization sucsessfull : Var = 100
When an extended object is casted to base object, all the constraints in extended object are solved along with the constraints in base object.
EXAMPLE: class Base;
randinteger Var;
constraint range_1 { Var < 100 ; Var > 0 ;}
endclass
class Extended extends Base;
constraint range_2 { Var > 50 ;}
endclass
program inhe_34;
Extended obj_e;
Base obj_b;
initial begin obj_e = new();
obj_b = obj_e;
for(int i=0 ; i < 10 ; i++)
if(obj_b.randomize())
$display(" Randomization sucsessfull : Var = %0d ",obj_b.Var);
else $display("Randomization failed");
end endprogram RESULTS:
# Randomization sucsessfull : Var = 91 # Randomization sucsessfull : Var = 93 # Randomization sucsessfull : Var = 77 # Randomization sucsessfull : Var = 68 # Randomization sucsessfull : Var = 67 # Randomization sucsessfull : Var = 52 # Randomization sucsessfull : Var = 71 # Randomization sucsessfull : Var = 98 # Randomization sucsessfull : Var = 69 # Randomization sucsessfull : Var = 70