1 #include "cyclegraph.h"
4 /** Initializes a CycleGraph object. */
5 CycleGraph::CycleGraph() {
9 /** Returns the CycleNode for a given ModelAction. */
10 CycleNode * CycleGraph::getNode(ModelAction * action) {
11 CycleNode *node=actionToNode.get(action);
13 node=new CycleNode(action);
14 actionToNode.put(action, node);
19 /** Adds an edge between two ModelActions. */
20 void CycleGraph::addEdge(ModelAction *from, ModelAction *to) {
21 CycleNode *fromnode=getNode(from);
22 CycleNode *tonode=getNode(to);
25 hasCycles=checkReachable(fromnode, tonode);
27 fromnode->addEdge(tonode);
30 /** Checks whether the first CycleNode can reach the second one. */
31 bool CycleGraph::checkReachable(CycleNode *from, CycleNode *to) {
32 std::vector<CycleNode *> queue;
33 HashTable<CycleNode *, CycleNode *, uintptr_t, 4> discovered;
35 queue.push_back(from);
36 discovered.put(from, from);
37 while(!queue.empty()) {
38 CycleNode * node=queue.back();
43 for(unsigned int i=0;i<node->getEdges()->size();i++) {
44 CycleNode *next=(*node->getEdges())[i];
45 if (!discovered.contains(next)) {
46 discovered.put(next,next);
47 queue.push_back(next);
54 /** Returns whether a CycleGraph contains cycles. */
55 bool CycleGraph::checkForCycles() {
59 /** Constructor for a CycleNode. */
60 CycleNode::CycleNode(ModelAction *modelaction) {
64 /** Returns a vector of the edges from a CycleNode. */
65 std::vector<CycleNode *> * CycleNode::getEdges() {
69 /** Adds an edge to a CycleNode. */
70 void CycleNode::addEdge(CycleNode * node) {
71 edges.push_back(node);