| 1 | /* This file is part of the FaCT++ DL reasoner |
| 2 | Copyright (C) 2015-2015 Dmitry Tsarkov and The University of Manchester |
| 3 | Copyright (C) 2015-2017 Dmitry Tsarkov |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #ifndef TRIPLEGATHERER_H |
| 21 | #define TRIPLEGATHERER_H |
| 22 | |
| 23 | #include <vector> |
| 24 | #include <string> |
| 25 | #include <functional> |
| 26 | #include "taxVertex.h" |
| 27 | #include "RDF_URIs.h" |
| 28 | #include "tIndividual.h" |
| 29 | |
| 30 | void pushTriple(std::set<std::vector<std::string>>& triples, const char* subj, const char* role, const char* obj, const char* literal_type = "") |
| 31 | { |
| 32 | std::vector<std::string> triple; |
| 33 | |
| 34 | triple.push_back(toRdf(subj)); |
| 35 | triple.push_back(toRdf(role)); |
| 36 | triple.push_back(toRdf(obj)); |
| 37 | triple.push_back(literal_type); |
| 38 | |
| 39 | triples.insert(triple); |
| 40 | } |
| 41 | |
| 42 | class TripleGatherer { |
| 43 | std::set<std::vector<std::string>>* triples; |
| 44 | bool isDirect; |
| 45 | const char* roleName; |
| 46 | const char* nodeName; |
| 47 | std::function<bool(const ClassifiableEntry*)> filter; |
| 48 | |
| 49 | public: |
| 50 | |
| 51 | TripleGatherer(std::set<std::vector<std::string>>* triples, bool isDirect, const char* roleName, const char* nodeName) : triples(triples), isDirect(isDirect), roleName(roleName), nodeName(nodeName) |
| 52 | { |
| 53 | filter = [](const ClassifiableEntry*) |
| 54 | { |
| 55 | return true; |
| 56 | }; |
| 57 | } |
| 58 | |
| 59 | TripleGatherer(std::set<std::vector<std::string>>* triples, bool isDirect, const char* roleName, const char* nodeName, std::function<bool(const ClassifiableEntry*)> filter) : triples(triples), isDirect(isDirect), roleName(roleName), nodeName(nodeName), filter(filter) |
| 60 | {} |
| 61 | |
| 62 | void clear() |
| 63 | {} |
| 64 | |
| 65 | void addTriple(const ClassifiableEntry* entry) |
| 66 | { |
| 67 | if (filter(entry)) |
| 68 | { |
| 69 | const char* entityName = toRdf(entry->getName()); |
| 70 | |
| 71 | std::vector<std::string> triple; |
| 72 | |
| 73 | if (isDirect) |
| 74 | pushTriple(*triples, nodeName, roleName, entityName); |
| 75 | else |
| 76 | pushTriple(*triples, entityName, roleName, nodeName); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /// taxonomy walking method. |
| 81 | /// @return true if node was processed |
| 82 | /// @return false if node can not be processed in current settings |
| 83 | bool apply(const TaxonomyVertex& vertex) |
| 84 | { |
| 85 | addTriple(vertex.getPrimer()); |
| 86 | |
| 87 | for (const auto& synonym : vertex.synonyms()) |
| 88 | addTriple(synonym); |
| 89 | |
| 90 | return true; |
| 91 | } |
| 92 | }; // TripleGatherer |
| 93 | |
| 94 | #endif |