#include "GraphStructuredStack.h" GraphStructuredStack::GraphStructuredStack() { // } GraphStructuredStack::~GraphStructuredStack() { // } NodeTree* GraphStructuredStack::newNode(int stateNum) { return new NodeTree("gssNode", stateNum); } void GraphStructuredStack::addToFrontier(int frontier, NodeTree* node) { //First, make sure our vector has this and lesser frontiers. If not, add it and up to it while (gss.size() <= frontier) { std::cout << "Adding a new frontier: " << gss.size() << std::endl; gss.push_back(new std::vector*>()); } std::cout << "Adding " << node << " (" << node->getData() << ") to frontier " << frontier << std::endl; gss[frontier]->push_back(node); } NodeTree* GraphStructuredStack::inFrontier(int frontier, int state) { if (frontierIsEmpty(frontier)) return NULL; for (std::vector*>::size_type i = 0; i < gss[frontier]->size(); i++) { if ((*(gss[frontier]))[i]->getData() == state) return (*(gss[frontier]))[i]; } return NULL; } int GraphStructuredStack::getContainingFrontier(NodeTree* node) { for (std::vector*>*>::size_type i = 0; i < gss.size(); i++) { if (frontierIsEmpty(frontier)) continue; for (std::vector*>::size_type j = 0; j < gss[i]->size(); j++) { if (*((*(gss[i]))[j]) == *node) return i; } } return -1; } bool GraphStructuredStack::frontierIsEmpty(int frontier) { return frontier >= gss.size() || gss[frontier]->size() == 0; } NodeTree* GraphStructuredStack::frontierGetAccState(int frontier) { //The acc state is always state 1, for now return inFrontier(frontier, 1); } std::vector*>* GraphStructuredStack::getReachable(NodeTree* start, int length) { std::vector*>* reachableList = new std::vector*>(); std::queue*> currentNodes; std::queue*> nextNodes; currentNodes.push(start); for (int i = 0; i < length; i++) { while (!currentNodes.empty()) { NodeTree* currentNode = currentNodes.front(); currentNodes.pop(); std::vector*> children = currentNode->getChildren(); std::cout << currentNode->getData() << " has children "; for (std::vector*>::size_type j = 0; j < children.size(); j++) { std::cout << children[j]->getData() << " "; nextNodes.push(children[j]); } std::cout << std::endl; } currentNodes = nextNodes; //No clear function, so go through and remove while(!nextNodes.empty()) nextNodes.pop(); } while (!currentNodes.empty()) { reachableList->push_back(currentNodes.front()); std::cout << currentNodes.front()->getData() << " is reachable from " << start->getData() << " by length " << length << std::endl; currentNodes.pop(); } return reachableList; } std::vector*> >* GraphStructuredStack::getReachablePaths(NodeTree* start, int length) { std::vector*> >* paths = new std::vector*> >(); std::vector*> currentPath; recursivePathFind(start, length, currentPath, paths); return paths; } void GraphStructuredStack::recursivePathFind(NodeTree* start, int length, std::vector*> currentPath, std::vector*> >* paths) { currentPath.push_back(start); if (length == 0) { paths->push_back(currentPath); return; } std::vector*>* children = start->getChildren(); for (std::vector*>::size_type i = 0; i < children->size(); i++) { recursivePathFind((*children)[i], length-1, currentPath, paths); } } bool GraphStructuredStack::hasEdge(NodeTree* start, NodeTree* end) { //Really, either testing for parent or child should work. return start->findChild(end) != -1; } NodeTree* GraphStructuredStack::getEdge(NodeTree* start, NodeTree* end) { return edges.get(std::make_pair(start, end), NULL); } void GraphStructuredStack::addEdge(NodeTree* start, NodeTree* end, NodeTree edge) { start->addChild(end); end->addParent(start); edges[std::make_pair(start, end)] = edge; } std::string GraphStructuredStack::toString() { std::string tostring = ""; for (std::vector*>*>::size_type i = 0; i < gss.size(); i++) { tostring += "Frontier: " + intToString(i) + "\n"; for (std::vector*>::size_type j = 0; j < gss[i]->size(); j++) { tostring += "|" + intToString((*(gss[i]))[j]->getData()) + "| "; } tostring += "\n"; } return tostring; }