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