logic: refactored GraphAccess and GraphController to use single call that returns Graph
This change refactors the GraphController to use the GraphAccess only with a single call that returns a Graph. The Graph contains all information for visually representing the active Token with all parent and child nodes and edges. This reduces coupling of the data and logic tiers.
This commit is contained in:
+24
-136
@@ -347,127 +347,39 @@ std::vector<std::string> Storage::getNamesForNodesWithNamePrefix(const std::stri
|
||||
return names;
|
||||
}
|
||||
|
||||
std::vector<Id> Storage::getIdsOfNeighbours(const Id id) const
|
||||
std::shared_ptr<Graph> Storage::getGraphForActiveTokenIds(const std::vector<Id>& tokenIds) const
|
||||
{
|
||||
std::vector<Id> result;
|
||||
std::shared_ptr<Graph> graph = std::make_shared<Graph>();
|
||||
|
||||
Node* node = m_graph.findNode([&](Node* node){
|
||||
return node->getId() == id;
|
||||
});
|
||||
|
||||
if (node != NULL)
|
||||
for (Id tokenId : tokenIds)
|
||||
{
|
||||
std::map<Id, bool> addedIds;
|
||||
addedIds[id] = true;
|
||||
Token* token = m_graph.getTokenById(tokenId);
|
||||
if (!token)
|
||||
{
|
||||
LOG_ERROR_STREAM(<< "Token with id " << tokenId << " was not found");
|
||||
continue;
|
||||
}
|
||||
|
||||
node->forEachEdge(
|
||||
[&result, &addedIds](Edge* e)
|
||||
if (token->isNode())
|
||||
{
|
||||
Node* node = dynamic_cast<Node*>(token);
|
||||
graph->addNodeAndAllChildrenAsPlainCopy(node->getLastParentNode());
|
||||
|
||||
node->forEachEdge(
|
||||
[graph](Edge* edge)
|
||||
{
|
||||
Id fromId = e->getFrom()->getId();
|
||||
if (addedIds.find(fromId) == addedIds.end())
|
||||
{
|
||||
result.push_back(fromId);
|
||||
addedIds[fromId] = true;
|
||||
}
|
||||
|
||||
Id toId = e->getTo()->getId();
|
||||
if (addedIds.find(toId) == addedIds.end())
|
||||
{
|
||||
result.push_back(toId);
|
||||
addedIds[toId] = true;
|
||||
}
|
||||
graph->addEdgeAndAllChildrenAsPlainCopy(edge);
|
||||
}
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
Edge* edge = dynamic_cast<Edge*>(token);
|
||||
graph->addEdgeAndAllChildrenAsPlainCopy(edge);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<std::tuple<Id, Id, Id>> Storage::getNeighbourEdgesOfNode(const Id id) const
|
||||
{
|
||||
std::vector<std::tuple<Id, Id, Id>> result;
|
||||
|
||||
Node* node = m_graph.findNode([&](Node* node){
|
||||
return node->getId() == id;
|
||||
});
|
||||
|
||||
if (node != NULL)
|
||||
{
|
||||
node->forEachEdge(
|
||||
[&result](Edge* e)
|
||||
{
|
||||
result.push_back(std::tuple<Id, Id, Id>(e->getFrom()->getId(), e->getTo()->getId(), e->getId()));
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<std::tuple<Id, Id, Id>> Storage::getMemberEdgesOfNode(const Id id) const
|
||||
{
|
||||
return getEdgesOfTypeOfNode(id, Edge::EdgeType::EDGE_MEMBER);
|
||||
}
|
||||
|
||||
std::vector<std::tuple<Id, Id, Id>> Storage::getUsageEdgesOfNode(const Id id) const
|
||||
{
|
||||
return getEdgesOfTypeOfNode(id, Edge::EdgeType::EDGE_USAGE);
|
||||
}
|
||||
|
||||
std::vector<std::tuple<Id, Id, Id>> Storage::getCallEdgesOfNode(const Id id) const
|
||||
{
|
||||
return getEdgesOfTypeOfNode(id, Edge::EdgeType::EDGE_CALL);
|
||||
}
|
||||
|
||||
std::vector<std::tuple<Id, Id, Id>> Storage::getTypeOfEdgesOfNode(const Id id) const
|
||||
{
|
||||
return getEdgesOfTypeOfNode(id, Edge::EdgeType::EDGE_TYPE_OF);
|
||||
}
|
||||
|
||||
std::vector<std::tuple<Id, Id, Id>> Storage::getReturnTypeOfEdgesOfNode(const Id id) const
|
||||
{
|
||||
return getEdgesOfTypeOfNode(id, Edge::EdgeType::EDGE_RETURN_TYPE_OF);
|
||||
}
|
||||
|
||||
std::vector<std::tuple<Id, Id, Id>> Storage::getParameterOfEdgesOfNode(const Id id) const
|
||||
{
|
||||
return getEdgesOfTypeOfNode(id, Edge::EdgeType::EDGE_PARAMETER_TYPE_OF);
|
||||
}
|
||||
|
||||
std::vector<std::tuple<Id, Id, Id>> Storage::getInheritanceEdgesOfNode(const Id id) const
|
||||
{
|
||||
return getEdgesOfTypeOfNode(id, Edge::EdgeType::EDGE_INHERITANCE);
|
||||
}
|
||||
|
||||
std::pair<Id, Id> Storage::getNodesOfEdge(const Id id) const
|
||||
{
|
||||
std::pair<Id, Id> result;
|
||||
|
||||
Edge* edge = m_graph.findEdge([&](Edge* edge){
|
||||
return edge->getId() == id;
|
||||
});
|
||||
|
||||
if(edge != NULL)
|
||||
{
|
||||
result.first = edge->getFrom()->getId();
|
||||
result.second = edge->getTo()->getId();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool Storage::checkTokenIsNode(const Id id) const
|
||||
{
|
||||
Token* token = m_graph.findToken([&](Token* token){
|
||||
return token->getId() == id;
|
||||
});
|
||||
|
||||
if(token != NULL)
|
||||
{
|
||||
return token->isNode();
|
||||
}
|
||||
|
||||
return false;
|
||||
return graph;
|
||||
}
|
||||
|
||||
std::vector<Id> Storage::getActiveTokenIdsForId(Id tokenId) const
|
||||
@@ -790,27 +702,3 @@ void Storage::log(std::string type, std::string str, const ParseLocation& locati
|
||||
<< location.endLineNumber << ":" << location.endColumnNumber << ">"
|
||||
);
|
||||
}
|
||||
|
||||
std::vector<std::tuple<Id, Id, Id>> Storage::getEdgesOfTypeOfNode(const Id id, const Edge::EdgeType type) const
|
||||
{
|
||||
std::vector<std::tuple<Id, Id, Id>> result;
|
||||
|
||||
Node* node = m_graph.findNode([&](Node* node){
|
||||
return node->getId() == id;
|
||||
});
|
||||
|
||||
if (node != NULL)
|
||||
{
|
||||
node->forEachEdge(
|
||||
[&result, &type](Edge* e)
|
||||
{
|
||||
if(e->getType() == type)
|
||||
{
|
||||
result.push_back(std::tuple<Id, Id, Id>(e->getFrom()->getId(), e->getTo()->getId(), e->getId()));
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
+1
-14
@@ -68,19 +68,8 @@ public:
|
||||
virtual Id getIdForNodeWithName(const std::string& fullName) const;
|
||||
virtual std::string getNameForNodeWithId(Id id) const;
|
||||
virtual std::vector<std::string> getNamesForNodesWithNamePrefix(const std::string& prefix) const;
|
||||
virtual std::vector<Id> getIdsOfNeighbours(const Id id) const;
|
||||
virtual std::vector<std::tuple<Id, Id, Id>> getNeighbourEdgesOfNode(const Id id) const;
|
||||
virtual std::vector<std::tuple<Id, Id, Id>> getMemberEdgesOfNode(const Id id) const;
|
||||
virtual std::vector<std::tuple<Id, Id, Id>> getUsageEdgesOfNode(const Id id) const;
|
||||
virtual std::vector<std::tuple<Id, Id, Id>> getCallEdgesOfNode(const Id id) const;
|
||||
virtual std::vector<std::tuple<Id, Id, Id>> getTypeOfEdgesOfNode(const Id id) const;
|
||||
virtual std::vector<std::tuple<Id, Id, Id>> getReturnTypeOfEdgesOfNode(const Id id) const;
|
||||
virtual std::vector<std::tuple<Id, Id, Id>> getParameterOfEdgesOfNode(const Id id) const;
|
||||
virtual std::vector<std::tuple<Id, Id, Id>> getInheritanceEdgesOfNode(const Id id) const;
|
||||
|
||||
virtual std::pair<Id, Id> getNodesOfEdge(const Id id) const;
|
||||
|
||||
virtual bool checkTokenIsNode(const Id id) const;
|
||||
virtual std::shared_ptr<Graph> getGraphForActiveTokenIds(const std::vector<Id>& tokenIds) const;
|
||||
|
||||
virtual std::vector<Id> getActiveTokenIdsForId(Id tokenId) const;
|
||||
virtual std::vector<Id> getLocationIdsForTokenIds(const std::vector<Id>& tokenIds) const;
|
||||
@@ -117,8 +106,6 @@ private:
|
||||
|
||||
void log(std::string type, std::string str, const ParseLocation& location) const;
|
||||
|
||||
std::vector<std::tuple<Id, Id, Id>> getEdgesOfTypeOfNode(const Id id, const Edge::EdgeType type) const;
|
||||
|
||||
StorageGraph m_graph;
|
||||
TokenLocationCollection m_locationCollection;
|
||||
SearchIndex m_index;
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
#ifndef GRAPH_ACCESS_H
|
||||
#define GRAPH_ACCESS_H
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "data/graph/Graph.h"
|
||||
#include "utility/types.h"
|
||||
|
||||
class Edge;
|
||||
class Token;
|
||||
|
||||
class GraphAccess
|
||||
{
|
||||
public:
|
||||
@@ -17,19 +16,8 @@ public:
|
||||
virtual Id getIdForNodeWithName(const std::string& name) const = 0;
|
||||
virtual std::string getNameForNodeWithId(Id id) const = 0;
|
||||
virtual std::vector<std::string> getNamesForNodesWithNamePrefix(const std::string& prefix) const = 0;
|
||||
virtual std::vector<Id> getIdsOfNeighbours(const Id id) const = 0;
|
||||
virtual std::vector<std::tuple<Id, Id, Id>> getNeighbourEdgesOfNode(const Id id) const = 0;
|
||||
virtual std::vector<std::tuple<Id, Id, Id>> getMemberEdgesOfNode(const Id id) const = 0;
|
||||
virtual std::vector<std::tuple<Id, Id, Id>> getUsageEdgesOfNode(const Id id) const = 0;
|
||||
virtual std::vector<std::tuple<Id, Id, Id>> getCallEdgesOfNode(const Id id) const = 0;
|
||||
virtual std::vector<std::tuple<Id, Id, Id>> getTypeOfEdgesOfNode(const Id id) const = 0;
|
||||
virtual std::vector<std::tuple<Id, Id, Id>> getReturnTypeOfEdgesOfNode(const Id id) const = 0;
|
||||
virtual std::vector<std::tuple<Id, Id, Id>> getParameterOfEdgesOfNode(const Id id) const = 0;
|
||||
virtual std::vector<std::tuple<Id, Id, Id>> getInheritanceEdgesOfNode(const Id id) const = 0;
|
||||
|
||||
virtual std::pair<Id, Id> getNodesOfEdge(const Id id) const = 0;
|
||||
|
||||
virtual bool checkTokenIsNode(const Id id) const = 0;
|
||||
virtual std::shared_ptr<Graph> getGraphForActiveTokenIds(const std::vector<Id>& tokenIds) const = 0;
|
||||
|
||||
virtual std::vector<Id> getActiveTokenIdsForId(Id tokenId) const = 0;
|
||||
virtual std::vector<Id> getLocationIdsForTokenIds(const std::vector<Id>& tokenIds) const = 0;
|
||||
|
||||
@@ -57,114 +57,14 @@ std::vector<std::string> GraphAccessProxy::getNamesForNodesWithNamePrefix(const
|
||||
return std::vector<std::string>();
|
||||
}
|
||||
|
||||
std::vector<Id> GraphAccessProxy::getIdsOfNeighbours(const Id id) const
|
||||
std::shared_ptr<Graph> GraphAccessProxy::getGraphForActiveTokenIds(const std::vector<Id>& tokenIds) const
|
||||
{
|
||||
if (hasSubject())
|
||||
{
|
||||
return m_subject->getIdsOfNeighbours(id);
|
||||
return m_subject->getGraphForActiveTokenIds(tokenIds);
|
||||
}
|
||||
|
||||
return std::vector<Id>();
|
||||
}
|
||||
|
||||
std::vector<std::tuple<Id, Id, Id>> GraphAccessProxy::getNeighbourEdgesOfNode(const Id id) const
|
||||
{
|
||||
if (hasSubject())
|
||||
{
|
||||
return m_subject->getNeighbourEdgesOfNode(id);
|
||||
}
|
||||
|
||||
return std::vector<std::tuple<Id, Id, Id>>();
|
||||
}
|
||||
|
||||
std::vector<std::tuple<Id, Id, Id>> GraphAccessProxy::getMemberEdgesOfNode(const Id id) const
|
||||
{
|
||||
if (hasSubject())
|
||||
{
|
||||
return m_subject->getMemberEdgesOfNode(id);
|
||||
}
|
||||
|
||||
return std::vector<std::tuple<Id, Id, Id>>();
|
||||
}
|
||||
|
||||
std::vector<std::tuple<Id, Id, Id>> GraphAccessProxy::getUsageEdgesOfNode(const Id id) const
|
||||
{
|
||||
if (hasSubject())
|
||||
{
|
||||
return m_subject->getUsageEdgesOfNode(id);
|
||||
}
|
||||
|
||||
return std::vector<std::tuple<Id, Id, Id>>();
|
||||
}
|
||||
|
||||
std::vector<std::tuple<Id, Id, Id>> GraphAccessProxy::getCallEdgesOfNode(const Id id) const
|
||||
{
|
||||
if (hasSubject())
|
||||
{
|
||||
return m_subject->getCallEdgesOfNode(id);
|
||||
}
|
||||
|
||||
return std::vector<std::tuple<Id, Id, Id>>();
|
||||
}
|
||||
|
||||
std::vector<std::tuple<Id, Id, Id>> GraphAccessProxy::getTypeOfEdgesOfNode(const Id id) const
|
||||
{
|
||||
if (hasSubject())
|
||||
{
|
||||
return m_subject->getTypeOfEdgesOfNode(id);
|
||||
}
|
||||
|
||||
return std::vector<std::tuple<Id, Id, Id>>();
|
||||
}
|
||||
|
||||
std::vector<std::tuple<Id, Id, Id>> GraphAccessProxy::getReturnTypeOfEdgesOfNode(const Id id) const
|
||||
{
|
||||
if (hasSubject())
|
||||
{
|
||||
return m_subject->getReturnTypeOfEdgesOfNode(id);
|
||||
}
|
||||
|
||||
return std::vector<std::tuple<Id, Id, Id>>();
|
||||
}
|
||||
|
||||
std::vector<std::tuple<Id, Id, Id>> GraphAccessProxy::getParameterOfEdgesOfNode(const Id id) const
|
||||
{
|
||||
if (hasSubject())
|
||||
{
|
||||
return m_subject->getParameterOfEdgesOfNode(id);
|
||||
}
|
||||
|
||||
return std::vector<std::tuple<Id, Id, Id>>();
|
||||
}
|
||||
|
||||
std::vector<std::tuple<Id, Id, Id>> GraphAccessProxy::getInheritanceEdgesOfNode(const Id id) const
|
||||
{
|
||||
if (hasSubject())
|
||||
{
|
||||
return m_subject->getInheritanceEdgesOfNode(id);
|
||||
}
|
||||
|
||||
return std::vector<std::tuple<Id, Id, Id>>();
|
||||
}
|
||||
|
||||
std::pair<Id, Id> GraphAccessProxy::getNodesOfEdge(const Id id) const
|
||||
{
|
||||
if (hasSubject())
|
||||
{
|
||||
return m_subject->getNodesOfEdge(id);
|
||||
}
|
||||
|
||||
return std::pair<Id, Id>();
|
||||
}
|
||||
|
||||
bool GraphAccessProxy::checkTokenIsNode(const Id id) const
|
||||
{
|
||||
if (hasSubject())
|
||||
{
|
||||
return m_subject->checkTokenIsNode(id);
|
||||
}
|
||||
|
||||
return false;
|
||||
return std::make_shared<Graph>();
|
||||
}
|
||||
|
||||
std::vector<Id> GraphAccessProxy::getActiveTokenIdsForId(Id tokenId) const
|
||||
|
||||
@@ -16,19 +16,8 @@ public:
|
||||
virtual Id getIdForNodeWithName(const std::string& name) const;
|
||||
virtual std::string getNameForNodeWithId(Id id) const;
|
||||
virtual std::vector<std::string> getNamesForNodesWithNamePrefix(const std::string& prefix) const;
|
||||
virtual std::vector<Id> getIdsOfNeighbours(const Id id) const;
|
||||
virtual std::vector<std::tuple<Id, Id, Id>> getNeighbourEdgesOfNode(const Id id) const;
|
||||
virtual std::vector<std::tuple<Id, Id, Id>> getMemberEdgesOfNode(const Id id) const;
|
||||
virtual std::vector<std::tuple<Id, Id, Id>> getUsageEdgesOfNode(const Id id) const;
|
||||
virtual std::vector<std::tuple<Id, Id, Id>> getCallEdgesOfNode(const Id id) const;
|
||||
virtual std::vector<std::tuple<Id, Id, Id>> getTypeOfEdgesOfNode(const Id id) const;
|
||||
virtual std::vector<std::tuple<Id, Id, Id>> getReturnTypeOfEdgesOfNode(const Id id) const;
|
||||
virtual std::vector<std::tuple<Id, Id, Id>> getParameterOfEdgesOfNode(const Id id) const;
|
||||
virtual std::vector<std::tuple<Id, Id, Id>> getInheritanceEdgesOfNode(const Id id) const;
|
||||
|
||||
virtual std::pair<Id, Id> getNodesOfEdge(const Id id) const;
|
||||
|
||||
virtual bool checkTokenIsNode(const Id id) const;
|
||||
virtual std::shared_ptr<Graph> getGraphForActiveTokenIds(const std::vector<Id>& tokenIds) const;
|
||||
|
||||
virtual std::vector<Id> getActiveTokenIdsForId(Id tokenId) const;
|
||||
virtual std::vector<Id> getLocationIdsForTokenIds(const std::vector<Id>& tokenIds) const;
|
||||
|
||||
@@ -244,6 +244,32 @@ Edge* Graph::addEdgeAsPlainCopy(Edge* edge)
|
||||
return copy.get();
|
||||
}
|
||||
|
||||
Node* Graph::addNodeAndAllChildrenAsPlainCopy(Node* node)
|
||||
{
|
||||
Node* n = addNodeAsPlainCopy(node);
|
||||
|
||||
node->forEachEdgeOfType(Edge::EDGE_MEMBER,
|
||||
[node, this](Edge* edge)
|
||||
{
|
||||
if (edge->getFrom() == node)
|
||||
{
|
||||
addEdgeAsPlainCopy(edge);
|
||||
addNodeAndAllChildrenAsPlainCopy(edge->getTo());
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
Edge* Graph::addEdgeAndAllChildrenAsPlainCopy(Edge* edge)
|
||||
{
|
||||
addNodeAndAllChildrenAsPlainCopy(edge->getFrom()->getLastParentNode());
|
||||
addNodeAndAllChildrenAsPlainCopy(edge->getTo()->getLastParentNode());
|
||||
|
||||
return addEdgeAsPlainCopy(edge);
|
||||
}
|
||||
|
||||
void Graph::removeEdgeInternal(Edge* edge)
|
||||
{
|
||||
std::map<Id, std::shared_ptr<Edge> >::const_iterator it = m_edges.find(edge->getId());
|
||||
|
||||
@@ -49,6 +49,9 @@ public:
|
||||
Node* addNodeAsPlainCopy(Node* node);
|
||||
Edge* addEdgeAsPlainCopy(Edge* edge);
|
||||
|
||||
Node* addNodeAndAllChildrenAsPlainCopy(Node* node);
|
||||
Edge* addEdgeAndAllChildrenAsPlainCopy(Edge* edge);
|
||||
|
||||
protected:
|
||||
std::map<Id, std::shared_ptr<Node>> m_nodes;
|
||||
std::map<Id, std::shared_ptr<Edge>> m_edges;
|
||||
|
||||
@@ -89,6 +89,16 @@ Node* Node::getParentNode() const
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Node* Node::getLastParentNode()
|
||||
{
|
||||
Node* parent = getParentNode();
|
||||
if (parent)
|
||||
{
|
||||
return parent->getLastParentNode();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
Edge* Node::getMemberEdge() const
|
||||
{
|
||||
return findEdgeOfType(Edge::EDGE_MEMBER,
|
||||
@@ -179,9 +189,12 @@ void Node::forEachEdgeOfType(Edge::EdgeType type, std::function<void(Edge*)> fun
|
||||
void Node::forEachChildNode(std::function<void(Node*)> func) const
|
||||
{
|
||||
forEachEdgeOfType(Edge::EDGE_MEMBER,
|
||||
[func](Edge* e)
|
||||
[func, this](Edge* e)
|
||||
{
|
||||
func(e->getTo());
|
||||
if (this != e->getTo())
|
||||
{
|
||||
func(e->getTo());
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@@ -54,6 +54,7 @@ public:
|
||||
void removeEdge(Edge* edge);
|
||||
|
||||
Node* getParentNode() const;
|
||||
Node* getLastParentNode();
|
||||
Edge* getMemberEdge() const;
|
||||
|
||||
Edge* findEdge(std::function<bool(Edge*)> func) const;
|
||||
|
||||
Reference in New Issue
Block a user