diff --git a/graphtool/Graph.cpp b/graphtool/Graph.cpp index fb5c832..194d15f 100644 --- a/graphtool/Graph.cpp +++ b/graphtool/Graph.cpp @@ -866,3 +866,41 @@ bool Graph::isForest() { isForestFlag = true; return true; } + + +Graph *Graph::loadAdjacencyFile(string file) { + string line; + ifstream myfile(file); + + vector> result; + if (myfile.is_open()) { + while (getline(myfile, line)) { + + vector cols; + boost::split(cols, line, boost::is_any_of(",")); + result.push_back(cols); + } + myfile.close(); + + + // check if the first column is a name + bool isFirstColName = result.at(0).size() != result.size(); + + //Load into vector + vector> adjacencyMatrix = getZeroizedMatrix((int) result.size(), (int) result.size()); + for (unsigned long rIndex = 0; rIndex < result.size(); rIndex++) { + // add values to matrix + for (unsigned long cIndex = 0; cIndex < result.size(); cIndex++) { + unsigned long tmpCIndex = isFirstColName ? cIndex + 1 : cIndex; + try { + adjacencyMatrix.at(rIndex).at(cIndex) = stoi(result.at(rIndex)[tmpCIndex]); + } catch (const invalid_argument &e) { + throw invalid_argument("matrix entries must be and contain at least one integer."); + } + } + } + return new Graph(adjacencyMatrix); + } else { + return nullptr; + } +} diff --git a/graphtool/Graph.h b/graphtool/Graph.h index 6147874..0061eb8 100644 --- a/graphtool/Graph.h +++ b/graphtool/Graph.h @@ -158,7 +158,7 @@ class Graph { * * @return a zeroized matrix */ - vector> getZeroizedMatrix(int rows, int cols); + static vector> getZeroizedMatrix(int rows, int cols); /** * Checks whether matrix A (m x n) and B (m x n) are identical. @@ -181,9 +181,9 @@ class Graph { const vector> &getAdjacencyMatrix() const; - public: Graph(vector> adjacencyMatrix); + Graph(vector> adjacencyMatrix, vector node_names); /** @@ -362,4 +362,10 @@ class Graph { */ string exportDot(vector path); + /** + * Load the Graph from a file within an adjacency matrix. + * @param file Path to the file + * @return New Graph object + */ + static Graph *loadAdjacencyFile(string file); }; diff --git a/gui/main.cpp b/gui/main.cpp index b4c6c18..2b8c527 100644 --- a/gui/main.cpp +++ b/gui/main.cpp @@ -106,7 +106,13 @@ int main(int argc, char **argv) { */ // Get input from csv file - // TODO @ForrestFalcon issue #10, vgl. Implementierung inputMatlab + vector inputCSV = getValues("-i", allArgs); + for (int i = 0; i < inputCSV.size(); i++) { + Graph *g = Graph::loadAdjacencyFile(inputCSV[i]); + printv("Graph %d (CSV-Datei) \"%s\" hinzugefügt.", graphs.size(), inputCSV[i].c_str()); + graphs.push_back(g); + } + printv("%d CSV-Dateien gefunden.", inputCSV.size()); // Get input from matlab string vector inputMatlab = getValues("-iml", allArgs); diff --git a/test/main.cpp b/test/main.cpp index 3877508..628e621 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -1,6 +1,7 @@ #include "gtest/gtest.h" #include #include +#include #include "Graph.h" TEST(graph_check, test_directed) { @@ -453,4 +454,64 @@ TEST(graph_check, test_forest_undirected) { bool forest = g->isForest(); ASSERT_FALSE(forest); +} +TEST(graph_check, test_adjazenzmatrix_load_file) { + ofstream myfile ("tmp.txt"); + if (myfile.is_open()) { + myfile << "1,0,1\n"; + myfile << "1,1,1\n"; + myfile << "0,0,1\n"; + myfile.close(); + } else FAIL(); + + Graph *g = Graph::loadAdjacencyFile("tmp.txt"); + string mat = g->getAdjacencyMatrixString(); + remove("tmp.txt"); + + std::vector strs; + boost::split(strs, mat, boost::is_any_of("\n")); + + ASSERT_EQ(strs[0], "1,0,1"); + ASSERT_EQ(strs[1], "1,1,1"); + ASSERT_EQ(strs[2], "0,0,1"); +} +TEST(graph_check, test_adjazenzmatrix_load_file_space) { + ofstream myfile ("tmp.txt"); + if (myfile.is_open()) { + myfile << "1 ,0,1\n"; + myfile << "1,1 ,1\n"; + myfile << "0,0, 1\n"; + myfile.close(); + } else FAIL(); + + Graph *g = Graph::loadAdjacencyFile("tmp.txt"); + string mat = g->getAdjacencyMatrixString(); + remove("tmp.txt"); + + std::vector strs; + boost::split(strs, mat, boost::is_any_of("\n")); + + ASSERT_EQ(strs[0], "1,0,1"); + ASSERT_EQ(strs[1], "1,1,1"); + ASSERT_EQ(strs[2], "0,0,1"); +} +TEST(graph_check, test_adjazenzmatrix_load_file_graph_name) { + ofstream myfile ("tmp.txt"); + if (myfile.is_open()) { + myfile << "Demo,1,0,1\n"; + myfile << "Test,1,1,1\n"; + myfile << "42,0,0,1\n"; + myfile.close(); + } else FAIL(); + + Graph *g = Graph::loadAdjacencyFile("tmp.txt"); + string mat = g->getAdjacencyMatrixString(); + remove("tmp.txt"); + + std::vector strs; + boost::split(strs, mat, boost::is_any_of("\n")); + + ASSERT_EQ(strs[0], "1,0,1"); + ASSERT_EQ(strs[1], "1,1,1"); + ASSERT_EQ(strs[2], "0,0,1"); } \ No newline at end of file