【问题标题】:Boost GraphML reader and yEdBoost GraphML 阅读器和 yEd
【发布时间】:2013-07-28 19:12:30
【问题描述】:

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

【问题讨论】:

  • 本质上,主要问题在于 yEd 引入 w.r.t 的 yFiles 扩展。香草 GraphML 格式。我不知道如何处理<key for="edge" id="d10" yfiles.type="edgegraphics"/> 中的 yfiles.type。

标签: boost boost-graph graphml


【解决方案1】:

必须过滤 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 文件。

【讨论】:

    【解决方案2】:

    试试这个解决方法:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多