1 #include "cyclegraph.h"
4 /** Initializes a CycleGraph object. */
6 CycleGraph::CycleGraph() {
10 /** Returns the CycleNode for a given ModelAction. */
12 CycleNode * CycleGraph::getNode(ModelAction * action) {
13 CycleNode *node=actionToNode.get(action);
15 node=new CycleNode(action);
16 actionToNode.put(action, node);
21 /** Adds an edge between two ModelActions. */
23 void CycleGraph::addEdge(ModelAction *from, ModelAction *to) {
24 CycleNode *fromnode=getNode(from);
25 CycleNode *tonode=getNode(to);
28 hasCycles=checkReachable(fromnode, tonode);
30 fromnode->addEdge(tonode);
33 /** Checks whether the first CycleNode can reach the second one. */
35 bool CycleGraph::checkReachable(CycleNode *from, CycleNode *to) {
36 std::vector<CycleNode *> queue;
37 HashTable<CycleNode *, CycleNode *, uintptr_t, 4> discovered;
39 queue.push_back(from);
40 discovered.put(from, from);
41 while(!queue.empty()) {
42 CycleNode * node=queue.back();
47 for(unsigned int i=0;i<node->getEdges()->size();i++) {
48 CycleNode *next=(*node->getEdges())[i];
49 if (!discovered.contains(next)) {
50 discovered.put(next,next);
51 queue.push_back(next);
58 /** Returns whether a CycleGraph contains cycles. */
59 bool checkForCycles() {
63 /** Constructor for a CycleNode. */
65 CycleNode::CycleNode(ModelAction *modelaction) {
69 /** Returns a vector of the edges from a CycleNode. */
71 std::vector<CycleNode *> * CycleNode::getEdges() {
75 /** Adds an edge to a CycleNode. */
77 void CycleNode::addEdge(CycleNode * node) {
78 edges.push_back(node);