Skip to content

Commit

Permalink
Introduced Working Directory for Export to file
Browse files Browse the repository at this point in the history
Fix #16
Upgrade of Export feature related to #4
  • Loading branch information
ZigRazor authored Jul 5, 2021
1 parent f3c54b9 commit 1c1c622
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.9)

# set the project name and version
project(CXXGraph VERSION 0.0.8)
project(CXXGraph VERSION 0.0.9)

configure_file(CXXGraphConfig.h.in ${PROJECT_SOURCE_DIR}/include/CXXGraphConfig.h)

Expand Down
4 changes: 2 additions & 2 deletions docs/Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ PROJECT_NAME = "CXXGraph"
# could be handy for archiving the generated documentation or if some version
# control system is used.

PROJECT_NUMBER = 0.0.6
PROJECT_NUMBER = 0.0.9

# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer a
Expand All @@ -58,7 +58,7 @@ PROJECT_LOGO =
# entered, it will be relative to the location where doxygen was started. If
# left blank the current directory will be used.

OUTPUT_DIRECTORY =
OUTPUT_DIRECTORY =

# If the CREATE_SUBDIRS tag is set to YES then doxygen will create 4096 sub-
# directories (in 2 levels) under the output directory of each output format and
Expand Down
31 changes: 19 additions & 12 deletions include/Graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,15 +466,16 @@ namespace CXXGRAPH
private:
std::set<const Edge<T> *> edgeSet;
void addElementToAdjMatrix(AdjacencyMatrix<T> &adjMatrix, const Node<T> *nodeFrom, const Node<T> *nodeTo, const Edge<T> *edge) const;
int writeToStandardFile(std::string OFileName, bool compress, bool writeNodeFeat, bool writeEdgeWeight) const;
int writeToStandardFile_csv(const std::string &workingDir, const std::string &OFileName, bool compress, bool writeNodeFeat, bool writeEdgeWeight) const;

public:
typedef enum E_OutputFormat
/// Specify the Input/Output format of the Graph for Import/Export functions
typedef enum E_InputOutputFormat
{
STANDARD,
STANDARD_CSV, ///< A standard csv format
OUT_1,
OUT_2
} OutputFormat;
} InputOutputFormat;

Graph() = default;
Graph(const std::set<const Edge<T> *> &edgeSet);
Expand Down Expand Up @@ -553,13 +554,14 @@ namespace CXXGRAPH
* This function write the graph in an output file
*
* @param format The Output format of the file
* @param workingDir The path to the directory in which will be placed the output file
* @param OFileName The Output File Name ( )
* @param compress Indicates if the output will be compressed
* @param writeNodeFeat Indicates if export also Node Features
* @param writeEdgeWeight Indicates if export also Edge Weights
* @return 0 if all OK, else return a negative value
*/
int writeToFile(OutputFormat format = OutputFormat::STANDARD, std::string OFileName = "graph.csv", bool compress = false, bool writeNodeFeat = false, bool writeEdgeWeight = false) const;
int writeToFile(InputOutputFormat format = InputOutputFormat::STANDARD_CSV, const std::string &workingDir = ".", const std::string &OFileName = "graph", bool compress = false, bool writeNodeFeat = false, bool writeEdgeWeight = false) const;

friend std::ostream &operator<<<>(std::ostream &os, const Graph<T> &graph);
friend std::ostream &operator<<<>(std::ostream &os, const AdjacencyMatrix<T> &adj);
Expand Down Expand Up @@ -637,10 +639,11 @@ namespace CXXGRAPH
}

template <typename T>
int Graph<T>::writeToStandardFile(std::string OFileName, bool compress, bool writeNodeFeat, bool writeEdgeWeight) const
int Graph<T>::writeToStandardFile_csv(const std::string &workingDir, const std::string &OFileName, bool compress, bool writeNodeFeat, bool writeEdgeWeight) const
{
std::ofstream ofileGraph;
ofileGraph.open(OFileName);
std::string completePathToFileGraph = workingDir + "/" + OFileName + ".csv";
ofileGraph.open(completePathToFileGraph);
auto printOutGraph = [&ofileGraph](const Edge<T> *e)
{ ofileGraph << e->getId() << "," << e->getNodePair().first->getId() << "," << e->getNodePair().second->getId() << std::endl; };
std::for_each(edgeSet.cbegin(), edgeSet.cend(), printOutGraph);
Expand All @@ -649,7 +652,9 @@ namespace CXXGRAPH
if (writeNodeFeat)
{
std::ofstream ofileNodeFeat;
ofileNodeFeat.open("NodeFeat_" + OFileName);
std::string completePathToFileNodeFeat = workingDir + "/" + OFileName + "_NodeFeat"
".csv";
ofileNodeFeat.open(completePathToFileNodeFeat);
auto printOutNodeFeat = [&ofileNodeFeat](const Node<T> *node)
{ ofileNodeFeat << node->getId() << "," << node->getData() << std::endl; };
auto nodeSet = getNodeSet();
Expand All @@ -660,7 +665,9 @@ namespace CXXGRAPH
if (writeEdgeWeight)
{
std::ofstream ofileEdgeWeight;
ofileEdgeWeight.open("EdgeWeight_" + OFileName);
std::string completePathToFileEdgeWeight = workingDir + "/" + OFileName + "_EdgeWeight"
".csv";
ofileEdgeWeight.open(completePathToFileEdgeWeight);
auto printOutEdgeWeight = [&ofileEdgeWeight](const Edge<T> *e)
{ ofileEdgeWeight << e->getId() << "," << (e->isWeighted().has_value() && e->isWeighted().value() ? (dynamic_cast<const Weighted *>(e))->getWeight() : 0.0) << std::endl; };

Expand Down Expand Up @@ -1055,11 +1062,11 @@ namespace CXXGRAPH
}

template <typename T>
int Graph<T>::writeToFile(OutputFormat format, std::string OFileName, bool compress, bool writeNodeFeat, bool writeEdgeWeight) const
int Graph<T>::writeToFile(InputOutputFormat format, const std::string &workingDir, const std::string &OFileName, bool compress, bool writeNodeFeat, bool writeEdgeWeight) const
{
if (format == OutputFormat::STANDARD)
if (format == InputOutputFormat::STANDARD_CSV)
{
return writeToStandardFile(OFileName, compress, writeNodeFeat, writeEdgeWeight);
return writeToStandardFile_csv(workingDir, OFileName, compress, writeNodeFeat, writeEdgeWeight);
}
else
{
Expand Down
12 changes: 6 additions & 6 deletions test/RWOutputTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ TEST(RWOutputTest, test_2)
edgeSet.insert(&edge2);
edgeSet.insert(&edge3);
CXXGRAPH::Graph<int> graph(edgeSet);
int res = graph.writeToFile(CXXGRAPH::Graph<int>::OutputFormat::STANDARD);
int res = graph.writeToFile(CXXGRAPH::Graph<int>::InputOutputFormat::STANDARD_CSV);
ASSERT_EQ(res, 0);
ASSERT_TRUE(exists_test("graph.csv"));
ASSERT_FALSE(exists_test("NodeFeat_graph.csv"));
Expand All @@ -63,7 +63,7 @@ TEST(RWOutputTest, test_3)
edgeSet.insert(&edge2);
edgeSet.insert(&edge3);
CXXGRAPH::Graph<int> graph(edgeSet);
int res = graph.writeToFile(CXXGRAPH::Graph<int>::OutputFormat::STANDARD, "test_3.csv");
int res = graph.writeToFile(CXXGRAPH::Graph<int>::InputOutputFormat::STANDARD_CSV, ".", "test_3");
ASSERT_EQ(res, 0);
ASSERT_TRUE(exists_test("test_3.csv"));
ASSERT_FALSE(exists_test("NodeFeat_test_3.csv"));
Expand All @@ -81,7 +81,7 @@ TEST(RWOutputTest, test_4)
edgeSet.insert(&edge2);
edgeSet.insert(&edge3);
CXXGRAPH::Graph<int> graph(edgeSet);
int res = graph.writeToFile(CXXGRAPH::Graph<int>::OutputFormat::OUT_1, "test_4.csv");
int res = graph.writeToFile(CXXGRAPH::Graph<int>::InputOutputFormat::OUT_1, "test_4");
ASSERT_EQ(res, -1);
ASSERT_FALSE(exists_test("test_4.csv"));
ASSERT_FALSE(exists_test("NodeFeat_test_4.csv"));
Expand All @@ -99,7 +99,7 @@ TEST(RWOutputTest, test_5)
edgeSet.insert(&edge2);
edgeSet.insert(&edge3);
CXXGRAPH::Graph<int> graph(edgeSet);
int res = graph.writeToFile(CXXGRAPH::Graph<int>::OutputFormat::STANDARD, "test_5.csv", false, true, true);
int res = graph.writeToFile(CXXGRAPH::Graph<int>::InputOutputFormat::STANDARD_CSV, ".", "test_5", false, true, true);
ASSERT_EQ(res, 0);
ASSERT_TRUE(exists_test("test_5.csv"));
ASSERT_TRUE(exists_test("NodeFeat_test_5.csv"));
Expand All @@ -117,7 +117,7 @@ TEST(RWOutputTest, test_6)
edgeSet.insert(&edge2);
edgeSet.insert(&edge3);
CXXGRAPH::Graph<int> graph(edgeSet);
int res = graph.writeToFile(CXXGRAPH::Graph<int>::OutputFormat::STANDARD, "test_6.csv", false, false, true);
int res = graph.writeToFile(CXXGRAPH::Graph<int>::InputOutputFormat::STANDARD_CSV, ".", "test_6", false, false, true);
ASSERT_EQ(res, 0);
ASSERT_TRUE(exists_test("test_6.csv"));
ASSERT_FALSE(exists_test("NodeFeat_test_6.csv"));
Expand All @@ -135,7 +135,7 @@ TEST(RWOutputTest, test_7)
edgeSet.insert(&edge2);
edgeSet.insert(&edge3);
CXXGRAPH::Graph<int> graph(edgeSet);
int res = graph.writeToFile(CXXGRAPH::Graph<int>::OutputFormat::STANDARD, "test_7.csv", false, true, false);
int res = graph.writeToFile(CXXGRAPH::Graph<int>::InputOutputFormat::STANDARD_CSV, ".", "test_7", false, true, false);
ASSERT_EQ(res, 0);
ASSERT_TRUE(exists_test("test_7.csv"));
ASSERT_TRUE(exists_test("NodeFeat_test_7.csv"));
Expand Down

0 comments on commit 1c1c622

Please sign in to comment.