How to pass parameter to constrain?

Hi,
In my verification environment, the mainly different between my cases is constrain, for example:

//==============================================
   class Image;  
    
    rand int width;  
    rand int height;  
    ...
    endclass  

   case 1:  img.randomize() with  { img_width > 100 ; img_height > 100}
   case 2:  img.randomize() with  {img_width < 120 ; img_height > 120}
   case 3:  img.randomize() with  {img_width != 120 ; img_height > 300}

I want to modify the constraint block to be configurable so that I only need to compile once but can run with different constraints.

I have do some try, use variable in constraint_block

   if (!$value$plusargs("WIDTH=%d", img.width)) begin  
       $display("WIDTH argument not provided, using default value 640");  
       img.width = 640; 
   end  
  
   if (!$value$plusargs("HEIGHT=%d", img.height)) begin  
       $display("HEIGHT argument not provided, using default value 480");  
       img.height = 480; 
   end  
   img.randomize() with { img_width > width ; img_height > height}

However, this approach only allows changing the data and not the operator (e.g., “>”). Additionally, it is inconvenient. How can I solve this issue?
Thanks!

You probably want to encapsulate the constraint in a separate class.
Try something like this, which should be easier to configure.

class policy_base #(type T=uvm_object);
    T item;
    virtual function set_item(T item);
        this.item = item;
    endfunction
endclass

class policy_base #(type T=uvm_object);
    T item;
    virtual function set_item(T item);
        this.item = item;
    endfunction
endclass

class policy_one extends policy_base;
    constraint c { ... }
endclass

class image extends uvm_sequence_item;
    policy_base #(image)  policy[$];
    function pre_randomize();
        foreach(policy[i])
            policy[i].set_item = this;
    endfunction
endclass

class sequence exntends uvm_sequence;
    image my_image = new;
    policy_one policy_one_item = new;
    policy_two policy_two_item = new;
    my_image.policy = {policy_one_item, policy_two_item};
endclass

Thank you for your response. My main goal is to achieve dynamic binding of constraints at runtime, rather than at compile time.

The reason for this requirement is that I frequently need to modify the constraints, and each time, only the constraints are changed while the rest of the sequence remains the same. I don’t want to recompile the entire project for every minor constraint modification, as it is very time-consuming.

If this requirement is met, I would only need to compile my project once and could run simulations with different constraints by passing the constraint expressions during runtime.

You may want to look at strategies for separate and incremental compilation of your design and testbench. These are going to be tool specific.