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 if (typeIdIndex_.find(node) == typeIdIndex_.end()) {
38 LOG_ERROR(UDMF_CLIENT, "invalid typeId. typeId:%{public}s ", node.c_str());
39 return false;
40 }
41 return true;
42 }
43
GetIndex(const std::string & node)44 int32_t UtdGraph::GetIndex(const std::string &node)
45 {
46 auto idx = typeIdIndex_.find(node);
47 if (idx == typeIdIndex_.end()) {
48 return -1;
49 }
50 return idx->second;
51 }
52
InitUtdGraph(const std::vector<TypeDescriptorCfg> & descriptorCfgs)53 void UtdGraph::InitUtdGraph(const std::vector<TypeDescriptorCfg> &descriptorCfgs)
54 {
55 typeIdIndex_.clear();
56 uint32_t descriptorsNum = static_cast<uint32_t>(descriptorCfgs.size());
57 std::unique_lock<std::mutex> lock(graphMutex_);
58 graph_ = std::make_unique<Graph>(descriptorsNum);
59 for (uint32_t i = 0; i < descriptorsNum; i++) {
60 typeIdIndex_.insert(std::make_pair(descriptorCfgs[i].typeId, i));
61 }
62 for (const auto &descriptorCfg : descriptorCfgs) {
63 std::vector<std::string> belongsTo = descriptorCfg.belongingToTypes;
64 for (auto belongsToType : belongsTo) {
65 AddEdge(belongsToType, descriptorCfg.typeId);
66 }
67 }
68 LOG_INFO(UDMF_CLIENT, "InitUtdGraph success, descriptorsNum:%{public}u. ", descriptorsNum);
69 }
70
AddEdge(const std::string & startNode,const std::string & endNode)71 void UtdGraph::AddEdge(const std::string &startNode, const std::string &endNode)
72 {
73 int32_t start = GetIndex(startNode);
74 int32_t end = GetIndex(endNode);
75 if (start < 0 || end < 0) {
76 LOG_WARN(UDMF_CLIENT, "abnormal edge, startNode:%{public}s, endNode:%{public}s. ",
77 startNode.c_str(), endNode.c_str());
78 return;
79 }
80 graph_->AddEdge(start, end);
81 }
82
IsLowerLevelType(const std::string & lowerLevelType,const std::string & heigitLevelType)83 bool UtdGraph::IsLowerLevelType(const std::string &lowerLevelType, const std::string &heigitLevelType)
84 {
85 bool isFind = false;
86 int32_t start = GetIndex(lowerLevelType);
87 int32_t end = GetIndex(heigitLevelType);
88 if (start < 0 || end < 0) {
89 return false;
90 }
91 uint32_t uStart = static_cast<uint32_t>(start);
92 uint32_t uEnd = static_cast<uint32_t>(end);
93 std::unique_lock<std::mutex> lock(graphMutex_);
94 graph_->Dfs(uStart, [&isFind, &uEnd](uint32_t currNode)-> bool {
95 if (uEnd == currNode) {
96 isFind = true;
97 return true;
98 }
99 return false;
100 });
101 return isFind;
102 }
103
IsDAG()104 bool UtdGraph::IsDAG()
105 {
106 std::unique_lock<std::mutex> lock(graphMutex_);
107 return graph_->DfsUnconnectedGraph([&](uint32_t currNode) -> bool {return false; });
108 }
109 } // namespace UDMF
110 } // namespace OHOS
111