1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #define LOG_TAG "UtdGraph"
16 #include "utd_graph.h"
17 #include "logger.h"
18 namespace OHOS {
19 namespace UDMF {
UtdGraph()20 UtdGraph::UtdGraph()
21 {
22 LOG_INFO(UDMF_CLIENT, "construct UtdGraph sucess.");
23 }
24
~UtdGraph()25 UtdGraph::~UtdGraph()
26 {
27 }
28
GetInstance()29 UtdGraph &UtdGraph::GetInstance()
30 {
31 static auto instance = new UtdGraph();
32 return *instance;
33 }
34
IsValidType(const std::string & node)35 bool UtdGraph::IsValidType(const std::string &node)
36 {
37 std::unique_lock<std::mutex> lock(graphMutex_);
38 return graph_->IsValidType(node);
39 }
40
InitUtdGraph(const std::vector<TypeDescriptorCfg> & descriptorCfgs)41 void UtdGraph::InitUtdGraph(const std::vector<TypeDescriptorCfg> &descriptorCfgs)
42 {
43 std::map<std::string, uint32_t> typeIdIndex;
44 uint32_t descriptorsNum = static_cast<uint32_t>(descriptorCfgs.size());
45 for (uint32_t i = 0; i < descriptorsNum; i++) {
46 typeIdIndex.insert(std::make_pair(descriptorCfgs[i].typeId, i));
47 }
48 std::unique_lock<std::mutex> lock(graphMutex_);
49 graph_ = std::make_unique<Graph>(descriptorsNum, typeIdIndex);
50 for (const auto &descriptorCfg : descriptorCfgs) {
51 std::vector<std::string> belongsTo = descriptorCfg.belongingToTypes;
52 for (auto belongsToType : belongsTo) {
53 graph_->AddEdge(belongsToType, descriptorCfg.typeId);
54 }
55 }
56 LOG_INFO(UDMF_CLIENT, "InitUtdGraph success, descriptorsNum:%{public}u. ", descriptorsNum);
57 }
58
IsLowerLevelType(const std::string & lowerLevelType,const std::string & heigitLevelType)59 bool UtdGraph::IsLowerLevelType(const std::string &lowerLevelType, const std::string &heigitLevelType)
60 {
61 if (graph_ == nullptr) {
62 LOG_ERROR(UDMF_CLIENT, "graph_ is nullptr.");
63 return false;
64 }
65 std::unique_lock<std::mutex> lock(graphMutex_);
66 bool isFind = false;
67 int32_t start = graph_->GetIndex(lowerLevelType);
68 int32_t end = graph_->GetIndex(heigitLevelType);
69 if (start < 0 || end < 0) {
70 return false;
71 }
72 uint32_t uStart = static_cast<uint32_t>(start);
73 uint32_t uEnd = static_cast<uint32_t>(end);
74 graph_->Dfs(uStart, [&isFind, &uEnd](uint32_t currNode)-> bool {
75 if (uEnd == currNode) {
76 isFind = true;
77 return true;
78 }
79 return false;
80 });
81 return isFind;
82 }
83
ConstructNewGraph(const std::vector<TypeDescriptorCfg> & descriptorCfgs)84 std::unique_ptr<Graph> UtdGraph::ConstructNewGraph(const std::vector<TypeDescriptorCfg> &descriptorCfgs)
85 {
86 std::map<std::string, uint32_t> typeIdIndex;
87 uint32_t descriptorsNum = static_cast<uint32_t>(descriptorCfgs.size());
88 for (uint32_t i = 0; i < descriptorsNum; i++) {
89 typeIdIndex.insert(std::make_pair(descriptorCfgs[i].typeId, i));
90 }
91 auto graph = std::make_unique<Graph>(descriptorsNum, typeIdIndex);
92
93 for (const auto &descriptorCfg : descriptorCfgs) {
94 std::vector<std::string> belongsTo = descriptorCfg.belongingToTypes;
95 for (auto belongsToType : belongsTo) {
96 graph->AddEdge(belongsToType, descriptorCfg.typeId);
97 }
98 }
99 return graph;
100 }
101
Update(std::unique_ptr<Graph> graph)102 void UtdGraph::Update(std::unique_ptr<Graph> graph)
103 {
104 std::unique_lock<std::mutex> lock(graphMutex_);
105 graph_ = std::move(graph);
106 }
107 } // namespace UDMF
108 } // namespace OHOS
109