Mercurial > hg > piper-cpp
comparison vamp-support/RdfTypes.h @ 223:a29ce4af17bc
More filling in the RDF type loading code
| author | Chris Cannam <cannam@all-day-breakfast.com> | 
|---|---|
| date | Fri, 09 Jun 2017 15:18:32 +0100 | 
| parents | e0e3d9efa774 | 
| children | 16655424db89 | 
   comparison
  equal
  deleted
  inserted
  replaced
| 222:e0e3d9efa774 | 223:a29ce4af17bc | 
|---|---|
| 41 | 41 | 
| 42 #include <vamp-hostsdk/PluginLoader.h> | 42 #include <vamp-hostsdk/PluginLoader.h> | 
| 43 | 43 | 
| 44 #include <sord/sord.h> | 44 #include <sord/sord.h> | 
| 45 | 45 | 
| 46 #include <mutex> | |
| 47 | |
| 46 namespace piper_vamp { | 48 namespace piper_vamp { | 
| 47 | 49 | 
| 48 // NB not thread-safe | |
| 49 class RdfTypes | 50 class RdfTypes | 
| 50 { | 51 { | 
| 51 public: | 52 public: | 
| 52 StaticOutputInfo loadStaticOutputInfo(Vamp::HostExt::PluginLoader::PluginKey) { | 53 RdfTypes() : | 
| 53 return {}; | 54 m_world(sord_world_new()) | 
| 55 {} | |
| 56 | |
| 57 ~RdfTypes() { | |
| 58 sord_world_free(m_world); | |
| 59 } | |
| 60 | |
| 61 StaticOutputInfo loadStaticOutputInfo(Vamp::HostExt::PluginLoader::PluginKey | |
| 62 pluginKey) { | |
| 63 | |
| 64 StaticOutputInfo info; | |
| 65 SordModel *model = sord_new(m_world, SORD_SPO|SORD_OPS|SORD_POS, false); | |
| 66 if (loadRdf(model, candidateRdfFilesFor(pluginKey))) { | |
| 67 // we want to find a graph like | |
| 68 // :plugin vamp:output :output1 | |
| 69 // :plugin vamp:output :output2 | |
| 70 // :plugin vamp:output :output3 | |
| 71 // :output1 vamp:computes_event_type :event | |
| 72 // :output2 vamp:computes_feature :feature | |
| 73 // :output3 vamp:computes_signal_type :signal | |
| 74 } | |
| 75 sord_free(model); | |
| 76 return info; | |
| 77 } | |
| 78 | |
| 79 private: | |
| 80 SordWorld *m_world; | |
| 81 | |
| 82 bool loadRdf(SordModel *targetModel, std::vector<std::string> filenames) { | |
| 83 for (auto f: filenames) { | |
| 84 if (loadRdfFile(targetModel, f)) { | |
| 85 return true; | |
| 86 } | |
| 87 } | |
| 88 return false; | |
| 89 } | |
| 90 | |
| 91 bool loadRdfFile(SordModel *targetModel, std::string filename) { | |
| 92 std::string base = "file://" + filename; | |
| 93 SerdURI bu; | |
| 94 if (serd_uri_parse((const uint8_t *)base.c_str(), &bu) != | |
| 95 SERD_SUCCESS) { | |
| 96 std::cerr << "Failed to parse base URI " << base << std::endl; | |
| 97 return false; | |
| 98 } | |
| 99 SerdNode bn = serd_node_from_string(SERD_URI, | |
| 100 (const uint8_t *)base.c_str()); | |
| 101 SerdEnv *env = serd_env_new(&bn); | |
| 102 SerdReader *reader = sord_new_reader(targetModel, env, SERD_TURTLE, 0); | |
| 103 SerdStatus rv = serd_reader_read_file | |
| 104 (reader, (const uint8_t *)filename.c_str()); | |
| 105 bool success = (rv == SERD_SUCCESS); | |
| 106 if (!success) { | |
| 107 std::cerr << "Failed to import RDF from " << filename << std::endl; | |
| 108 } else { | |
| 109 std::cerr << "Imported RDF from " << filename << std::endl; | |
| 110 } | |
| 111 serd_reader_free(reader); | |
| 112 serd_env_free(env); | |
| 113 return success; | |
| 114 } | |
| 115 | |
| 116 std::vector<std::string> candidateRdfFilesFor(Vamp::HostExt:: | |
| 117 PluginLoader::PluginKey key) { | |
| 118 | |
| 119 std::string library = Vamp::HostExt::PluginLoader::getInstance()-> | |
| 120 getLibraryPathForPlugin(key); | |
| 121 | |
| 122 auto li = library.rfind('.'); | |
| 123 if (li == std::string::npos) return {}; | |
| 124 auto withoutSuffix = library.substr(0, li); | |
| 125 | |
| 126 std::vector<std::string> suffixes { "ttl", "TTL", "n3", "N3" }; | |
| 127 std::vector<std::string> candidates; | |
| 128 | |
| 129 for (auto suffix : suffixes) { | |
| 130 candidates.push_back(withoutSuffix + "." + suffix); | |
| 131 } | |
| 132 | |
| 133 return candidates; | |
| 54 } | 134 } | 
| 55 }; | 135 }; | 
| 56 | 136 | 
| 57 } | 137 } | 
| 58 | 138 | 
