In reply to dave_59:
Hi Dave,
I need to implement a function in db_manager something like below
function myClass db_manager::getRandomItem (
input expression_type find_clause, // Operates only on const fields of myClass
input expression_type constraints // Operates only on random fields of myClass
);
myClass obj;
int matching_idxs[$];
matching_idxs = q.find_index () with (find_clause);
noMatchingElements: assert (matching_idxs.size() > 0) else $fatal ("No matching elements found");
// Take a copy of matching item
obj = q[matching_idxs[$urandom_range(matching_idxs.size() - 1)]].copy()
constraintFaulire: assert (obj.randomize() with constraints) else $fatal (
"Randomizing failed at line", `__LINE__
);
return obj;
endfunction : getRandomItem
and call it from somewhere as below
initial begin
myClass obj;
db_manager db;
obj = db.getRandomItem (
// find_clause
(
(item.a inside {A0, A1, A2}) &&
(item.b inside {B0, B1, B2})
),
// constraints
(
ar inside {['d0:'d20]};
br inside {['d0:'d20]};
)
);
end
Sample output (3 iterations)…
a b c ar br cr
A0 B1 C5 'd10 'd13 'd89
A1 B1 C9 'd19 'd13 'd48
A2 B2 C2 'd11 'd10 'd20