Constraint block contains declarative state ments which restrict the range of varable or defines the relation between variables.Constraint programming is a pwerful mehod that lets users build generic ,resuble objects that can be extended or more constrained later.Constraint solver can only support 2 stet variables.Constraint solver fails only if there is no solution which satisfies all the constraints.constraint block can also have nonrandom variables,but atleast one random variable is needed for randomization.
Constraints are tied to objects.This allows inheritance,hirarchical constraints,controlling the constraints of specific object.
EXAMPLE: class Base{
randinteger Var;
constraint range { Var < 100 ; Var > 0 ;}
}
class Extended extends Base{
constraint range { Var < 100 ; Var > 50 ;} // Overrighting the Base class constraints.
}
program inhe{
Extended obj;
integer i;
obj = new();
for(i=0 ; i < 100 ; i++)
if(obj.randomize())
printf(" Randomization sucsessfull : Var = %0d ",obj.Var);
else printf("Randomization failed");
}
RESULTS:
Randomization sucsessfull : Var = 57 Randomization sucsessfull : Var = 82 Randomization sucsessfull : Var = 68 Randomization sucsessfull : Var = 81 Randomization sucsessfull : Var = 90
Inline Constraints
Inline constraints allows to add extra constraints to allready existing conrtints which are declared inside class.If you have constraints already defined for variavle var, solver solves those constraints wlong with the inline constraints.
EXAMPLE
class inline{
randinteger Var;
constraint default_c { Var > 0 ; Var < 100;}
}
program inline_p{
inline obj;
obj = new();
void = obj.randomize() with { Var == 50;} ;
printf(" Randodmize sucessful Var %d ",obj.Var);
}
RESULTS:
Randomization sucsessfull : Var = 50
Disabling Constraint Block
openvera supports to change the status of constraint block dynamically.To change the staus of a Constraint block,built in constraint_mode() method is used.By default all the constraint blocks are active.
When it is called as task,the arguments to the task determines the operation to be performed.
0 OFF Sets the specified constraint block to inactive
so that it is not enforced by subsequent calls
to the randomize() method.
1 ON Sets the specified constraint block to active so
that it is considered on subsequent calls to the
randomize() method.
When it is called as function,it returns the active status of the specified constrant block.