Files
2022-12-20 13:40:24 -06:00

742 lines
24 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::undirectedS,
boost::property < boost::vertex_name_t, std::string >,
boost::property < boost::edge_index_t, size_t >
>;
using SubGraph = boost::subgraph < Graph >;
struct Pin6 {
Pin6 ( void ) {
}
Pin6 ( 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, ' ' );
}
Pin6 ( const Pin &copy ) {
connector = copy.connector;
cable = copy.cable;
name = copy.name;
pin = copy.pin;
leg = copy.leg;
}
virtual ~Pin6 ( void ) {
}
std::string connector;
std::string cable;
std::string name;
std::string pin;
std::string leg;
bool visted = false;
bool ciaLeg = false;
int32_t graphVertex = -1; // this is in global space
};
using SharedPin = std::shared_ptr < Pin6 >;
using PinList = std::list < SharedPin >;
using PinMap = std::map < uint32_t, SharedPin >;
using PinPair = std::list < std::pair < SharedPin, SharedPin >>;
using PinPairList = std::list < PinPair >;
SubGraph & findSubGraph6 ( 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;
}
void fillGraph6 ( SubGraph &graph, PinMap &pinMap ) {
std::ifstream fileStream;
fileStream.open ( "test.csv" );
get_property ( graph, boost::graph_name ) = "W653";
get_property ( graph, boost::graph_graph_attribute )["rankdir"] = "LR";
// dp2.property ( "rankdir", boost::make_constant_property < Graph3 * > ( std::string ( "LR" ) ) );
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];
if ( pinAName.at ( 0 ) != '#' ) {
auto pinAConnectorType = values[2];
auto pinACia = values[3];
auto pinBName = values[4];
auto pinBConnectorType = values[6];
auto pinBCia = values[7];
SharedPin pinA;
SharedPin 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 < Pin6 > ( "W653", pinAName, pinAConnectorType );
}
if ( !pinB ) {
pinB = std::make_shared < Pin6 > ( "W653", pinBName, pinBConnectorType );
}
if ( pinACia == "TRUE" ) {
pinA->ciaLeg = true;
}
if ( pinBCia == "TRUE" ) {
pinB->ciaLeg = true;
}
auto & g1 = findSubGraph6 ( graph, pinA->leg );
auto & g2 = findSubGraph6 ( graph, 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, true, graph );
debugOut = fmt::format ( "Add edge between {} ({}) and {} ({})", pinA->name, pinA->graphVertex, pinB->name, pinB->graphVertex );
qDebug () << debugOut.data ();
}
}
}
fileStream.close ();
}
PinList findPinsOnLeg6 ( PinMap pinMap, const std::string &leg ) {
PinList pins;
for ( auto it : pinMap ) {
if ( it.second->leg == leg ) {
pins.push_back ( it.second );
}
}
return pins;
}
SharedPin findPin6 ( const uint32_t &graphIndex, PinMap pinMap ) {
return pinMap.at ( graphIndex );
}
SharedPin findPinByName6 ( const std::string &name, PinMap pinMap ) {
for ( auto it : pinMap ) {
if ( it.second->name == name ) {
return it.second;
}
}
return SharedPin ();
}
void printNeighborsUsingAdjacentVertices6 ( SharedPin pin, const 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 = findPin6 ( *it, pinMap );
std::cout << fmt::format ( "{} is attached to {}", pin->name.data (), workingPin->name.data () ) << std::endl;
printNeighborsUsingAdjacentVertices6 ( workingPin, graph, pinMap );
}
}
SharedPin getAdjacentVertex6 ( SharedPin pin, const SubGraph & graph, PinMap pinMap ) {
auto adjacent_vertices = boost::adjacent_vertices ( pin->graphVertex, graph );
if ( boost::degree ( pin->graphVertex, graph ) > 1 ) {
std::cout << "Bad stuff happened, this shouldn't be called on a pin with more than one edge" << std::endl;
}
else {
return findPin6 ( *adjacent_vertices.first, pinMap );
}
return SharedPin ();
}
bool checkSameLeg ( SharedPin pin, const SubGraph & graph, PinMap pinMap ) {
auto adjacent_vertices = boost::adjacent_vertices ( pin->graphVertex, graph );
if ( boost::degree ( pin->graphVertex, graph ) > 1 ) {
std::cout << "Bad stuff happened, this shouldn't be called on a pin with more than one edge" << std::endl;
}
else {
auto adjacentPin = findPin6 ( *adjacent_vertices.first, pinMap );
if ( pin->leg == adjacentPin->leg ) {
return true;
}
}
return false;
}
void getAllEdges6 ( SharedPin pin, const 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::degree ( *it, graph );
auto workingPin = findPin6 ( *it, pinMap );
qDebug () << "Debug Degree : " << *edgeCount;
getAllEdges6 ( workingPin, graph, pinMap, edgeCount );
}
}
void getAllAdjacentVertices ( SharedPin pin, const SubGraph & graph, PinMap pinMap, PinList &pinList ) {
auto adjacent_vertices = boost::adjacent_vertices ( pin->graphVertex, graph );
for (auto it = adjacent_vertices.first; it != adjacent_vertices.second; ++it) {
auto workingPin = findPin6 ( *it, pinMap );
pinList.push_back ( workingPin );
getAllAdjacentVertices ( workingPin, graph, pinMap, pinList );
}
}
bool checkCircular ( void ) {
return false;
}
class Processor {
public:
Processor ( SubGraph & graph, PinMap pinMap ) {
pinGraph = graph;
pinToVertexMap = pinMap;
}
virtual ~Processor ( void ) {
}
void process ( SharedPin pin ) {
std::cout << " ***** " << std::endl;
std::cout << "Processing " << pin->name.data ();
PinList list;
getAllAdjacentVertices ( pin, list );
for ( auto p : list ) {
std::cout << " <-> " << p->name;
}
std::cout << std::endl;
if ( pin->visted ) {
std::cout << "Already visited." << std::endl;
return; // already processed, gtfo.
}
uint32_t outDegree = 0;
getTotalEdges ( pin, &outDegree ); // this isn't working correctly.
std::cout << "Degree of " << outDegree << std::endl;
if ( outDegree == 0 ) {
std::cout << "Degree was ZERO... either this is an error or there is a pin that has no edge." << std::endl;
}
else if ( outDegree == 1 ) {
if ( pin->ciaLeg && getAdjacentVertex ( pin )->ciaLeg ) {
// Any pin pair that exists on the same leg that is attached to the CIA.
// CIA: Px-n to Px-n
processOneDegreeCiaPin ( pin );
}
else if ( !pin->ciaLeg && !getAdjacentVertex ( pin )->ciaLeg ) {
// If the pins aren't on the CIA side we'll to steal pins to test with.
// this needs to steal two pins
// TF: Px-n to Px-n
processOneDegreeTFPin ( pin );
}
else {
// If the pins aren't on the same leg then we just tie them all together.
// Px-n to Py-n
processOneDegreePin ( pin );
}
}
else {
// multi degree pins
if ( checkCircular () ) {
}
else {
PinList pinList;
getAllAdjacentVertices ( pin, pinList );
if ( pinList.size () > oneDegreePins.size () ) {
std::cout << "Error!! There are not enough pins to test this n degree junction." << std::endl;
}
else {
PinPair nDegreeGroup;
for ( auto pin : pinList ) {
auto pinSteal = oneDegreePins.back ();
oneDegreePins.pop_back ();
nDegreeGroup.push_back ( std::make_pair ( pinSteal, pin ) );
}
nDegreePins.push_back ( nDegreeGroup );
pin->visted = true;
}
}
}
}
void printShortingList ( void ) {
// Print adapter pin lists
// Print 1 degree pins
std::cout << "1 Degree: Short the following:" << std::endl;
for ( auto pin : oneDegreePins ) {
std::cout << pin->name << " ";
}
std::cout << std::endl;
// Print 1 Degree special case
std::cout << "1 Degree: Isolated TF Pins:" << std::endl;
for ( auto pinGroup : oneDegreeTFPins ) {
std::cout << "N Degree: Short the following:" << std::endl;
for ( auto pin : pinGroup ) {
auto to = pin.first;
auto from = pin.second;
std::cout << fmt::format ( "{} to {}", from->name, to->name ) << std::endl;
}
}
std::cout << std::endl;
// Print n degree pins
for ( auto pinGroup : nDegreePins ) {
std::cout << "N Degree: Short the following:" << std::endl;
for ( auto pin : pinGroup ) {
auto to = pin.first;
auto from = pin.second;
std::cout << fmt::format ( "{} to {}", from->name, to->name ) << std::endl;
}
}
for ( auto pinIt : pinToVertexMap ) {
if ( !pinIt.second->visted ) {
std::cout << pinIt.second->name << " has not been visted." << std::endl;
}
}
}
private:
void getTotalEdges ( SharedPin pin, uint32_t *edgeCount ) {
auto adjacent_vertices = boost::adjacent_vertices ( pin->graphVertex, pinGraph );
// Iterate over adjacent vertices
for (auto it = adjacent_vertices.first; it != adjacent_vertices.second; ++it) {
// *edgeCount += boost::degree ( *it, pinGraph );
*edgeCount += 1;
auto workingPin = findPin6 ( *it, pinToVertexMap );
getTotalEdges ( workingPin, edgeCount );
}
}
void getAllAdjacentVertices ( SharedPin pin, PinList &pinList ) {
auto adjacent_vertices = boost::adjacent_vertices ( pin->graphVertex, pinGraph );
for (auto it = adjacent_vertices.first; it != adjacent_vertices.second; ++it) {
auto workingPin = findPin6 ( *it, pinToVertexMap );
pinList.push_back ( workingPin );
getAllAdjacentVertices ( workingPin, pinList );
}
}
bool checkSameLeg ( SharedPin pin ) {
auto adjacent_vertices = boost::adjacent_vertices ( pin->graphVertex, pinGraph );
if ( boost::degree ( pin->graphVertex, pinGraph ) > 1 ) {
std::cout << "Bad stuff happened, this shouldn't be called on a pin with more than one edge" << std::endl;
}
else {
auto adjacentPin = findPin6 ( *adjacent_vertices.first, pinToVertexMap );
if ( pin->leg == adjacentPin->leg ) {
return true;
}
}
return false;
}
SharedPin getAdjacentVertex ( SharedPin pin ) {
auto adjacent_vertices = boost::adjacent_vertices ( pin->graphVertex, pinGraph );
if ( boost::degree ( pin->graphVertex, pinGraph ) > 1 ) {
std::cout << "Bad stuff happened, this shouldn't be called on a pin with more than one edge" << std::endl;
}
else {
return findPin6 ( *adjacent_vertices.first, pinToVertexMap );
}
return SharedPin ();
}
void processOneDegreeCiaPin ( SharedPin pin ) {
auto from = pin;
auto to = getAdjacentVertex ( pin );
oneDegreeCIAPins.push_back ( to );
from->visted = true;
to->visted = true;
std::cout << fmt::format ( "CIA Side {} to {}\n", from->name, to->name );
}
void processOneDegreeTFPin ( SharedPin pin ) {
if ( oneDegreePins.size () < 2 ) {
std::cout << "We've run out of pins to steal from!" << std::endl;
return;
}
else {
PinPair pinPair;
auto from = pin;
auto to = getAdjacentVertex ( pin );
// Grab the first pin
auto pinSteal = oneDegreePins.back ();
oneDegreePins.pop_back ();
pinPair.push_back ( std::make_pair ( pinSteal, from ) );
oneDegreeTFPins.push_back ( pinPair );
// Grab the second pin
pinSteal = oneDegreePins.back ();
oneDegreePins.pop_back ();
pinPair.push_back ( std::make_pair ( pinSteal, to ) );
oneDegreeTFPins.push_back ( pinPair );
from->visted = true;
to->visted = true;
std::cout << fmt::format ( "TF Side {} to {}\n", from->name, to->name );
}
}
void processOneDegreePin ( SharedPin pin ) {
auto from = pin;
auto to = getAdjacentVertex ( pin );
oneDegreePins.push_back ( to );
from->visted = true;
to->visted = true;
std::cout << fmt::format ( "CIA/TF Side {} to {}\n", from->name, to->name );
}
bool checkCircular ( void ) {
return false;
}
SubGraph pinGraph;
PinMap pinToVertexMap;
PinList oneDegreePins;
PinList oneDegreeCIAPins;
PinPairList oneDegreeTFPins;
PinPairList nDegreePins;
};
void test6 ( void ) {
SubGraph W653;
PinMap pinMap;
fillGraph6 ( W653, pinMap );
Processor processor ( W653, pinMap );
for ( auto it : pinMap ) {
processor.process ( it.second );
}
processor.printShortingList ();
/*
// Find all pins on the CIA side.
auto ciaPins = findPinsOnLeg6 ( pinMap, "P5" ); // P5 as a test
// hmm chris was probably right...
// if we do edges and then just roll through all the edges
// we can process them the same way. just look at the pair....
// this might become a pain in the ass keeping track of though.
PinList oneDegreePins;
PinList oneDegreeSpecialCIAPins;
PinPairList oneDegreeSpecialTFPins;
PinPairList nDegreeGroupPins;
for ( auto pin : ciaPins ) {
std::cout << pin->name.data () << std::endl;
printNeighborsUsingAdjacentVertices6 ( pin, W653, pinMap );
// ok, first find all edges with a degree of 1... which should tell me there is only one connection
// special cases...
// 1. Edge is on the same leg of a CIA side connector. so PX-n to PX-n
// 2. Edge is on the same leg of a TF side connector. so PX-n to PX-n, this becomse a small problem when we're only looking at pins from CIA side pins... hmm
// Easiest way is to just run through all the pins and find pins we haven't visited... a special special case.
//
// then deal with n degree pins
// special cases...
// 1. circular connections? Should we just ignore them? remove them list. they would be delt with differently in the application that makes tests.
// 2. Edges is on the same leg of a CIA side connector. so PX-n to PX-n
// 3. Edges is on the same leg of a TF side connector. so PX-n to PX-n, we'd have to do the same thing as above... that might be better.
uint32_t outDegree = 0;
getAllEdges6 ( pin, W653, pinMap, &outDegree ); // this isn't working correctly.
qDebug () << "Degree : " << outDegree;
if ( outDegree == 0 ) {
std::cout << "Degree was ZERO... either this is an error or there is a pin that has no edge." << std::endl;
}
else if ( outDegree == 1 ) {
// check special cases
if ( checkSameLeg ( pin, W653, pinMap ) ) {
if ( pin->leg == "P5" ) { // this would be any CIA connector which will be stored in the structure... probably.
auto from = pin;
auto to = getAdjacentVertex6 ( pin, W653, pinMap );
oneDegreeSpecialCIAPins.push_back ( to );
from->visted = true;
to->visted = true;
}
else {
// this needs to steal two pins
if ( oneDegreePins.size () < 2 ) {
std::cout << "We've run out of pins to steal from!" << std::endl;
return;
}
else {
PinPair pinPair;
auto from = pin;
auto to = getAdjacentVertex6 ( pin, W653, pinMap );
// Grab the first pin
auto pinSteal = oneDegreePins.back ();
oneDegreePins.pop_back ();
pinPair.push_back ( std::make_pair ( pinSteal, from ) );
oneDegreeSpecialTFPins.push_back ( pinPair );
// Grab the second pin
pinSteal = oneDegreePins.back ();
oneDegreePins.pop_back ();
pinPair.push_back ( std::make_pair ( pinSteal, to ) );
oneDegreeSpecialTFPins.push_back ( pinPair );
from->visted = true;
to->visted = true;
}
}
}
else {
auto from = pin;
auto to = getAdjacentVertex6 ( pin, W653, pinMap );
oneDegreePins.push_back ( to );
from->visted = true;
to->visted = true;
}
}
else if ( outDegree > 1 ) {
// special case?
if ( checkCircular () ) {
}
else {
// okay now for pins that have n degrees
// i think we need a single one degree pin to tie into each n degree pin
PinList pinList;
getAllAdjacentVertices ( pin, W653, pinMap, pinList );
if ( pinList.size () > oneDegreePins.size () ) {
std::cout << "Error!! There are not enough pins to test this n degree junction." << std::endl;
}
else {
PinPair nDegreeGroup;
for ( auto pin : pinList ) {
auto pinSteal = oneDegreePins.back ();
oneDegreePins.pop_back ();
nDegreeGroup.push_back ( std::make_pair ( pinSteal, pin ) );
}
nDegreeGroupPins.push_back ( nDegreeGroup );
pin->visted = true;
}
}
}
}
// Print adapter pin lists
// Print 1 degree pins
std::cout << "1 Degree: Short the following:" << std::endl;
for ( auto pin : oneDegreePins ) {
std::cout << pin->name << " ";
}
std::cout << std::endl;
// Print 1 Degree special case
std::cout << "1 Degree: Isolated TF Pins:" << std::endl;
for ( auto pinGroup : oneDegreeSpecialTFPins ) {
std::cout << "N Degree: Short the following:" << std::endl;
for ( auto pin : pinGroup ) {
auto to = pin.first;
auto from = pin.second;
std::cout << fmt::format ( "{} to {}", from->name, to->name ) << std::endl;
}
}
std::cout << std::endl;
// Print n degree pins
for ( auto pinGroup : nDegreeGroupPins ) {
std::cout << "N Degree: Short the following:" << std::endl;
for ( auto pin : pinGroup ) {
auto to = pin.first;
auto from = pin.second;
std::cout << fmt::format ( "{} to {}", from->name, to->name ) << std::endl;
}
}
for ( auto pinIt : pinMap ) {
if ( !pinIt.second->visted ) {
std::cout << pinIt.second->name << " has not been visted." << std::endl;
}
}
*/
// Test, looks like undirectedS is not respected.
PinList pinList;
auto testPin = findPinByName6 ( "P2-10", pinMap );
getAllAdjacentVertices ( testPin, W653, pinMap, pinList );
// with an undirected graph this should show me P5-03 but it does not.
std::cout << "Test " << testPin->name << std::endl;
for ( auto pin : pinList ) {
std::cout << pin->name << std::endl;
}
}