在ned语言中,可以通过xml和xmldoc设置类型为xml的参数。在cpp文件中,通过par("**").xmlValue()解析出cXMLElement*的元素。
由于帮助文档中的xml例子并不完整,我将其补充完整,如下:
data.xml文件:
Code
<data>
<profile id="gen1">
<element>3</element>
<element>5</element>
</profile>
<profile id="gen2">
<element>9</element>
</profile>
</data>
<data>
<profile id="gen1">
<element>3</element>
<element>5</element>
</profile>
<profile id="gen2">
<element>9</element>
</profile>
</data>
定义ned模型
Code
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.
//
simple TrafGen {
parameters:
xml profile;
// gates:
// output out;
}
module Node {
submodules:
trafGen1 : TrafGen {
profile = xmldoc("data.xml", "data/profile[@id='gen1']");
}
trafGen2 : TrafGen {
profile = xmldoc("data.xml", "data/profile[@id='gen2']");
}
}
network test{
parameters:
@display("i=icon");
submodules:
node1:Node;
connections allowunconnected:
}
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.
//
simple TrafGen {
parameters:
xml profile;
// gates:
// output out;
}
module Node {
submodules:
trafGen1 : TrafGen {
profile = xmldoc("data.xml", "data/profile[@id='gen1']");
}
trafGen2 : TrafGen {
profile = xmldoc("data.xml", "data/profile[@id='gen2']");
}
}
network test{
parameters:
@display("i=icon");
submodules:
node1:Node;
connections allowunconnected:
}
simple Moudle TrafGen的cpp定义如下:
TrafGen.h
Code
#ifndef TRAFGEN_H_
#define TRAFGEN_H_
#include <omnetpp.h>
#include <string>
using namespace std;
class TrafGen:public cSimpleModule {
public:
TrafGen();
virtual ~TrafGen();
protected:
virtual void initialize();
};
#endif /* TRAFGEN_H_ */
#ifndef TRAFGEN_H_
#define TRAFGEN_H_
#include <omnetpp.h>
#include <string>
using namespace std;
class TrafGen:public cSimpleModule {
public:
TrafGen();
virtual ~TrafGen();
protected:
virtual void initialize();
};
#endif /* TRAFGEN_H_ */
TrafGen.cpp
Code
#include "TrafGen.h"
Define_Module(TrafGen)
TrafGen::TrafGen() {
// TODO Auto-generated constructor stub
}
TrafGen::~TrafGen() {
// TODO Auto-generated destructor stub
}
void TrafGen::initialize(){
cXMLElement* element=par("profile").xmlValue();
string str=element->tostr(0);
EV<<getId()<<" string:"<<str<<endl;
}
#include "TrafGen.h"
Define_Module(TrafGen)
TrafGen::TrafGen() {
// TODO Auto-generated constructor stub
}
TrafGen::~TrafGen() {
// TODO Auto-generated destructor stub
}
void TrafGen::initialize(){
cXMLElement* element=par("profile").xmlValue();
string str=element->tostr(0);
EV<<getId()<<" string:"<<str<<endl;
}
最后设置omnetpp.ini文件,则可以运行该例子。
小人本潜水在思源的贴边 ID又多 又有钱 快活乐无边 谁知道站总监 他蛮横不留情面 他勾结站长目无天 占我ID夺我钱 我马甲跟他来翻脸 反被他来把经验减 我同学骂他欺新人 反被他捉进了小黑屋里面 874了一百遍啊一百遍 啊 最后他咬舌自尽 遗恨人间 他还将我和马甲赶出了思源 流落在人间 我为求回思源 无奈行乞在贴前 谁知道站总监他实在太阴险 知道此情形竟派人来暗算将我发文狂删到0篇 小人ID强 残命独留全 可怜马甲他 竟遭删 为求养ID 惟有傍人卖身自作践 一面苦赚钱 一面写诗篇 发誓把名气显 手刃总监意志坚啊 从此总监ID念心间 我永铭记此仇不供戴天 |