According to the given package name, deserialize the serialized model file with the ReuseModel.fbs by FlatBuffers \param packageName The package name of the tested application
| 441 | /// by FlatBuffers |
| 442 | /// \param packageName The package name of the tested application |
| 443 | void ModelReusableAgent::loadReuseModel(const std::string &packageName) { |
| 444 | std::string modelFilePath = STORAGE_PREFIX + packageName + ".fbm"; |
| 445 | |
| 446 | this->_modelSavePath = modelFilePath; |
| 447 | if (!this->_modelSavePath.empty()) { |
| 448 | this->_defaultModelSavePath = STORAGE_PREFIX + packageName + ".tmp.fbm"; |
| 449 | } |
| 450 | BLOG("begin load model: %s", this->_modelSavePath.c_str()); |
| 451 | |
| 452 | std::ifstream modelFile(modelFilePath, std::ios::binary | std::ios::in); |
| 453 | if (modelFile.fail()) { |
| 454 | BLOG("read model file %s failed, check if file exists!", modelFilePath.c_str()); |
| 455 | return; |
| 456 | } |
| 457 | |
| 458 | // load model file to struct |
| 459 | std::filebuf *fileBuffer = modelFile.rdbuf(); |
| 460 | // returns the new position of the altered control stream, which is the end of the file |
| 461 | std::size_t filesize = fileBuffer->pubseekoff(0, modelFile.end, modelFile.in); |
| 462 | // reset the position of the controlled stream |
| 463 | fileBuffer->pubseekpos(0, modelFile.in); |
| 464 | char *modelFileData = new char[filesize]; |
| 465 | //Reads count characters from the input sequence and stores them into a character array. |
| 466 | fileBuffer->sgetn(modelFileData, static_cast<int>(filesize)); |
| 467 | auto reuseFBModel = GetReuseModel(modelFileData); |
| 468 | |
| 469 | // to std::map |
| 470 | { |
| 471 | std::lock_guard<std::mutex> reuseGuard(this->_reuseModelLock); |
| 472 | this->_reuseModel.clear(); |
| 473 | this->_reuseQValue.clear(); |
| 474 | } |
| 475 | auto reusedModelDataPtr = reuseFBModel->model(); |
| 476 | if (!reusedModelDataPtr) { |
| 477 | BLOG("%s", "model data is null"); |
| 478 | return; |
| 479 | } |
| 480 | for (int entryIndex = 0; entryIndex < reusedModelDataPtr->size(); entryIndex++) { |
| 481 | auto reuseEntryInReuseModel = reusedModelDataPtr->Get(entryIndex); |
| 482 | uint64_t actionHash = reuseEntryInReuseModel->action(); |
| 483 | auto activityEntry = reuseEntryInReuseModel->targets(); |
| 484 | ReuseEntryM entryPtr; |
| 485 | for (int targetIndex = 0; targetIndex < activityEntry->size(); targetIndex++) { |
| 486 | auto targetEntry = activityEntry->Get(targetIndex); |
| 487 | BDLOG("load model hash: %llu %s %d", actionHash, |
| 488 | targetEntry->activity()->str().c_str(), (int) targetEntry->times()); |
| 489 | entryPtr.insert(std::make_pair( |
| 490 | std::make_shared<std::string>(targetEntry->activity()->str()), |
| 491 | (int) targetEntry->times())); |
| 492 | } |
| 493 | if (!entryPtr.empty()) { |
| 494 | std::lock_guard<std::mutex> reuseGuard(this->_reuseModelLock); |
| 495 | // this->_reuseQValue.insert(std::make_pair(actionHash, reuseEntryInReuseModel->quality())); |
| 496 | this->_reuseModel.insert(std::make_pair(actionHash, entryPtr)); |
| 497 | } |
| 498 | } |
| 499 | BLOG("loaded model contains actions: %zu", this->_reuseModel.size()); |
| 500 | delete[] modelFileData; |
no test coverage detected