Files
2022-12-14 14:10:18 -06:00

397 lines
11 KiB
C++

#include "main.h"
#include "utility.h"
#include <algorithm>
#define FMT_HEADER_ONLY
#include <fmt/format.h>
using CustomGraph = boost::adjacency_list < boost::vecS, boost::vecS, boost::directedS,
boost::property < boost::vertex_name_t, std::string >,
boost::property < boost::edge_index_t, size_t >
>;
using SubGraph = boost::subgraph < Graph >;
struct Pin2 {
Pin2 ( void ) {
}
Pin2 ( const std::string & cableName, const std::string & pinName, const std::string & connectorType ) {
connector = connectorType;
cable = cableName;
name = pinName;
auto values = split ( pinName, '-' );
leg = values.at ( 0 );
pin = values.at ( 1 );
std::erase ( leg, ' ' );
std::erase ( pin, ' ' );
}
Pin2 ( const Pin &copy ) {
connector = copy.connector;
cable = copy.cable;
name = copy.name;
pin = copy.pin;
leg = copy.leg;
}
virtual ~Pin2 ( void ) {
}
auto key ( void ) const {
return std::tie ( cable, name );
}
bool operator < ( const Pin &rhs ) const {
return key () < rhs.key ();
}
bool operator == ( const Pin &rhs ) const {
return key () == rhs.key ();
}
bool operator != ( const Pin &rhs ) const {
return key () != rhs.key ();
}
std::string connector;
std::string cable;
std::string name;
std::string pin;
std::string leg;
bool visted = false;
int32_t graphVertex = -1; // this is in global space
};
using PinList = std::list < std::shared_ptr < Pin2 >>;
using PinMap = std::map < uint32_t, std::shared_ptr < Pin2 >>;
SubGraph & findSubGraph ( SubGraph &graph, const std::string &subgraphName ) {
for ( auto &value : boost::make_iterator_range ( graph.children () ) ) {
auto graphName = get_property ( value, boost::graph_name );
if ( graphName == std::string ( "cluster" ) + subgraphName ) {
return value;
}
}
// didn't find it, add it
auto &tmpGraph = graph.create_subgraph ();
get_property ( tmpGraph, boost::graph_name ) = std::string ( "cluster" ) + subgraphName;
get_property ( tmpGraph, boost::graph_graph_attribute ) ["label"] = subgraphName;
return tmpGraph;
}
int32_t findGraphIndex ( std::shared_ptr < Pin2 > pin, PinMap pinMap ) {
for ( auto it : pinMap ) {
if ( it.second == pin ) {
return it.first;
}
}
return -1;
}
int32_t findPin ( const std::string &name, PinMap pinMap ) {
for ( auto it : pinMap ) {
if ( it.second->name == name ) {
return it.first;
}
}
return -1;
}
std::shared_ptr < Pin2 > findPin ( const uint32_t &graphIndex, PinMap pinMap ) {
return pinMap.at ( graphIndex );
}
std::shared_ptr < Pin2 > findPinByName ( const std::string &name, PinMap pinMap ) {
for ( auto it : pinMap ) {
if ( it.second->name == name ) {
return it.second;
}
}
return std::shared_ptr < Pin2 > ();
}
PinList findNeighbors ( std::shared_ptr < Pin2 > pin, SubGraph & graph, PinMap pinMap ) {
PinList neighborPins;
for ( auto it : pinMap ) {
if ( it.second == pin ) {
auto neighbours = boost::adjacent_vertices ( it.first, graph );
for (auto vd : make_iterator_range ( neighbours ) ) {
neighborPins.push_back ( pinMap[vd] );
}
}
}
return neighborPins;
}
void printNeighborsUsingAdjacentVertices ( std::shared_ptr<Pin2> pin, SubGraph & graph, PinMap pinMap ) {
boost::graph_traits<Graph>::vertex_descriptor v = pin->graphVertex;
std::pair<boost::graph_traits<Graph>::adjacency_iterator, boost::graph_traits<Graph>::adjacency_iterator> adjacent_vertices;
adjacent_vertices = boost::adjacent_vertices(v, graph);
//Iterate over adjacent vertices
for (auto it = adjacent_vertices.first; it != adjacent_vertices.second; ++it) {
auto workingPin = findPin ( *it, pinMap );
auto debugOut = fmt::format ( "{} is attached to {}", pin->name.data (), workingPin->name.data () );
qDebug () << debugOut.data ();
printNeighborsUsingAdjacentVertices (workingPin, graph, pinMap );
}
}
PinList findPinsOnLeg ( PinMap pinMap, const std::string &leg ) {
PinList pins;
for ( auto it : pinMap ) {
if ( it.second->leg == leg ) {
pins.push_back ( it.second );
}
}
return pins;
}
void getTotalEdges ( std::shared_ptr<Pin2> pin, SubGraph &graph, PinMap pinMap, uint32_t *edgeCount ) {
std::pair<boost::graph_traits<Graph>::adjacency_iterator, boost::graph_traits<Graph>::adjacency_iterator> adjacent_vertices;
adjacent_vertices = boost::adjacent_vertices(pin->graphVertex, graph);
//Iterate over adjacent vertices
for (auto it = adjacent_vertices.first; it != adjacent_vertices.second; ++it) {
*edgeCount = boost::out_degree ( *it, graph );
auto workingPin = findPin ( *it, pinMap );
getTotalEdges (workingPin, graph, pinMap, edgeCount );
}
}
void test5 ( void ) {
std::ifstream fileStream;
fileStream.open ( "test.csv" );
SubGraph W653;
get_property ( W653, boost::graph_name ) = "W653";
get_property ( W653, boost::graph_graph_attribute )["rankdir"] = "LR";
// dp2.property ( "rankdir", boost::make_constant_property < Graph3 * > ( std::string ( "LR" ) ) );
PinMap pinMap;
std::string debugOut;
if ( fileStream.is_open () ) {
std::string inputLine;
// Skip Header
std::getline ( fileStream, inputLine );
while ( std::getline ( fileStream, inputLine ) ) {
auto values = split ( inputLine, ',' );
auto pinAName = values[0];
auto pinAConnectorType = values[2];
auto pinBName = values[4];
auto pinBConnectorType = values[6];
std::shared_ptr < Pin2 > pinA;
std::shared_ptr < Pin2 > pinB;
for ( auto it: pinMap ) {
if ( it.second->name == pinAName ) {
pinA = it.second;
}
if ( it.second->name == pinBName ) {
pinB = it.second;
}
}
if ( !pinA ) {
pinA = std::make_shared < Pin2 > ( "W653", pinAName, pinAConnectorType );
}
if ( !pinB ) {
pinB = std::make_shared < Pin2 > ( "W653", pinBName, pinBConnectorType );
}
auto &g1 = findSubGraph ( W653, pinA->leg );
auto &g2 = findSubGraph ( W653, pinB->leg );
uint32_t v1 = 0;
uint32_t v2 = 0;
if ( pinA->graphVertex == -1 ) {
v1 = boost::add_vertex ( g1 );
boost::put ( boost::get ( boost::vertex_attribute, g1 ), v1, GraphvizAttributes {
{ "label", pinA->name }
} );
pinA->graphVertex = g1.local_to_global ( v1 );
pinMap.insert ( { pinA->graphVertex, pinA } );
}
if ( pinB->graphVertex == -1 ) {
v2 = boost::add_vertex ( g2 );
boost::put ( boost::get ( boost::vertex_attribute, g2 ), v2, GraphvizAttributes {
{ "label", pinB->name }
} );
pinB->graphVertex = g2.local_to_global ( v2 );
pinMap.insert ( { pinB->graphVertex, pinB } );
}
// should be a global vertex now.
v1 = pinA->graphVertex;
v2 = pinB->graphVertex;
// local
boost::add_edge ( v1, v2, W653 );
debugOut = fmt::format ( "Add edge between {} ({}) and {} ({})", pinA->name, pinA->graphVertex, pinB->name, pinB->graphVertex );
qDebug () << debugOut.data ();
}
}
fileStream.close ();
// each cable will have a pin map;
auto P1PinList = findPinsOnLeg ( pinMap, "P5" );
for ( auto pin : P1PinList ) {
// qDebug () << std::string ( pin->leg + "-" + pin->pin ).data ();
auto neighbors = findNeighbors ( pin, W653, pinMap );
std::string debugOutput = std::string ( pin->name + " " );
for ( auto neighbor : neighbors ) {
debugOutput += "-> " + neighbor->name;
}
qDebug () << debugOutput.data ();
}
auto es = boost::edges ( W653 );
for (auto eit = es.first; eit != es.second; ++eit) {
std::cout << boost::source ( *eit, W653 ) << ' ' << boost::target ( *eit, W653 ) << std::endl;
}
// iterate over edges
qDebug () << "Listing Edges";
typedef boost::graph_traits < SubGraph > ::edge_iterator edge_iterator;
edge_iterator b, e;
for (boost::tie ( b, e ) = boost::edges ( W653 ); b != e; ++b) {
std::cout << *b << std::endl; // this already reports the attached vector
auto u = boost::source ( *b, W653 );
auto v = boost::target ( *b, W653 );
qDebug () << "Attached to source " << u;
qDebug () << "Attached to target " << v;
auto pinA = findPin ( u, pinMap );
auto pinB = findPin ( v, pinMap );
qDebug () << pinA->name.data ();
qDebug () << pinB->name.data ();
}
// iterator over outer edges
qDebug () << "Listing Outer Edges";
typedef boost::graph_traits < SubGraph > ::vertex_iterator vertex_iterator;
typedef boost::graph_traits < SubGraph > ::out_edge_iterator out_edge_iterator;
typedef boost::graph_traits < SubGraph > ::vertex_descriptor Vertex;
Vertex v = 2;
out_edge_iterator ob, oe;
vertex_iterator vb, ve;
for (boost::tie ( vb, ve ) = vertices ( W653 ); vb != ve; ++vb) {
v = *vb;
auto mainPin = findPin ( v, pinMap );
qDebug () << mainPin->name.data () << " :";
for (boost::tie ( ob, oe ) = out_edges ( v, W653 ); ob != oe; ++ob) {
std::cout << *ob << std::endl;
auto u = boost::source ( *ob, W653 );
auto v = boost::target ( *ob, W653 );
auto pinA = findPin ( u, pinMap );
auto pinB = findPin ( v, pinMap );
qDebug () << pinA->name.data ();
qDebug () << pinB->name.data ();
}
}
// hmmmm
qDebug () << "Test P5-29";
auto graphVertex = findPin ( "P2-01", pinMap );
qDebug () << "Vertex: " << graphVertex;
std::cout << "Number of edges connected to vertex " << graphVertex << ": "
<< boost::out_degree ( graphVertex, W653 ) << std::endl;
for (boost::tie ( ob, oe ) = out_edges ( graphVertex, W653 ); ob != oe; ++ob) {
std::cout << *ob << std::endl;
auto u = boost::source ( *ob, W653 );
auto v = boost::target ( *ob, W653 );
auto pinA = findPin ( u, pinMap );
auto pinB = findPin ( v, pinMap );
qDebug () << pinA->name.data ();
qDebug () << pinB->name.data ();
}
// print adjacent pins
qDebug () << "Using Adjacent Pins";
auto pin = findPinByName ( "P5-29", pinMap );
printNeighborsUsingAdjacentVertices ( pin, W653, pinMap );
// EdgeCount tests.
qDebug () << "Edge Count Tests";
uint32_t edgeCount = 0;
getTotalEdges (findPinByName ( "P5-01", pinMap ), W653, pinMap, &edgeCount );
qDebug () << "Total Edges: " << edgeCount;
// Adjacent pins will find all connected Pins
// out_degree will list the number of edges between vertices.
// std::ofstream dotFile ( "test.dot" );
// write_graphviz ( dotFile, W653 );
}