Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chatbot smart pointer #39

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/chatbot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,65 @@ ChatBot::~ChatBot()
//// STUDENT CODE
////

ChatBot::ChatBot(const ChatBot &source) {
std::cout << "ChatBot Copy Constructor" << std::endl;

// image is owned, so we allocate the memory and copy content
_image = new wxBitmap;
*_image = *source._image;

// not-owned, so we just copy pointers
_currentNode = source._currentNode;
_rootNode = source._rootNode;
_chatLogic = source._chatLogic;
}

ChatBot& ChatBot::operator=(const ChatBot &source) {
std::cout << "ChatBot Copy Assignment" << std::endl;
if (this == &source) {
return *this;
}

delete _image;
_image = new wxBitmap;
*_image = *source._image;

_currentNode = source._currentNode;
_rootNode = source._rootNode;
_chatLogic = source._chatLogic;

return *this;
}

ChatBot::ChatBot(ChatBot &&source) noexcept {
std::cout << "ChatBot Move Constructor" << std::endl;
_image = source._image;
_currentNode = source._currentNode;
_rootNode = source._rootNode;
_chatLogic = source._chatLogic;

source._image = nullptr;
source._currentNode = nullptr;
source._rootNode = nullptr;
source._chatLogic = nullptr;
}

ChatBot& ChatBot::operator=(ChatBot &&source) noexcept {
std::cout << "ChatBot Move Assignment" << std::endl;
if (this == &source) {
return *this;
}

delete _image;
_image = new wxBitmap;
*_image = *source._image;

_currentNode = source._currentNode;
_rootNode = source._rootNode;
_chatLogic = source._chatLogic;

return *this;
}
////
//// EOF STUDENT CODE

Expand Down
6 changes: 5 additions & 1 deletion src/chatbot.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ class ChatBot

//// STUDENT CODE
////

ChatBot(const ChatBot &source);
ChatBot& operator=(const ChatBot &source);
ChatBot(ChatBot &&source) noexcept;
ChatBot& operator=(ChatBot &&source) noexcept;

////
//// EOF STUDENT CODE

Expand Down
4 changes: 2 additions & 2 deletions src/chatgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ ChatBotPanelDialog::ChatBotPanelDialog(wxWindow *parent, wxWindowID id)
////

// create chat logic instance
_chatLogic = new ChatLogic();
_chatLogic = std::make_unique<ChatLogic>();

// pass pointer to chatbot dialog so answers can be displayed in GUI
_chatLogic->SetPanelDialogHandle(this);
Expand All @@ -135,7 +135,7 @@ ChatBotPanelDialog::~ChatBotPanelDialog()
//// STUDENT CODE
////

delete _chatLogic;
_chatLogic.reset();

////
//// EOF STUDENT CODE
Expand Down
6 changes: 3 additions & 3 deletions src/chatgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define CHATGUI_H_

#include <wx/wx.h>
#include <memory>

class ChatLogic; // forward declaration

Expand All @@ -16,8 +17,7 @@ class ChatBotPanelDialog : public wxScrolledWindow
//// STUDENT CODE
////

ChatLogic *_chatLogic;

std::unique_ptr<ChatLogic> _chatLogic;
////
//// EOF STUDENT CODE

Expand All @@ -27,7 +27,7 @@ class ChatBotPanelDialog : public wxScrolledWindow
~ChatBotPanelDialog();

// getter / setter
ChatLogic *GetChatLogicHandle() { return _chatLogic; }
ChatLogic *GetChatLogicHandle() { return _chatLogic.get(); }

// events
void paintEvent(wxPaintEvent &evt);
Expand Down
49 changes: 14 additions & 35 deletions src/chatlogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ ChatLogic::ChatLogic()
//// STUDENT CODE
////

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

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

////
//// EOF STUDENT CODE
}
Expand All @@ -32,21 +26,6 @@ ChatLogic::~ChatLogic()
//// STUDENT CODE
////

// delete chatbot instance
delete _chatBot;

// delete all nodes
for (auto it = std::begin(_nodes); it != std::end(_nodes); ++it)
{
delete *it;
}

// delete all edges
for (auto it = std::begin(_edges); it != std::end(_edges); ++it)
{
delete *it;
}

////
//// EOF STUDENT CODE
}
Expand Down Expand Up @@ -127,16 +106,16 @@ void ChatLogic::LoadAnswerGraphFromFile(std::string filename)
////

// check if node with this ID exists already
auto newNode = std::find_if(_nodes.begin(), _nodes.end(), [&id](GraphNode *node) { return node->GetID() == id; });
auto newNode = std::find_if(_nodes.begin(), _nodes.end(), [&id](const std::unique_ptr<GraphNode>& node) { return node->GetID() == id; });

// create new element if ID does not yet exist
if (newNode == _nodes.end())
{
_nodes.emplace_back(new GraphNode(id));
_nodes.emplace_back(std::make_unique<GraphNode>(id));
newNode = _nodes.end() - 1; // get iterator to last element

// add all answers to current node
AddAllTokensToElement("ANSWER", tokens, **newNode);
AddAllTokensToElement("ANSWER", tokens, *((*newNode).get()));
}

////
Expand All @@ -156,21 +135,20 @@ void ChatLogic::LoadAnswerGraphFromFile(std::string filename)
if (parentToken != tokens.end() && childToken != tokens.end())
{
// get iterator on incoming and outgoing node via ID search
auto parentNode = std::find_if(_nodes.begin(), _nodes.end(), [&parentToken](GraphNode *node) { return node->GetID() == std::stoi(parentToken->second); });
auto childNode = std::find_if(_nodes.begin(), _nodes.end(), [&childToken](GraphNode *node) { return node->GetID() == std::stoi(childToken->second); });
auto parentNode = std::find_if(_nodes.begin(), _nodes.end(), [&parentToken](const std::unique_ptr<GraphNode>& node) { return node->GetID() == std::stoi(parentToken->second); });
auto childNode = std::find_if(_nodes.begin(), _nodes.end(), [&childToken](const std::unique_ptr<GraphNode>& node) { return node->GetID() == std::stoi(childToken->second); });

// create new edge
GraphEdge *edge = new GraphEdge(id);
edge->SetChildNode(*childNode);
edge->SetParentNode(*parentNode);
_edges.push_back(edge);
auto edge = std::make_shared<GraphEdge>(id);
edge->SetChildNode((*childNode).get());
edge->SetParentNode((*parentNode).get());

// find all keywords for current node
AddAllTokensToElement("KEYWORD", tokens, *edge);

// store reference in child node and parent node
(*childNode)->AddEdgeToParentNode(edge);
(*parentNode)->AddEdgeToChildNode(edge);
(*parentNode)->AddEdgeToChildNode(std::move(edge));
}

////
Expand Down Expand Up @@ -206,7 +184,7 @@ void ChatLogic::LoadAnswerGraphFromFile(std::string filename)

if (rootNode == nullptr)
{
rootNode = *it; // assign current node to root
rootNode = (*it).get(); // assign current node to root
}
else
{
Expand All @@ -215,9 +193,10 @@ void ChatLogic::LoadAnswerGraphFromFile(std::string filename)
}
}

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

////
//// EOF STUDENT CODE
Expand Down
4 changes: 2 additions & 2 deletions src/chatlogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <vector>
#include <string>
#include "chatgui.h"
#include <memory>

// forward declarations
class ChatBot;
Expand All @@ -17,8 +18,7 @@ class ChatLogic
////

// data handles (owned)
std::vector<GraphNode *> _nodes;
std::vector<GraphEdge *> _edges;
std::vector<std::unique_ptr<GraphNode>> _nodes;

////
//// EOF STUDENT CODE
Expand Down
23 changes: 11 additions & 12 deletions src/graphnode.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "graphedge.h"
#include "graphnode.h"

#include <iostream>
GraphNode::GraphNode(int id)
{
_id = id;
Expand All @@ -11,8 +11,6 @@ GraphNode::~GraphNode()
//// STUDENT CODE
////

delete _chatBot;

////
//// EOF STUDENT CODE
}
Expand All @@ -22,28 +20,29 @@ void GraphNode::AddToken(std::string token)
_answers.push_back(token);
}

void GraphNode::AddEdgeToParentNode(GraphEdge *edge)
void GraphNode::AddEdgeToParentNode(const std::shared_ptr<GraphEdge> &edge)
{
_parentEdges.push_back(edge);
}

void GraphNode::AddEdgeToChildNode(GraphEdge *edge)
void GraphNode::AddEdgeToChildNode(std::shared_ptr<GraphEdge> edge)
{
_childEdges.push_back(edge);
_childEdges.push_back(std::move(edge));
}

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

}

void GraphNode::MoveChatbotToNewNode(GraphNode *newNode)
{
newNode->MoveChatbotHere(_chatBot);
_chatBot = nullptr; // invalidate pointer at source
newNode->MoveChatbotHere(std::move(_chatBot));
}
////
//// EOF STUDENT CODE
Expand All @@ -53,7 +52,7 @@ GraphEdge *GraphNode::GetChildEdgeAtIndex(int index)
//// STUDENT CODE
////

return _childEdges[index];
return _childEdges[index].get();

////
//// EOF STUDENT CODE
Expand Down
15 changes: 8 additions & 7 deletions src/graphnode.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#include <vector>
#include <string>
#include "chatbot.h"

#include "chatlogic.h"
#include <memory>

// forward declarations
class GraphEdge;
Expand All @@ -16,11 +17,11 @@ class GraphNode
////

// data handles (owned)
std::vector<GraphEdge *> _childEdges; // edges to subsequent nodes
std::vector<std::shared_ptr<GraphEdge>> _childEdges; // edges to subsequent nodes

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

////
//// EOF STUDENT CODE
Expand All @@ -43,13 +44,13 @@ class GraphNode

// proprietary functions
void AddToken(std::string token); // add answers to list
void AddEdgeToParentNode(GraphEdge *edge);
void AddEdgeToChildNode(GraphEdge *edge);
void AddEdgeToParentNode(const std::shared_ptr<GraphEdge>& edge);
void AddEdgeToChildNode(std::shared_ptr<GraphEdge> edge);

//// STUDENT CODE
////

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

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