Object handles and randomization

In reply to Bahaa Osman:

Off the top of my head, this is how you could implement it:


class car;
  // Not rand, since you can't use randomization on a car object to suddenly make it a Toyota
  brand_t brand_e;

  // Adds other constraints, methods and variables related to all cars
endclass


class toyota extends car;
  function new();
    brand_e = TOYOTA;
  endfunction

  // Adds other constraints, methods and variables related only to this class
endclass


class txn;
  rand brand_t brands[];
  rand car mycars_arr[];

  function void allocate_cars();
    mycars_arr = new[brands.size()];
    foreach (brands[i])
      case (brands[i])
        TOYOTA: mycars_arr[i] = toyota::new();
        // ...
      endcase
  endfunction
endclass


class test;
  function void run();
    txn t = new();
    t.randomize(brands) with {
      // whatever constraints you want on brands
    };
    t.allocate_cars();
    t.randomize(mycars_arr) with {
      // whatever constraints you want on cars
    };
  endfunction
endclass

This is really ugly since it’s not very intuitive. Users not familiar with your ‘txn’ class will scratch their heads when they see the steps required to randomize an array of cars.