Vous êtes sur la page 1sur 5

Verification Academy Patterns Library

Verification Academy Patterns Library


Pattern Name: Strategy Pattern
 Intent: Define a family of algorithms, encapsulate each one, and make them
interchangeable. Strategy lets the algorithm vary independently from clients
that use it.

 Motivation: When multiple behaviors are needed in a class, we typically use


multiple conditional statements in its operations. Instead of many conditionals,
move relates conditional behaviors into their own Strategy class.

 Applicability: This pattern helps implement one of the basic principles of Object
Oriented programming, i.e. Open Close Principle. This principle states classes
should be open for extension but closed for modification. It implies “change”
what the modules do without changing them.

 Structure:

Page 1 © Mentor Graphics Corporation, all rights reserved.


Verification Academy Patterns Library

 Implementation:
The implementation of the Strategy Pattern has 3 basic classes/structures.

1. Strategy Class/Structure, which declares the common interface.


2. ConcreteStrategy Class/Structure, which is basically a subclass of
strategy. Each of the subclass implements an algorithm/behavior.
3. Context Class/Structure, which interacts with the Strategy and calls the
interface defined by the Strategy. A Context forwards requests from its
clients to its Strategy. A Strategy and Context interact to implement the
chosen algorithm.

If a Client exists then use the Client interfaces or uses the context class.

 Example:
1. Policies in OVM/UVM: Each of UVM’s policy classes perform a specific
task for uvm_object-based objects like printing, comparing, recording,
packing, and unpacking. They are implemented separately from
uvm_object so that users can use different ways to print, compare, etc.
without modifying the actual object class being operated on. The user can
simply apply a different printer or compare “policy” to change how an
object is printed or compared.

2. UVM Sequences and tests are a common example of having a base class
with a common interface with subclasses capturing different algorithms.
The context class here would be testbench top from where a test is
selected.

// This library contains the three test cases which are inherited from the
// base. Each subclass here represents a different algorithm/behaviour. From
// the top, any of the test can be invoked/called seamlessly.
// bus_test_base - Takes care of the common set up and configuration
// seq_rand_test - How to use sequence randomization to configure a sequence
// complete_transfer_test - Using sequence object persistence
// rand_transfer_test - Sequences executed in a random order

// All of the example test cases inherit from this base class
//

Page 2 © Mentor Graphics Corporation, all rights reserved.


Verification Academy Patterns Library

class bus_test_base extends uvm_test;

`uvm_component_utils(bus_test_base)

bus_agent m_agent;
bus_agent_config m_cfg;

function new(string name = "bus_test_base", uvm_component parent = null);


super.new(name, parent);
endfunction

function void build_phase(uvm_phase phase);


m_cfg = bus_agent_config::type_id::create("m_cfg");
if(!uvm_config_db #(virtual bus_if)::get(this, "", "BUS_vif", m_cfg.BUS))
begin
`uvm_error("Build_phase", "Unable to find BUS_vif")
end
uvm_config_db #(bus_agent_config)::set(this, "*", "config", m_cfg);
m_agent = bus_agent::type_id::create("m_agent", this);
endfunction: build_phase

endclass: bus_test_base

//
// This test shows how to randomize the memory_trans_seq
// to set it up for a block transfer
//
class seq_rand_test extends bus_test_base;

`uvm_component_utils(seq_rand_test)

function new(string name = "seq_rand_test", uvm_component parent = null);


super.new(name);
endfunction

task run_phase(uvm_phase phase);


mem_trans_seq seq = mem_trans_seq::type_id::create("seq");

phase.raise_objection(this, "Starting test");


// Using randomization and constraints to set the initial values

Page 3 © Mentor Graphics Corporation, all rights reserved.


Verification Academy Patterns Library

//
// This could also be done directly
//
if(!seq.randomize() with {src_addr == 32'h0100_0800;
dst_addr inside {[32'h0101_0000:32'h0103_0000]};
transfer_size == 128;}) begin
`uvm_error("run_phase", "seq randomization failure")
end
seq.start(m_agent.m_sequencer);
phase.drop_objection(this, "Finishing test");
endtask: run_phase

endclass: seq_rand_test

class complete_transfer_test extends bus_test_base;

`uvm_component_utils(complete_transfer_test)

function new(string name = "complete_transfer_test", uvm_component parent =


null);
super.new(name);
endfunction

task run_phase(uvm_phase phase);


rpt_mem_trans_seq seq = rpt_mem_trans_seq::type_id::create("seq");

phase.raise_objection(this, "Starting test");


seq.start(m_agent.m_sequencer);
phase.drop_objection(this, "Finishing test");
endtask: run_phase

endclass: complete_transfer_test

class rand_transfer_test extends bus_test_base;

`uvm_component_utils(rand_transfer_test)

function new(string name = "rand_transfer_test", uvm_component parent = null);


super.new(name);
endfunction

Page 4 © Mentor Graphics Corporation, all rights reserved.


Verification Academy Patterns Library

task run_phase(uvm_phase phase);


rand_order_seq seq = rand_order_seq::type_id::create("seq");

phase.raise_objection(this, "Starting test");


repeat(3) begin
seq.start(m_agent.m_sequencer);
end
phase.drop_objection(this, "Finishing test");
endtask: run_phase

endclass: rand_transfer_test

 Consequences: The client that finally uses the algorithm using the context
should be aware of all the types of strategies/algorithms that are available.

 Related Patterns:

o Strategy is like Template Method except in its granularity.


o State is like Strategy except in its intent.
o Strategy lets you change the guts of an object. Decorator lets you change
the skin.

 Contributor: Pradeep Salla

 Release Date: March 16, 2016

Corrections and Suggestions: To improve the quality of the Verification Academy


Patterns Library we welcome your comments, suggestions, and corrections. Please
contact us at: https://verificationacademy.com/contact

Page 5 © Mentor Graphics Corporation, all rights reserved.

Vous aimerez peut-être aussi