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     std::unique_lock<std::mutex> lock(graphMutex_);
62     bool isFind = false;
63     int32_t start = graph_->GetIndex(lowerLevelType);
64     int32_t end = graph_->GetIndex(heigitLevelType);
65     if (start < 0 || end < 0) {
66         return false;
67     }
68     uint32_t uStart = static_cast<uint32_t>(start);
69     uint32_t uEnd = static_cast<uint32_t>(end);
70     graph_->Dfs(uStart, [&isFind, &uEnd](uint32_t currNode)-> bool {
71         if (uEnd == currNode) {
72             isFind = true;
73             return true;
74         }
75         return false;
76     });
77     return isFind;
78 }
79 
ConstructNewGraph(const std::vector<TypeDescriptorCfg> & descriptorCfgs)80 std::unique_ptr<Graph> UtdGraph::ConstructNewGraph(const std::vector<TypeDescriptorCfg> &descriptorCfgs)
81 {
82     std::map<std::string, uint32_t> typeIdIndex;
83     uint32_t descriptorsNum = static_cast<uint32_t>(descriptorCfgs.size());
84     for (uint32_t i = 0; i < descriptorsNum; i++) {
85         typeIdIndex.insert(std::make_pair(descriptorCfgs[i].typeId, i));
86     }
87     auto graph = std::make_unique<Graph>(descriptorsNum, typeIdIndex);
88 
89     for (const auto &descriptorCfg : descriptorCfgs) {
90         std::vector<std::string> belongsTo = descriptorCfg.belongingToTypes;
91         for (auto belongsToType : belongsTo) {
92             graph->AddEdge(belongsToType, descriptorCfg.typeId);
93         }
94     }
95     return graph;
96 }
97 
Update(std::unique_ptr<Graph> graph)98 void UtdGraph::Update(std::unique_ptr<Graph> graph)
99 {
100     std::unique_lock<std::mutex> lock(graphMutex_);
101     graph_ = std::move(graph);
102 }
103 } // namespace UDMF
104 } // namespace OHOS
105