check_and_cast() error in Omnet++ - omnet++

I'm simulating network in Omnet++, with INET framework. I want to get the position (coordination x & y) of node. So I do this code:
cModule *host = getContainingNode(this);
IMobility *mobility = check_and_cast<IMobility *>(host->getSubmodule("mobility"));
... = mobility -> getCurrentPosition();
But, when I ran the simulation, I got this error
check_and_cast(): cannot cast nullptr to type 'inet::IMobility *'
Can you explain to me this error?
As I see, if the simulator notify that, so host->getSubmodule("mobility") is nullptr?
By the way, I have define mobilityType in the NED file and include IMobility.h

The NED code you shared shows that you have a simple module that implements the IMobility interface, while the C++ code looks like something that is inside a simple module which is INSIDE a compound module that represent the network host and this compound module also contains a separate simple module called "mobility" that implements the IMobility interface. (this is how code is implemented in INET).
You should either:
arrange your code so you have a compound module as a networkNode with several simple modules inside it (one of them is the mobility module).
if you insist having a simple module which already implements the IMobility interface (in C++) then you already have access to the position on that object using the getCurrentPosition method.
I assume (and recommend) that you co on the first route, so you should reaarange your NED file.

Related

Calling function of Exe project in referenced aticvex project in vb6

Calling function of Exe project in referenced aticvex project in vb6
I have one AtiveX project in VB6 (Lets call it A )
i have another Exe project in VB6 (Lets call it B )
Project A is referenced in Project B
but now i have a requirement that i want to call function of B inside A
is there a way Can we do it in VB6?
if yes what do i need to search.Already tried searching on Google but not sure i could explain what to search
As i didn't work much on VB6 so i am kind a stuck here what to do
First off let me say that when you run into this problem it is often a sign that the code organization could be improved. Perhaps some modules in A actually should be in B, or they could be broken down in a more modular way that would resolve the problem. I would first encourage you to think about the problem this way.
That said, there are technical ways of doing what you want, I've suggested some examples below.
One approach to this is to utilize an interface. The interface would be provided by A.dll and the class used to implement it is provided by B.exe.
This is rough pseudocode just to sketch out the concept.
Within project A:
Create a class to act as an interface (called ISample). Within ISample have at least one sub or function defined, so you would have ISample.SomeProcedure.
Elsewhere in project A, you need some property or procedure SomeClassInA.RunSampleFunction which will accept an object of type ISample and then call SomeFunction.
Within project B:
Create another class SampleImplementation which Implements the ISample interface. This means it will have an actual concrete implementation of SomeFunction.
From B, create a new object of type SampleImplementation and then pass it to the code in A:
Dim impl As SampleImplementation
Dim objectFromA As SomeClassInA
Set impl = New SampleImplementation
Set objectFromA As New SomeClassInA
' Pass the object from B to A, where A can call its methods:
objectFromA.SomeProcedure impl
This avoids circular references and is a generally object-oriented pattern which I've used before. I will note however that the interface concept in VB6 has some annoyances that you'll have to live with, but for something like this it can be a good tool.
A different approach would be to have a class in A which exposes an event. You can then declare an object from that class in B using WithEvents and attach an event handler to the class. The event handler is just a procedure which exists in B, but now can be called by A.
Events in VB6 also have some limitations (specifically that the WithEvents object has to be within global scope IIRC) but you can usually live with / work around those problems as well.
The interface approach may be more general and a more powerful way to share information between A and B, but the event approach could be quicker to get working. Depends a lot on the specifics of what you need to accomplish.

Why use clone() of ReLU in torch's inception net?

I'm reading https://github.com/Element-Research/dpnn/blob/master/Inception.lua
You can see tons of clone()'s in this source. like
mlp:add(self.transfer:clone())
self.transfer is nothing more than nn.ReLU().
Then,
Why does this code call activation functions using clone()? Does this only concern memory issues?
I thought that the clone shares parameters. Is this right? If it's right, it means all activations of this inception module share parameters. It looks like nonsense. Do I misunderstand Inception-Net?
If you don't clone the module self.transfer then all modules transfer in your net mlp will have the same state variables output and gradInput.
Look for example at this toy code
require 'nn'
module = nn.ReLU()
net = nn.Sequential():add(nn.Linear(2,2)):add(module):add(nn.Linear(2,1)):add(module)
input = torch.Tensor(2,2):random()
net:forward(input)
print(net:get(2).output)
print(net:get(4).output)
Both print statements will return the same value. Modifying one of the module outputs will modify the other one. Since we do not want this behavior we have to clone the module. (However in your case, cloning a simple nn.ReLU() is not that useful.)
The documentation says
If arguments are provided to the clone(...) function it also calls share(...) with those arguments on the cloned module after creating it, hence making a deep copy of this module with some shared parameters.
Therefore if you don't provide any arguments the parameters won't be shared.

'like'keyword in NED language

Hi can anyone tell me the use of 'like' keyword in NED Language. And how does it exactly work.
I was trying to understand castalia code and found this:
simple ValueReporting like node.application.iApplication {
}
Like is used to define 'interfaces' that describe how a module looks like from the outside (i.e. you can define the number and name of gates in an interface). Modules can implement an interface using the like keyword (using the syntax in your question).
The advantage of this is, that when you use this interface in any other compound module, you don't have to specify the exact type of the module. Later you can drop into that place any module that implements the given interface (because from the outside, that module looks exactly the same as the interface itself).
Still, the best place to look for information is the OMNeT++ manual: https://omnetpp.org/doc/omnetpp/manual/#sec:ned-lang:submodule-like

Error in submodule connection in OMNeT++

I have two submodules in compound module. I try to connect them using gate, but it creates this error:
Error in module(cmodule) wnetwork.host0 during network setup.
Gate 'wnetwork.host[0].gate$i[0]' is not connected to a submodule(or
internally to another gate of the same module)
My compound module code is
wirelessnode.ned:
package core;
import org.mixim.modules.power.battery.BatteryStats;
import org.mixim.modules.power.battery.SimpleBattery;
module wirelessnode
{
parameters:
volatile double Energy #unit(mW) = default(10.0mW);
double Tx_energy #unit(mW) = default(0.8mW);
double Tx_interval #unit(s) = default(0.5s);
#display("bgb=210,450,white;i=device/palm;i2=status/battery;b=40,40,rect");
submodules:
batteryStats: BatteryStats {
#display("p=110,221;i=block/table,#FF8040");
}
battery: SimpleBattery {
#display("p=101,90;i=block/plug,#FF8000");
}
}
wirelessnodehost.ned:
package core;
module wirelessnodehost extends wirelessnode
{
gates:
inout gate[];
}
wnetwork.ned:
package core;
import core.wirelessnodehost;
network wnetwork
{
parameters:
int numDevices=default(3);
submodules:
host[numDevices]: wirelessnodehost;
connections:
for i = 0..numDevices-2 {
host[i].gate++ <--> host[i+1].gate++;
}
}
How to solve this error?
If you check the error message, it says: wnetwork.host[0].gate$i[0] is not connected to a submodule (or internally to another gate of the same module). (see emphasis), so the problem is NOT that connecting the two module is wrong somehow, but instead the internals of the wirelessnodehost are incorrect.
Specifically, you are defining the wirelessnodehost as a compound module (a module built up from other modules by connecting them together, while you do not specify any submodules in it). I assume that you have some C++ code for the wirelessnodehost. In this case you must define it as simple wirelessnodehost. Only simple modules have a corresponding code and they are allowed to process the incoming messages using their code. On the other hand compound modules should always pass the incoming message to a submodule for processing, that's why the runtime complaining. You have not connected the gate internally so the runtime does not know where to pass the incoming message.
As a side note, the fact that you extend wirelessnode module (which itself should have a corresponding C++ code and should be defined as 'simple') is rather suspicious. If the code that handles the behavior of the node is implemented in the wirelessnode class, then it does not know anything about the gate that is defined in the wirelessnodehost. I would suggest to take a deeper look at the part of the OMNeT++ manual which describes the differences between simple and compound modules.

Getting the location of two different modules

I have 2 types of submodules in my network.
AdhocHost which is standard definition in INET framework.
module of type IMobility with its mobility type
of type RandomWPMobility, both are standard INET modules.
I can get the location of modules of type AdhocHost by this code:
module = simulation.getModuleByPath("Mynet.host[1]");
c = MobilityAccess().get(module)->getCurrentPosition();
host[*] being of type AdhocHost.
But when I replace host[1] with blockage[1] (blockage is of type IMobility), before running simulation this error appears:
Error in module (MyMobileController) Mynet.mymobilecontroller during
network initialization: Model error: module (IMobility)mobility not
found.
The module mymobilecontroller is the module that contains this fraction of code. This module does not need to move. Its duty is just to record the location of mobile modules in the network.
What is the problem?
The 'MobilityAccess' code expects that you pass a NetworkNode to it that contains a submodule named mobility with the type IMobility. In the first case it is true (with a StandardHost) however in the second case you pass directly an module that has a type of IMobility.
Long story short, a module with a type of IMobility is meant to exist INSIDE of a network node and not at the network level.
Solution: I'm not sure what is the blockage module supposed to do, but it should NOT be a type of IMobility, instead it should be a module that contains a submodule with a type IMobility.

Resources