data: database storage

* replaced storage by a new version that utilizes sqlite for persistence.
* added SqliteStorage to manage direct access to the database.
* added testsuit for sqlite.
* slimmed down the protected interface of the storage.
* changed tests in StorageTestSuit to avoid using protected methods of the storage.
* added functions to begin and commit a transaction to the storage.
* removed GraphFilterTestSuite.
* removed GraphFilterConductorTestSuite.
* added Node and Edge constructor that takes id as parameter
* added constructor for TokenComponentNameCached that accepts NameHierarchy as parameter
* added TokenLocation constructor that takes locationId as parameter.
* created respective construction functions in TokenLocatonLine, -File and -Collection.

fortune cookie message = Recognition will come from unexpected sources.
This commit is contained in:
malte_langkabel
2015-08-25 18:07:02 +02:00
parent d996cf384d
commit e628241b02
63 changed files with 167982 additions and 3582 deletions
+56
View File
@@ -8,6 +8,50 @@
#include "utility/logging/logging.h"
#include "utility/utilityString.h"
int Edge::typeToInt(EdgeType type)
{
return type;
}
Edge::EdgeType Edge::intToType(int value)
{
switch (value)
{
case 0x1:
return EDGE_MEMBER;
case 0x2:
return EDGE_TYPE_OF;
case 0x4:
return EDGE_RETURN_TYPE_OF;
case 0x8:
return EDGE_PARAMETER_TYPE_OF;
case 0x10:
return EDGE_TYPE_USAGE;
case 0x20:
return EDGE_USAGE;
case 0x40:
return EDGE_CALL;
case 0x80:
return EDGE_INHERITANCE;
case 0x100:
return EDGE_OVERRIDE;
case 0x200:
return EDGE_TYPEDEF_OF;
case 0x400:
return EDGE_TEMPLATE_PARAMETER_OF;
case 0x800:
return EDGE_TEMPLATE_ARGUMENT_OF;
case 0x1000:
return EDGE_TEMPLATE_DEFAULT_ARGUMENT_OF;
case 0x2000:
return EDGE_TEMPLATE_SPECIALIZATION_OF;
case 0x4000:
return EDGE_INCLUDE;
case 0x8000:
return EDGE_AGGREGATION;
}
}
Edge::Edge(EdgeType type, Node* from, Node* to)
: m_type(type)
, m_from(from)
@@ -19,6 +63,18 @@ Edge::Edge(EdgeType type, Node* from, Node* to)
checkType();
}
Edge::Edge(Id id, EdgeType type, Node* from, Node* to)
: Token(id)
, m_type(type)
, m_from(from)
, m_to(to)
{
m_from->addEdge(this);
m_to->addEdge(this);
checkType();
}
Edge::Edge(const Edge& other, Node* from, Node* to)
: Token(other)
, m_type(other.m_type)
+3
View File
@@ -36,8 +36,11 @@ public:
EDGE_AGGREGATION = 0x8000
};
static int typeToInt(EdgeType type);
static EdgeType intToType(int value);
Edge(EdgeType type, Node* from, Node* to);
Edge(Id id, EdgeType type, Node* from, Node* to);
Edge(const Edge& other, Node* from, Node* to);
virtual ~Edge();
-68
View File
@@ -1,68 +0,0 @@
#include "data/graph/FilterableGraph.h"
#include "data/graph/Edge.h"
#include "data/graph/Node.h"
FilterableGraph::FilterableGraph()
{
}
FilterableGraph::~FilterableGraph()
{
}
size_t FilterableGraph::size() const
{
return getNodeCount() + getEdgeCount();
}
Token* FilterableGraph::getTokenById(Id id) const
{
Token* token = getNodeById(id);
if (!token)
{
token = getEdgeById(id);
}
return token;
}
void FilterableGraph::print(std::ostream& ostream) const
{
ostream << "Graph:\n";
ostream << "nodes (" << getNodeCount() << ")\n";
forEachNode(
[&ostream](Node* n)
{
ostream << *n << '\n';
}
);
ostream << "edges (" << getEdgeCount() << ")\n";
forEachEdge(
[&ostream](Edge* e)
{
ostream << *e << '\n';
}
);
}
void FilterableGraph::printBasic(std::ostream& ostream) const
{
ostream << getNodeCount() << " nodes:";
forEachNode(
[&ostream](Node* n)
{
ostream << ' ' << n->getTypeString() << ':' << n->getFullName();
}
);
ostream << '\n';
ostream << getEdgeCount() << " edges:";
forEachEdge(
[&ostream](Edge* e)
{
ostream << ' ' << e->getName();
}
);
ostream << '\n';
}
-45
View File
@@ -1,45 +0,0 @@
#ifndef FILTERABLE_GRAPH_H
#define FILTERABLE_GRAPH_H
#include <functional>
#include <ostream>
#include "utility/types.h"
class Edge;
class Node;
class Token;
class FilterableGraph
{
public:
FilterableGraph();
virtual ~FilterableGraph();
virtual void copy(const FilterableGraph* other) = 0;
virtual void clear() = 0;
virtual void add(const FilterableGraph* other) = 0;
virtual void forEachNode(std::function<void(Node*)> func) const = 0;
virtual void forEachEdge(std::function<void(Edge*)> func) const = 0;
virtual void forEachToken(std::function<void(Token*)> func) const = 0;
virtual void addNode(Node* node) = 0;
virtual void addEdge(Edge* edge) = 0;
virtual size_t getNodeCount() const = 0;
virtual size_t getEdgeCount() const = 0;
virtual Node* getNodeById(Id id) const = 0;
virtual Edge* getEdgeById(Id id) const = 0;
size_t size() const;
Token* getTokenById(Id id) const;
void print(std::ostream& ostream) const;
void printBasic(std::ostream& ostream) const;
};
#endif // FILTERABLE_GRAPH_H
+58 -2
View File
@@ -12,7 +12,7 @@ Graph::~Graph()
m_nodes.clear();
}
void Graph::copy(const FilterableGraph* other)
void Graph::copy(const Graph* other)
{
clear();
add(other);
@@ -24,7 +24,7 @@ void Graph::clear()
m_nodes.clear();
}
void Graph::add(const FilterableGraph* other)
void Graph::add(const Graph* other)
{
other->forEachNode(std::bind(&Graph::addNode, this, std::placeholders::_1));
other->forEachEdge(std::bind(&Graph::addEdge, this, std::placeholders::_1));
@@ -282,6 +282,62 @@ Edge* Graph::addEdgeAndAllChildrenAsPlainCopy(Edge* edge)
return addEdgeAsPlainCopy(edge);
}
size_t Graph::size() const
{
return getNodeCount() + getEdgeCount();
}
Token* Graph::getTokenById(Id id) const
{
Token* token = getNodeById(id);
if (!token)
{
token = getEdgeById(id);
}
return token;
}
void Graph::print(std::ostream& ostream) const
{
ostream << "Graph:\n";
ostream << "nodes (" << getNodeCount() << ")\n";
forEachNode(
[&ostream](Node* n)
{
ostream << *n << '\n';
}
);
ostream << "edges (" << getEdgeCount() << ")\n";
forEachEdge(
[&ostream](Edge* e)
{
ostream << *e << '\n';
}
);
}
void Graph::printBasic(std::ostream& ostream) const
{
ostream << getNodeCount() << " nodes:";
forEachNode(
[&ostream](Node* n)
{
ostream << ' ' << n->getTypeString() << ':' << n->getFullName();
}
);
ostream << '\n';
ostream << getEdgeCount() << " edges:";
forEachEdge(
[&ostream](Edge* e)
{
ostream << ' ' << e->getName();
}
);
ostream << '\n';
}
void Graph::removeEdgeInternal(Edge* edge)
{
std::map<Id, std::shared_ptr<Edge> >::const_iterator it = m_edges.find(edge->getId());
+10 -5
View File
@@ -6,21 +6,19 @@
#include <deque>
#include "data/graph/Edge.h"
#include "data/graph/FilterableGraph.h"
#include "data/graph/Node.h"
class Graph
: public FilterableGraph
{
public:
Graph();
virtual ~Graph();
// FilterableGraph implementation
virtual void copy(const FilterableGraph* other);
// FilterableGraph implementation // deprecated
virtual void copy(const Graph* other);
virtual void clear();
virtual void add(const FilterableGraph* other);
virtual void add(const Graph* other);
virtual void forEachNode(std::function<void(Node*)> func) const;
virtual void forEachEdge(std::function<void(Edge*)> func) const;
@@ -52,6 +50,13 @@ public:
Node* addNodeAndAllChildrenAsPlainCopy(Node* node);
Edge* addEdgeAndAllChildrenAsPlainCopy(Edge* edge);
size_t size() const;
Token* getTokenById(Id id) const;
void print(std::ostream& ostream) const;
void printBasic(std::ostream& ostream) const;
protected:
std::map<Id, std::shared_ptr<Node>> m_nodes;
std::map<Id, std::shared_ptr<Edge>> m_edges;
+52
View File
@@ -18,6 +18,13 @@ Node::Node(NodeType type, std::shared_ptr<TokenComponentName> nameComponent)
{
}
Node::Node(Id id, NodeType type, std::shared_ptr<TokenComponentName> nameComponent)
: Token(id)
, m_type(type)
, m_nameComponent(nameComponent)
{
}
Node::Node(const Node& other)
: Token(other)
, m_type(other.m_type)
@@ -371,6 +378,51 @@ std::string Node::getTypeString(NodeType type)
return "";
}
int Node::typeToInt(NodeType type)
{
return type;
}
Node::NodeType Node::intToType(int value)
{
switch (value)
{
case 0x1:
return NODE_UNDEFINED;
case 0x2:
return NODE_UNDEFINED_TYPE;
case 0x4:
return NODE_UNDEFINED_VARIABLE;
case 0x8:
return NODE_UNDEFINED_FUNCTION;
case 0x10:
return NODE_STRUCT;
case 0x20:
return NODE_CLASS;
case 0x40:
return NODE_GLOBAL_VARIABLE;
case 0x80:
return NODE_FIELD;
case 0x100:
return NODE_FUNCTION;
case 0x200:
return NODE_METHOD;
case 0x400:
return NODE_NAMESPACE;
case 0x800:
return NODE_ENUM;
case 0x1000:
return NODE_ENUM_CONSTANT;
case 0x2000:
return NODE_TYPEDEF;
case 0x4000:
return NODE_TEMPLATE_PARAMETER_TYPE;
case 0x8000:
return NODE_FILE;
}
return NODE_UNDEFINED;
}
std::string Node::getTypeString() const
{
return getTypeString(m_type);
+3
View File
@@ -44,8 +44,11 @@ public:
};
static std::string getTypeString(NodeType type);
static int typeToInt(NodeType type);
static NodeType intToType(int value);
Node(NodeType type, std::shared_ptr<TokenComponentName> nameComponent);
Node(Id id, NodeType type, std::shared_ptr<TokenComponentName> nameComponent);
Node(const Node& other);
virtual ~Node();
-217
View File
@@ -1,217 +0,0 @@
#include "data/graph/StorageGraph.h"
#include "data/graph/token_component/TokenComponentAggregation.h"
#include "data/graph/token_component/TokenComponentName.h"
#include "utility/logging/logging.h"
#include "utility/utilityString.h"
StorageGraph::StorageGraph()
{
}
StorageGraph::~StorageGraph()
{
}
Node* StorageGraph::createNodeHierarchy(Node::NodeType type, SearchNode* searchNode)
{
Node* node = getNodeById(searchNode->getFirstTokenId());
if (!node)
{
return insertNodeHierarchy(type, searchNode);
}
if (node->getType() < type)
{
node->setType(type);
}
return node;
}
Node* StorageGraph::createNodeHierarchyWithDistinctSignature(
Node::NodeType type, SearchNode* searchNode, std::shared_ptr<TokenComponentSignature> signature
){
Node* node = getNodeById(searchNode->getFirstTokenId());
if (!node)
{
node = insertNodeHierarchy(type, searchNode);
}
else
{
std::function<bool(Node*)> findSignature =
[signature](Node* n)
{
TokenComponentSignature* sig = n->getComponent<TokenComponentSignature>();
return sig && *sig == *signature.get();
};
Node* parentNode = node->getParentNode();
if (parentNode)
{
node = parentNode->findChildNode(findSignature);
}
else
{
node = findNode(findSignature);
}
if (!node)
{
node = insertNode(type, parentNode, searchNode);
}
else
{
if (node->getType() < type)
{
node->setType(type);
}
return node;
}
}
node->addComponentSignature(signature);
return node;
}
Edge* StorageGraph::createEdge(Edge::EdgeType type, Node* from, Node* to)
{
Edge* edge = from->findEdgeOfType(type,
[from, to](Edge* e)
{
return e->getFrom() == from && e->getTo() == to;
}
);
if (edge)
{
return edge;
}
edge = insertEdge(type, from, to);
if (from->getLastParentNode() != to->getLastParentNode())
{
Id edgeId = edge->getId();
updateAggregationEdges(from->getParentNode(), to, edgeId, 0);
updateAggregationEdges(from, to->getParentNode(), edgeId, 0);
}
return edge;
}
void StorageGraph::removeEdge(Edge* edge)
{
Node* from = edge->getFrom();
Node* to = edge->getTo();
if (from->getLastParentNode() != to->getLastParentNode())
{
Id edgeId = edge->getId();
updateAggregationEdges(from->getParentNode(), to, 0, edgeId);
updateAggregationEdges(from, to->getParentNode(), 0, edgeId);
}
Graph::removeEdge(edge);
}
Node* StorageGraph::insertNodeHierarchy(Node::NodeType type, SearchNode* searchNode)
{
std::deque<SearchNode*> searchNodes = searchNode->getParentsWithoutTokenId();
if (!searchNodes.size())
{
LOG_ERROR("There are no nodes without a set tokenId so this method shouldn't have been called.");
return nullptr;
}
Node* parentNode = nullptr;
SearchNode* parentSearchNode = searchNodes.front()->getParent();
if (parentSearchNode)
{
parentNode = getNodeById(parentSearchNode->getFirstTokenId());
}
while (searchNodes.size() > 0)
{
searchNode = searchNodes.front();
searchNodes.pop_front();
parentNode = insertNode(searchNodes.size() > 0 ? Node::NODE_UNDEFINED : type, parentNode, searchNode);
}
return parentNode;
}
Node* StorageGraph::insertNode(Node::NodeType type, Node* parentNode, SearchNode* searchNode)
{
std::shared_ptr<Node> node =
std::make_shared<Node>(type, std::make_shared<TokenComponentNameReferenced>(searchNode));
m_nodes.emplace(node->getId(), node);
searchNode->addTokenId(node->getId());
if (parentNode)
{
createEdge(Edge::EDGE_MEMBER, parentNode, node.get());
}
return node.get();
}
Edge* StorageGraph::insertEdge(Edge::EdgeType type, Node* from, Node* to)
{
std::shared_ptr<Edge> edgePtr = std::make_shared<Edge>(type, from, to);
m_edges.emplace(edgePtr->getId(), edgePtr);
return edgePtr.get();
}
void StorageGraph::updateAggregationEdges(Node* from, Node* to, Id addEdgeId, Id removeEdgeId)
{
if (!from || !to || from == to)
{
return;
}
const Node::NodeTypeMask mask = Node::NODE_UNDEFINED_TYPE | Node::NODE_CLASS | Node::NODE_STRUCT | Node::NODE_ENUM;
const Node::NodeTypeMask varFuncMask =
Node::NODE_UNDEFINED_FUNCTION | Node::NODE_FUNCTION | Node::NODE_UNDEFINED_VARIABLE | Node::NODE_GLOBAL_VARIABLE;
if ((from->isType(mask) && to->isType(mask | varFuncMask)) ||
(from->isType(mask | varFuncMask) && to->isType(mask)))
{
Edge* edge = from->findEdgeOfType(Edge::EDGE_AGGREGATION,
[from, to](Edge* e)
{
const Node* f = e->getFrom();
const Node* t = e->getTo();
return (f == from && t == to) || (f == to && t == from);
}
);
if (!edge && addEdgeId)
{
edge = insertEdge(Edge::EDGE_AGGREGATION, from, to);
edge->addComponentAggregation(std::make_shared<TokenComponentAggregation>());
}
if (addEdgeId)
{
bool forward = (edge->getFrom() == from);
edge->getComponent<TokenComponentAggregation>()->addAggregationId(addEdgeId, forward);
}
if (edge && removeEdgeId)
{
edge->getComponent<TokenComponentAggregation>()->removeAggregationId(removeEdgeId);
}
if (edge && edge->getComponent<TokenComponentAggregation>()->getAggregationCount() == 0)
{
Graph::removeEdge(edge);
}
}
updateAggregationEdges(from->getParentNode(), to, addEdgeId, removeEdgeId);
updateAggregationEdges(from, to->getParentNode(), addEdgeId, removeEdgeId);
}
-29
View File
@@ -1,29 +0,0 @@
#ifndef STORAGE_GRAPH_H
#define STORAGE_GRAPH_H
#include "data/graph/Graph.h"
#include "data/graph/token_component/TokenComponentSignature.h"
#include "data/search/SearchNode.h"
class StorageGraph
: public Graph
{
public:
StorageGraph();
virtual ~StorageGraph();
Node* createNodeHierarchy(Node::NodeType type, SearchNode* searchNode);
Node* createNodeHierarchyWithDistinctSignature(
Node::NodeType type, SearchNode* searchNode, std::shared_ptr<TokenComponentSignature> signature);
Edge* createEdge(Edge::EdgeType type, Node* from, Node* to);
void removeEdge(Edge* edge);
private:
Node* insertNodeHierarchy(Node::NodeType type, SearchNode* searchNode);
Node* insertNode(Node::NodeType type, Node* parentNode, SearchNode* searchNode);
Edge* insertEdge(Edge::EdgeType type, Node* from, Node* to);
void updateAggregationEdges(Node* from, Node* to, Id addEdgeId, Id removeEdgeId);
};
#endif // STORAGE_GRAPH_H
-136
View File
@@ -1,136 +0,0 @@
#include "data/graph/SubGraph.h"
#include "data/graph/Edge.h"
#include "data/graph/Node.h"
SubGraph::SubGraph()
{
}
SubGraph::~SubGraph()
{
}
void SubGraph::copy(const FilterableGraph* other)
{
clear();
add(other);
}
void SubGraph::clear()
{
m_edges.clear();
m_nodes.clear();
}
void SubGraph::add(const FilterableGraph* other)
{
other->forEachNode(std::bind(&SubGraph::addNode, this, std::placeholders::_1));
other->forEachEdge(std::bind(&SubGraph::addEdge, this, std::placeholders::_1));
}
void SubGraph::forEachNode(std::function<void(Node*)> func) const
{
for (const std::pair<Id, Node*>& node : m_nodes)
{
func(node.second);
}
}
void SubGraph::forEachEdge(std::function<void(Edge*)> func) const
{
for (const std::pair<Id, Edge*>& edge : m_edges)
{
func(edge.second);
}
}
void SubGraph::forEachToken(std::function<void(Token*)> func) const
{
forEachNode(func);
forEachEdge(func);
}
void SubGraph::addNode(Node* node)
{
m_nodes.emplace(node->getId(), node);
}
void SubGraph::addEdge(Edge* edge)
{
m_edges.emplace(edge->getId(), edge);
}
size_t SubGraph::getNodeCount() const
{
return m_nodes.size();
}
size_t SubGraph::getEdgeCount() const
{
return m_edges.size();
}
Node* SubGraph::getNodeById(Id id) const
{
std::map<Id, Node*>::const_iterator it = m_nodes.find(id);
if (it != m_nodes.end())
{
return it->second;
}
return nullptr;
}
Edge* SubGraph::getEdgeById(Id id) const
{
std::map<Id, Edge*>::const_iterator it = m_edges.find(id);
if (it != m_edges.end())
{
return it->second;
}
return nullptr;
}
const std::map<Id, Node*>& SubGraph::getNodes() const
{
return m_nodes;
}
const std::map<Id, Edge*>& SubGraph::getEdges() const
{
return m_edges;
}
std::vector<Id> SubGraph::getTokenIds() const
{
std::vector<Id> ids;
for (const std::pair<Id, Node*>& node : m_nodes)
{
ids.push_back(node.first);
}
for (const std::pair<Id, Edge*>& edge : m_edges)
{
ids.push_back(edge.first);
}
return ids;
}
void SubGraph::subtract(const SubGraph& other)
{
for (const std::pair<Id, Node*>& node : other.m_nodes)
{
m_nodes.erase(node.first);
}
for (const std::pair<Id, Edge*>& edge : other.m_edges)
{
m_edges.erase(edge.first);
}
}
std::ostream& operator<<(std::ostream& ostream, const SubGraph& graph)
{
graph.print(ostream);
return ostream;
}
-54
View File
@@ -1,54 +0,0 @@
#ifndef SUB_GRAPH_H
#define SUB_GRAPH_H
#include <functional>
#include <map>
#include <vector>
#include "data/graph/FilterableGraph.h"
class Edge;
class Node;
class Token;
class SubGraph
: public FilterableGraph
{
public:
SubGraph();
virtual ~SubGraph();
// FilterableGraph implementation
virtual void copy(const FilterableGraph* other);
virtual void clear();
virtual void add(const FilterableGraph* other);
virtual void forEachNode(std::function<void(Node*)> func) const;
virtual void forEachEdge(std::function<void(Edge*)> func) const;
virtual void forEachToken(std::function<void(Token*)> func) const;
virtual void addNode(Node* node);
virtual void addEdge(Edge* edge);
virtual size_t getNodeCount() const;
virtual size_t getEdgeCount() const;
virtual Node* getNodeById(Id id) const;
virtual Edge* getEdgeById(Id id) const;
const std::map<Id, Node*>& getNodes() const;
const std::map<Id, Edge*>& getEdges() const;
std::vector<Id> getTokenIds() const;
void subtract(const SubGraph& other);
private:
std::map<Id, Node*> m_nodes;
std::map<Id, Edge*> m_edges;
};
std::ostream& operator<<(std::ostream& ostream, const SubGraph& graph);
#endif // SUB_GRAPH_H
+6 -1
View File
@@ -8,11 +8,16 @@ void Token::resetNextId()
s_nextId = 1;
}
Token::Token()
Token::Token() // TODO: remove this constructor
: m_id(s_nextId++)
{
}
Token::Token(Id id)
: m_id(id)
{
}
Token::~Token()
{
}
+1
View File
@@ -13,6 +13,7 @@ public:
static void resetNextId();
Token();
Token(Id id);
virtual ~Token();
Id getId() const;
-66
View File
@@ -1,66 +0,0 @@
#include "data/graph/filter/GraphFilter.h"
#include "data/graph/Edge.h"
#include "data/graph/FilterableGraph.h"
#include "utility/logging/logging.h"
GraphFilter::GraphFilter()
: m_outGraph(nullptr)
{
}
GraphFilter::~GraphFilter()
{
}
void GraphFilter::apply(const FilterableGraph* in, FilterableGraph* out)
{
if (!in || !out)
{
LOG_ERROR("GraphFilter not called with correct pointers.");
return;
}
else if (in == out)
{
LOG_ERROR("In and out graphs are the same.");
return;
}
m_outGraph = out;
in->forEachNode(
[this](Node* node)
{
visitNode(node);
}
);
// in->forEachEdge(
// [this](Edge* edge)
// {
// visitEdge(edge);
// }
// );
m_outGraph = nullptr;
}
void GraphFilter::visitNode(Node* node)
{
}
// void GraphFilter::visitEdge(Edge* edge)
// {
// }
void GraphFilter::addNode(Node* node)
{
m_outGraph->addNode(node);
}
void GraphFilter::addEdge(Edge* edge)
{
m_outGraph->addNode(edge->getFrom());
m_outGraph->addNode(edge->getTo());
m_outGraph->addEdge(edge);
}
-27
View File
@@ -1,27 +0,0 @@
#ifndef GRAPH_FILTER_H
#define GRAPH_FILTER_H
class Edge;
class FilterableGraph;
class Node;
class GraphFilter
{
public:
GraphFilter();
virtual ~GraphFilter();
virtual void apply(const FilterableGraph* in, FilterableGraph* out);
protected:
virtual void visitNode(Node* node);
// virtual void visitEdge(Edge* edge);
void addNode(Node* node);
void addEdge(Edge* edge);
private:
FilterableGraph* m_outGraph;
};
#endif // GRAPH_FILTER_H
@@ -1,196 +0,0 @@
#include "data/graph/filter/GraphFilterConductor.h"
#include "data/graph/filter/GraphFilter.h"
#include "data/graph/filter/GraphFilterImplementations.h"
#include "data/graph/SubGraph.h"
#include "data/query/QueryCommand.h"
#include "data/query/QueryNode.h"
#include "data/query/QueryOperator.h"
#include "data/query/QueryToken.h"
#include "data/query/QueryTree.h"
#include "utility/logging/logging.h"
GraphFilterConductor::GraphFilterConductor()
{
}
GraphFilterConductor::~GraphFilterConductor()
{
}
void GraphFilterConductor::filter(const QueryTree* tree, const FilterableGraph* in, FilterableGraph* out)
{
if (tree->isValid())
{
m_inGraph = in;
filterRecursively(tree->getRoot().get(), in, out);
}
}
void GraphFilterConductor::filterRecursively(const QueryNode* node, const FilterableGraph* in, FilterableGraph* out) const
{
if (!node)
{
return;
}
if (node->isOperator())
{
filterOperatorNode(dynamic_cast<const QueryOperator*>(node), in, out);
}
else if (node->isCommand())
{
filterCommandNode(dynamic_cast<const QueryCommand*>(node), in, out);
}
else if (node->isToken())
{
filterTokenNode(dynamic_cast<const QueryToken*>(node), out);
}
}
void GraphFilterConductor::filterOperatorNode(const QueryOperator* node, const FilterableGraph* in, FilterableGraph* out) const
{
switch (node->getType())
{
case QueryOperator::OPERATOR_NOT:
{
SubGraph sub, sub2;
filterRecursively(node->getRight().get(), in, &sub);
sub2.copy(in);
sub2.subtract(sub);
out->add(&sub2);
}
break;
case QueryOperator::OPERATOR_SUB:
case QueryOperator::OPERATOR_AND:
{
SubGraph sub;
filterRecursively(node->getLeft().get(), in, &sub);
filterRecursively(node->getRight().get(), &sub, out);
}
break;
case QueryOperator::OPERATOR_HAS:
{
SubGraph sub, sub2;
filterRecursively(node->getLeft().get(), in, &sub);
GraphFilterCommandMember().apply(&sub, &sub2);
filterRecursively(node->getRight().get(), &sub2, out);
}
break;
case QueryOperator::OPERATOR_OR:
filterRecursively(node->getLeft().get(), in, out);
filterRecursively(node->getRight().get(), in, out);
break;
default:
break;
}
}
void GraphFilterConductor::filterCommandNode(const QueryCommand* node, const FilterableGraph* in, FilterableGraph* out) const
{
switch (node->getType())
{
case QueryCommand::COMMAND_UNDEFINED:
GraphFilterCommandNodeType(
Node::NODE_UNDEFINED | Node::NODE_UNDEFINED_TYPE |
Node::NODE_UNDEFINED_VARIABLE | Node::NODE_UNDEFINED_FUNCTION).apply(in, out);
break;
case QueryCommand::COMMAND_MEMBER:
GraphFilterCommandMember().apply(in, out);
break;
case QueryCommand::COMMAND_PARENT:
GraphFilterCommandParent().apply(in, out);
break;
case QueryCommand::COMMAND_FUNCTION:
GraphFilterCommandNodeType(Node::NODE_FUNCTION).apply(in, out);
break;
case QueryCommand::COMMAND_GLOBAL_VARIABLE:
GraphFilterCommandNodeType(Node::NODE_GLOBAL_VARIABLE).apply(in, out);
break;
case QueryCommand::COMMAND_CLASS:
GraphFilterCommandNodeType(Node::NODE_CLASS).apply(in, out);
break;
case QueryCommand::COMMAND_METHOD:
GraphFilterCommandNodeType(Node::NODE_METHOD).apply(in, out);
break;
case QueryCommand::COMMAND_FIELD:
GraphFilterCommandNodeType(Node::NODE_FIELD).apply(in, out);
break;
case QueryCommand::COMMAND_NAMESPACE:
GraphFilterCommandNodeType(Node::NODE_NAMESPACE).apply(in, out);
break;
case QueryCommand::COMMAND_STRUCT:
GraphFilterCommandNodeType(Node::NODE_STRUCT).apply(in, out);
break;
case QueryCommand::COMMAND_ENUM:
GraphFilterCommandNodeType(Node::NODE_ENUM).apply(in, out);
break;
case QueryCommand::COMMAND_ENUM_CONSTANT:
GraphFilterCommandNodeType(Node::NODE_ENUM_CONSTANT).apply(in, out);
break;
case QueryCommand::COMMAND_TYPEDEF:
GraphFilterCommandNodeType(Node::NODE_TYPEDEF).apply(in, out);
break;
case QueryCommand::COMMAND_CONST:
GraphFilterCommandConst().apply(in, out);
break;
case QueryCommand::COMMAND_STATIC:
GraphFilterCommandStatic().apply(in, out);
break;
case QueryCommand::COMMAND_VIRTUAL:
GraphFilterCommandAbstractionType(TokenComponentAbstraction::ABSTRACTION_VIRTUAL).apply(in, out);
break;
case QueryCommand::COMMAND_PURE_VIRTUAL:
GraphFilterCommandAbstractionType(TokenComponentAbstraction::ABSTRACTION_PURE_VIRTUAL).apply(in, out);
break;
case QueryCommand::COMMAND_PUBLIC:
GraphFilterCommandAccessType(TokenComponentAccess::ACCESS_PUBLIC).apply(in, out);
break;
case QueryCommand::COMMAND_PROTECTED:
GraphFilterCommandAccessType(TokenComponentAccess::ACCESS_PROTECTED).apply(in, out);
break;
case QueryCommand::COMMAND_PRIVATE:
GraphFilterCommandAccessType(TokenComponentAccess::ACCESS_PRIVATE).apply(in, out);
break;
case QueryCommand::COMMAND_CALLER:
GraphFilterCommandCall(true).apply(in, out);
break;
case QueryCommand::COMMAND_CALLEE:
GraphFilterCommandCall(false).apply(in, out);
break;
case QueryCommand::COMMAND_USAGE:
GraphFilterCommandUsage().apply(in, out);
break;
case QueryCommand::COMMAND_SUPER_CLASS:
GraphFilterCommandInheritance(true).apply(in, out);
break;
case QueryCommand::COMMAND_SUB_CLASS:
GraphFilterCommandInheritance(false).apply(in, out);
break;
case QueryCommand::COMMAND_FILE:
GraphFilterCommandNodeType(Node::NODE_FILE).apply(in, out);
break;
default:
LOG_ERROR_STREAM(<< "QueryCommand not supported: " << node->getType());
GraphFilter().apply(in, out);
break;
}
}
void GraphFilterConductor::filterTokenNode(const QueryToken* node, FilterableGraph* out) const
{
GraphFilterToken(node->getTokenName(), node->getTokenIds()).apply(m_inGraph, out);
}
@@ -1,28 +0,0 @@
#ifndef GRAPH_FILTER_CONDUCTOR_H
#define GRAPH_FILTER_CONDUCTOR_H
class FilterableGraph;
class QueryCommand;
class QueryNode;
class QueryOperator;
class QueryToken;
class QueryTree;
class GraphFilterConductor
{
public:
GraphFilterConductor();
~GraphFilterConductor();
void filter(const QueryTree* tree, const FilterableGraph* in, FilterableGraph* out);
private:
void filterRecursively(const QueryNode* node, const FilterableGraph* in, FilterableGraph* out) const;
void filterOperatorNode(const QueryOperator* node, const FilterableGraph* in, FilterableGraph* out) const;
void filterCommandNode(const QueryCommand* node, const FilterableGraph* in, FilterableGraph* out) const;
void filterTokenNode(const QueryToken* node, FilterableGraph* out) const;
const FilterableGraph* m_inGraph;
};
#endif // GRAPH_FILTER_CONDUCTOR_H
@@ -1,303 +0,0 @@
#ifndef GRAPH_FILTER_IMPLEMENTATIONS_H
#define GRAPH_FILTER_IMPLEMENTATIONS_H
#include <set>
#include "data/graph/Edge.h"
#include "data/graph/filter/GraphFilter.h"
#include "data/graph/FilterableGraph.h"
#include "data/graph/Node.h"
#include "data/graph/token_component/TokenComponentAbstraction.h"
#include "data/graph/token_component/TokenComponentAccess.h"
#include "data/graph/token_component/TokenComponentConst.h"
#include "data/graph/token_component/TokenComponentStatic.h"
/*
* empty GraphFilterImplementation for copy-pasting
*
class GraphFilter: public GraphFilter
{
protected:
virtual void visitNode(Node* node)
{
}
};
*/
class GraphFilterCommandMember
: public GraphFilter
{
protected:
virtual void visitNode(Node* node)
{
node->forEachChildNode(
[this](Node* n)
{
addNode(n);
}
);
}
};
class GraphFilterCommandParent
: public GraphFilter
{
protected:
virtual void visitNode(Node* node)
{
Node* parent = node->getParentNode();
if (parent)
{
addNode(parent);
}
}
};
class GraphFilterCommandNodeType
: public GraphFilter
{
public:
GraphFilterCommandNodeType(Node::NodeTypeMask mask)
: m_mask(mask)
{
}
protected:
virtual void visitNode(Node* node)
{
if (node->isType(m_mask))
{
addNode(node);
}
}
private:
const Node::NodeTypeMask m_mask;
};
class GraphFilterCommandConst
: public GraphFilter
{
protected:
virtual void visitNode(Node* node)
{
if (node->getType() == Node::NODE_METHOD && node->getComponent<TokenComponentConst>())
{
addNode(node);
}
// TODO: add const component to field and variable nodes..
}
};
class GraphFilterCommandStatic
: public GraphFilter
{
protected:
virtual void visitNode(Node* node)
{
if (node->getComponent<TokenComponentStatic>())
{
addNode(node);
}
}
};
class GraphFilterCommandAccessType
: public GraphFilter
{
public:
GraphFilterCommandAccessType(TokenComponentAccess::AccessType type)
: m_type(type)
{
}
protected:
virtual void visitNode(Node* node)
{
Edge* edge = node->getMemberEdge();
if (!edge)
{
return;
}
TokenComponentAccess* access = edge->getComponent<TokenComponentAccess>();
if (access && access->getAccess() == m_type)
{
addNode(edge->getTo());
}
}
private:
const TokenComponentAccess::AccessType m_type;
};
class GraphFilterCommandAbstractionType
: public GraphFilter
{
public:
GraphFilterCommandAbstractionType(TokenComponentAbstraction::AbstractionType type)
: m_type(type)
{
}
protected:
virtual void visitNode(Node* node)
{
TokenComponentAbstraction* abstraction = node->getComponent<TokenComponentAbstraction>();
if (abstraction && abstraction->getAbstraction() == m_type)
{
addNode(node);
}
}
private:
const TokenComponentAbstraction::AbstractionType m_type;
};
class GraphFilterCommandCall
: public GraphFilter
{
public:
GraphFilterCommandCall(bool callers)
: m_callers(callers)
{
}
protected:
virtual void visitNode(Node* node)
{
node->forEachEdgeOfType(Edge::EDGE_CALL,
[this, node](Edge* edge)
{
Node* from = edge->getFrom();
Node* to = edge->getTo();
if (m_callers && node == to)
{
addNode(from);
}
else if (!m_callers && node == from)
{
addNode(to);
}
}
);
}
private:
const bool m_callers;
};
class GraphFilterCommandUsage
: public GraphFilter
{
protected:
virtual void visitNode(Node* node)
{
Edge::EdgeTypeMask mask;
mask = Edge::EDGE_TYPE_OF | Edge::EDGE_RETURN_TYPE_OF | Edge::EDGE_PARAMETER_TYPE_OF;
mask = Edge::EDGE_TYPE_USAGE | Edge::EDGE_USAGE | Edge::EDGE_TYPEDEF_OF | mask;
node->forEachEdge(
[this, node, mask](Edge* edge)
{
if (node == edge->getTo() && edge->isType(mask))
{
addNode(edge->getFrom());
}
}
);
}
};
class GraphFilterCommandInheritance
: public GraphFilter
{
public:
GraphFilterCommandInheritance(bool super)
: m_super(super)
{
}
protected:
virtual void visitNode(Node* node)
{
node->forEachEdgeOfType(Edge::EDGE_INHERITANCE,
[this, node](Edge* edge)
{
Node* from = edge->getFrom();
Node* to = edge->getTo();
if (m_super && node == from)
{
addNode(to);
}
else if (!m_super && node == to)
{
addNode(from);
}
}
);
}
private:
const bool m_super;
};
class GraphFilterToken
: public GraphFilter
{
public:
GraphFilterToken(const std::string& tokenName, const std::set<Id>& tokenIds)
: m_tokenName(tokenName)
, m_tokenIds(tokenIds)
{
}
virtual void apply(const FilterableGraph* in, FilterableGraph* out)
{
std::vector<Node*> nodes;
for (Id tokenId : m_tokenIds)
{
Node* node = in->getNodeById(tokenId);
if (node)
{
nodes.push_back(node);
}
else
{
nodes.clear();
break;
}
}
if (nodes.size())
{
for (Node* node : nodes)
{
out->addNode(node);
}
}
else
{
GraphFilter::apply(in, out);
}
}
protected:
virtual void visitNode(Node* node)
{
if (node->getFullName() == m_tokenName)
{
addNode(node);
}
}
private:
const std::string m_tokenName;
const std::set<Id> m_tokenIds;
};
#endif // GRAPH_FILTER_IMPLEMENTATIONS_H
@@ -52,6 +52,14 @@ TokenComponentNameCached::TokenComponentNameCached(const std::vector<std::string
{
}
TokenComponentNameCached::TokenComponentNameCached(const NameHierarchy& nameHierarchy)
{
for (size_t i = 0; i < nameHierarchy.size(); i++)
{
m_nameHierarchy.push_back(nameHierarchy[i]->getFullName());
}
}
TokenComponentNameCached::~TokenComponentNameCached()
{
}
@@ -4,6 +4,7 @@
#include <string>
#include "data/graph/token_component/TokenComponent.h"
#include "data/name/NameHierarchy.h"
#include "data/search/SearchNode.h"
class TokenComponentName
@@ -46,6 +47,7 @@ class TokenComponentNameCached
{
public:
TokenComponentNameCached(const std::vector<std::string>& nameHierarchy);
TokenComponentNameCached(const NameHierarchy& nameHierarchy);
virtual ~TokenComponentNameCached();
virtual std::shared_ptr<TokenComponent> copy() const;
@@ -56,7 +58,7 @@ public:
virtual const SearchNode* getSearchNode() const;
private:
const std::vector<std::string> m_nameHierarchy;
std::vector<std::string> m_nameHierarchy; // TODO: use const NameHierarchy here
};
#endif // TOKEN_COMPONENT_NAME_H