1 #include "cyclegraph.h"
4 /** Initializes a CycleGraph object. */
5 CycleGraph::CycleGraph() {
9 CycleGraph::~CycleGraph() {
12 /** Returns the CycleNode for a given ModelAction. */
13 CycleNode * CycleGraph::getNode(const ModelAction * action) {
14 CycleNode *node=actionToNode.get(action);
16 node=new CycleNode(action);
17 actionToNode.put(action, node);
22 /** Adds an edge between two ModelActions. */
23 void CycleGraph::addEdge(const ModelAction *from, const ModelAction *to) {
24 CycleNode *fromnode=getNode(from);
25 CycleNode *tonode=getNode(to);
28 hasCycles=checkReachable(tonode, fromnode);
30 fromnode->addEdge(tonode);
33 /** Checks whether the first CycleNode can reach the second one. */
34 bool CycleGraph::checkReachable(CycleNode *from, CycleNode *to) {
35 std::vector<CycleNode *> queue;
36 HashTable<CycleNode *, CycleNode *, uintptr_t, 4> discovered;
38 queue.push_back(from);
39 discovered.put(from, from);
40 while(!queue.empty()) {
41 CycleNode * node=queue.back();
46 for(unsigned int i=0;i<node->getEdges()->size();i++) {
47 CycleNode *next=(*node->getEdges())[i];
48 if (!discovered.contains(next)) {
49 discovered.put(next,next);
50 queue.push_back(next);
57 /** Returns whether a CycleGraph contains cycles. */
58 bool CycleGraph::checkForCycles() {
62 /** Constructor for a CycleNode. */
63 CycleNode::CycleNode(const ModelAction *modelaction) {
67 /** Returns a vector of the edges from a CycleNode. */
68 std::vector<CycleNode *> * CycleNode::getEdges() {
72 /** Adds an edge to a CycleNode. */
73 void CycleNode::addEdge(CycleNode * node) {
74 edges.push_back(node);