0

我正在尝试读取 yEd ( yEd ) 生成的 .graphml。我能够读取简单和手动生成的 .graphml 文件,但 yEd 文件包含几个要定义的属性。有没有人有一个运行示例来说明如何处理此类 yEd 文件?

4

2 回答 2

3

必须过滤 yED 文件以删除 boost::read_graphml 无法识别的所有 yEd 内容。如果你想要的只是顶点和边,这很简单。但是,如果您确实需要某些属性,那么它会变得更加复杂,并且取决于您需要什么。

这是一些过滤掉所有 yED 内容的代码,节点标签的文本除外,它被转换为 boost::read_graphml 可以解析并存储在捆绑属性中的最简单的节点标签属性。

/**

  Check for a yEd file

  @param[in] n  the filename
  @return true if the file weas written by yED

  The input file is copied to a new file graphex_processed.graphml
  If the intput file was NOT produced by yEd, then the copy is perfect
  If input was produced by yEd then the copy is filtered so that it can be
  read by boost::read_graphml
  Most of the yEd stuff is discarded, except for the node labels
  the text of which are copied to a simple node attribute "label"

*/

bool cGraph::IsGraphMLbyYED(const std::wstring& n)
{
    bool yEd = false;

    // open the input file
    std::ifstream fin;
    fin.open(n.c_str(), std::ifstream::in);
    if( ! fin.is_open() ) {
        return false;
    }
    // open the output file
    std::ofstream fout;
    fout.open("graphex_processed.graphml", std::ifstream::out );
    if( ! fout.is_open() ) {
        return false;
    }


    // loop over input file lines
    fin.clear();
    char buf[1000];
    while( fin.good() ) {
        fin.getline( buf,999 );
        std::string l( buf ); 

        // check for file produced by yEd
        if( l.find("<!--Created by yFiles") != -1 ) {
            yEd = true;
            // convert NodeLabel text to simple label attribute
            fout << "<key id=\"key0\" for=\"node\" attr.name=\"label\" attr.type=\"string\" />\n";
        }

        // check for file already identified as yEd
        if( yEd ) {

            // filter out yED attributes
            if( l.find("<key") != -1 ) {
                continue;
            }
            // convert NodeLabel text
            if( l.find("<y:NodeLabel") != -1 ) {
                int p = l.find(">")+1;
                int q = l.find("<",p);
                std::string label = l.substr(p,q-p);
                fout << "<data key=\"key0\">" << label << "</data>\n";
                continue;
            }

            // filter out outher yEd stuff
            if( l.find("<y:") != -1 ) {
                continue;
            }
            if( l.find("</y:") != -1 ) {
                continue;
            }
            if( l.find("<data") != -1 ) {
                continue;
            }
            if( l.find("</data") != -1 ) {
                continue;
            }
        }
        // copy input line to output
        fout << buf << std::endl;
    }

    // close files
    fin.close();
    fout.close();

    // return true if yED file
    return yEd;

}

这是一些读取过滤文件的代码

void cGraph::ReadGraphML(const std::wstring& n)
{
    // check if file was produced by yEd
    IsGraphMLbyYED( n ); 

    boost::dynamic_properties dp;
    dp.property("label", boost::get(&cVertex::myName, myGraph));


    myGraph.clear();
    std::ifstream fin;
    fin.open("graphex_processed.graphml", std::ifstream::in);
    if( ! fin.is_open() ) {
        return;
    }
    boost::read_graphml( fin, myGraph, dp );
}

如果您想查看在应用程序中运行的示例,请查看Graphex,这是 BGL 的 GUI,它可以使用此代码读取 yEd 文件。

于 2013-08-01T15:23:04.397 回答
-1

试试这个解决方法:

https://stackoverflow.com/a/55807107/4761831

我只是继承了一个类并删除了一些导致异常的代码。

于 2019-04-23T08:30:47.250 回答