diff options
author | David Robillard <d@drobilla.net> | 2007-05-01 04:01:04 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2007-05-01 04:01:04 +0000 |
commit | c5eb1148b99cf88555c0a516e1218bdb74ab0080 (patch) | |
tree | 4f72d46c43ff7fe39ebce3bd39911c3b09952868 /src/RDFModel.cpp | |
parent | a5ce2e3af283f3ffeb2d30a34546ceab0a84d702 (diff) | |
download | raul-c5eb1148b99cf88555c0a516e1218bdb74ab0080.tar.gz raul-c5eb1148b99cf88555c0a516e1218bdb74ab0080.tar.bz2 raul-c5eb1148b99cf88555c0a516e1218bdb74ab0080.zip |
Converted Raul (and thus Ingen and Machina) to use Redland over Raptor/Rasqal independently.
Fixed patch loading for Ingen (local only, still something wrong with remote...).
git-svn-id: http://svn.drobilla.net/lad/raul@486 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'src/RDFModel.cpp')
-rw-r--r-- | src/RDFModel.cpp | 215 |
1 files changed, 215 insertions, 0 deletions
diff --git a/src/RDFModel.cpp b/src/RDFModel.cpp new file mode 100644 index 0000000..d05864f --- /dev/null +++ b/src/RDFModel.cpp @@ -0,0 +1,215 @@ +/* This file is part of Raul. + * Copyright (C) 2007 Dave Robillard <http://drobilla.net> + * + * Raul is free software; you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation; either version 2 of the License, or (at your option) any later + * version. + * + * Raul is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include <sstream> +#include "raul/RDFWorld.h" +#include "raul/RDFModel.h" +#include "raul/RDFNode.h" +#include "raul/AtomRedland.h" + +#define U(x) ((const unsigned char*)(x)) + +using namespace std; + +namespace Raul { +namespace RDF { + + +//static const char* const RDF_LANG = "rdfxml-abbrev"; +static const char* const RDF_LANG = "turtle"; + + +/** Create an empty in-memory RDF model. + */ +Model::Model(RDF::World& world) + : _world(world) + , _serializer(NULL) +{ + _storage = librdf_new_storage(_world.world(), "hashes", NULL, "hash-type='memory'"); + _model = librdf_new_model(_world.world(), _storage, NULL); +} + + +/** Load a model from a URI (local file or remote). + */ +Model::Model(World& world, const Glib::ustring& data_uri) + : _world(world) + , _serializer(NULL) +{ + _storage = librdf_new_storage(_world.world(), "hashes", NULL, "hash-type='memory'"); + _model = librdf_new_model(_world.world(), _storage, NULL); + + librdf_uri* uri = librdf_new_uri(world.world(), (const unsigned char*)data_uri.c_str()); + librdf_parser* parser = librdf_new_parser(world.world(), "guess", NULL, NULL); + librdf_parser_parse_into_model(parser, uri, NULL, _model); + librdf_free_uri(uri); + librdf_free_parser(parser); + + cout << endl << "Loaded model from " << data_uri << ":" << endl; + serialize_to_file_handle(stdout); + cout << endl << endl; +} + + +Model::~Model() +{ + if (_serializer) + librdf_free_serializer(_serializer); + + librdf_free_model(_model); + librdf_free_storage(_storage); +} + + +void +Model::setup_prefixes() +{ + assert(_serializer); + + for (Namespaces::const_iterator i = _world.prefixes().begin(); i != _world.prefixes().end(); ++i) { + librdf_serializer_set_namespace(_serializer, + librdf_new_uri(_world.world(), U(i->second.c_str())), i->first.c_str()); + } +} + + +/** Begin a serialization to a C file handle. + * + * This must be called before any write methods. + */ +void +Model::serialize_to_file_handle(FILE* fd) +{ + _serializer = librdf_new_serializer(_world.world(), RDF_LANG, NULL, NULL); + setup_prefixes(); + librdf_serializer_serialize_model_to_file_handle(_serializer, fd, NULL, _model); + librdf_free_serializer(_serializer); + _serializer = NULL; +} + + +/** Begin a serialization to a file. + * + * This must be called before any write methods. + */ +void +Model::serialize_to_file(const string& filename) +{ + _serializer = librdf_new_serializer(_world.world(), RDF_LANG, NULL, NULL); + setup_prefixes(); + librdf_serializer_serialize_model_to_file(_serializer, filename.c_str(), NULL, _model); + librdf_free_serializer(_serializer); + _serializer = NULL; +} + + +/** Begin a serialization to a string. + * + * This must be called before any write methods. + * + * The results of the serialization will be returned by the finish() method after + * the desired objects have been serialized. + */ +string +Model::serialize_to_string() +{ + _serializer = librdf_new_serializer(_world.world(), RDF_LANG, NULL, NULL); + setup_prefixes(); + + unsigned char* c_str + = librdf_serializer_serialize_model_to_string(_serializer, NULL, _model); + + string result((const char*)c_str); + free(c_str); + + librdf_free_serializer(_serializer); + _serializer = NULL; + + return result; +} + + +void +Model::add_statement(const Node& subject, + const Node& predicate, + const Node& object) +{ + librdf_statement* triple = librdf_new_statement_from_nodes(_world.world(), + subject.get_node(), predicate.get_node(), object.get_node()); + + librdf_model_add_statement(_model, triple); +} + + +void +Model::add_statement(const Node& subject, + const string& predicate_id, + const Node& object) +{ + const string predicate_uri = _world.expand_uri(predicate_id); + librdf_node* predicate = librdf_new_node_from_uri_string(_world.world(), + (const unsigned char*)predicate_uri.c_str()); + + librdf_statement* triple = librdf_new_statement_from_nodes(_world.world(), + subject.get_node(), predicate, object.get_node()); + + librdf_model_add_statement(_model, triple); +} + + +void +Model::add_statement(const Node& subject, + const Node& predicate, + const Atom& object) +{ + librdf_node* atom_node = AtomRedland::atom_to_rdf_node(_world.world(), object); + + if (atom_node) { + librdf_statement* triple = librdf_new_statement_from_nodes(_world.world(), + subject.get_node(), predicate.get_node(), atom_node); + librdf_model_add_statement(_model, triple); + } else { + cerr << "WARNING: Unable to add statement (unserializable Atom)" << endl; + } +} + + +void +Model::add_statement(const Node& subject, + const string& predicate_id, + const Atom& object) +{ + const string predicate_uri = _world.expand_uri(predicate_id); + librdf_node* predicate = librdf_new_node_from_uri_string(_world.world(), + (const unsigned char*)predicate_uri.c_str()); + + librdf_node* atom_node = AtomRedland::atom_to_rdf_node(_world.world(), object); + + if (atom_node) { + librdf_statement* triple = librdf_new_statement_from_nodes(_world.world(), + subject.get_node(), predicate, atom_node); + librdf_model_add_statement(_model, triple); + } else { + cerr << "WARNING: Unable to add statement (unserializable Atom)" << endl; + } +} + + + +} // namespace RDF +} // namespace Raul + |