|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r77342 - sandbox/icl/libs/xplore/br1/sqlbrowser
From: afojgo_at_[hidden]
Date: 2012-03-15 14:28:06
Author: jofaber
Date: 2012-03-15 14:28:05 EDT (Thu, 15 Mar 2012)
New Revision: 77342
URL: http://svn.boost.org/trac/boost/changeset/77342
Log:
Exploration of boost::graph.
Text files modified:
sandbox/icl/libs/xplore/br1/sqlbrowser/browser.cpp | 7 +
sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel.cpp | 53 ++++++++++++++++++
sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel.h | 116 +++++++++++++++++++++++++++++++++++++++
3 files changed, 173 insertions(+), 3 deletions(-)
Modified: sandbox/icl/libs/xplore/br1/sqlbrowser/browser.cpp
==============================================================================
--- sandbox/icl/libs/xplore/br1/sqlbrowser/browser.cpp (original)
+++ sandbox/icl/libs/xplore/br1/sqlbrowser/browser.cpp 2012-03-15 14:28:05 EDT (Thu, 15 Mar 2012)
@@ -88,10 +88,13 @@
int dbg_size = xpQuery.size();
// Populate the Dag Model from an sql-Query
- dagmo->fromSql(xpQuery);
+ //JODO dagmo->fromSql(xpQuery);
+ dagmo->getEdges(xpQuery);
+ dagmo->makeDag();
QMessageBox msgBox;
- QString dagStr = dagmo->toString();
+ //QString dagStr = dagmo->toString();
+ QString dagStr = dagmo->dagToString();
msgBox.setText(dagStr);
msgBox.exec();
Modified: sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel.cpp
==============================================================================
--- sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel.cpp (original)
+++ sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel.cpp 2012-03-15 14:28:05 EDT (Thu, 15 Mar 2012)
@@ -9,6 +9,8 @@
#include "dagitem.h"
#include "dagmodel.h"
+using namespace boost;
+
DagModel::DagModel(const QStringList &headers, //const QString &data,
QObject *parent)
: QAbstractItemModel(parent)
@@ -260,6 +262,20 @@
}
}
+void DagModel::getEdges(QSqlQuery& query)
+{
+ while(query.next())
+ {
+ //create an edge
+ QVector<QVariant> data;
+ //fill node
+ fillData(data, query);
+ m_edges.append(data);
+ }
+}
+
+
+
//JODO Populate a DagModel from an sql-query that provides the DAG as
// (ParentId -> ChildId), ParentName, ChildName, ChildType
void DagModel::fromSql(QSqlQuery& query)
@@ -273,6 +289,41 @@
}
}
+void DagModel::makeDag()
+{
+ m_nameTags = get(name_tag(), m_dag);
+
+ for(tEdgeList::iterator iter = m_edges.begin(); iter != m_edges.end(); iter++)
+ {
+ int source = (*iter)[m_parentId].toInt();
+ int target = (*iter)[m_childId].toInt();
+ if(!(source==0 && target==0))
+ {
+ boost::add_edge(source, target, m_dag);
+ m_nameTags[source] = (*iter)[m_parentName].toString();
+ m_nameTags[target] = (*iter)[m_childName].toString();
+ }
+ }
+}
+
+QString DagModel::dagToString()
+{
+ QString dagst;
+
+ boost::depth_first_search(
+ m_dag
+ , boost::visitor(make_dfs_visitor(boost::make_list(
+ node_arrival(&dagst, m_nameTags)
+ , edge_visit(&dagst, m_nameTags)
+ , node_final(&dagst, m_nameTags)
+ )
+ ))
+ );
+
+ return dagst;
+}
+
+
// The result indicates: False: No more data. True: Data available.
DagItem* DagModel::fromSql(QSqlQuery& query, DagItem* parent, int depth)
@@ -303,7 +354,9 @@
//While records available: Read children.
DagItem* curChild;
while((curChild = fromSql(query, curNode, depth+1)) != NULL)
+ {
curNode->addChild(curChild);
+ }
}
return curNode;
Modified: sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel.h
==============================================================================
--- sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel.h (original)
+++ sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel.h 2012-03-15 14:28:05 EDT (Thu, 15 Mar 2012)
@@ -6,6 +6,11 @@
#ifndef DAGMODEL_H
#define DAGMODEL_H
+#include <boost/graph/graph_traits.hpp>
+#include <boost/graph/adjacency_list.hpp>
+#include <boost/graph/depth_first_search.hpp>
+#include <boost/graph/graph_utility.hpp>
+
#include <QAbstractItemModel>
#include <QModelIndex>
#include <QVariant>
@@ -54,10 +59,15 @@
bool removeRows(int position, int rows,
const QModelIndex &parent = QModelIndex());
-
+ //--------------------------------------------------------------------------
//JOFA Populating a DAG from an Sql-query
+ void getEdges(QSqlQuery& query);
void fromSql(QSqlQuery& query);
+ void makeDag();
+
+ QString dagToString();
+
DagItem* fromSql(QSqlQuery& query, DagItem* node, int depth);
//JOFA Iteration example: The container as String
@@ -83,6 +93,110 @@
int m_childName ;
int m_childType ;
+ typedef QVector<QVariant> tEdgeData;
+ typedef QList<tEdgeData> tEdgeList;
+ tEdgeList m_edges;
+
+ //==========================================================================
+ // Graph
+ struct name_tag {
+ typedef boost::vertex_property_tag kind;
+ };
+
+ typedef boost::property<name_tag, QString> tNameTag;
+
+ typedef boost::adjacency_list
+ < boost::vecS
+ , boost::vecS
+ , boost::directedS
+ , tNameTag
+ > tDag;
+
+ tDag m_dag;
+
+ typedef boost::property_map<tDag, name_tag>::type
+ tNameTagMap;
+
+ tNameTagMap m_nameTags;
+
+ //--------------------------------------------------------------------------
+ // Visitors
+ struct node_arrival : public boost::base_visitor<node_arrival>
+ {
+ node_arrival(QString* result, const tNameTagMap& names)
+ : p_result(result), r_names(names){}
+
+ typedef boost::on_discover_vertex event_filter;
+
+ template<class Vertex, class Graph>
+ void operator()(Vertex node, Graph& dag)
+ {
+ if(boost::out_degree(node, dag) > 0)
+ {
+ *p_result += "(";
+ *p_result += r_names[node];
+ *p_result += "\n";
+ }
+ }
+
+ //CL Example for iterating over edges.
+ template<class Vertex, class Graph>
+ int edge_count(Vertex node, Graph& dag)
+ {
+ typedef graph_traits<Graph> GraphTraits;
+ typename GraphTraits::out_edge_iterator out_i, out_end;
+ typename GraphTraits::edge_descriptor ed;
+
+ int edge_cnt = 0;
+ for(boost::tie(out_i, out_end) = boost::out_edges(node, dag); out_i != out_end; ++out_i)
+ ++edge_cnt;
+
+ return edge_cnt;
+ }
+
+ QString* p_result;
+ const tNameTagMap& r_names;
+ };
+
+ struct edge_visit : public boost::base_visitor<edge_visit>
+ {
+ edge_visit(QString* result, const tNameTagMap& names)
+ : p_result(result), r_names(names){}
+
+ typedef boost::on_examine_edge event_filter;
+
+ template<class Edge, class Graph>
+ void operator()(Edge edge, Graph& dag)
+ {
+ *p_result += " ->" + r_names[target(edge, dag)];
+ *p_result += "\n";
+ }
+
+ QString* p_result;
+ const tNameTagMap& r_names;
+ };
+
+ struct node_final : public boost::base_visitor<node_final>
+ {
+ node_final(QString* result, const tNameTagMap& names)
+ : p_result(result), r_names(names){}
+
+ typedef boost::on_finish_vertex event_filter;
+
+ template<class Vertex, class Graph>
+ void operator()(Vertex node, Graph& dag)
+ {
+ if(boost::out_degree(node, dag) > 0)
+ {
+ *p_result += ")";
+ *p_result += "\n";
+ }
+ }
+
+ QString* p_result;
+ const tNameTagMap& r_names;
+ };
+
};
#endif
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk