在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>

定义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:
}

 

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_ */

 

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;
}

 

最后设置omnetpp.ini文件,则可以运行该例子。

posted on 2009-08-26 09:50  CUCmehp(likesmiles)  阅读(623)  评论(1编辑  收藏  举报