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
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::shared_mutex> Lock(graphMutex_);
58 graph_ = new 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::set<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 uint32_t start = GetIndex(lowerLevelType);
87 uint32_t end = GetIndex(heigitLevelType);
88 std::shared_lock<decltype(graphMutex_)> Lock(graphMutex_);
89 graph_->Dfs(start, [&isFind, &end](uint32_t currNode)-> bool {
90 if (end == currNode) {
91 isFind = true;
92 return true;
93 }
94 return false;
95 });
96 return isFind;
97 }
98
IsDAG()99 bool UtdGraph::IsDAG()
100 {
101 std::shared_lock<decltype(graphMutex_)> Lock(graphMutex_);
102 return graph_->DfsUnconnectedGraph([&](uint32_t currNode) -> bool {return false; });
103 }
104 } // namespace UDMF
105 } // namespace OHOS
106