2013-05-11 16:13:46 -04:00
|
|
|
#include "NodeTree.h"
|
|
|
|
|
|
2013-06-23 05:54:58 -04:00
|
|
|
int NodeTree::idCounter;
|
|
|
|
|
|
2013-05-11 16:13:46 -04:00
|
|
|
NodeTree::NodeTree() {
|
|
|
|
|
parent = NULL;
|
|
|
|
|
name = "UnnamedNode";
|
2013-06-23 05:54:58 -04:00
|
|
|
|
|
|
|
|
id = idCounter++;
|
2013-05-11 16:13:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NodeTree::NodeTree(std::string name) {
|
|
|
|
|
parent = NULL;
|
|
|
|
|
this->name = name;
|
2013-06-23 05:54:58 -04:00
|
|
|
|
|
|
|
|
id = idCounter++;
|
2013-05-11 16:13:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NodeTree::~NodeTree() {
|
|
|
|
|
children.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NodeTree::setParent(NodeTree* parent) {
|
|
|
|
|
if (this->parent != NULL) {
|
|
|
|
|
this->parent->removeChild(this);
|
|
|
|
|
}
|
|
|
|
|
this->parent = parent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NodeTree* NodeTree::getParent() {
|
|
|
|
|
return parent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NodeTree::addChild(NodeTree* child) {
|
|
|
|
|
if (findChild(child) == -1)
|
|
|
|
|
children.push_back(child);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int NodeTree::findChild(NodeTree* child) {
|
|
|
|
|
for (int i = 0; i < children.size(); i++) {
|
|
|
|
|
if (children[i] == child) {
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NodeTree::removeChild(int index) {
|
|
|
|
|
children[index] = NULL;
|
|
|
|
|
children.erase(children.begin()+index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NodeTree::removeChild(NodeTree* child) {
|
|
|
|
|
int index = findChild(child);
|
|
|
|
|
if (index != 0) {
|
|
|
|
|
removeChild(index);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int NodeTree::size() {
|
|
|
|
|
int count = 0;
|
|
|
|
|
for (int i = 0; i < children.size(); i++) {
|
|
|
|
|
count += children[i]->size();
|
|
|
|
|
}
|
|
|
|
|
return 1+count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NodeTree* NodeTree::get(int index) {
|
|
|
|
|
return children[index];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string NodeTree::getName() {
|
|
|
|
|
return name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NodeTree::setName(std::string name) {
|
|
|
|
|
this->name = name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string NodeTree::DOTGraphString() {
|
|
|
|
|
return( "digraph Kraken { \n" + DOTGraphStringHelper() + "}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string NodeTree::DOTGraphStringHelper() {
|
|
|
|
|
std::string ourDOTRelation = "";
|
|
|
|
|
for (int i = 0; i < children.size(); i++) {
|
2013-06-23 05:54:58 -04:00
|
|
|
ourDOTRelation += getDOTName() + " -> " + children[i]->getDOTName() + ";\n" + children[i]->DOTGraphStringHelper();
|
2013-05-11 16:13:46 -04:00
|
|
|
}
|
|
|
|
|
return(ourDOTRelation);
|
2013-06-23 05:54:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string NodeTree::getDOTName() {
|
|
|
|
|
if (name.at(0) == '\"')
|
|
|
|
|
return truncateEnd(name) + "_" + intToString(id) + "\""; //Note that terminals already have a quote in the front of their name, so we don't need to add one
|
|
|
|
|
return "\"" + name + "_" + intToString(id) + "\"";
|
2013-05-11 16:13:46 -04:00
|
|
|
}
|