chris@16: /* chris@16: Copyright 2011-2013 David Robillard chris@16: chris@16: Permission to use, copy, modify, and/or distribute this software for any chris@16: purpose with or without fee is hereby granted, provided that the above chris@16: copyright notice and this permission notice appear in all copies. chris@16: chris@16: THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES chris@16: WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF chris@16: MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR chris@16: ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES chris@16: WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN chris@16: ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF chris@16: OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. chris@16: */ chris@16: chris@16: /** chris@16: @file sordmm.hpp chris@16: Public Sord C++ API. chris@16: */ chris@16: chris@16: #ifndef SORD_SORDMM_HPP chris@16: #define SORD_SORDMM_HPP chris@16: chris@16: #include chris@16: #include chris@16: #include chris@16: #include chris@16: #include chris@16: #include chris@16: #include chris@16: chris@16: #include "serd/serd.h" chris@16: #include "sord/sord.h" chris@16: chris@16: #define SORD_NS_XSD "http://www.w3.org/2001/XMLSchema#" chris@16: chris@16: namespace Sord { chris@16: chris@16: /** Utility base class to prevent copying. */ chris@16: class Noncopyable { chris@16: protected: chris@16: Noncopyable() {} chris@16: ~Noncopyable() {} chris@16: private: chris@16: Noncopyable(const Noncopyable&); chris@16: const Noncopyable& operator=(const Noncopyable&); chris@16: }; chris@16: chris@16: /** C++ wrapper for a Sord object. */ chris@16: template chris@16: class Wrapper { chris@16: public: chris@16: inline Wrapper(T c_obj = NULL) : _c_obj(c_obj) {} chris@16: chris@16: inline T c_obj() { return _c_obj; } chris@16: inline const T c_obj() const { return _c_obj; } chris@16: chris@16: protected: chris@16: T _c_obj; chris@16: }; chris@16: chris@16: /** Collection of RDF namespaces with prefixes. */ chris@16: class Namespaces : public Wrapper { chris@16: public: chris@16: Namespaces() : Wrapper(serd_env_new(NULL)) {} chris@16: ~Namespaces() { serd_env_free(_c_obj); } chris@16: chris@16: static inline SerdNode string_to_node(SerdType type, const std::string& s) { chris@16: SerdNode ret = { chris@16: (const uint8_t*)s.c_str(), s.length(), s.length(), 0, type }; chris@16: return ret; chris@16: } chris@16: chris@16: inline void add(const std::string& name, chris@16: const std::string& uri) { chris@16: const SerdNode name_node = string_to_node(SERD_LITERAL, name); chris@16: const SerdNode uri_node = string_to_node(SERD_URI, uri); chris@16: serd_env_set_prefix(_c_obj, &name_node, &uri_node); chris@16: } chris@16: chris@16: inline std::string qualify(std::string uri) const { chris@16: const SerdNode uri_node = string_to_node(SERD_URI, uri); chris@16: SerdNode prefix; chris@16: SerdChunk suffix; chris@16: if (serd_env_qualify(_c_obj, &uri_node, &prefix, &suffix)) { chris@16: std::string ret((const char*)prefix.buf, prefix.n_bytes); chris@16: ret.append(":").append((const char*)suffix.buf, suffix.len); chris@16: return ret; chris@16: } chris@16: return uri; chris@16: } chris@16: chris@16: inline std::string expand(const std::string& curie) const { chris@16: assert(curie.find(":") != std::string::npos); chris@16: SerdNode curie_node = string_to_node(SERD_CURIE, curie); chris@16: SerdChunk uri_prefix; chris@16: SerdChunk uri_suffix; chris@16: if (!serd_env_expand(_c_obj, &curie_node, &uri_prefix, &uri_suffix)) { chris@16: std::string ret((const char*)uri_prefix.buf, uri_prefix.len); chris@16: ret.append((const char*)uri_suffix.buf, uri_suffix.len); chris@16: return ret; chris@16: } chris@16: std::cerr << "CURIE `" << curie << "' has unknown prefix." << std::endl; chris@16: return curie; chris@16: } chris@16: }; chris@16: chris@16: /** Sord library state. */ chris@16: class World : public Noncopyable, public Wrapper { chris@16: public: chris@16: inline World() chris@16: : _next_blank_id(0) chris@16: { chris@16: _c_obj = sord_world_new(); chris@16: } chris@16: chris@16: inline ~World() { chris@16: sord_world_free(_c_obj); chris@16: } chris@16: chris@16: inline uint64_t blank_id() { return _next_blank_id++; } chris@16: chris@16: inline void add_prefix(const std::string& prefix, const std::string& uri) { chris@16: _prefixes.add(prefix, uri); chris@16: } chris@16: chris@16: inline const Namespaces& prefixes() const { return _prefixes; } chris@16: inline SordWorld* world() { return _c_obj; } chris@16: chris@16: private: chris@16: Namespaces _prefixes; chris@16: std::set _blank_ids; chris@16: uint64_t _next_blank_id; chris@16: }; chris@16: chris@16: /** An RDF Node (resource, literal, etc) chris@16: */ chris@16: class Node : public Wrapper { chris@16: public: chris@16: enum Type { chris@16: UNKNOWN = 0, chris@16: URI = SORD_URI, chris@16: BLANK = SORD_BLANK, chris@16: LITERAL = SORD_LITERAL chris@16: }; chris@16: chris@16: inline Node() : Wrapper(NULL), _world(NULL) {} chris@16: chris@16: inline Node(World& world, Type t, const std::string& s); chris@16: inline Node(World& world); chris@16: inline Node(World& world, const SordNode* node); chris@16: inline Node(World& world, SordNode* node, bool copy=false); chris@16: inline Node(const Node& other); chris@16: inline ~Node(); chris@16: chris@16: inline Type type() const { chris@16: return _c_obj ? (Type)sord_node_get_type(_c_obj) : UNKNOWN; chris@16: } chris@16: chris@16: inline const SordNode* get_node() const { return _c_obj; } chris@16: inline SordNode* get_node() { return _c_obj; } chris@16: chris@16: const SerdNode* to_serd_node() { chris@16: return sord_node_to_serd_node(_c_obj); chris@16: } chris@16: chris@16: inline bool is_valid() const { return type() != UNKNOWN; } chris@16: chris@16: inline bool operator<(const Node& other) const { chris@16: if (type() != other.type()) { chris@16: return type() < other.type(); chris@16: } else { chris@16: return to_string() < other.to_string(); chris@16: } chris@16: } chris@16: chris@16: Node& operator=(const Node& other) { chris@16: if (&other != this) { chris@16: if (_c_obj) { chris@16: sord_node_free(_world->c_obj(), _c_obj); chris@16: } chris@16: _world = other._world; chris@16: _c_obj = other._c_obj ? sord_node_copy(other._c_obj) : NULL; chris@16: } chris@16: return *this; chris@16: } chris@16: chris@16: inline bool operator==(const Node& other) const { chris@16: return sord_node_equals(_c_obj, other._c_obj); chris@16: } chris@16: chris@16: inline const uint8_t* to_u_string() const; chris@16: inline const char* to_c_string() const; chris@16: inline std::string to_string() const; chris@16: chris@16: inline bool is_literal_type(const char* type_uri) const; chris@16: chris@16: inline bool is_uri() const { return _c_obj && type() == URI; } chris@16: inline bool is_blank() const { return _c_obj && type() == BLANK; } chris@16: inline bool is_int() const { return is_literal_type(SORD_NS_XSD "integer"); } chris@16: inline bool is_float() const { return is_literal_type(SORD_NS_XSD "decimal"); } chris@16: inline bool is_bool() const { return is_literal_type(SORD_NS_XSD "boolean"); } chris@16: chris@16: inline int to_int() const; chris@16: inline float to_float() const; chris@16: inline bool to_bool() const; chris@16: chris@16: inline static Node blank_id(World& world, const std::string base="b") { chris@16: const uint64_t num = world.blank_id(); chris@16: std::ostringstream ss; chris@16: ss << base << num; chris@16: return Node(world, Node::BLANK, ss.str()); chris@16: } chris@16: chris@16: private: chris@16: World* _world; chris@16: }; chris@16: chris@16: inline std::ostream& chris@16: operator<<(std::ostream& os, const Node& node) chris@16: { chris@16: return os << node.to_string(); chris@16: } chris@16: chris@16: class URI : public Node { chris@16: public: chris@16: inline URI(World& world, const std::string& s) chris@16: : Node(world, Node::URI, s) {} chris@16: inline URI(World& world, const std::string& s, const std::string& base) chris@16: : Node(world, sord_new_relative_uri(world.world(), chris@16: (const uint8_t*)s.c_str(), chris@16: (const uint8_t*)base.c_str())) chris@16: {} chris@16: }; chris@16: chris@16: class Curie : public Node { chris@16: public: chris@16: inline Curie(World& world, const std::string& s) chris@16: : Node(world, Node::URI, world.prefixes().expand(s)) {} chris@16: }; chris@16: chris@16: class Literal : public Node { chris@16: public: chris@16: inline Literal(World& world, const std::string& s) chris@16: : Node(world, Node::LITERAL, s) {} chris@16: chris@16: static inline Node decimal(World& world, double d, unsigned frac_digits) { chris@16: const SerdNode val = serd_node_new_decimal(d, 7); chris@16: const SerdNode type = serd_node_from_string( chris@16: SERD_URI, (const uint8_t*)SORD_NS_XSD "decimal"); chris@16: chris@16: return Node( chris@16: world, chris@16: sord_node_from_serd_node( chris@16: world.c_obj(), world.prefixes().c_obj(), &val, &type, NULL), chris@16: false); chris@16: } chris@16: chris@16: static inline Node integer(World& world, int64_t i) { chris@16: const SerdNode val = serd_node_new_integer(i); chris@16: const SerdNode type = serd_node_from_string( chris@16: SERD_URI, (const uint8_t*)SORD_NS_XSD "integer"); chris@16: chris@16: return Node( chris@16: world, chris@16: sord_node_from_serd_node( chris@16: world.c_obj(), world.prefixes().c_obj(), &val, &type, NULL), chris@16: false); chris@16: } chris@16: }; chris@16: chris@16: inline chris@16: Node::Node(World& world, Type type, const std::string& s) chris@16: : _world(&world) chris@16: { chris@16: switch (type) { chris@16: case URI: chris@16: _c_obj = sord_new_uri( chris@16: world.world(), (const unsigned char*)s.c_str()); chris@16: break; chris@16: case LITERAL: chris@16: _c_obj = sord_new_literal( chris@16: world.world(), NULL, (const unsigned char*)s.c_str(), NULL); chris@16: break; chris@16: case BLANK: chris@16: _c_obj = sord_new_blank( chris@16: world.world(), (const unsigned char*)s.c_str()); chris@16: break; chris@16: default: chris@16: _c_obj = NULL; chris@16: } chris@16: chris@16: assert(this->type() == type); chris@16: } chris@16: chris@16: inline chris@16: Node::Node(World& world) chris@16: : _world(&world) chris@16: { chris@16: Node me = blank_id(world); chris@16: *this = me; chris@16: } chris@16: chris@16: inline chris@16: Node::Node(World& world, const SordNode* node) chris@16: : _world(&world) chris@16: { chris@16: _c_obj = sord_node_copy(node); chris@16: } chris@16: chris@16: inline chris@16: Node::Node(World& world, SordNode* node, bool copy) chris@16: : _world(&world) chris@16: { chris@16: _c_obj = copy ? sord_node_copy(node) : node; chris@16: } chris@16: chris@16: inline chris@16: Node::Node(const Node& other) chris@16: : Wrapper() chris@16: , _world(other._world) chris@16: { chris@16: if (_world) { chris@16: _c_obj = other._c_obj ? sord_node_copy(other._c_obj) : NULL; chris@16: } chris@16: chris@16: assert((!_c_obj && !other._c_obj) || to_string() == other.to_string()); chris@16: } chris@16: chris@16: inline chris@16: Node::~Node() chris@16: { chris@16: if (_world) { chris@16: sord_node_free(_world->c_obj(), _c_obj); chris@16: } chris@16: } chris@16: chris@16: inline std::string chris@16: Node::to_string() const chris@16: { chris@16: return _c_obj ? (const char*)sord_node_get_string(_c_obj) : ""; chris@16: } chris@16: chris@16: inline const char* chris@16: Node::to_c_string() const chris@16: { chris@16: return (const char*)sord_node_get_string(_c_obj); chris@16: } chris@16: chris@16: inline const uint8_t* chris@16: Node::to_u_string() const chris@16: { chris@16: return sord_node_get_string(_c_obj); chris@16: } chris@16: chris@16: inline bool chris@16: Node::is_literal_type(const char* type_uri) const chris@16: { chris@16: if (_c_obj && sord_node_get_type(_c_obj) == SORD_LITERAL) { chris@16: const SordNode* datatype = sord_node_get_datatype(_c_obj); chris@16: if (datatype && !strcmp((const char*)sord_node_get_string(datatype), chris@16: type_uri)) chris@16: return true; chris@16: } chris@16: return false; chris@16: } chris@16: chris@16: inline int chris@16: Node::to_int() const chris@16: { chris@16: assert(is_int()); chris@16: char* endptr; chris@16: return strtol((const char*)sord_node_get_string(_c_obj), &endptr, 10); chris@16: } chris@16: chris@16: inline float chris@16: Node::to_float() const chris@16: { chris@16: assert(is_float()); chris@16: char* endptr; chris@16: return serd_strtod((const char*)sord_node_get_string(_c_obj), &endptr); chris@16: } chris@16: chris@16: inline bool chris@16: Node::to_bool() const chris@16: { chris@16: assert(is_bool()); chris@16: return !strcmp((const char*)sord_node_get_string(_c_obj), "true"); chris@16: } chris@16: chris@16: struct Iter : public Wrapper { chris@16: inline Iter(World& world, SordIter* c_obj) chris@16: : Wrapper(c_obj), _world(world) {} chris@16: inline ~Iter() { sord_iter_free(_c_obj); } chris@16: inline bool end() const { return sord_iter_end(_c_obj); } chris@16: inline bool next() const { return sord_iter_next(_c_obj); } chris@16: inline Iter& operator++() { chris@16: assert(!end()); chris@16: next(); chris@16: return *this; chris@16: } chris@16: inline const Node get_subject() const { chris@16: SordQuad quad; chris@16: sord_iter_get(_c_obj, quad); chris@16: return Node(_world, quad[SORD_SUBJECT]); chris@16: } chris@16: inline const Node get_predicate() const { chris@16: SordQuad quad; chris@16: sord_iter_get(_c_obj, quad); chris@16: return Node(_world, quad[SORD_PREDICATE]); chris@16: } chris@16: inline const Node get_object() const { chris@16: SordQuad quad; chris@16: sord_iter_get(_c_obj, quad); chris@16: return Node(_world, quad[SORD_OBJECT]); chris@16: } chris@16: World& _world; chris@16: }; chris@16: chris@16: /** An RDF Model (collection of triples). chris@16: */ chris@16: class Model : public Noncopyable, public Wrapper { chris@16: public: chris@16: inline Model(World& world, chris@16: const std::string& base_uri, chris@16: unsigned indices = (SORD_SPO | SORD_OPS), chris@16: bool graphs = true); chris@16: chris@16: inline ~Model(); chris@16: chris@16: inline const Node& base_uri() const { return _base; } chris@16: chris@16: size_t num_quads() const { return sord_num_quads(_c_obj); } chris@16: chris@16: inline void load_file(SerdEnv* env, chris@16: SerdSyntax syntax, chris@16: const std::string& uri, chris@16: const std::string& base_uri=""); chris@16: chris@16: inline void load_string(SerdEnv* env, chris@16: SerdSyntax syntax, chris@16: const char* str, chris@16: size_t len, chris@16: const std::string& base_uri); chris@16: chris@16: inline SerdStatus write_to_file( chris@16: const std::string& uri, chris@16: SerdSyntax syntax = SERD_TURTLE, chris@16: SerdStyle style = (SerdStyle)(SERD_STYLE_ABBREVIATED chris@16: |SERD_STYLE_CURIED chris@16: |SERD_STYLE_RESOLVED)); chris@16: chris@16: inline std::string write_to_string( chris@16: const std::string& base_uri, chris@16: SerdSyntax syntax = SERD_TURTLE, chris@16: SerdStyle style = (SerdStyle)(SERD_STYLE_ABBREVIATED chris@16: |SERD_STYLE_CURIED chris@16: |SERD_STYLE_RESOLVED)); chris@16: chris@16: inline void add_statement(const Node& subject, chris@16: const Node& predicate, chris@16: const Node& object); chris@16: chris@16: inline Iter find(const Node& subject, chris@16: const Node& predicate, chris@16: const Node& object); chris@16: chris@16: inline Node get(const Node& subject, chris@16: const Node& predicate, chris@16: const Node& object); chris@16: chris@16: inline World& world() const { return _world; } chris@16: chris@16: private: chris@16: World& _world; chris@16: Node _base; chris@16: SerdWriter* _writer; chris@16: size_t _next_blank_id; chris@16: }; chris@16: chris@16: /** Create an empty in-memory RDF model. chris@16: */ chris@16: inline chris@16: Model::Model(World& world, chris@16: const std::string& base_uri, chris@16: unsigned indices, chris@16: bool graphs) chris@16: : _world(world) chris@16: , _base(world, Node::URI, base_uri) chris@16: , _writer(NULL) chris@16: { chris@16: _c_obj = sord_new(_world.world(), indices, graphs); chris@16: } chris@16: chris@16: inline void chris@16: Model::load_string(SerdEnv* env, chris@16: SerdSyntax syntax, chris@16: const char* str, chris@16: size_t len, chris@16: const std::string& base_uri) chris@16: { chris@16: SerdReader* reader = sord_new_reader(_c_obj, env, syntax, NULL); chris@16: serd_reader_read_string(reader, (const uint8_t*)str); chris@16: serd_reader_free(reader); chris@16: } chris@16: chris@16: inline Model::~Model() chris@16: { chris@16: sord_free(_c_obj); chris@16: } chris@16: chris@16: inline void chris@16: Model::load_file(SerdEnv* env, chris@16: SerdSyntax syntax, chris@16: const std::string& data_uri, chris@16: const std::string& base_uri) chris@16: { chris@16: uint8_t* path = serd_file_uri_parse((const uint8_t*)data_uri.c_str(), NULL); chris@16: if (!path) { chris@16: fprintf(stderr, "Failed to parse file URI <%s>\n", data_uri.c_str()); chris@16: return; chris@16: } chris@16: chris@16: // FIXME: blank prefix parameter? chris@16: SerdReader* reader = sord_new_reader(_c_obj, env, syntax, NULL); chris@16: serd_reader_read_file(reader, path); chris@16: serd_reader_free(reader); chris@16: free(path); chris@16: } chris@16: chris@16: inline SerdStatus chris@16: Model::write_to_file(const std::string& uri, SerdSyntax syntax, SerdStyle style) chris@16: { chris@16: uint8_t* path = serd_file_uri_parse((const uint8_t*)uri.c_str(), NULL); chris@16: if (!path) { chris@16: fprintf(stderr, "Failed to parse file URI <%s>\n", uri.c_str()); chris@16: return SERD_ERR_BAD_ARG; chris@16: } chris@16: chris@16: FILE* const fd = fopen((const char*)path, "w"); chris@16: if (!fd) { chris@16: fprintf(stderr, "Failed to open file %s\n", path); chris@16: free(path); chris@16: return SERD_ERR_UNKNOWN; chris@16: } chris@16: free(path); chris@16: chris@16: SerdURI base_uri = SERD_URI_NULL; chris@16: if (serd_uri_parse((const uint8_t*)uri.c_str(), &base_uri)) { chris@16: fprintf(stderr, "Invalid base URI <%s>\n", uri.c_str()); chris@16: fclose(fd); chris@16: return SERD_ERR_BAD_ARG; chris@16: } chris@16: chris@16: SerdWriter* writer = serd_writer_new(syntax, chris@16: style, chris@16: _world.prefixes().c_obj(), chris@16: &base_uri, chris@16: serd_file_sink, chris@16: fd); chris@16: chris@16: serd_env_foreach(_world.prefixes().c_obj(), chris@16: (SerdPrefixSink)serd_writer_set_prefix, chris@16: writer); chris@16: chris@16: sord_write(_c_obj, writer, 0); chris@16: serd_writer_free(writer); chris@16: fclose(fd); chris@16: chris@16: return SERD_SUCCESS; chris@16: } chris@16: chris@16: static size_t chris@16: string_sink(const void* buf, size_t len, void* stream) chris@16: { chris@16: std::string* str = (std::string*)stream; chris@16: str->append((const char*)buf, len); chris@16: return len; chris@16: } chris@16: chris@16: inline std::string chris@16: Model::write_to_string(const std::string& base_uri_str, chris@16: SerdSyntax syntax, chris@16: SerdStyle style) chris@16: { chris@16: SerdURI base_uri = SERD_URI_NULL; chris@16: if (serd_uri_parse((const uint8_t*)base_uri_str.c_str(), &base_uri)) { chris@16: fprintf(stderr, "Invalid base URI <%s>\n", base_uri_str.c_str()); chris@16: return ""; chris@16: } chris@16: chris@16: std::string ret; chris@16: chris@16: SerdWriter* writer = serd_writer_new(syntax, chris@16: style, chris@16: _world.prefixes().c_obj(), chris@16: &base_uri, chris@16: string_sink, chris@16: &ret); chris@16: chris@16: serd_env_foreach(_world.prefixes().c_obj(), chris@16: (SerdPrefixSink)serd_writer_set_prefix, chris@16: writer); chris@16: chris@16: sord_write(_c_obj, writer, 0); chris@16: chris@16: serd_writer_free(writer); chris@16: return ret; chris@16: } chris@16: chris@16: inline void chris@16: Model::add_statement(const Node& subject, chris@16: const Node& predicate, chris@16: const Node& object) chris@16: { chris@16: SordQuad quad = { subject.c_obj(), chris@16: predicate.c_obj(), chris@16: object.c_obj(), chris@16: NULL }; chris@16: chris@16: sord_add(_c_obj, quad); chris@16: } chris@16: chris@16: inline Iter chris@16: Model::find(const Node& subject, chris@16: const Node& predicate, chris@16: const Node& object) chris@16: { chris@16: SordQuad quad = { subject.c_obj(), chris@16: predicate.c_obj(), chris@16: object.c_obj(), chris@16: NULL }; chris@16: chris@16: return Iter(_world, sord_find(_c_obj, quad)); chris@16: } chris@16: chris@16: inline Node chris@16: Model::get(const Node& subject, chris@16: const Node& predicate, chris@16: const Node& object) chris@16: { chris@16: SordNode* c_node = sord_get( chris@16: _c_obj, subject.c_obj(), predicate.c_obj(), object.c_obj(), NULL); chris@16: Node node(_world, c_node); chris@16: sord_node_free(_world.c_obj(), c_node); chris@16: return node; chris@16: } chris@16: chris@16: } // namespace Sord chris@16: chris@16: #endif // SORD_SORDMM_HPP chris@16: