PKDatasetFinder.cpp
Go to the documentation of this file.
1// Simox
2#include <SimoxUtility/algorithm/vector.hpp>
3
4// BaseClass
5#include "PKDatasetFinder.h"
6
7// ArmarX
9{
10 void
12 const std::string& prefix)
13 {
14 if (not defs->hasDefinition(prefix + "packageName"))
15 {
16 defs->optional(this->packageName,
17 prefix + "packageName",
18 "The name of the prior knowledge data package.");
19 }
20 }
21
22 void
27
28 bool
29 PKDatasetFinder::checkAll(const std::string& dataset) const
30 {
31 const std::filesystem::path absPathToDataset = this->getFullPath(dataset);
32 if (std::filesystem::is_regular_file(absPathToDataset))
33 {
34 ARMARX_WARNING << "The entered path is leading to a file!";
35 return false;
36 }
37
38 for (const auto& d : std::filesystem::directory_iterator(absPathToDataset))
39 {
40 if (!d.is_directory())
41 {
42 ARMARX_WARNING << "Found invalid path. Ignoring: " << d.path();
43 continue;
44 }
45 std::string k = d.path().filename();
46 if (simox::alg::contains(ID_BLACKLIST, k))
47 {
48 continue;
49 }
50
51 if (not this->check(dataset, k))
52 {
53 return false;
54 }
55 }
56 return true;
57 }
58
59 bool
61 {
62 const std::filesystem::path absPath = Base::Base::getFullPath();
63 if (std::filesystem::is_regular_file(absPath))
64 {
65 ARMARX_WARNING << "The entered path is leading to a file!";
66 return false;
67 }
68
69 for (const auto& d : std::filesystem::directory_iterator(absPath))
70 {
71 if (!d.is_directory())
72 {
73 ARMARX_WARNING << "Found invalid path: " << d.path();
74 continue;
75 }
76 std::string k = d.path().filename();
77 if (simox::alg::contains(DATASET_FOLDERS_BLACKLIST, k))
78 {
79 continue;
80 }
81
82 if (not this->checkAll(k))
83 {
84 return false;
85 }
86 }
87 return true;
88 }
89
90 bool
91 PKDatasetFinder::check(const std::string& dataset, const std::string& id) const
92 {
93 if (simox::alg::contains(DATASET_FOLDERS_BLACKLIST, dataset))
94 {
95 return false;
96 }
97
98 if (simox::alg::contains(ID_BLACKLIST, id))
99 {
100 return false;
101 }
102
103 const std::filesystem::path idPath = std::filesystem::path(dataset) / id;
104 const std::filesystem::path absPathToId = this->getFullPath(idPath);
105
106 return this->accept(absPathToId);
107 }
108
109 bool
110 PKDatasetFinder::check(const std::string& id) const
111 {
112 const std::filesystem::path absPath = Base::Base::getFullPath();
113 if (std::filesystem::is_regular_file(absPath))
114 {
115 ARMARX_WARNING << "The entered path is leading to a file!";
116 return false;
117 }
118
119 for (const auto& d : std::filesystem::directory_iterator(absPath))
120 {
121 if (!d.is_directory())
122 {
123 ARMARX_WARNING << "Found invalid path: " << d.path();
124 continue;
125 }
126 std::string k = d.path().filename();
127 if (simox::alg::contains(DATASET_FOLDERS_BLACKLIST, k))
128 {
129 continue;
130 }
131
132 if (this->check(k, id))
133 {
134 return true;
135 }
136 }
137 return false;
138 }
139
140 std::optional<PKDatasetFinderInfo>
141 PKDatasetFinder::find(const std::string& dataset, const std::string& id) const
142 {
143 const std::filesystem::path idPath = std::filesystem::path(dataset) / id;
144 const std::filesystem::path absPathToId = this->getFullPath(idPath);
145
146 if (this->check(dataset, id))
147 {
150 getRelativePath() / dataset,
151 id,
152 dataset,
153 id};
154 }
155 return std::nullopt;
156 }
157
158 std::optional<PKDatasetFinderInfo>
159 PKDatasetFinder::find(const std::string& id) const
160 {
161 const std::filesystem::path absPath = Base::Base::getFullPath();
162 if (std::filesystem::is_regular_file(absPath))
163 {
164 ARMARX_WARNING << "The entered path is leading to a file!";
165 return std::nullopt;
166 }
167
168 std::vector<PKDatasetFinderInfo> ret;
169 for (const auto& d : std::filesystem::directory_iterator(absPath))
170 {
171 if (!d.is_directory())
172 {
173 ARMARX_WARNING << "Found invalid path: " << d.path();
174 continue;
175 }
176 std::string k = d.path().filename();
177 if (simox::alg::contains(DATASET_FOLDERS_BLACKLIST, k))
178 {
179 continue;
180 }
181
182 if (auto op = this->find(k, id); op.has_value())
183 {
184 return op;
185 }
186 }
187 return std::nullopt;
188 }
189
190 std::vector<PKDatasetFinderInfo>
192 {
193 const std::filesystem::path absPath = Base::Base::getFullPath();
194 if (std::filesystem::is_regular_file(absPath))
195 {
196 ARMARX_WARNING << "The entered path is leading to a file!";
197 return {};
198 }
199
200 std::vector<PKDatasetFinderInfo> ret;
201 for (const auto& d : std::filesystem::directory_iterator(absPath))
202 {
203 if (!d.is_directory())
204 {
205 ARMARX_WARNING << "Found invalid path: " << d.path();
206 continue;
207 }
208 std::string k = d.path().filename();
209 if (simox::alg::contains(DATASET_FOLDERS_BLACKLIST, k))
210 {
211 continue;
212 }
213
214 auto idsForDataset = this->findAll(k);
215 simox::alg::append(ret, idsForDataset);
216 }
217 return ret;
218 }
219
220 std::vector<PKDatasetFinderInfo>
221 PKDatasetFinder::findAll(const std::string& dataset) const
222 {
223 const std::filesystem::path absPathToDataset = this->getFullPath(dataset);
224 if (std::filesystem::is_regular_file(absPathToDataset))
225 {
226 ARMARX_WARNING << "The entered path is leading to a file!";
227 return {};
228 }
229
230 std::vector<PKDatasetFinderInfo> ret;
231 for (const auto& d : std::filesystem::directory_iterator(absPathToDataset))
232 {
233 if (!d.is_directory())
234 {
235 ARMARX_WARNING << "Found invalid path: " << d.path();
236 continue;
237 }
238 std::string k = d.path().filename();
239 if (simox::alg::contains(ID_BLACKLIST, k))
240 {
241 continue;
242 }
243
244 if (auto op = this->find(dataset, k); op.has_value())
245 {
246 ret.emplace_back(op.value());
247 }
248 }
249 return ret;
250 }
251
252} // namespace armarx::priorknowledge::core
std::filesystem::path getFullPath() const
Definition FinderBase.h:84
virtual bool accept(const std::filesystem::path &idPath) const =0
std::filesystem::path getAbsolutePackagePath() const
Definition FinderBase.h:77
std::filesystem::path getRelativePath() const
Definition FinderBase.h:71
The PKDatasetFinderInfo class Specialization of the DatasetFinderInfo with strings as dataset and id ...
std::optional< PKDatasetFinderInfo > find(const std::string &dataset, const std::string &id) const override
std::vector< PKDatasetFinderInfo > findAll() const override
virtual void registerPropertyDefinitions(armarx::PropertyDefinitionsPtr &defs, const std::string &prefix)
bool check(const std::string &id) const override
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.