FSMBase.h #pragma once #include<iostream> #include<string> #include<vector> class…
- User
FSMBase.h
#pragma once
#include #include #include
class FSMBase { public: std::string StateName; std::string EventName; std::vector<FSMBase*> subitem; FSMBase* parent;
FSMBase(); void getdata(); virtual void Add(FSMBase* b1); ~FSMBase();};
FSMBase.cpp
#include #include #include #include<rapidjson/document.h> #include<rapidjson/filereadstream.h> #include #include "FSMBase.h" #include "PrimitiveTool.h" #include "Event.h" #include "State.h" #include "FSMBuilder.h"
using namespace std; using namespace rapidjson;
FSMBase::FSMBase() { std::cout << "default constructor called" << std::endl; } void FSMBase::getdata() { std::cout << "FSMBase called" << std::endl; } void FSMBase::Add(FSMBase* b1) { subitem.push_back(b1); b1->getdata(); } FSMBase::~FSMBase() { std::cout << " destructor called" << std::endl; }int main() {
Document doc; ifstream file("FSMImplementationJSON.json"); if (!file.is_open()) { cerr << "Could not open file: FSMImplementationJSON.json" << endl; return 1; } string json((istreambuf_iterator<char>(file)), istreambuf_iterator<char>()); doc.Parse(json.c_str()); if (doc.HasParseError()) { cerr << "Error parsing JSON: " << doc.GetParseError() << endl; return 1; } PrimitiveTool* prTool1 = new PrimitiveTool(); FSMBuilder::createStatesFromJson(prTool1, doc); std::cout << "calling...................." << std::endl; delete prTool1; return 0;} FSMBuilder.cpp
#include "FSMBuilder.h" #include "rapidjson/document.h" #include
using namespace rapidjson; using namespace std;
void FSMBuilder::createStatesFromJson(PrimitiveTool* primitiveTool, rapidjson::Document& doc) { if (doc.HasMember("PrimitiveTool") && doc["PrimitiveTool"].HasMember("State") && doc["PrimitiveTool"]["State"].IsArray()) { const Value& states = doc["PrimitiveTool"]["State"]; for (SizeType i = 0; i < states.Size(); ++i) { const Value& stateVal = states[i]; State* StateObj = new State(primitiveTool);
if (stateVal.HasMember("Name") && stateVal["Name"].IsString()) { StateObj->getdata(stateVal["Name"].GetString(), stateVal["EntryMethodName"].GetString()); } if (stateVal.HasMember("Event")) { if (stateVal["Event"].IsArray()) { const Value& events = stateVal["Event"]; for (SizeType j = 0; j < events.Size(); ++j) { const Value& eventVal = events[j]; Event* EventObj = new Event(StateObj); if (eventVal.HasMember("Name") && eventVal["Name"].IsString()) { string eventName = eventVal["Name"].GetString(); string actionType = eventVal.HasMember("ActionType") ? eventVal["ActionType"].GetString() : ""; string actionParam = eventVal.HasMember("ActionParam") ? eventVal["ActionParam"].GetString() : ""; EventObj->getdata(eventName, actionType, actionParam); StateObj->Add(EventObj); } } } } primitiveTool->Add(StateObj); } } - Assistant
- User
in the above code after executing the createStatesFromJson() and adding all the states and events oobjects to FSMBase ,in the default or initial state currentState="AcceptedState" which is last state of FSM JSON file i want to set the initial current state to "StartToolState" which is first state of the json file.Provide the code for executing that
- Assistant
- User
after the code adiction i am getting output as
Event: ClickEndPoint ActionType InvokeMethod ActionParam: EnterEVelPointEdit FSMBase called default constructor called Event: ClickAVelPoint ActionType InvokeMethod ActionParam: EnterAVelPointEdit FSMBase called default constructor called Event: ClickEvent ActionType InvokeMethod ActionParam: SelectManualToolStart FSMBase called FSMBase called calling.................... Found STATE :StartToolState Initial state set to: StartToolState Initial Active State: StartToolState default constructor called Event: CompleteEvent ActionType ChangeState ActionParam: WaitForAccept Found STATE :StartToolState searched state is : NO MATCHING EVENT FOUND Active State after Event1: StartToolState destructor called destructor called
- Assistant
- User
- Assistant
- User
still ichange state is not happening i am getting output as
calling.................... Found STATE :StartToolState Initial state set to: StartToolState Initial Active State: StartToolState default constructor called Event: CompleteEvent ActionType ChangeState ActionParam: WaitForAccept Found STATE :StartToolState searched state is : NO MATCHING EVENT FOUND Active State after CompleteEvent: StartToolState destructor called destructor called
- Assistant
- User
now i am getting output as
calling.................... Found STATE :StartToolState Initial state set to: StartToolState Initial Active State: StartToolState default constructor called Event: CompleteEvent ActionType ChangeState ActionParam: WaitForAccept Found STATE :StartToolState searched state is : Found event :CompleteEvent reached inEvent of event class matched event is : CompleteEventaction type is : ChangeState Active State after CompleteEvent: StartToolState destructor called destructor called
void Event::InEvent(Event* event) { std::cout << "reached inEvent of event class" << std::endl;
if (ActionType=="ChangeState") { if (parentState) { PrimitiveTool* pTool = dynamic_cast<PrimitiveTool*>(parentState->parent); if (pTool) { pTool->ChangeState(ActionParam); } } }}
change state doesnt happens InEvent() of event class is getting triggered but the its not executing if condition
- Assistant
- User
after debugging i think pTool is getting nullptr
what should i do to correct the code?
- Assistant
- User
explain below code lines
if (parentState) { PrimitiveTool* pTool = dynamic_cast<PrimitiveTool*>(parentState->parent); - Assistant
- User
in the above code
class FSMBase { public:
FSMBase* parent;}
class State : public FSMBase { private: PrimitiveTool* parentTool;
}
class Event : public FSMBase { private: State* parentState;
}
Still i am getting pTool as Null
- Assistant
- User
void PrimitiveTool::Add(FSMBase* b1) { state->parent = this; subitem.push_back(b1); b1->getdata();
if (State* state = dynamic_cast<State*>(b1)) { currentState = state; }} curently i am getting below error
Severity Code Description Project File Line Suppression State Details Error C2440 '=': cannot convert from 'PrimitiveTool *' to 'FSMBase *' FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\State.h 17 Error C2440 '=': cannot convert from 'PrimitiveTool *' to 'FSMBase *' FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\State.h 17 Error C2440 '=': cannot convert from 'PrimitiveTool *' to 'FSMBase *' FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\State.h 17 Error C2440 '=': cannot convert from 'PrimitiveTool *' to 'FSMBase *' FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\State.h 17 Error C2065 'state': undeclared identifier FsmImplementation C:\Users\1038586\Downloads\FsmImplementation11142024\FsmImplementation\FsmImplementation\PrimitiveTool.cpp 5
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.