From b0e3cda41e6dd01f9f17323aa4d19a96b8a9c5eb Mon Sep 17 00:00:00 2001 From: Eberhard Graether Date: Sun, 27 Jul 2014 02:22:45 +0200 Subject: [PATCH] data: Added getName() method to Edge format = EdgeType:fromNode->toNode --- src/lib/data/Storage.cpp | 17 +++++++++++++++-- src/lib/data/graph/Edge.cpp | 23 ++++++++++++++--------- src/lib/data/graph/Edge.h | 2 ++ src/lib/data/graph/Graph.cpp | 12 ++++++------ src/test/GraphTestSuite.h | 19 +++++++++++++++++++ 5 files changed, 56 insertions(+), 17 deletions(-) diff --git a/src/lib/data/Storage.cpp b/src/lib/data/Storage.cpp index cea7d49f..5ed2cf94 100644 --- a/src/lib/data/Storage.cpp +++ b/src/lib/data/Storage.cpp @@ -246,8 +246,21 @@ Id Storage::getIdForNodeWithName(const std::string& fullName) const std::string Storage::getNameForNodeWithId(Id id) const { - Node* node = m_graph.getNodeById(id); - return (node ? node->getFullName() : ""); + Token* token = m_graph.getTokenById(id); + + if (!token) + { + return ""; + } + + if (token->isEdge()) + { + return dynamic_cast(token)->getName(); + } + else + { + return dynamic_cast(token)->getFullName(); + } } std::vector Storage::getNamesForNodesWithNamePrefix(const std::string& prefix) const diff --git a/src/lib/data/graph/Edge.cpp b/src/lib/data/graph/Edge.cpp index 0853c60a..a19b5c89 100644 --- a/src/lib/data/graph/Edge.cpp +++ b/src/lib/data/graph/Edge.cpp @@ -53,6 +53,11 @@ Node* Edge::getTo() const return m_to; } +std::string Edge::getName() const +{ + return getTypeString() + ":" + getFrom()->getFullName() + "->" + getTo()->getFullName(); +} + bool Edge::isNode() const { return false; @@ -101,21 +106,21 @@ std::string Edge::getTypeString() const switch (m_type) { case EDGE_MEMBER: - return "has child"; + return "child"; case EDGE_TYPE_OF: - return "is type of"; + return "type use"; case EDGE_RETURN_TYPE_OF: - return "has return type"; + return "return type"; case EDGE_PARAMETER_TYPE_OF: - return "has parameter of type"; + return "parameter type"; case EDGE_INHERITANCE: - return "is derived from"; + return "inheritance"; case EDGE_CALL: - return "calls"; + return "call"; case EDGE_USAGE: - return "uses"; + return "usage"; case EDGE_TYPEDEF_OF: - return "is typedef of"; + return "typedef"; } return ""; } @@ -123,7 +128,7 @@ std::string Edge::getTypeString() const std::string Edge::getAsString() const { std::stringstream str; - str << "[" << getId() << "] \"" << m_from->getName() << "\" " << getTypeString() << " \"" + m_to->getName() << "\""; + str << "[" << getId() << "] " << getTypeString() << ": \"" << m_from->getName() << "\" -> \"" + m_to->getName() << "\""; TokenComponentAccess* component = getComponent(); if (component) diff --git a/src/lib/data/graph/Edge.h b/src/lib/data/graph/Edge.h index 344f9088..5eaca092 100644 --- a/src/lib/data/graph/Edge.h +++ b/src/lib/data/graph/Edge.h @@ -34,6 +34,8 @@ public: Node* getFrom() const; Node* getTo() const; + std::string getName() const; + // Token implementation virtual bool isNode() const; virtual bool isEdge() const; diff --git a/src/lib/data/graph/Graph.cpp b/src/lib/data/graph/Graph.cpp index 25c2e156..835d2849 100644 --- a/src/lib/data/graph/Graph.cpp +++ b/src/lib/data/graph/Graph.cpp @@ -59,12 +59,12 @@ Edge* Graph::getEdgeById(Id id) const Token* Graph::getTokenById(Id id) const { - return findToken( - [id](Token* t) - { - return t->getId() == id; - } - ); + Token* token = getNodeById(id); + if (!token) + { + token = getEdgeById(id); + } + return token; } Node* Graph::createNodeHierarchy(const std::string& fullName) diff --git a/src/test/GraphTestSuite.h b/src/test/GraphTestSuite.h index a8b764cd..657e168a 100644 --- a/src/test/GraphTestSuite.h +++ b/src/test/GraphTestSuite.h @@ -360,6 +360,25 @@ public: TS_ASSERT_DIFFERS(x, b); } + void test_node_has_name_and_full_name() + { + TestGraph graph; + Node* n = graph.createNodeHierarchy("A::B::C"); + + TS_ASSERT_EQUALS(n->getName(), "C"); + TS_ASSERT_EQUALS(n->getFullName(), "A::B::C"); + } + + void test_edge_has_name() + { + TestGraph graph; + Node* a = graph.createNodeHierarchy(Node::NODE_FUNCTION, "A"); + Node* b = graph.createNodeHierarchy(Node::NODE_FUNCTION, "B"); + Edge* e = graph.createEdge(Edge::EDGE_CALL, a, b); + + TS_ASSERT_EQUALS(e->getName(), "call:A->B"); + } + void test_graph_saves_nodes_with_distinct_signatures() { TestGraph graph;