data: Moved EdgeComponent system to Token and extended it to cover all type specific Edge and Token fields

This change moves the EdgeComponent system to Token and redefines them as TokenComponents.
* Performance tests on the side showed that using typeid() in getComponent() is faster than dynamically casting every
pointer.
* The hasComponent() method was deliberately left out in the implementation, because it used to look up the right
component and discard it again. This check for presence can also be achieved by just using getComponent().
* The method addComponent() is protected in Token, so that Node and Edge can guard which Component is set on which type
by implementing specific setters for each Component.
* The GraphTestSuite was extended to include tests for the Component implementation (and missing tests for locationIds
in Token and copying of Node and Edge were added).
This commit is contained in:
Eberhard Graether
2014-07-26 02:26:59 +02:00
parent 4262be133a
commit 641acc2bde
29 changed files with 711 additions and 443 deletions
+44 -67
View File
@@ -3,45 +3,41 @@
#include <sstream>
#include "data/graph/Node.h"
#include "data/graph/edgeComponent/EdgeComponent.h"
#include "data/graph/token_component/TokenComponentAccess.h"
#include "data/graph/token_component/TokenComponentDataType.h"
#include "utility/logging/logging.h"
Edge::Edge(EdgeType type, Node* from, Node* to)
: m_type(type)
, m_from(from)
, m_to(to)
, m_access(ACCESS_NONE)
{
m_from->addEdge(this);
m_to->addEdge(this);
}
Edge::Edge(const Edge& other, Node* from, Node* to)
: Token(other)
, m_type(other.m_type)
, m_from(from)
, m_to(to)
{
m_from->addEdge(this);
m_to->addEdge(this);
if (m_from == other.m_from || m_to == other.m_to ||
m_from->getId() != other.m_from->getId() || m_to->getId() != other.m_to->getId())
{
LOG_ERROR("Nodes are not plain copies.");
}
}
Edge::~Edge()
{
m_from->removeEdge(this);
m_to->removeEdge(this);
}
std::shared_ptr<Edge> Edge::createPlainCopy(Node* from, Node* to) const
{
if (from == m_from || to == m_to || from->getId() != m_from->getId() || to->getId() != m_to->getId())
{
LOG_ERROR("Nodes are not plain copies.");
return nullptr;
}
std::shared_ptr<Edge> edge(new Edge(getId(), m_type, from, to));
edge->setAccess(m_access);
for (std::shared_ptr<EdgeComponent> component: m_components)
{
edge->addComponent(component->copy());
}
return edge;
}
Edge::EdgeType Edge::getType() const
{
return m_type;
@@ -67,24 +63,37 @@ bool Edge::isEdge() const
return true;
}
Edge::AccessType Edge::getAccess() const
void Edge::addComponentAccess(std::shared_ptr<TokenComponentAccess> component)
{
return m_access;
if (getComponent<TokenComponentAccess>())
{
LOG_ERROR("TokenComponentAccess has been set before!");
}
else if (m_type != EDGE_MEMBER && m_type != EDGE_INHERITANCE)
{
LOG_ERROR("TokenComponentAccess can't be set on edge of type: " + getTypeString());
}
else
{
addComponent(component);
}
}
void Edge::setAccess(AccessType access)
void Edge::addComponentDataType(std::shared_ptr<TokenComponentDataType> component)
{
if (access != ACCESS_NONE && m_type != EDGE_MEMBER && m_type != EDGE_INHERITANCE)
if (getComponent<TokenComponentDataType>())
{
LOG_ERROR("Setting access on wrong edge type.");
return;
LOG_ERROR("TokenComponentDataType has been set before!");
}
if (m_access != ACCESS_NONE && access != m_access)
else if (m_type != EDGE_TYPEDEF_OF && m_type != EDGE_TYPE_OF
&& m_type != EDGE_RETURN_TYPE_OF && m_type != EDGE_PARAMETER_TYPE_OF)
{
LOG_WARNING("Different AccessType was already set before.");
LOG_ERROR("TokenComponentDataType can't be set on edge of type: " + getTypeString());
}
else
{
addComponent(component);
}
m_access = access;
}
std::string Edge::getTypeString() const
@@ -111,52 +120,20 @@ std::string Edge::getTypeString() const
return "";
}
std::string Edge::getAccessString() const
{
switch (m_access)
{
case ACCESS_PUBLIC:
return "public";
case ACCESS_PROTECTED:
return "protected";
case ACCESS_PRIVATE:
return "private";
case ACCESS_NONE:
return "";
}
return "";
}
std::string Edge::getAsString() const
{
std::stringstream str;
str << "[" << getId() << "] \"" << m_from->getName() << "\" " << getTypeString() << " \"" + m_to->getName() << "\"";
if (m_access != ACCESS_NONE)
TokenComponentAccess* component = getComponent<TokenComponentAccess>();
if (component)
{
str << " " << getAccessString();
str << " " << component->getAccessString();
}
return str.str();
}
void Edge::addComponent(std::shared_ptr<EdgeComponent> component)
{
m_components.push_back(component);
component->setEdge(this);
}
Edge::Edge(Id id, EdgeType type, Node* from, Node* to)
: Token(id)
, m_type(type)
, m_from(from)
, m_to(to)
, m_access(ACCESS_NONE)
{
m_from->addEdge(this);
m_to->addEdge(this);
}
std::ostream& operator<<(std::ostream& ostream, const Edge& edge)
{
ostream << edge.getAsString();