Skip to content

Commit

Permalink
implemented task 5 in theory, it should work, but right now throws se…
Browse files Browse the repository at this point in the history
…g fault error, further debug is needed.
  • Loading branch information
gary-vladimir committed Jan 31, 2025
1 parent cf586a0 commit e13bf39
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
20 changes: 11 additions & 9 deletions src/chatlogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ ChatLogic::ChatLogic()
////

// create instance of chatbot
_chatBot = new ChatBot("../images/chatbot.png");
//_chatBot = new ChatBot("../images/chatbot.png");

// add pointer to chatlogic so that chatbot answers can be passed on to the GUI
_chatBot->SetChatLogicHandle(this);
//_chatBot->SetChatLogicHandle(this);

////
//// EOF STUDENT CODE
Expand All @@ -33,7 +33,7 @@ ChatLogic::~ChatLogic()
////

// delete chatbot instance
delete _chatBot;
//delete _chatBot;

// No need to manually delete edges now.
// The child nodes' unique_ptr<GraphEdge> will handle that.
Expand Down Expand Up @@ -195,26 +195,28 @@ void ChatLogic::LoadAnswerGraphFromFile(std::string filename)

// identify root node
GraphNode *rootNode = nullptr;
for (auto &node : _nodes)
for (auto it = std::begin(_nodes); it != std::end(_nodes); ++it)
{
// search for nodes which have no incoming edges
if (node->GetNumberOfParents() == 0)
if ((*it)->GetNumberOfParents() == 0)
{

if (rootNode == nullptr)
{
rootNode = node.get(); // assign current node to root
rootNode = (*it).get(); // assign current node to root
}
else
{
std::cout << "ERROR : Multiple root nodes detected" << std::endl;
}
}
}

ChatBot chatbot("../images/chatbot.png");
SetChatbotHandle(&chatbot);
// add chatbot to graph root node
_chatBot->SetRootNode(rootNode);
rootNode->MoveChatbotHere(_chatBot);
chatbot.SetChatLogicHandle(this);
chatbot.SetRootNode(rootNode);
rootNode->MoveChatbotHere(std::move(chatbot));

////
//// EOF STUDENT CODE
Expand Down
10 changes: 5 additions & 5 deletions src/graphnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ void GraphNode::AddEdgeToChildNode(std::unique_ptr<GraphEdge> edge)

//// STUDENT CODE
////
void GraphNode::MoveChatbotHere(ChatBot *chatbot)
void GraphNode::MoveChatbotHere(ChatBot chatbot)
{
_chatBot = chatbot;
_chatBot->SetCurrentNode(this);
_chatBot = std::move(chatbot);
_chatBot.SetCurrentNode(this);
}

void GraphNode::MoveChatbotToNewNode(GraphNode *newNode)
{
newNode->MoveChatbotHere(_chatBot);
_chatBot = nullptr; // invalidate pointer at source
newNode->MoveChatbotHere(std::move(_chatBot));
//_chatBot = nullptr; // invalidate pointer at source
}
////
//// EOF STUDENT CODE
Expand Down
4 changes: 2 additions & 2 deletions src/graphnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class GraphNode

// data handles (not owned)
std::vector<GraphEdge *> _parentEdges; // edges to preceding nodes
ChatBot *_chatBot;
ChatBot _chatBot;

////
//// EOF STUDENT CODE
Expand Down Expand Up @@ -49,7 +49,7 @@ class GraphNode
//// STUDENT CODE
////

void MoveChatbotHere(ChatBot *chatbot);
void MoveChatbotHere(ChatBot chatbot);

////
//// EOF STUDENT CODE
Expand Down

0 comments on commit e13bf39

Please sign in to comment.