Why uvm_transaction class when we always extend from uvm_sequence_item? - system-verilog

I was going through basics of UVM tutorials. Everywhere I read the transaction objects are always extended from uvm_sequence_item and not uvm_transaction since uvm_sequence_item has additional features like transaction id, etc. If that is the case, why is the uvm_transaction class even there in the UVM class hierarchy?
Who is using uvm_transaction other than uvm_sequence_item extending from it?
Is it because of legacy?

This is what the UVM Class Reference says about this:
"The uvm_transaction class is the root base class for UVM transactions. Inheriting all the methods of uvm_object, uvm_transaction adds a timing and recording interface.
This class provides timestamp properties, notification events, and transaction recording support.
Use of this class as a base for user-defined transactions is deprecated. Its subtype, uvm_sequence_item, shall be used as the base class for all user-defined transaction types."

If you refer uvm class hierarchy (Link:[https://www.google.co.in/search?biw=1366&bih=620&tbm=isch&sa=1&btnG=Search&q=uvm+class+hierarchy#imgrc=Laxc9UWNpnGTpM%3A][1] ) then you find out that uvm_transaction parent class while uvm_sequence is child class.
So, child class can access all the property of parent class.
But parent class can not access child class property.
uvm_sequence_item has its own functionality like get_sequencer_id,set_sequencer_id, get_root_sequence.
These methods used by sequencer internally in case of layer sequences.
You call sequence by start method, uvm_do family or config_db.
Each of these method call start_item(req) and finish_item(req).
task start_item(uvm_sequence_item item)
task finish_item(uvm_sequence_item item)
If you observes data type in first argument in both the function is uvm_sequence_item.
There are total eight uvm classes.
(1) uvm_driver.svh
(2) uvm_push_driver.svh
(3) uvm_sequencer.svh
(4) uvm_push_sequencer.svh
(5) uvm_sequencer_library.svh
(6) uvm_sequencer_analysis_fifo.svh
(7) uvm_sequence.svh
(8) uvm_sequencer_param_base.svh
These classes are parameterized with uvm_sequence_item not with uvm_transaction.
If you use uvm_transaction instead of uvm_sequence_item then ti will shout an error(set_sequence_id not found which is property of uvm_sequence_item not uvm_transaction ) from uvm_sequencer_param_base.svh.
So, communication of sequence,sequencer and driver is not completed.
In most of the cases which I observe if you have a code in which you are not going to use uvm_sequencer then you can use uvm_transaction it will not shout an error.
But if your code contains uvm_sequener and you use uvm_transaction then it will shout an error (Could not find member 'set_sequence_id').

Note that from uvm_transaction class, uvm_sequence_item as well as uvm_objects are inherited. So, the static behaviour and non-static behaviour (so to say) class hierarchy all starts from uvm_transaction.
Now, I can add whatever functionality I want to, to uvm_sequence_item so that every inheritor of uvm_sequence_item can get this. I can do this without modifying uvm_transction_item. Otherwise, any change in uvm_transaction would lead to unwanted functionality in the component class hierarchy (static behaviour) and can even lead to unintended side effects.
PS: one of the difference between other OOPs languages and SV is that multiple inheritance is not allowed in SV. For example, in case of C++, I can inherit from 2 classes in a new class. This is not allowed in SV. So, the only way one can get the properties of uvm_transaction as well as from uvm_sequence_item is by inheriting from uvm_sequence_item. This maybe the source of your confusion.

Related

Is it better to implement two classes or one class in the following case?

I have a class "Vertex" with 4 attributes and a class "Vertex_" with one attribute. The one attribute in Vertex_ is also in Vertex. Is it a good design to keep the two classes or is it better to program just the class Vertex, although there will be 3 variables, which are not used, when I instantiate an object which needs just the one attribute?
Class Vertex_ is actually somewhat a duplicate of Class Vertex.
I would suggest using inheritance and having Class Vertex inherit the attribute from the parent Class Vertex_ while having the 3 other attributes Class Vertex_ does not have.
TL;DR
This is a question that deserves a very long answer.There are two reasons for inheritance and the reason for doing it can depend on the language being used. One reason is for code reuse. Without knowing anything else about your situation, it would seem you are inheriting simply to reuse an attribute (but I suspect there could be more you will be reusing). But, there are other ways of getting code reuse without inheritance, for example containment, which is often a better way.
A powerful feature of object-oriented programming is the ability to substitute one type of object for another. When a message is sent to that object, the correct method implementation is invoked according the actual type of object receiving the message. This is one type of polymorphism. But in some languages the ability to substitute one object for another is constrained. In Java I can only substitute an instance of class B for an instance of class A if B is a descendant of A. So inheritance becomes important in Java to support polymorphism.
But what does it mean to be able to substitute a B instance for an A instance? Will it work? Class A has established a contract stating what each of its methods requires before you can successfully call it and at the same time states what each method promises to deliver. Will the methods of class B live up to that contract? If not, you really cannot substitute a B for an A and expect the program to run correctly. B may be a subclass of A but it is not a subtype of A (see Liskov substitution principle]).
In a language such as Python, inheritance is not required for polymorphism and coders are more apt to use it as code-reuse mechanism. Nevertheless, some people feel that subclassing should only be used to express subtyping. So, if Vertex_ is only using one of the four attributes it has inherited, I am doubtful that an instance of Vertex_ could be safely substituted for an instance of Vertex. I would not do the inheritance unless the language were C++ and then I would use private inheritance.

When should I use a regular class in Scala?

It seems to me that I can make just about anything using object, trait, abstract class and in rare occasions, case class. Most of this is in the form object extends trait. So, I'm wondering, when should I, if ever, use a plain, standard class?
This is not a right place to ask this question
Looks like you are new Scala
Class is a specification for something(some entity) you want to model . It contains behavior and state
There is only one way to declare so called regular class using keyword class
Both trait and abstract class are used for inheritance.
trait is used for inheritance (generally to put common behavior in there). trait is akin to interface in Java. multiple inheritance possible with traits but not abstract class.
A class can extends one class or abstract class but can mixin any number of traits. Traits can have behavior and state.
case class is a nothing but a class but compiler produces some boilerplate code for us to make things easy and look good.
object is used when you want to declare some class but you want to have single instance of the class in the JVM (remember singleton pattern).
If an object performs stateful computations on its members i.e. its members are declared with vars;
Or, even if its member are only declared with vals but those vals store mutable data structures which can be edited in place, then it should be an ordinary (mutable) class akin to a Java mutable object.
The idiomatic way of using Case classes in Scala is as immutable types i.e. all the constructor arguments are vals. We could use vars but then we lose the advantages of case classes like equality comparisons will break over time.
Some advise from Programming in Scala by Odersky et al on deciding between using traits, abstract classes and concrete classes:
If the behavior will not be reused, then make it a concrete class. It is not reusable behavior after all.
If it might be reused in multiple, unrelated classes, make it a trait.
Only traits can be mixed into different parts of the class hierarchy.
If you want to inherit from it in Java code, use an abstract class.
Since traits with code do not have a close Java analog, it tends to be
awkward to inherit from a trait in a Java class. Inheriting from a
Scala class, meanwhile, is exactly like inheriting from a Java class.
As one exception, a Scala trait with only abstract members translates
directly to a Java interface, so you should feel free to define such
traits even if you expect Java code to inherit from it. See Chapter 29
for more information on working with Java and Scala together.
If you plan to distribute it in compiled form, and you expect outside
groups to write classes inheriting from it, you might lean towards
using an abstract class. The issue is that when a trait gains or loses
a member, any classes that inherit from it must be recompiled, even if
they have not changed. If outside clients will only call into the
behavior, instead of inheriting from it, then using a trait is fine.
If efficiency is very important, lean towards using a class. Most Java
runtimes make a virtual method invocation of a class member a faster
operation than an interface method invocation. Traits get compiled to
interfaces and therefore may pay a slight performance overhead.
However, you should make this choice only if you know that the trait
in question constitutes a performance bottleneck and have evidence
that using a class instead actually solves the problem.
If you still do not know, after considering the above, then start by
making it as a trait. You can always
change it later, and in general using a trait keeps more options open.

Choose between abstract class and interface

Among my two processes' functionality, there is a common function to merge files. I need not going to insist any of the processes to have some methods as interface does. And, also the two processes are independent. So, is it fine I just go with an Abstract class and have the implementation in that abstract class itself? Also I do not need any abstract method.
Inheritance is used when there is IS-A relation between subclass and the base class. I don't think it is the case here. You didn't specify the language, but from your profile I guess you use Java. So if you use an Abstract Class you won't be able to inherit from other, more appropriate class in the future.
Instead of inheritance you can use composition. Which means that you create a regular file merging class which has this method to merge files. And in classes where you want to have this functionality you just instantiate this new file merging class. It lets you inherit from other class in the future.
If you want to inform the world that those classes can merge files (to use polymorphism), and you use Java 8 you can create default method inside an interface and implement this interface without override this default method. But I think composition will be better in this case.

FXCop rule Interface methods should be callable by child types

When running FxCop I get the error that interface methods should be callable by child types.
The resolution states the following:
"Make 'MenuPreview' sealed (a breaking change if this class has previously shipped),
implement the method non-explicitly, or implement a new method that exposes
the functionality of 'IComponentConnector.Connect(int, object)'
and is visible to derived classes."
I get this for all classes the derive from Window or some other UI class. Is this a red herring that I can ignore, or is there something I should be doing?
I think the issue is that if an interface is implemented explicitly, it will be impossible for a derived class to both change the interface behavior and make use of the base-class behavior. A common pattern to get around this difficulty in cases where explicit interface implementation would be required is to have the interface do nothing but call a protected virtual method, and have any derived classes that wish to override the behavior of the interface do so by means of the protected virtual method.
Consider IDisposable.Dispose(). If the code in an explicit implementation were actually responsible for performing the disposal, there would be no way for a derived class to add its own dispose logic except by reimplementing IDisposable, and there would be no way for a class which reimplemented IDisposable to access its parent's Dispose method. Although Microsoft could have had IDisposable.Dispose call a protected function with a different name, it opted to use the same name but add a dummy parameter of type Boolean.

When to use an abstract class with no interface?

Whenever I create an abstract class I tend to create an interface to go along with it and have other code refer to the interface and not the abstract class. Usually when I don't create an interface to start with I regret it (such as having to override all implimented methods to stub the class for unit testing or later down the line new classes don't need any of the implimentation and override everything also finding themselves unable to extend any other class).
At first I tried to distinguish when to use an interface and when to use an abstract class by considering is-a vs able-to but I still would end up suffering later down the line for not making an interface to start with.
So the question is when is it a good idea to only have an abstract class and no interface at all?
When you wish to "give" some base class functionality to derived classes but when this functionality is not sufficient to instantiate a usable class, then go for abstract classes.
When you wish that some classes completely implement a set of methods (a public contract), then it is a convenient to define such contract with interfaces and enforce them onto classes by making them inherit this interface.
In short:
With abstract classes you give some common base functionality to derived classes. No further actions are necessary unless abstract class has some stubs (which have to be implemented down there).
With interfaces you require derived classes to implement a set of functions and you do not pass along any implementation.
So the question is when is it a good idea to only have an abstract class and no interface at all?
When you do not wish to enforce any public contract (a set of methods/properties defined by an interface).
Also when you do not plan to use certain coding techniques like casting object to an interface type (run-time polymorphism) or limit allowed input (some method argument will only accept object of types which implement certain interfaces).
Well, the main case it is useful to have only an abstract class without any interface is to mark a certain type. It is useful to be able to check if an object "is-a" something. These interface "mark" an objet to be of a certain type. Depending on the language you use, different design patterns apply ...
These sort of abstract classes exist in java. You can also use them in C++ with RTTI.
my2c