How to test the current instance name? - instance

I need to use the current instance name (full hierarchical name) in a generate if condition:
generate
if (current_instance_name() == "a.b.c.foo")
...
Is there any way to do that in Verilog or SystemVerilog?
I know %m, but it only allows to print the instance name.
Thanks.

The way to do this is
localparam string current_path = $sformatf("%m");
generate
if (current_path == "a.b.c.foo") begin ...

You can still use %m by passing it to $psprintf which will return a string. The following function should solve your problem:
function string current_instance_name();
return $psprintf("%m");
endfunction

Related

String concatenation in stringtemplate

I'm trying to make a lookup to a hash table m.notes using concatenated value of position.qid and literal ".qid" like this:
$tag(name="itemId", content=m.notes.(position.qid".qid").itemId)$
I tried different options, but I get runtime error. Can someone correct my syntax?
Put the 2 items in an array. StringTemplate concatenates all items in an array (or as they call it, a multi-valued-attribute) when it executes a ToString() on it.
[position.qid, ".qid"]
So, if position.qid evaluates to "hello", this expression would become
hello.qid.
Not sure whether such concatenation is possible in string template. Why don't you use a different method that could do the concatenation and return the value.
e.g:
position.fullQid instead of position.qid
where,
public String getFullQid(){
return getQid() + ".qid";
}
in template group, I can do like this, first, define a concantenate template:
concantenate(substr)::=""
then use as following
(concantenate([position.qid,".qid"]))

Object of class Codeception\Maybe could not be converted to int

I'm checking out Codeception, and I'm trying to write my own grabber.
In my WebHelper.php:
function grabMaxOffers()
{
return 10;
}
(note: eventually, this will return a dynamic value)
In my TestCept.php file:
$max = $I->grabMaxOffers();
$I->wantToTest("Maximum offers ($max)");
There error I always get is:
PHP Notice: Object of class Codeception\Maybe could not be converted to int in tests/acceptance/TestCept.php on line 21
What am I missing? I wrote two other grabbers (returning strings) that worked fine.
You should not pass parameters into wantTo. wantTo is test name, nothing more. That's why test is failing.
Codeception uses Maybe proxy object while analyzing the test. It mocks strings, array, or anything else. But it should be implicitly converted to string with (string)$max.
Probably you wanted to use comments, and not wantTo statement. You should try amGoingTo method.
Cheers

How can i use powershell embedded parameters?

$b = "a00000"
Now due to a complicated application, i want to access $a00000.ID
The application stores a return object inside a variablename (which is a string value of another)
When i try "$b" it shows the value, but $"$b".ID is an error
${$b}.ID is also an error
$'"$b"'.ID is also an error
How do i access value of $a00000.ID given that previously $b=a00000 ?
(one variable name is assigned by a previous string value)
Give this a try:
(Get-Variable $b -ValueOnly).Id

can Velocity set a default value for a variable when no value found in VelocityContext?

Velocity just print the tag name if no value was found in VelocityContext, ie, $name in my template file, but there is no value for "name" in VelocityContext, so just "$name" was printed. I want Velocity to print a default value if there is no value for the variable, I just tried to extends AbstractCotnext and override internalGet() method, but the return value of internalGet() will be cast to Node object, I don't know how to create a new Node object in my internalGet() method, and also I think this way is very complex.
is there a simple way to set a default value (default value is just a string) ?
thanks.
Not easily for all variables as far as I see, I only managed to do it for some variables specifically as follows:
Template:
#if ( !$somevar )
#set ( $somevar = "mycontent" )
#end
Var is: $somevar
Result:
Var is: mycontent
Create a velocimacro in your template:
#macro(defaultValue $parm)
#if (!$!parm || $!parm == "")
i-like-will
#else
$parm
#end
#end
And call it like this in the same template:
#defaultValue($name)
Check Apache Velocity - Velocity User Guide for more info on velocimacros (and velocity in general).
A little late to the party, but you could also perform a check when defining a variable. I had to compress this to one line to remove excess space in the output, but here is an example from one of my projects:
#set ( $qlDefault = "qlinks" )
#set ( $qlClass = "#if($sharedCtaImage.getChild('path').value != '/')$qlDefault#else$qlDefault full#end" )
Default class is defined, then I check if another, specific value is filled in to determine if I keep the default class or append an additional class. This could also work for swapping out classes as well.
Google around for Velocity ReferenceInsertionEventHandler for a way to do it broadly.
Consider the DisplayTool's alt() method for individual cases (part of the VelocityTools project)
For an empty default value, $!name will do. Otherwise,
#{if}($name)${name}#{else}${default_value}#{end}
See http://velocity.apache.org/engine/2.0/user-guide.html#quiet-reference-notation
There are a couple things you can do short of hacking Velocity internals. Have a look at this question.

How to convert string into integer in the Velocity template?

I have a Velocity template file which has the data from XML. I want to convert the string into integer type.
How can I do that?
Aha! Been there.
#set($intString = "9")
#set($Integer = 0)
$Integer.parseInt($intString)
Doing this uses the java underlying velocity. The $Integer variable is nothing more that a java Integer object which you can then use to access .parseInt
Edit: The above code is for demonstration. Of course there are ways to optimize it.
If you have some control over the velocity context, here's an alternative that alleviates the need to set a variable in the Velocity template.
Context velocityContext = new Context();
velocityContext.put(Integer.class.getSimpleName(), Integer.class);
This allows you to call the static methods of java.lang.Integer in your template using $Integer.parseInt($value) and doesn't rely upon the #set having been called prior to performing the type conversion in the template.
The problem with parseInt is that it throws an exception in case the string is not parseable.
In case you have the NumberTool loaded into your context a better solution than parseInt is the following:
#set($intString = "009")
#set($Integer=$numberTool.toNumber($intString).intValue())
#if($Integer)
## ok
#else
## nok
#end
Sometimes the NumberTool is also loaded as $number.
However, a little drawback is, that the NumberTool simply parses the first number it finds and ignores the rest, so "123a" => 123.
Nice and easy:
#set( $stringToCast = "0" )
$number.toNumber($stringToCast)
$number is the default key name for the NumberTool, but it can be override by specifying a different name in the configuration (for example $numberTool). You have to check what name for NumberTool is used in your Velocity environment.
toNumber method returns:
the object as a Number or null if no conversion is possible
If you want to have explicite an int variable, not a Number object, you can use the intValue method on the result. So the above code will looks like this:
#set( $stringToCast = "0" )
$number.toNumber($stringToCast).intValue()
Of course, you can assign the result to another variable (for example $intVal).
So the full code can look like this:
#set( $stringToCast = "0" )
#set( $intVal = $number.toNumber($stringToCast).intValue() )