bool CFileSystem::StartMonitoringDirectory(const wchar_t* dir) { BEHAVIAC_UNUSED_VAR(dir); #if BEHAVIAC_CCDEFINE_GCC_LINUX if (!s_bThreadFinish) { return true; } s_bThreadFinish = false; behaviac::wstring dirW = dir; //to use static! static behaviac::string s_buffer = ""; s_buffer = behaviac::StringUtils::Wide2Char(dirW); if (pthread_create(&s_tid, NULL, ThreadFunc, const_cast<char*>(s_buffer.c_str())) < 0) { return false; } //pthread_detach(tid); #endif return true; }
/// convert multibyte string to wide char string bool MBSToWCS(behaviac::wstring& resultString, const behaviac::string& str, const char* locale) { BEHAVIAC_UNUSED_VAR(resultString); BEHAVIAC_UNUSED_VAR(str); BEHAVIAC_UNUSED_VAR(locale); bool ret = false; #if BEHAVIAC_COMPILER_MSVC const int cp = (strcmp(locale, LOCALE_CN_UTF8) == 0) ? CP_UTF8 : CP_ACP; const int dwNum = MultiByteToWideChar(cp, 0, str.c_str(), -1, 0, 0); wchar_t* buffer = (wchar_t*)BEHAVIAC_MALLOC_WITHTAG(dwNum * 2 + 2, "MBSToWCS"); if (buffer) { MultiByteToWideChar(cp, 0, str.c_str(), -1, buffer, dwNum); resultString = buffer; BEHAVIAC_FREE(buffer); ret = true; } #else uint32_t dwNum = (str.size() + 1) * 4; wchar_t* buffer = (wchar_t*)BEHAVIAC_MALLOC_WITHTAG(dwNum, "MBSToWCS"); if (buffer) { //remember it to restore it later char* currrentLocale = setlocale(LC_ALL, 0); char* loc = setlocale(LC_ALL, locale); if (loc) { mbstowcs(buffer, str.c_str(), dwNum); ret = true; } //restore setlocale(LC_ALL, currrentLocale); resultString = buffer; BEHAVIAC_FREE(buffer); } #endif//#if BEHAVIAC_COMPILER_MSVC return ret; }
//suppose params are seprated by ',' static void ParseForParams(const behaviac::string& tsrc, behaviac::vector<behaviac::string>& params) { int tsrcLen = (int)tsrc.size(); int startIndex = 0; int index = 0; int quoteDepth = 0; for (; index < tsrcLen; ++index) { if (tsrc[index] == '"') { quoteDepth++; //if (quoteDepth == 1) //{ // startIndex = index; //} if ((quoteDepth & 0x1) == 0) { //closing quote quoteDepth -= 2; BEHAVIAC_ASSERT(quoteDepth >= 0); } } else if (quoteDepth == 0 && tsrc[index] == ',') { //skip ',' inside quotes, like "count, count" int lengthTemp = index - startIndex; behaviac::string strTemp = tsrc.substr(startIndex, lengthTemp); params.push_back(strTemp); startIndex = index + 1; } }//end for // the last param int lengthTemp = index - startIndex; if (lengthTemp > 0) { behaviac::string strTemp = tsrc.substr(startIndex, lengthTemp); params.push_back(strTemp); //params.push_back(strTemp); } }
void test_stochastic_distribution_0(behaviac::string tree, behaviac::Workspace::EFileFormat format) { behaviac::Profiler::CreateInstance(); behaviac::Config::SetSocketing(false); behaviac::Config::SetLogging(false); //behaviac::Agent::Register<AgentNodeTest>(); registerAllTypes(); AgentNodeTest* myTestAgent = AgentNodeTest::DynamicCast(behaviac::Agent::Create<AgentNodeTest>()); behaviac::Agent::SetIdMask(1); myTestAgent->SetIdFlag(1); //ms_workspace = new BehaviacWorkspace(); myTestAgent->btload(tree.c_str()); myTestAgent->btsetcurrent(tree.c_str()); myTestAgent->resetProperties(); int counts[3] = { 0, 0, 0 }; int loopCount = STOCHASTIC_SAMPLE_COUNT; while (loopCount > 0) { myTestAgent->btexec(); ++(counts[myTestAgent->testVar_0]); --loopCount; } #if BEHAVIAC_COMPILER_MSVC for (int i = 0; i < 3; ++i) { int k = counts[i]; int bias = abs(k - STOCHASTIC_SAMPLE_COUNT / 3); CHECK_LESS(bias, (STOCHASTIC_SAMPLE_COUNT / 20)); } #endif unregisterAllTypes(); BEHAVIAC_DELETE(myTestAgent); //behaviac::Agent::UnRegister<AgentNodeTest>(); behaviac::Profiler::DestroyInstance(); }
void LogManager::Log(const behaviac::Agent* pAgent, const char* typeName, const char* varName, const char* value) { BEHAVIAC_UNUSED_VAR(pAgent); BEHAVIAC_UNUSED_VAR(typeName); BEHAVIAC_UNUSED_VAR(varName); BEHAVIAC_UNUSED_VAR(value); if (Config::IsLoggingOrSocketing()) { //BEHAVIAC_PROFILE("LogManager::LogVar"); if (pAgent && pAgent->IsMasked()) { const char* agentClassName = pAgent->GetObjectTypeName(); const behaviac::string agentInstanceName = pAgent->GetName(); //[property]WorldState::World WorldState::time->276854364 //[property]Ship::Ship_1 GameObject::HP->100 //[property]Ship::Ship_1 GameObject::age->0 //[property]Ship::Ship_1 GameObject::speed->0.000000 behaviac::string buffer; bool bIsPar = IsParVar(varName); if (bIsPar) { behaviac::string tn = typeName; //filter out "signed " const char* p = strstr(typeName, "signed "); if (p) { behaviac::string tn2; for (const char* pi = typeName; pi < p; ++pi) { tn2 += *pi; } tn2 += (p + 7); tn = tn2; } buffer = FormatString("[property]%s#%s %s %s->%s\n", agentClassName, agentInstanceName.c_str(), tn.c_str(), varName, value); } else { buffer = FormatString("[property]%s#%s %s->%s\n", agentClassName, agentInstanceName.c_str(), varName, value); } this->Output(pAgent, buffer.c_str()); Socket::SendText(buffer.c_str()); } } }
void test_stochastic_distribution_1(behaviac::string tree, behaviac::Workspace::EFileFormat format, int loopCount = STOCHASTIC_SAMPLE_COUNT, int referenceValue = STOCHASTIC_SAMPLE_COUNT / 3, int checkValue = STOCHASTIC_SAMPLE_COUNT / 30) { behaviac::Profiler::CreateInstance(); behaviac::Config::SetSocketing(false); behaviac::Config::SetLogging(false); //behaviac::Agent::Register<AgentNodeTest>(); registerAllTypes(); AgentNodeTest* myTestAgent = AgentNodeTest::DynamicCast(behaviac::Agent::Create<AgentNodeTest>()); behaviac::Agent::SetIdMask(1); myTestAgent->SetIdFlag(1); int predicateValueCount = 0; myTestAgent->btload(tree.c_str()); myTestAgent->btsetcurrent(tree.c_str()); myTestAgent->resetProperties(); while (loopCount > 0) { myTestAgent->resetProperties(); myTestAgent->btexec(); if (myTestAgent->testVar_0 == 0) { predicateValueCount++; } --loopCount; } #if BEHAVIAC_COMPILER_MSVC int bias = abs(predicateValueCount - referenceValue); CHECK_LESS(bias, checkValue); #endif unregisterAllTypes(); BEHAVIAC_DELETE(myTestAgent); //behaviac::Agent::UnRegister<AgentNodeTest>(); behaviac::Profiler::DestroyInstance(); }
/// Construct. begin a profiling block with the specified name and optional call count. AutoProfileBlockSend(Profiler* profiler, const behaviac::string& taskClassid, const Agent* agent) : profiler_(profiler) { if (Config::IsProfiling()) { profiler_ = profiler; if (profiler_) { profiler_->BeginBlock(taskClassid.c_str(), agent); } } }
//it returns true if 'str' starts with a count followed by ':' //3:{....} bool IsArrayString(const behaviac::string& str, int posStart, behaviac::string::size_type& posEnd) { //begin of the count of an array? //int posStartOld = posStart; bool bIsDigit = false; int strLen = (int)str.size(); while (posStart < strLen) { char c = str[posStart++]; if (isdigit(c)) { bIsDigit = true; } else if (c == ':' && bIsDigit) { //transit_points = 3:{coordX = 0; coordY = 0; } | {coordX = 0; coordY = 0; } | {coordX = 0; coordY = 0; }; //skip array item which is possible a struct int depth = 0; for (int posStart2 = posStart; posStart2 < strLen; posStart2++) { char c1 = str[posStart2]; if (c1 == ';' && depth == 0) { //the last ';' posEnd = posStart2; break; } else if (c1 == '{') { BEHAVIAC_ASSERT(depth < 10); depth++; } else if (c1 == '}') { BEHAVIAC_ASSERT(depth > 0); depth--; } } return true; } else { break; } } return false; }
//< Selector Probability Tests void test_stochastic_distribution_2(behaviac::string tree, behaviac::Workspace::EFileFormat format, int refs[3]) { behaviac::Profiler::CreateInstance(); behaviac::Config::SetSocketing(false); behaviac::Config::SetLogging(false); //behaviac::Agent::Register<AgentNodeTest>(); registerAllTypes(); AgentNodeTest* myTestAgent = AgentNodeTest::DynamicCast(behaviac::Agent::Create<AgentNodeTest>()); behaviac::Agent::SetIdMask(1); myTestAgent->SetIdFlag(1); myTestAgent->btload(tree.c_str()); myTestAgent->btsetcurrent(tree.c_str()); myTestAgent->resetProperties(); int counts[3] = { 0, 0, 0 }; int loopCount = 10000; while (loopCount > 0) { myTestAgent->btexec(); ++(counts[myTestAgent->testVar_0]); --loopCount; } for (int i = 0; i < 3; ++i) { int k = counts[i]; int bias = abs(k - refs[i]); CHECK_LESS(bias, 1000); } unregisterAllTypes(); BEHAVIAC_DELETE(myTestAgent); //behaviac::Agent::UnRegister<AgentNodeTest>(); behaviac::Profiler::DestroyInstance(); }
bool ConnectorInterface::ReadText(behaviac::string& text) { if (this->IsConnected()) { ScopedLock lock(ms_cs); text = this->ms_texts; this->ms_texts.clear(); return !text.empty(); } return false; }
void Planner::LogPlanNodeEnd(Agent* a, BehaviorNode* n, const behaviac::string& result) { BEHAVIAC_UNUSED_VAR(a); BEHAVIAC_UNUSED_VAR(n); BEHAVIAC_UNUSED_VAR(result); #if !BEHAVIAC_RELEASE if (Config::IsLoggingOrSocketing()) { behaviac::string ni = BehaviorTask::GetTickInfo(a, n, NULL); LogManager::GetInstance()->Log("[plan_node_end]%s %s\n", ni.c_str(), result.c_str()); } #endif }
void Planner::LogPlanForEachEnd(Agent* a, DecoratorIterator* pForEach, int index, int count, const behaviac::string& result) { BEHAVIAC_UNUSED_VAR(a); BEHAVIAC_UNUSED_VAR(pForEach); BEHAVIAC_UNUSED_VAR(count); BEHAVIAC_UNUSED_VAR(index); BEHAVIAC_UNUSED_VAR(result); #if !BEHAVIAC_RELEASE if (Config::IsLoggingOrSocketing()) { behaviac::string ni = BehaviorTask::GetTickInfo(a, pForEach, NULL); LogManager::GetInstance()->Log("[plan_foreach_end]%s %d %d %s\n", ni.c_str(), index, count, result.c_str()); } #endif }
XmlNodeRef MakeXmlNodeStruct(const char* str, const behaviac::string& typeNameT) { behaviac::string src = str; //{color=0;id=;type={bLive=false;name=0;weight=0;};} //the first char is '{' //the last char is '}' behaviac::string::size_type posCloseBrackets = behaviac::StringUtils::Private::SkipPairedBrackets(src); BEHAVIAC_ASSERT(posCloseBrackets != behaviac::string::npos); bool bIsStructMember = false; XmlNodeRef xmlNode = CreateXmlNode(typeNameT.c_str()); //{color=0;id=;type={bLive=false;name=0;weight=0;};} //{color=0;id=;type={bLive=false;name=0;weight=0;};transit_points=3:{coordX=0;coordY=0;}|{coordX=0;coordY=0;}|{coordX=0;coordY=0;};} behaviac::string::size_type posBegin = 1; behaviac::string::size_type posEnd = src.find_first_of(';', posBegin); while (posEnd != behaviac::string::npos) { BEHAVIAC_ASSERT(src[posEnd] == ';'); //the last one might be empty if (posEnd > posBegin) { behaviac::string::size_type posEqual = src.find_first_of('=', posBegin); BEHAVIAC_ASSERT(posEqual > posBegin); size_t length = posEqual - posBegin; behaviac::string memmberName = src.substr(posBegin, length); behaviac::string memmberValue; char c = src[posEqual + 1]; if (c != '{') { //to check if it is an array if (IsArrayString(src, posEqual + 1, posEnd)) { length = posEnd - posEqual - 1; memmberValue = src.substr(posEqual + 1, length); } else { length = posEnd - posEqual - 1; memmberValue = src.substr(posEqual + 1, length); } } else { bIsStructMember = true; const char* pStructBegin = src.c_str(); pStructBegin += posEqual + 1; const char* posCloseBrackets_ = behaviac::StringUtils::Private::SkipPairedBrackets(pStructBegin); length = posCloseBrackets_ - pStructBegin + 1; memmberValue = src.substr(posEqual + 1, length); posEnd = posEqual + 1 + length; } if (bIsStructMember) { XmlNodeRef memberNode = MakeXmlNodeStruct(memmberValue.c_str(), memmberName); xmlNode->addChild(memberNode); } else { //behaviac::string memmberNameFull = typeNameT + "::" + memmberName; //xmlNode->setAttr(memmberNameFull.c_str(), memmberValue.c_str()); xmlNode->setAttr(memmberName.c_str(), memmberValue.c_str()); } } bIsStructMember = false; //skip ';' posBegin = posEnd + 1; //{color=0;id=;type={bLive=false;name=0;weight=0;};transit_points=3:{coordX=0;coordY=0;}|{coordX=0;coordY=0;}|{coordX=0;coordY=0;};} posEnd = src.find_first_of(';', posBegin); if (posEnd > posCloseBrackets) { break; } } return xmlNode; }