Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r79838 - in sandbox/icl/libs/xplore/br1/sqlbrowser: . Dag
From: afojgo_at_[hidden]
Date: 2012-08-01 13:56:55


Author: jofaber
Date: 2012-08-01 13:56:53 EDT (Wed, 01 Aug 2012)
New Revision: 79838
URL: http://svn.boost.org/trac/boost/changeset/79838

Log:
Adapting visitor for dag representation.
Added:
   sandbox/icl/libs/xplore/br1/sqlbrowser/CreatorVisitor2.h (contents, props changed)
   sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel2.cpp (contents, props changed)
   sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel2.h (contents, props changed)
Binary files modified:
   sandbox/icl/libs/xplore/br1/sqlbrowser/objects1.db
Text files modified:
   sandbox/icl/libs/xplore/br1/sqlbrowser/CreatorVisitor.h | 141 ----------------------------------------
   sandbox/icl/libs/xplore/br1/sqlbrowser/Dag.h | 21 ++---
   sandbox/icl/libs/xplore/br1/sqlbrowser/Dag/DbBasedGraph.h | 78 ++++++++++++++++++++-
   sandbox/icl/libs/xplore/br1/sqlbrowser/Dag/DecoratedGraph.h | 19 ++++
   sandbox/icl/libs/xplore/br1/sqlbrowser/Dag/ObjectGraph.h | 2
   sandbox/icl/libs/xplore/br1/sqlbrowser/Dag/TypeGraph.h | 1
   sandbox/icl/libs/xplore/br1/sqlbrowser/StringVisitor2.h | 3
   sandbox/icl/libs/xplore/br1/sqlbrowser/browser.cpp | 5 +
   sandbox/icl/libs/xplore/br1/sqlbrowser/sqlbrowser4.pro | 7 +
   9 files changed, 110 insertions(+), 167 deletions(-)

Modified: sandbox/icl/libs/xplore/br1/sqlbrowser/CreatorVisitor.h
==============================================================================
--- sandbox/icl/libs/xplore/br1/sqlbrowser/CreatorVisitor.h (original)
+++ sandbox/icl/libs/xplore/br1/sqlbrowser/CreatorVisitor.h 2012-08-01 13:56:53 EDT (Wed, 01 Aug 2012)
@@ -156,145 +156,4 @@
 }; // CreatorVisitor
 
 //------------------------------------------------------------------------------
-//------------------------------------------------------------------------------
-struct CreatorVisitor2
-{
- //--------------------------------------------------------------------------
- // Visitors
- struct OnDiscoverVertex : public boost::base_visitor<OnDiscoverVertex>
- {
- OnDiscoverVertex(DagItem* curItem, QString* result, Dag::tAttributesMap& attrs)
- : p_curItem(curItem), p_result(result), r_attrs(attrs)
- {
- r_attrs[0].setDagItem(p_curItem); //Root node
- r_attrs[0].setParentItem(0); //Root node
- }
-
- typedef boost::on_discover_vertex event_filter;
-
- template<class Vertex, class Graph>
- void operator()(Vertex node, Graph& dag)
- {
- // "Visitation by descent". All nodes are reached through this function on
- // descend, which is always done first for "depth first search. Therefore
- // we can create all nodes for the Qt DagModel in this functor:
- //
- // Create a DagItem. The node that has been visited last should be the parent.
- // While we are descending, we build a chain going "down".
-
- tVariVector itemData(dag::node::sizeOf_node); // ItemData node(id, name, ..)
- // will only by a part of the data from sql that represented
- // edges. Via r_attrs we only obtain associated node data
- // from the boost::graph
- dag::copyBoostNode2DagItem(r_attrs[node], itemData);
-
- // Discoverage is always on the way down. So we should maintain the invariant
- // p_curItem should be initialized to the DagModel's DatItem* rootItem
-
- if(boost::out_degree(node, dag) > 0)
- {
- *p_result += indentation(r_attrs[node].depth()) + "(";
- *p_result += r_attrs[node].name();
- *p_result += QString(" d:%1").arg(r_attrs[node].depth());
- *p_result += " -> " + (p_curItem==0 ? QString("{}") : p_curItem->data()[dag::node::posName].toString());
- *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;
- }
-
- DagItem* p_curItem;
- QString* p_result;
- Dag::tAttributesMap& r_attrs;
- };
-
- struct OnExamineEdge : public boost::base_visitor<OnExamineEdge>
- {
- OnExamineEdge(DagItem* curItem, QString* result, Dag::tAttributesMap& names)
- : p_curItem(curItem), p_result(result), r_attrs(names)
- {
- //CL r_attrs[0].setDagItem(p_curItem); //Root node
- }
-
- typedef boost::on_examine_edge event_filter;
-
- template<class Edge, class Graph>
- void operator()(Edge edge, Graph& dag)
- {
- Dag::vertex_descriptor source_node = source(edge, dag);
- Dag::vertex_descriptor target_node = target(edge, dag);
- int source_depth = r_attrs[source_node].depth();
- int target_depth = source_depth + 1;
- r_attrs[target_node].setDepth(target_depth);
- DagItem* sourceDagItem = r_attrs[source_node].dagItem();
- DagItem* targetDagItem = r_attrs[target_node].dagItem();
-
- Q_ASSERT(sourceDagItem);
-
- if(p_curItem != sourceDagItem)
- p_curItem = sourceDagItem;
-
- if(targetDagItem)
- sourceDagItem->addChild(targetDagItem);
- else
- {
- tVariVector itemData(dag::node::sizeOf_node);
- dag::copyBoostNode2DagItem(r_attrs[target_node], itemData);
- DagItem* newDagItem = new DagItem(itemData, p_curItem);
- sourceDagItem->addChild(newDagItem);
- r_attrs[target_node].setDagItem(newDagItem);
- newDagItem->setData(dag::node::posParentId, newDagItem->parent()->data(dag::node::posId));
- newDagItem->setData(dag::node::posParentName, newDagItem->parent()->data(dag::node::posName));
- }
-
- if(boost::out_degree(target(edge, dag), dag)==0)
- {
- *p_result += indentation(target_depth) + r_attrs[target(edge, dag)].name() + " ?";
- *p_result += "\n";
- }
- }
-
- DagItem* p_curItem;
- QString* p_result;
- Dag::tAttributesMap& r_attrs;
- };
-
- struct OnFinishVertex : public boost::base_visitor<OnFinishVertex>
- {
- OnFinishVertex(DagItem* curItem, QString* result, Dag::tAttributesMap& names)
- : p_curItem(curItem), p_result(result), r_attrs(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 += indentation(r_attrs[node].depth()) + ")";
- *p_result += "\n";
- }
- }
-
- DagItem* p_curItem;
- QString* p_result;
- Dag::tAttributesMap& r_attrs;
- };
-
-}; // CreatorVisitor2
 

Added: sandbox/icl/libs/xplore/br1/sqlbrowser/CreatorVisitor2.h
==============================================================================
--- (empty file)
+++ sandbox/icl/libs/xplore/br1/sqlbrowser/CreatorVisitor2.h 2012-08-01 13:56:53 EDT (Wed, 01 Aug 2012)
@@ -0,0 +1,168 @@
+/****************************************************************************
+**
+**
+****************************************************************************/
+
+#pragma once
+
+#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 "Dag/DbBasedGraph.h"
+//CL #include "Dag.h" //JODO remove
+
+#include <QDebug>
+
+class DagItem;
+
+//------------------------------------------------------------------------------
+template<class DbGraph>
+struct CreatorVisitor2
+{
+ typedef typename DbGraph::vertex_descriptor vertex_descriptor;
+ typedef typename DbGraph::tVertex2Depth tVertex2Depth;
+
+ //JODO Auxiliary Map. Could be optimized away.
+ typedef typename DbGraph::Vertex2AttributesMap Vertex2AttributesMap;
+
+ //--------------------------------------------------------------------------
+ // Visitors
+ struct OnDiscoverVertex : public boost::base_visitor<OnDiscoverVertex>
+ {
+ OnDiscoverVertex(DagItem* curItem, QString* result, Vertex2AttributesMap& attrs)
+ : p_curItem(curItem), p_result(result), r_attrs(attrs)
+ {
+ //JODO redundant?
+ r_attrs[0].setDagItem(p_curItem); //Root node
+ r_attrs[0].setParentItem(0); //Root node
+ }
+
+ typedef boost::on_discover_vertex event_filter;
+
+ template<class Vertex, class Graph>
+ void operator()(Vertex node, Graph& dag)
+ {
+ // "Visitation by descent". All nodes are reached through this function on
+ // descend, which is always done first for "depth first search. Therefore
+ // we can create all nodes for the Qt DagModel in this functor:
+ //
+ // Create a DagItem. The node that has been visited last should be the parent.
+ // While we are descending, we build a chain going "down".
+
+ tVariVector itemData(dag::node::sizeOf_node); // ItemData node(id, name, ..)
+ // will only by a part of the data from sql that represented
+ // edges. Via r_attrs we only obtain associated node data
+ // from the boost::graph
+ //JODO dag::copyBoostNode2DagItem(r_attrs[node], itemData);
+ //CL itemData[dag::node::posId] = QVariant(dag[node].key());
+ //CL itemData[dag::node::posName] = QVariant(dag[node].name());
+
+ dbg_str = QString("(%1)[%2] %3").arg(node).arg(dag[node].key()).arg(dag[node].name());
+
+ // Discoverage is always on the way down. So we should maintain the invariant
+ // p_curItem should be initialized to the DagModel's DatItem* rootItem
+ Q_ASSERT(r_attrs[node].dagItem() != 0);
+
+ if(boost::out_degree(node, dag) > 0)
+ {
+ *p_result += indentation(r_attrs[node].depth()) + "(";
+ *p_result += dag[node].name();
+ *p_result += QString(" d:%1").arg(r_attrs[node].depth());
+ *p_result += " -> " + (p_curItem==0 ? QString("{}") : p_curItem->data()[dag::node::posName].toString());
+ *p_result += "\n";
+ }
+ }
+
+ DagItem* p_curItem;
+ QString* p_result;
+ Vertex2AttributesMap& r_attrs;
+ QString dbg_str;
+ };
+
+ struct OnExamineEdge : public boost::base_visitor<OnExamineEdge>
+ {
+ OnExamineEdge(DagItem* curItem, QString* result, Vertex2AttributesMap& attrs)
+ : p_curItem(curItem), p_result(result), r_attrs(attrs)
+ {
+ //CL r_attrs[0].setDagItem(p_curItem); //Root node
+ }
+
+ typedef boost::on_examine_edge event_filter;
+
+ template<class Edge, class Graph>
+ void operator()(Edge edge, Graph& dag)
+ {
+ vertex_descriptor source_node = source(edge, dag);
+ dbg_str = QString("(%1)[%2] %3").arg(source_node).arg(dag[source_node].key()).arg(dag[source_node].name());
+ vertex_descriptor target_node = target(edge, dag);
+ dbg_str = QString("(%1)[%2] %3").arg(target_node).arg(dag[target_node].key()).arg(dag[target_node].name());
+
+ int source_depth = r_attrs[source_node].depth();
+ int target_depth = source_depth + 1;
+
+ r_attrs[target_node].setDepth(target_depth);
+ DagItem* sourceDagItem = r_attrs[source_node].dagItem();
+ DagItem* targetDagItem = r_attrs[target_node].dagItem();
+
+ Q_ASSERT(sourceDagItem);
+
+ if(p_curItem != sourceDagItem)
+ p_curItem = sourceDagItem;
+
+ if(targetDagItem)
+ sourceDagItem->addChild(targetDagItem);
+ else
+ {
+ tVariVector itemData(dag::node::sizeOf_node);
+ //JODO dag::copyBoostNode2DagItem(r_attrs[target_node], itemData);
+ itemData[dag::node::posId] = QVariant(dag[target_node].key());
+ itemData[dag::node::posName] = QVariant(dag[target_node].name());
+
+ DagItem* newDagItem = new DagItem(itemData, p_curItem);
+ sourceDagItem->addChild(newDagItem);
+ r_attrs[target_node].setDagItem(newDagItem);
+ newDagItem->setData(dag::node::posParentId, newDagItem->parent()->data(dag::node::posId));
+ newDagItem->setData(dag::node::posParentName, newDagItem->parent()->data(dag::node::posName));
+ }
+
+ if(boost::out_degree(target(edge, dag), dag)==0)
+ {
+ *p_result += indentation(target_depth) + dag[target(edge, dag)].name() + " ?";
+ *p_result += "\n";
+ }
+ }
+
+ DagItem* p_curItem;
+ QString* p_result;
+ Vertex2AttributesMap& r_attrs;
+ QString dbg_str;
+ };
+
+ struct OnFinishVertex : public boost::base_visitor<OnFinishVertex>
+ {
+ OnFinishVertex(DagItem* curItem, QString* result, Vertex2AttributesMap& names)
+ : p_curItem(curItem), p_result(result), r_attrs(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 += indentation(r_attrs[node].depth()) + ")";
+ *p_result += "\n";
+ }
+ }
+
+ DagItem* p_curItem;
+ QString* p_result;
+ Vertex2AttributesMap& r_attrs;
+ };
+
+}; // CreatorVisitor2
+

Modified: sandbox/icl/libs/xplore/br1/sqlbrowser/Dag.h
==============================================================================
--- sandbox/icl/libs/xplore/br1/sqlbrowser/Dag.h (original)
+++ sandbox/icl/libs/xplore/br1/sqlbrowser/Dag.h 2012-08-01 13:56:53 EDT (Wed, 01 Aug 2012)
@@ -49,20 +49,16 @@
     typedef dag::db::tObjectSharedPtr tObjectSharedPtr;
 
 public:
- VertexAttributes(): m_aKey(), m_depth() {}
-
- VertexAttributes(tKey key, const tObjectSharedPtr& pObject) //JODO REMOVE
- : m_aKey(key), m_pObject(pObject), m_depth(), p_dagItem() {}
+ VertexAttributes():
+ m_aKey(0), m_depth(0), p_dagItem(0), p_parentItem(0), m_name(){}
 
     VertexAttributes(tKey key, const QString& aName)
- : m_aKey(key), m_pObject(boost::make_shared<tObject>())
- , m_depth(), p_dagItem()
+ : m_aKey(key), m_depth(), p_dagItem(0), p_parentItem(0), m_name()
     {
- m_pObject->setName(aName);
     }
 
- void setName(const QString& name) { m_pObject->setName(name); }
- QString name()const { return m_pObject->name(); }
+ void setName(const QString& name) { m_name = name; }
+ QString name()const { return m_name; }
 
     void setDepth(int depth){ m_depth = depth; }
     int depth()const { return m_depth; }
@@ -82,14 +78,14 @@
     tKey m_aKey;
     int m_depth;
 
- //---- Db::Object associated to the Vertex ---------------------------------
- dag::db::tObjectSharedPtr m_pObject;
-
     //---- DagModel ------------------------------------------------------------
     DagItem* p_dagItem;
     DagItem* p_parentItem;
+
+ QString m_name;
 };
 
+
 class EdgeAttributes
 {
 public:
@@ -107,6 +103,7 @@
 
 //JODO: m_depth/depth() may be removed. It's currently only needed for debugging.
 
+
 namespace dag
 {
     namespace edge

Modified: sandbox/icl/libs/xplore/br1/sqlbrowser/Dag/DbBasedGraph.h
==============================================================================
--- sandbox/icl/libs/xplore/br1/sqlbrowser/Dag/DbBasedGraph.h (original)
+++ sandbox/icl/libs/xplore/br1/sqlbrowser/Dag/DbBasedGraph.h 2012-08-01 13:56:53 EDT (Wed, 01 Aug 2012)
@@ -5,6 +5,11 @@
 
 #pragma once
 
+#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 <boost/range.hpp>
 
 #include "Dag/Decoration.h"
@@ -12,8 +17,10 @@
 
 #include "Dag/DbType.h"
 #include "StringVisitor2.h"
+#include "CreatorVisitor2.h"
 
 class QSqlQuery;
+class DagModel2;
 
 namespace dag { namespace db
 {
@@ -30,14 +37,41 @@
     //! The DbBasedGraph is a DecoratedGraph
     typedef DecoratedGraph<tVertexDeco, tEdgeDeco> tDbBasedGraph;
     typedef typename DecoratedGraph<tVertexDeco, tEdgeDeco>::type tGraph;
- typedef typename tDbBasedGraph::vertex_descriptor vertex_descriptor;
- typedef typename tDbBasedGraph::edge_descriptor edge_descriptor;
- typedef typename tDbBasedGraph::tVertex2Depth tVertex2Depth;
+ typedef typename tDbBasedGraph::vertex_descriptor vertex_descriptor;
+ typedef typename tDbBasedGraph::edge_descriptor edge_descriptor;
+ typedef typename tDbBasedGraph::adjacency_iterator adjacency_iterator;
+ typedef typename tDbBasedGraph::out_edge_iterator out_edge_iterator;
+ typedef typename tDbBasedGraph::in_edge_iterator in_edge_iterator;
+ typedef typename tDbBasedGraph::vertex_iterator vertex_iterator;
+ typedef typename tDbBasedGraph::edge_iterator edge_iterator;
+
+ typedef typename tDbBasedGraph::directed_category directed_category;
+ typedef typename tDbBasedGraph::edge_parallel_category edge_parallel_category;
+ typedef typename tDbBasedGraph::traversal_category traversal_category;
+
+ typedef typename tDbBasedGraph::vertices_size_type vertices_size_type;
+ typedef typename tDbBasedGraph::edges_size_type edges_size_type;
+ typedef typename tDbBasedGraph::degree_size_type degree_size_type;
+
+ typedef typename tDbBasedGraph::tVertex2Depth tVertex2Depth;
 
     typedef std::map<tKey, vertex_descriptor> tKey2Vertex;
     typedef typename tKey2Vertex::iterator tKey2Vertex_iterator;
     typedef boost::iterator_range<tKey2Vertex_iterator> tKeyVertexRange;
 
+ //--------------------------------------------------------------------------
+ typedef std::map<vertex_descriptor, VertexAttributes> Vertex2AttributesMap;
+
+ //--------------------------------------------------------------------------
+ // Functions progagated from DbBasedGraph.
+ edges_size_type num_edges()const { return boost::num_edges(m_aGraph); }
+
+ std::pair<edge_descriptor, bool> add_edge(vertex_descriptor source, vertex_descriptor target)
+ { return boost::add_edge(source, target, m_aGraph); }
+
+ void clear() { m_aGraph.clear(); }
+ //--------------------------------------------------------------------------
+
     vertex_descriptor addVertex(tKey key)
     {
         Q_ASSERT(key > 0);
@@ -102,23 +136,57 @@
 
     tString depthFirstString()
     {
+ //Add an associative color map type.
+ typedef std::map<DbBasedGraph::vertex_descriptor, boost::default_color_type> color_map_t;
+ color_map_t color_map; //Declare a container
+
         tVertex2Depth vertexDepth;
         QString tygraAsString;
 
+ BGL_FORALL_VERTICES(vtx, m_aGraph, DbBasedGraph) {
+ color_map[vtx] = boost::white_color;
+ }
+
+ //Generate an assoc property map
+ boost::associative_property_map<color_map_t> pm_color(color_map);
+
         boost::depth_first_search(
             m_aGraph
- , boost::visitor(make_dfs_visitor(boost::make_list(
+ , make_dfs_visitor(boost::make_list(
                                                   StringVisitor2<DbBasedGraph>::OnDiscoverVertex (&tygraAsString, vertexDepth)
                                                 , StringVisitor2<DbBasedGraph>::OnExamineEdge (&tygraAsString, vertexDepth)
                                                 , StringVisitor2<DbBasedGraph>::OnForwardOrCrossEdge(&tygraAsString, vertexDepth)
                                                 , StringVisitor2<DbBasedGraph>::OnFinishVertex (&tygraAsString, vertexDepth)
                                                 )
- ))
+ )
+ , pm_color
+ , 15
+
         );
 
         return tygraAsString;
     }
 
+ tString makeDagModel(DagModel2* dagmo)
+ {
+ CreatorVisitor2<DbBasedGraph>::Vertex2AttributesMap vertex2AttrMap;
+ QString graphAsString;
+ DagItem* modelRoot = dagmo->rootItem();
+
+ boost::depth_first_search(
+ m_aGraph
+ , boost::visitor(make_dfs_visitor(boost::make_list(
+ CreatorVisitor2<DbBasedGraph>::OnDiscoverVertex (modelRoot, &graphAsString, vertex2AttrMap)
+ , CreatorVisitor2<DbBasedGraph>::OnExamineEdge (modelRoot, &graphAsString, vertex2AttrMap)
+ // , CreatorVisitor2<DbBasedGraph>::OnForwardOrCrossEdge(modelRoot, &graphAsString, vertex2AttrMap)
+ , CreatorVisitor2<DbBasedGraph>::OnFinishVertex (modelRoot, &graphAsString, vertex2AttrMap)
+ )
+ ))
+ );
+
+ return graphAsString;
+ }
+
 protected:
           tDerived* derived() { return static_cast<tDerived*>(this); }
     const tDerived* derived()const { return static_cast<const tDerived*>(this); }

Modified: sandbox/icl/libs/xplore/br1/sqlbrowser/Dag/DecoratedGraph.h
==============================================================================
--- sandbox/icl/libs/xplore/br1/sqlbrowser/Dag/DecoratedGraph.h (original)
+++ sandbox/icl/libs/xplore/br1/sqlbrowser/Dag/DecoratedGraph.h 2012-08-01 13:56:53 EDT (Wed, 01 Aug 2012)
@@ -41,10 +41,23 @@
> tGraph;
 
     typedef typename tGraph type;
- typedef typename tGraph::vertex_descriptor vertex_descriptor;
- typedef typename tGraph::edge_descriptor edge_descriptor;
+ typedef typename tGraph::vertex_descriptor vertex_descriptor;
+ typedef typename tGraph::edge_descriptor edge_descriptor;
+ typedef typename tGraph::adjacency_iterator adjacency_iterator;
+ typedef typename tGraph::out_edge_iterator out_edge_iterator;
+ typedef typename tGraph::in_edge_iterator in_edge_iterator;
+ typedef typename tGraph::vertex_iterator vertex_iterator;
+ typedef typename tGraph::edge_iterator edge_iterator;
+
+ typedef typename tGraph::directed_category directed_category;
+ typedef typename tGraph::edge_parallel_category edge_parallel_category;
+ typedef typename tGraph::traversal_category traversal_category;
+
+ typedef typename tGraph::vertices_size_type vertices_size_type;
+ typedef typename tGraph::edges_size_type edges_size_type;
+ typedef typename tGraph::degree_size_type degree_size_type;
 
- typedef std::map<vertex_descriptor, int> tVertex2Depth;
+ typedef std::map<vertex_descriptor, int> tVertex2Depth;
 
 };
 

Modified: sandbox/icl/libs/xplore/br1/sqlbrowser/Dag/ObjectGraph.h
==============================================================================
--- sandbox/icl/libs/xplore/br1/sqlbrowser/Dag/ObjectGraph.h (original)
+++ sandbox/icl/libs/xplore/br1/sqlbrowser/Dag/ObjectGraph.h 2012-08-01 13:56:53 EDT (Wed, 01 Aug 2012)
@@ -12,7 +12,7 @@
 namespace dag { namespace db
 {
 
-class ObjectGraph : public DbBasedGraph<TypeGraph, db::Object, db::Edge>
+class ObjectGraph : public DbBasedGraph<ObjectGraph, db::Object, db::Edge>
 {
 };
 

Modified: sandbox/icl/libs/xplore/br1/sqlbrowser/Dag/TypeGraph.h
==============================================================================
--- sandbox/icl/libs/xplore/br1/sqlbrowser/Dag/TypeGraph.h (original)
+++ sandbox/icl/libs/xplore/br1/sqlbrowser/Dag/TypeGraph.h 2012-08-01 13:56:53 EDT (Wed, 01 Aug 2012)
@@ -5,6 +5,7 @@
 
 #pragma once
 
+#include "Dag/DbType.h"
 #include "Dag/DbBasedGraph.h"
 
 class QSqlQuery;

Modified: sandbox/icl/libs/xplore/br1/sqlbrowser/StringVisitor2.h
==============================================================================
--- sandbox/icl/libs/xplore/br1/sqlbrowser/StringVisitor2.h (original)
+++ sandbox/icl/libs/xplore/br1/sqlbrowser/StringVisitor2.h 2012-08-01 13:56:53 EDT (Wed, 01 Aug 2012)
@@ -10,8 +10,7 @@
 #include <boost/graph/depth_first_search.hpp>
 #include <boost/graph/graph_utility.hpp>
 
-#include "Dag/TypeGraph.h"
-#include "Dag.h" //CL move indentation
+#include "Dag/DbBasedGraph.h"
 
 
 template<class DbGraph>

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-08-01 13:56:53 EDT (Wed, 01 Aug 2012)
@@ -43,6 +43,7 @@
 #include "browser.h"
 #include "qsqlconnectiondialog.h"
 #include "dagmodel.h"
+#include "dagmodel2.h"
 
 #include <QtGui>
 #include <QtSql>
@@ -250,7 +251,9 @@
     util::launchMsgBox(ogra.depthFirstString());
 
     //--------------------------------------------------------------------------
- DagModel* dagmo = new DagModel(); // Dag-Model
+ DagModel2* dagmo = new DagModel2(); // Dag-Model
+ //QString dbg_dagString = ogra.makeDagModel(dagmo);
+ //util::launchMsgBox(dbg_dagString);
     //dagmo->setDag(ogra); //Make the Model from a boost::graph internally
     //dagmo->makeDagModel();
     //--------------------------------------------------------------------------

Added: sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel2.cpp
==============================================================================
--- (empty file)
+++ sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel2.cpp 2012-08-01 13:56:53 EDT (Wed, 01 Aug 2012)
@@ -0,0 +1,491 @@
+/****************************************************************************
+**
+**
+****************************************************************************/
+
+#include <QtGui>
+#include <QtSql>
+
+#include "dagitem.h"
+#include "dagmodel2.h"
+#include "StringVisitor.h"
+#include "CreatorVisitor.h"
+
+using namespace boost;
+
+
+//------------------------------------------------------------------------------
+//------------------------------------------------------------------------------
+DagModel2::DagModel2(QObject *parent)
+ : QAbstractItemModel(parent)
+{
+ m_rootItem = createDagItem();
+}
+
+
+DagModel2::~DagModel2()
+{
+ delete m_rootItem;
+}
+
+
+int DagModel2::columnCount(const QModelIndex & /* parent */) const
+{
+ return m_rootItem->columnCount();
+}
+
+DagItem* DagModel2::createDagItem()
+{
+ QVector<QVariant> rootData;
+ rootData.resize(dag::node::sizeOf_node);
+ rootData[dag::node::posId] = QVariant(0);
+ rootData[dag::node::posName] = QVariant("NIL");
+ return new DagItem(rootData);
+}
+
+QVariant DagModel2::data(const QModelIndex &index, int role) const
+{
+ if (!index.isValid())
+ return QVariant();
+
+ if (role != Qt::DisplayRole && role != Qt::EditRole)
+ return QVariant();
+
+ DagItem *item = getItem(index);
+
+ return item->data(index.column());
+}
+
+
+Qt::ItemFlags DagModel2::flags(const QModelIndex &index) const
+{
+ if (!index.isValid())
+ return 0;
+
+ return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
+}
+
+
+DagItem *DagModel2::getItem(const QModelIndex &index) const
+{
+ if (index.isValid()) {
+ DagItem *item = static_cast<DagItem*>(index.internalPointer());
+ if (item) return item;
+ }
+ return m_rootItem;
+}
+
+
+QVariant DagModel2::headerData(int section, Qt::Orientation orientation,
+ int role) const
+{
+ if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
+ return m_rootItem->data(section);
+
+ return QVariant();
+}
+
+
+QModelIndex DagModel2::index(int row, int column, const QModelIndex &parent) const
+{
+ if (parent.isValid() && parent.column() != 0)
+ return QModelIndex();
+
+ DagItem *parentItem = getItem(parent);
+
+ DagItem *childItem = parentItem->child(row);
+ if (childItem)
+ return createIndex(row, column, childItem);
+ else
+ return QModelIndex();
+}
+
+bool DagModel2::insertColumns(int position, int columns, const QModelIndex &parent)
+{
+ bool success;
+
+ beginInsertColumns(parent, position, position + columns - 1);
+ success = m_rootItem->insertColumns(position, columns);
+ endInsertColumns();
+
+ return success;
+}
+
+void DagModel2::insertVertex(QVector<QVariant>& edgeData, const QModelIndex& index)
+{
+ // Insert the vretex (via edge) into the Dag
+ dagInsertVertex(edgeData, index);
+ // Insert the vertex into the DagModel2
+ modelInsertVertex(edgeData, index);
+}
+
+void DagModel2::dagInsertVertex(QVector<QVariant>& edgeData, const QModelIndex& index)
+{
+ int source = edgeData[m_parentId].toInt();
+ int target = edgeData[m_childId].toInt();
+ if(!(source==0 && target==0))
+ {
+ //JOFA MEMO: Build the graph: Add edges.
+ m_dag.add_edge(source, target);
+ //JOFA MEMO: Fill the associated objects. Aka. decorate the graph.
+ m_aVertexAttributes[source] = VertexAttributes(source, edgeData[m_parentName].toString());
+ m_aVertexAttributes[target] = VertexAttributes(target, edgeData[m_childName].toString());
+ }
+}
+
+void DagModel2::modelInsertVertex(QVector<QVariant>& edgeData, const QModelIndex& index)
+{
+ // Create a new vertex or DagItem and append it a the node inicated by 'index'
+ // PRE: index is valid and points to the parent node of insertion.
+
+ //fill node data. The target node is 'new'
+ fillDummyData(edgeData, index.data().toInt());
+
+ DagItem* parentNode = item(index);
+ Q_ASSERT(parentNode != 0);
+
+ tVariVector childData(dag::node::sizeOf_node);
+ childData[dag::node::posId] = edgeData[dag::edge::posChildId];
+ childData[dag::node::posParentId] = edgeData[dag::edge::posParentId];
+ childData[dag::node::posName] = edgeData[dag::edge::posChildName];
+ childData[dag::node::posParentName] = edgeData[dag::edge::posParentName];
+ DagItem* childNode = new DagItem(childData, parentNode);
+
+ parentNode->addChild(childNode);
+}
+
+bool DagModel2::insertRows(int position, int rows, const QModelIndex &parent)
+{
+ DagItem *parentItem = getItem(parent);
+ bool success;
+
+ beginInsertRows(parent, position, position + rows - 1);
+ success = parentItem->insertChildren(position, rows, m_rootItem->columnCount());
+ endInsertRows();
+
+ return success;
+}
+
+
+QModelIndex DagModel2::parent(const QModelIndex &index) const
+{
+ if (!index.isValid())
+ return QModelIndex();
+
+ DagItem *childItem = getItem(index);
+ DagItem *parentItem = childItem->parent();
+
+ if (parentItem == m_rootItem)
+ return QModelIndex();
+
+ return createIndex(parentItem->childNumber(), 0, parentItem);
+}
+
+
+bool DagModel2::removeColumns(int position, int columns, const QModelIndex &parent)
+{
+ bool success;
+
+ beginRemoveColumns(parent, position, position + columns - 1);
+ success = m_rootItem->removeColumns(position, columns);
+ endRemoveColumns();
+
+ if (m_rootItem->columnCount() == 0)
+ removeRows(0, rowCount());
+
+ return success;
+}
+
+bool DagModel2::removeRows(int position, int rows, const QModelIndex &parent)
+{
+ DagItem *parentItem = getItem(parent);
+ bool success = true;
+
+ beginRemoveRows(parent, position, position + rows - 1);
+ success = parentItem->removeChildren(position, rows);
+ endRemoveRows();
+
+ return success;
+}
+
+
+int DagModel2::rowCount(const QModelIndex &parent) const
+{
+ DagItem *parentItem = getItem(parent);
+
+ return parentItem->childCount();
+}
+
+
+bool DagModel2::setData(const QModelIndex &index, const QVariant &value,
+ int role)
+{
+ if (role != Qt::EditRole)
+ return false;
+
+ DagItem *item = getItem(index);
+ bool result = item->setData(index.column(), value);
+
+ if (result)
+ emit dataChanged(index, index);
+
+ return result;
+}
+
+bool DagModel2::setHeaderData(int section, Qt::Orientation orientation,
+ const QVariant &value, int role)
+{
+ if (role != Qt::EditRole || orientation != Qt::Horizontal)
+ return false;
+
+ bool result = m_rootItem->setData(section, value);
+
+ if (result)
+ emit headerDataChanged(orientation, section, section);
+
+ return result;
+}
+
+void DagModel2::setupModelData(const QStringList &lines, DagItem *parent)
+{
+ QList<DagItem*> parents;
+ QList<int> indentations;
+ parents << parent;
+ indentations << 0;
+
+ int number = 0;
+
+ while (number < lines.count()) {
+ int position = 0;
+ while (position < lines[number].length()) {
+ if (lines[number].mid(position, 1) != " ")
+ break;
+ position++;
+ }
+
+ QString lineData = lines[number].mid(position).trimmed();
+
+ if (!lineData.isEmpty()) {
+ // Read the column data from the rest of the line.
+ QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
+ QVector<QVariant> columnData;
+ for (int column = 0; column < columnStrings.count(); ++column)
+ columnData << columnStrings[column];
+
+ if (position > indentations.last()) {
+ // The last child of the current parent is now the new parent
+ // unless the current parent has no children.
+
+ if (parents.last()->childCount() > 0) {
+ parents << parents.last()->child(parents.last()->childCount()-1);
+ indentations << position;
+ }
+ } else {
+ while (position < indentations.last() && parents.count() > 0) {
+ parents.pop_back();
+ indentations.pop_back();
+ }
+ }
+
+ // Append a new item to the current parent's list of children.
+ DagItem *parent = parents.last();
+ parent->insertChildren(parent->childCount(), 1, m_rootItem->columnCount());
+ for (int column = 0; column < columnData.size(); ++column)
+ parent->child(parent->childCount() - 1)->setData(column, columnData[column]);
+ }
+
+ number++;
+ }
+}
+
+QString DagModel2::setupDag()
+{
+ QString dagAsString; //JODO CL
+ /*
+ boost::depth_first_search(
+ m_dag
+ , boost::visitor(make_dfs_visitor(boost::make_list(
+ CreatorVisitor::OnDiscoverVertex(m_rootItem, &dagAsString, m_aVertexAttributes)
+ , CreatorVisitor::OnExamineEdge (m_rootItem, &dagAsString, m_aVertexAttributes)
+ , CreatorVisitor::OnFinishVertex (m_rootItem, &dagAsString, m_aVertexAttributes)
+ )
+ ))
+ );
+
+ // Here I can change the headers by altering some attributes for root.
+ m_rootItem->setData(dag::node::posName, QVariant("Name"));
+ //JODO It is not possible to do this:
+ //m_rootItem->setData(dag::node::posId, QVariant("Id"));
+ //JODO So: Provide headers as strings before this point.
+
+ m_rootItem->setData(dag::node::posParentId, QVariant(0));
+ m_rootItem->setData(dag::node::posParentName, QVariant("Parent"));
+ */
+ return dagAsString;
+}
+
+void DagModel2::getEdges(QSqlQuery& query)
+{
+ while(query.next())
+ {
+ //create an edge
+ QVector<QVariant> data;
+ //fill node
+ fillData(data, query);
+ m_edges.append(data);
+ }
+}
+
+
+
+//JODO Populate a DagModel2 from an sql-query that provides the DAG as
+// (ParentId -> ChildId), ParentName, ChildName, ChildType
+void DagModel2::fromSql(QSqlQuery& query)
+{
+ if(!query.next())
+ return;
+ else
+ {
+ //We skip the first record and its NIL information.
+ fromSql(query, m_rootItem, 0);
+ }
+}
+
+/*CL
+void DagModel2::makeDag()
+{
+ m_dag.clear();
+
+ m_aVertexAttributes = get(Dag::vertex_attr_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_aVertexAttributes[source] = VertexAttributes(source, (*iter)[m_parentName].toString());
+ m_aVertexAttributes[target] = VertexAttributes(target, (*iter)[m_childName].toString());
+ }
+ }
+}
+*/
+
+QString DagModel2::dagToString()
+{
+ QString dagAsString;
+ m_dag.depthFirstString();
+/*
+ boost::depth_first_search(
+ m_dag
+ , boost::visitor(make_dfs_visitor(boost::make_list(
+ StringVisitor::OnDiscoverVertex(&dagAsString, m_aVertexAttributes)
+ , StringVisitor::OnExamineEdge(&dagAsString, m_aVertexAttributes, m_parentMap)
+ , StringVisitor::OnFinishVertex(&dagAsString, m_aVertexAttributes)
+ //, boost::record_predecessors(parentMap.begin(), boost::on_tree_edge())
+ )
+ ))
+ );
+*/
+ return dagAsString;
+}
+
+
+
+// The result indicates: False: No more data. True: Data available.
+DagItem* DagModel2::fromSql(QSqlQuery& query, DagItem* parent, int depth)
+{
+ // The function assumes, that the dags "expanded tree" exists in
+ // pre-order (the order, that is finally presented). This makes the
+ // traversal specifically simple.
+ Q_ASSERT(parent != NULL);
+
+ // Get the next record
+ if(!query.next())
+ return NULL;
+ else
+ {
+ //create a node
+ QVector<QVariant> data;
+ //fill node
+ fillData(data, query);
+
+ DagItem* curNode = (depth==0) ? new DagItem(data, 0) //curNode==m_rootItem
+ : new DagItem(data, parent);
+ if(depth == 0)
+ m_rootItem = curNode;
+
+ //if the new node is not a leaf, create children.
+ if(!curNode->isLeaf(m_typeId))
+ {
+ //While records available: Read children.
+ DagItem* curChild;
+ while((curChild = fromSql(query, curNode, depth+1)) != NULL)
+ {
+ curNode->addChild(curChild);
+ }
+ }
+
+ return curNode;
+ }
+}
+
+
+//JOFA Iteration example: The container as String
+QString DagModel2::toString()const
+{
+ return nodeToString(m_rootItem, 0);
+}
+
+QString DagModel2::nodeToString(DagItem* node, int depth)const
+{
+ if(node->childCount()==0)
+ return tr("%1(%2)\n").arg(indentation(depth), depth); //Print only structure and depth.
+ else
+ {
+ QString indent = indentation(depth);
+
+ QVariant childNameV = node->data(m_childName);
+ QString childName = childNameV.toString();
+ QString nodeRepr( tr("%1[%2\n").arg(indentation(depth), childName) );
+
+ for(int idx=0; idx<node->childCount(); idx++)
+ nodeRepr += nodeToString(node->child(idx), depth+1);
+
+ nodeRepr += tr("%1]\n").arg(indentation(depth));
+ return nodeRepr;
+ }
+}
+
+
+void DagModel2::fillData(QVector<QVariant>& data, QSqlQuery& query)
+{
+ QSqlRecord rec = query.record();
+ data.resize(rec.count());
+
+ m_parentId = rec.indexOf("ParentId");
+ m_childId = rec.indexOf("ChildId");
+ m_typeId = rec.indexOf("TypeId");
+ m_parentName = rec.indexOf("Parent");
+ m_childName = rec.indexOf("Child");
+ m_childType = rec.indexOf("Type");
+
+ data[m_parentId] = query.value(m_parentId);
+ data[m_childId] = query.value(m_childId);
+ data[m_typeId] = query.value(m_typeId);
+ data[m_parentName] = query.value(m_parentName);
+ data[m_childName] = query.value(m_childName);
+ data[m_childType] = query.value(m_childType);
+}
+
+void DagModel2::fillDummyData(QVector<QVariant>& data, int nodeId)
+{
+ data[m_parentId] = QVariant(nodeId);
+ data[m_childId] = QVariant(num_edges());
+ data[m_typeId] = QVariant(1);
+ data[m_parentName] = QVariant("Parent Name");
+ data[m_childName] = QVariant("Child Name");
+ data[m_childType] = QVariant(2);
+}
+

Added: sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel2.h
==============================================================================
--- (empty file)
+++ sandbox/icl/libs/xplore/br1/sqlbrowser/dagmodel2.h 2012-08-01 13:56:53 EDT (Wed, 01 Aug 2012)
@@ -0,0 +1,130 @@
+/****************************************************************************
+**
+**
+****************************************************************************/
+
+#ifndef DAGMODEL2_H
+#define DAGMODEL2_H
+
+#include <map>
+
+#include <QAbstractItemModel>
+#include <QModelIndex>
+#include <QVariant>
+
+#include "Dag.h"
+#include "Dag/ObjectGraph.h"
+
+
+class QSqlQuery;
+class DagItem;
+
+class DagModel2 : public QAbstractItemModel
+{
+ Q_OBJECT
+
+public:
+ //DagModel2(const QStringList &headers, const QString &data,
+ // QObject *parent = 0);
+ DagModel2(QObject *parent = 0);
+ ~DagModel2();
+
+ QVariant data(const QModelIndex &index, int role) const;
+ QVariant headerData(int section, Qt::Orientation orientation,
+ int role = Qt::DisplayRole) const;
+
+ QModelIndex index(int row, int column,
+ const QModelIndex &parent = QModelIndex()) const;
+ QModelIndex parent(const QModelIndex &index) const;
+
+ int rowCount(const QModelIndex &parent = QModelIndex()) const;
+ int columnCount(const QModelIndex &parent = QModelIndex()) const;
+
+ Qt::ItemFlags flags(const QModelIndex &index) const;
+ bool setData(const QModelIndex &index, const QVariant &value,
+ int role = Qt::EditRole);
+ bool setHeaderData(int section, Qt::Orientation orientation,
+ const QVariant &value, int role = Qt::EditRole);
+
+ bool insertColumns(int position, int columns,
+ const QModelIndex &parent = QModelIndex());
+ bool removeColumns(int position, int columns,
+ const QModelIndex &parent = QModelIndex());
+ bool insertRows(int position, int rows,
+ const QModelIndex &parent = QModelIndex());
+ 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);
+
+ //CL void makeDag();
+
+ QString dagToString();
+ QString setupDag();
+
+ DagItem* fromSql(QSqlQuery& query, DagItem* node, int depth);
+
+ //JOFA Iteration example: The container as String
+ QString toString()const;
+
+ QString nodeToString(DagItem* node, int depth)const;
+
+ DagItem* rootItem()const { return m_rootItem; } //CL DBG
+
+ int num_edges()const { return m_dag.num_edges(); }
+ //--------------------------------------------------------------------------
+
+private:
+ DagItem* item(const QModelIndex& index)const
+ {
+ if (!index.isValid())
+ return 0;
+ return static_cast<DagItem*>(index.internalPointer());
+ }
+
+ DagItem* createDagItem();
+
+ void setupModelData(const QStringList &lines, DagItem *parent);
+
+ DagItem *getItem(const QModelIndex &index) const;
+
+ void fillData(QVector<QVariant>& data, QSqlQuery& query);
+
+public:
+ // Fill edge with dummy data. JODO CL later
+ void fillDummyData(QVector<QVariant>& data, int nodeId);
+
+ void appendEdge(QVector<QVariant>& data){ m_edges.append(data);}//CL
+
+ void insertVertex(QVector<QVariant>& edgeData, const QModelIndex& index);
+
+private:
+ void dagInsertVertex(QVector<QVariant>& edgeData, const QModelIndex& index);
+ void modelInsertVertex(QVector<QVariant>& edgeData, const QModelIndex& index);
+
+private:
+ DagItem *m_rootItem;
+
+ int m_parentId ;
+ int m_childId ;
+ int m_typeId ;
+ int m_parentName;
+ int m_childName ;
+ int m_childType ;
+
+ typedef QVector<QVariant> tEdgeData;
+ typedef QList<tEdgeData> tEdgeList;
+ tEdgeList m_edges;
+
+ //==========================================================================
+ // Graph
+ //CL Dag::type m_dag;
+ dag::db::ObjectGraph m_dag;
+ Dag::tAttributesMap m_aVertexAttributes;
+ Dag::tParentMap m_parentMap;
+};
+
+#endif

Modified: sandbox/icl/libs/xplore/br1/sqlbrowser/objects1.db
==============================================================================
Binary files. No diff available.

Modified: sandbox/icl/libs/xplore/br1/sqlbrowser/sqlbrowser4.pro
==============================================================================
--- sandbox/icl/libs/xplore/br1/sqlbrowser/sqlbrowser4.pro (original)
+++ sandbox/icl/libs/xplore/br1/sqlbrowser/sqlbrowser4.pro 2012-08-01 13:56:53 EDT (Wed, 01 Aug 2012)
@@ -36,7 +36,9 @@
     StringVisitor2.h \
     Dag/ObjectGraph.h \
     Dag/DbBasedGraph.h \
- data/concept/DbBasedGraph.h
+ data/concept/DbBasedGraph.h \
+ dagmodel2.h \
+ CreatorVisitor2.h
 SOURCES = main.cpp browser.cpp connectionwidget.cpp qsqlconnectiondialog.cpp \
     exttableview.cpp \
     dagitem.cpp \
@@ -47,7 +49,8 @@
     gen/NumberGenerator.cpp \
     gen/DbGenerator.cpp \
     util/TestBoxes.cpp \
- data/concept/TypeGraphFunc.cpp
+ data/concept/TypeGraphFunc.cpp \
+ dagmodel2.cpp
 
 FORMS = browserwidget.ui qsqlconnectiondialog.ui
 build_all:!build_pass {


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