From 26d0171f9bbc4af8036c704d3a4cb69a5d9fe942 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 2 Dec 2006 22:47:23 +0000 Subject: More serialization work git-svn-id: http://svn.drobilla.net/lad/ingen@206 a436a847-0d15-0410-975c-d299462d15a1 --- src/libs/client/Makefile.am | 2 + src/libs/client/RDFWriter.cpp | 211 +++++++++++++++++++++++++++ src/libs/client/RDFWriter.h | 71 +++++++++ src/libs/client/Serializer.cpp | 316 ++++++++++------------------------------- src/libs/client/Serializer.h | 64 ++------- 5 files changed, 370 insertions(+), 294 deletions(-) create mode 100644 src/libs/client/RDFWriter.cpp create mode 100644 src/libs/client/RDFWriter.h (limited to 'src/libs/client') diff --git a/src/libs/client/Makefile.am b/src/libs/client/Makefile.am index 6c8b93dd..cde46fbd 100644 --- a/src/libs/client/Makefile.am +++ b/src/libs/client/Makefile.am @@ -28,6 +28,8 @@ libingenclient_la_SOURCES = \ PatchModel.h \ PatchModel.cpp \ PluginModel.h \ + RDFWriter.h \ + RDFWriter.cpp \ Serializer.h \ Serializer.cpp \ DeprecatedSerializer.h \ diff --git a/src/libs/client/RDFWriter.cpp b/src/libs/client/RDFWriter.cpp new file mode 100644 index 00000000..e1762ed2 --- /dev/null +++ b/src/libs/client/RDFWriter.cpp @@ -0,0 +1,211 @@ +/* This file is part of Ingen. Copyright (C) 2006 Dave Robillard. + * + * Ingen 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. + * + * Ingen 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 "RDFWriter.h" +#include "raul/AtomRaptor.h" + +#define U(x) ((const unsigned char*)(x)) + +static const char* const RDF_LANG = "rdfxml-abbrev"; +//static const char* const RDF_LANG = "turtle"; + + +RDFWriter::RDFWriter() + : _serializer(NULL) + , _string_output(NULL) +{ + //_prefixes["xsd"] = "http://www.w3.org/2001/XMLSchema#"; + _prefixes["rdf"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; + _prefixes["ingen"] = "http://codeson.net/ns/ingen#"; + _prefixes["ingenuity"] = "http://codeson.net/ns/ingenuity#"; +} + + +void +RDFWriter::setup_prefixes() +{ + for (map::const_iterator i = _prefixes.begin(); i != _prefixes.end(); ++i) { + raptor_serialize_set_namespace(_serializer, + raptor_new_uri(U(i->second.c_str())), U(i->first.c_str())); + } +} + + +/** Expands the prefix of URI, if the prefix is registered. + * + * If uri is not a valid URI, the empty string is returned (so invalid URIs won't be serialized). + */ +string +RDFWriter::expand_uri(const string& uri) +{ + // FIXME: slow, stupid + for (map::const_iterator i = _prefixes.begin(); i != _prefixes.end(); ++i) + if (uri.substr(0, i->first.length()+1) == i->first + ":") + return i->second + uri.substr(i->first.length()+1); + + // FIXME: find a correct way to validate a URI + if (uri.find(":") == string::npos && uri.find("/") == string::npos) + return ""; + else + return uri; +} + + + +/** Begin a serialization to a file. + * + * This must be called before any write methods. + */ +void +RDFWriter::start_to_filename(const string& filename) throw (std::logic_error) +{ + if (_serializer) + throw std::logic_error("start_to_string called with serialization in progress"); + + raptor_init(); + _serializer = raptor_new_serializer(RDF_LANG); + setup_prefixes(); + raptor_serialize_start_to_filename(_serializer, filename.c_str()); +} + + +/** 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. + */ +void +RDFWriter::start_to_string() throw (std::logic_error) +{ + if (_serializer) + throw std::logic_error("start_to_string called with serialization in progress"); + + raptor_init(); + _serializer = raptor_new_serializer(RDF_LANG); + setup_prefixes(); + raptor_serialize_start_to_string(_serializer, + NULL /*base_uri*/, + (void**)&_string_output, + NULL /*size*/); +} + + +/** Finish a serialization. + * + * If this was a serialization to a string, the serialization output + * will be returned, otherwise the empty string is returned. + */ +string +RDFWriter::finish() throw(std::logic_error) +{ + string ret = ""; + + if (!_serializer) + throw std::logic_error("finish() called with no serialization in progress"); + + raptor_serialize_end(_serializer); + + if (_string_output) { + ret = string((char*)_string_output); + free(_string_output); + _string_output = NULL; + } + + raptor_free_serializer(_serializer); + _serializer = NULL; + + return ret; +} + + +void +RDFWriter::write(const RdfId& subject, + const RdfId& predicate, + const RdfId& object) +{ + assert(_serializer); + + raptor_statement triple; + + // FIXME: leaks? + + if (subject.type() == RdfId::RESOURCE) { + triple.subject = (void*)raptor_new_uri((const unsigned char*)subject.to_string().c_str()); + triple.subject_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE; + } else { + assert(subject.type() == RdfId::ANONYMOUS); + triple.subject = (unsigned char*)(strdup(subject.to_string().c_str())); + triple.subject_type = RAPTOR_IDENTIFIER_TYPE_ANONYMOUS; + } + + assert(predicate.type() == RdfId::RESOURCE); + triple.predicate = (void*)raptor_new_uri((const unsigned char*)predicate.to_string().c_str()); + triple.predicate_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE; + + if (object.type() == RdfId::RESOURCE) { + triple.object = (void*)raptor_new_uri((const unsigned char*)object.to_string().c_str()); + triple.object_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE; + } else { + assert(object.type() == RdfId::ANONYMOUS); + triple.object = (unsigned char*)(strdup(object.to_string().c_str())); + triple.object_type = RAPTOR_IDENTIFIER_TYPE_ANONYMOUS; + } + + raptor_serialize_statement(_serializer, &triple); + + if (subject.type() == RdfId::RESOURCE) + raptor_free_uri((raptor_uri*)triple.subject); + + raptor_free_uri((raptor_uri*)triple.predicate); + + if (object.type() == RdfId::RESOURCE) + raptor_free_uri((raptor_uri*)triple.object); +} + + +void +RDFWriter::write(const RdfId& subject, + const RdfId& predicate, + const Atom& object) +{ + assert(_serializer); + + raptor_statement triple; + + if (subject.type() == RdfId::RESOURCE) { + triple.subject = (void*)raptor_new_uri((const unsigned char*)subject.to_string().c_str()); + triple.subject_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE; + } else { + assert(subject.type() == RdfId::ANONYMOUS); + triple.subject = (unsigned char*)(strdup(subject.to_string().c_str())); + triple.subject_type = RAPTOR_IDENTIFIER_TYPE_ANONYMOUS; + } + + assert(predicate.type() == RdfId::RESOURCE); + triple.predicate = (void*)raptor_new_uri((const unsigned char*)predicate.to_string().c_str()); + triple.predicate_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE; + + AtomRaptor::atom_to_triple_object(&triple, object); + + raptor_serialize_statement(_serializer, &triple); + + if (subject.type() == RdfId::RESOURCE) + raptor_free_uri((raptor_uri*)triple.subject); + + raptor_free_uri((raptor_uri*)triple.predicate); +} diff --git a/src/libs/client/RDFWriter.h b/src/libs/client/RDFWriter.h new file mode 100644 index 00000000..7bc4b14f --- /dev/null +++ b/src/libs/client/RDFWriter.h @@ -0,0 +1,71 @@ +/* This file is part of Ingen. Copyright (C) 2006 Dave Robillard. + * + * Ingen 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. + * + * Ingen 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 + */ + +#ifndef RDFWRITER_H +#define RDFWRITER_H + +#include +#include +#include +#include +#include "raul/Atom.h" +using std::string; using std::map; + + +class RdfId { +public: + enum Type { ANONYMOUS, RESOURCE }; + + RdfId(Type t, const string& s) : _type(t), _string(s) {} + + Type type() const { return _type; } + const string& to_string() const { return _string; } + +private: + Type _type; + string _string; ///< URI or blank node ID, depending on _type +}; + + +class RDFWriter { +public: + RDFWriter(); + + void setup_prefixes(); + string expand_uri(const string& uri); + + void start_to_filename(const string& filename) throw (std::logic_error); + void start_to_string() throw (std::logic_error); + string finish() throw (std::logic_error); + + bool serialization_in_progress() { return (_serializer != NULL); } + + void write(const RdfId& subject, + const RdfId& predicate, + const RdfId& object); + + void write(const RdfId& subject, + const RdfId& predicate, + const Atom& object); + +private: + raptor_serializer* _serializer; + unsigned char* _string_output; + map _prefixes; +}; + + +#endif // RDFWRITER_H diff --git a/src/libs/client/Serializer.cpp b/src/libs/client/Serializer.cpp index 23ea7257..f40d8d67 100644 --- a/src/libs/client/Serializer.cpp +++ b/src/libs/client/Serializer.cpp @@ -39,11 +39,6 @@ #include "raul/Atom.h" #include "raul/AtomRaptor.h" -#define U(x) ((const unsigned char*)(x)) - -static const char* const RDF_LANG = "rdfxml-abbrev"; -//static const char* const RDF_LANG = "turtle"; - using std::string; using std::vector; using std::pair; using std::cerr; using std::cout; using std::endl; using boost::optional; @@ -53,15 +48,9 @@ namespace Client { Serializer::Serializer(SharedPtr engine) - : _serializer(NULL) - , _string_output(NULL) - , _patch_search_path(".") + : _patch_search_path(".") , _engine(engine) { - //_prefixes["xsd"] = "http://www.w3.org/2001/XMLSchema#"; - _prefixes["rdf"] = "http://www.w3.org/1999/02/22-rdf-syntax-ns#"; - _prefixes["ingen"] = "http://codeson.net/ns/ingen#"; - _prefixes["ingenuity"] = "http://codeson.net/ns/ingenuity#"; } @@ -77,13 +66,7 @@ Serializer::~Serializer() void Serializer::start_to_filename(const string& filename) throw (std::logic_error) { - if (_serializer) - throw std::logic_error("start_to_string called with serialization in progress"); - - raptor_init(); - _serializer = raptor_new_serializer(RDF_LANG); - setup_prefixes(); - raptor_serialize_start_to_filename(_serializer, filename.c_str()); + _writer.start_to_filename(filename); } @@ -97,16 +80,7 @@ Serializer::start_to_filename(const string& filename) throw (std::logic_error) void Serializer::start_to_string() throw (std::logic_error) { - if (_serializer) - throw std::logic_error("start_to_string called with serialization in progress"); - - raptor_init(); - _serializer = raptor_new_serializer(RDF_LANG); - setup_prefixes(); - raptor_serialize_start_to_string(_serializer, - NULL /*base_uri*/, - (void**)&_string_output, - NULL /*size*/); + _writer.start_to_string(); } @@ -118,53 +92,23 @@ Serializer::start_to_string() throw (std::logic_error) string Serializer::finish() throw(std::logic_error) { - string ret = ""; - - if (!_serializer) - throw std::logic_error("finish() called with no serialization in progress"); - - raptor_serialize_end(_serializer); - - if (_string_output) { - ret = string((char*)_string_output); - free(_string_output); - _string_output = NULL; - } - - raptor_free_serializer(_serializer); - _serializer = NULL; - - return ret; -} - - -void -Serializer::setup_prefixes() -{ - for (map::const_iterator i = _prefixes.begin(); i != _prefixes.end(); ++i) { - raptor_serialize_set_namespace(_serializer, - raptor_new_uri(U(i->second.c_str())), U(i->first.c_str())); - } + return _writer.finish(); } -/** Expands the prefix of URI, if the prefix is registered. - * - * If uri is not a valid URI, the empty string is returned (so invalid URIs won't be serialized). +/** Convert a path to an RDF blank node ID for serializing. */ -string -Serializer::expand_uri(const string& uri) +RdfId +Serializer::path_to_node_id(const Path& path) { - // FIXME: slow, stupid - for (map::const_iterator i = _prefixes.begin(); i != _prefixes.end(); ++i) - if (uri.substr(0, i->first.length()+1) == i->first + ":") - return i->second + uri.substr(i->first.length()+1); + string ret = path.substr(1); - // FIXME: find a correct way to validate a URI - if (uri.find(":") == string::npos && uri.find("/") == string::npos) - return ""; - else - return uri; + for (size_t i=0; i < ret.length(); ++i) { + if (ret[i] == '/') + ret[i] = '_'; + } + + return RdfId(RdfId::ANONYMOUS, ret); } @@ -218,150 +162,29 @@ Serializer::find_file(const string& filename, const string& additional_path) } -void -Serializer::serialize_resource(const string& subject_uri, - const string& predicate_uri, - const string& object_uri) -{ - assert(_serializer); - - raptor_statement triple; - triple.subject = (void*)raptor_new_uri((const unsigned char*)subject_uri.c_str()); - triple.subject_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE; - triple.predicate = (void*)raptor_new_uri((const unsigned char*)predicate_uri.c_str()); - triple.predicate_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE; - triple.object = (void*)raptor_new_uri((const unsigned char*)object_uri.c_str()); - triple.object_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE; - - raptor_serialize_statement(_serializer, &triple); - - raptor_free_uri((raptor_uri*)triple.subject); - raptor_free_uri((raptor_uri*)triple.predicate); -} - - -void -Serializer::serialize_resource_blank(const string& node_id, - const string& predicate_uri, - const string& object_uri) -{ - assert(_serializer); - - raptor_statement triple; - triple.subject = (unsigned char*)strdup(node_id.c_str()); - triple.subject_type = RAPTOR_IDENTIFIER_TYPE_ANONYMOUS; - triple.predicate = (void*)raptor_new_uri((const unsigned char*)predicate_uri.c_str()); - triple.predicate_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE; - triple.object = (void*)raptor_new_uri((const unsigned char*)object_uri.c_str()); - triple.object_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE; - - raptor_serialize_statement(_serializer, &triple); - - raptor_free_uri((raptor_uri*)triple.predicate); - raptor_free_uri((raptor_uri*)triple.object); -} - - -void -Serializer::serialize_blank(const string& subject_uri, - const string& predicate_uri, - const string& object_node_id) -{ - assert(_serializer); - - raptor_statement triple; - triple.subject = (void*)raptor_new_uri((const unsigned char*)subject_uri.c_str()); - triple.subject_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE; - triple.predicate = (void*)raptor_new_uri((const unsigned char*)predicate_uri.c_str()); - triple.predicate_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE; - triple.object = (unsigned char*)strdup(object_node_id.c_str()); - triple.object_type = RAPTOR_IDENTIFIER_TYPE_ANONYMOUS; - - raptor_serialize_statement(_serializer, &triple); - - raptor_free_uri((raptor_uri*)triple.subject); - raptor_free_uri((raptor_uri*)triple.predicate); -} - - -void -Serializer::serialize_blank_blank(const string& subject_node_id, - const string& predicate_uri, - const string& object_node_id) -{ - assert(_serializer); - - raptor_statement triple; - triple.subject = (unsigned char*)strdup(subject_node_id.c_str()); - triple.subject_type = RAPTOR_IDENTIFIER_TYPE_ANONYMOUS; - triple.predicate = (void*)raptor_new_uri((const unsigned char*)predicate_uri.c_str()); - triple.predicate_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE; - triple.object = (unsigned char*)strdup(object_node_id.c_str()); - triple.object_type = RAPTOR_IDENTIFIER_TYPE_ANONYMOUS; - - raptor_serialize_statement(_serializer, &triple); - - raptor_free_uri((raptor_uri*)triple.predicate); -} - -void -Serializer::serialize_atom(const string& subject_uri, - const string& predicate_uri, - const Atom& atom) -{ - assert(_serializer); - - raptor_statement triple; - triple.subject = (void*)raptor_new_uri((const unsigned char*)subject_uri.c_str()); - triple.subject_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE; - triple.predicate = (void*)raptor_new_uri((const unsigned char*)predicate_uri.c_str()); - triple.predicate_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE; - - AtomRaptor::atom_to_triple_object(&triple, atom); - - raptor_serialize_statement(_serializer, &triple); -} - - -void -Serializer::serialize_atom_blank(const string& node_id, - const string& predicate_uri, - const Atom& atom) -{ - assert(_serializer); - - raptor_statement triple; - triple.subject = (unsigned char*)strdup(node_id.c_str()); - triple.subject_type = RAPTOR_IDENTIFIER_TYPE_ANONYMOUS; - triple.predicate = (void*)raptor_new_uri((const unsigned char*)predicate_uri.c_str()); - triple.predicate_type = RAPTOR_IDENTIFIER_TYPE_RESOURCE; - - AtomRaptor::atom_to_triple_object(&triple, atom); - - raptor_serialize_statement(_serializer, &triple); -} - void Serializer::serialize(SharedPtr object) throw (std::logic_error) { - if (!_serializer) - throw std::logic_error("serialize_patch called without serialization in progress"); + if (!_writer.serialization_in_progress()) + throw std::logic_error("serialize called without serialization in progress"); + // FIXME: depth + SharedPtr patch = PtrCast(object); if (patch) { - serialize_patch(patch); + serialize_patch(patch, 0); return; } SharedPtr node = PtrCast(object); if (node) { - serialize_node(node); + serialize_node(node, 0); return; } SharedPtr port = PtrCast(object); if (port) { - serialize_port(port); + serialize_port(port, 0); return; } @@ -371,40 +194,43 @@ Serializer::serialize(SharedPtr object) throw (std::logic_error) void -Serializer::serialize_patch(SharedPtr patch) +Serializer::serialize_patch(SharedPtr patch, unsigned depth) { assert(_serializer); - const string uri = "#"; + const RdfId patch_id = (depth == 0) + ? RdfId(RdfId::RESOURCE, string("#") + patch->path().substr(1)) + : path_to_node_id(patch->path()); // anonymous - serialize_resource( - uri.c_str(), + _writer.write( + patch_id, NS_RDF("type"), NS_INGEN("Patch")); if (patch->path().name().length() > 0) { - serialize_atom( - uri.c_str(), NS_INGEN("name"), + _writer.write( + patch_id, NS_INGEN("name"), Atom(patch->path().name().c_str())); } - serialize_atom( - uri.c_str(), + _writer.write( + patch_id, NS_INGEN("polyphony"), Atom((int)patch->poly())); for (NodeModelMap::const_iterator n = patch->nodes().begin(); n != patch->nodes().end(); ++n) { - serialize_node(n->second, "#"); - serialize_resource("#", NS_INGEN("node"), uri + n->second->path().name()); + _writer.write(patch_id, NS_INGEN("node"), path_to_node_id(n->second->path())); + serialize_node(n->second, depth+1); } for (PortModelList::const_iterator p = patch->ports().begin(); p != patch->ports().end(); ++p) { - serialize_port(*p, uri); - serialize_blank("#", NS_INGEN("port"), (*p)->path()); + _writer.write(patch_id, NS_INGEN("port"), path_to_node_id((*p)->path())); + serialize_port(*p, depth+1); + } for (ConnectionList::const_iterator c = patch->connections().begin(); c != patch->connections().end(); ++c) { - serialize_connection(*c/*, uri*/); + serialize_connection(*c); } //_engine->set_metadata(patch->path(), "uri", uri); @@ -412,32 +238,34 @@ Serializer::serialize_patch(SharedPtr patch) void -Serializer::serialize_node(SharedPtr node, const string ns_prefix) +Serializer::serialize_node(SharedPtr node, unsigned depth) { assert(_serializer); - const string node_uri = ns_prefix + node->path().name(); - - serialize_resource( - node_uri.c_str(), + const RdfId node_id = (depth == 0) + ? RdfId(RdfId::RESOURCE, string("#") + node->path().substr(1)) + : path_to_node_id(node->path()); // anonymous + + _writer.write( + node_id, NS_RDF("type"), NS_INGEN("Node")); - /*serialize_atom(_serializer, + /*_writer.write(_serializer, node_uri_ref.c_str(), NS_INGEN("name"), Atom(node->path().name()));*/ for (PortModelList::const_iterator p = node->ports().begin(); p != node->ports().end(); ++p) { - serialize_port(*p, node_uri + "/"); - serialize_blank(node_uri, NS_INGEN("port"), (*p)->path()); + serialize_port(*p, depth+1); + _writer.write(node_id, NS_INGEN("port"), path_to_node_id((*p)->path())); } for (MetadataMap::const_iterator m = node->metadata().begin(); m != node->metadata().end(); ++m) { - if (expand_uri(m->first) != "") { - serialize_atom( - node_uri.c_str(), - expand_uri(m->first.c_str()).c_str(), + if (_writer.expand_uri(m->first) != "") { + _writer.write( + node_id, + RdfId(RdfId::RESOURCE, _writer.expand_uri(m->first.c_str()).c_str()), m->second); } } @@ -448,26 +276,27 @@ Serializer::serialize_node(SharedPtr node, const string ns_prefix) * Audio output ports with no metadata will not be written, for example. */ void -Serializer::serialize_port(SharedPtr port, const string ns_prefix) +Serializer::serialize_port(SharedPtr port, unsigned depth) { assert(_serializer); - //const string port_uri_ref = ns_prefix + port->path().name(); - const string node_id = port->path(); + const RdfId port_id = (depth == 0) + ? RdfId(RdfId::RESOURCE, string("#") + port->path().substr(1)) + : path_to_node_id(port->path()); // anonymous if (port->is_input()) - serialize_resource_blank(node_id, NS_RDF("type"), NS_INGEN("InputPort")); + _writer.write(port_id, NS_RDF("type"), NS_INGEN("InputPort")); else - serialize_resource_blank(node_id, NS_RDF("type"), NS_INGEN("OutputPort")); + _writer.write(port_id, NS_RDF("type"), NS_INGEN("OutputPort")); - serialize_atom_blank(node_id, NS_INGEN("name"), Atom(port->path().name().c_str())); + _writer.write(port_id, NS_INGEN("name"), Atom(port->path().name().c_str())); if (port->metadata().size() > 0) { for (MetadataMap::const_iterator m = port->metadata().begin(); m != port->metadata().end(); ++m) { - if (expand_uri(m->first) != "") { - serialize_atom_blank( - node_id, - expand_uri(m->first).c_str(), + if (_writer.expand_uri(m->first) != "") { + _writer.write( + port_id, + RdfId(RdfId::RESOURCE, _writer.expand_uri(m->first).c_str()), m->second); } } @@ -480,24 +309,23 @@ Serializer::serialize_connection(SharedPtr connection) throw (s { assert(_serializer); - // (Double slash intentional) - const string node_id = connection->src_port_path() + "/" + connection->dst_port_path(); - - //raptor_identifier* c = raptor_new_identifier(RAPTOR_IDENTIFIER_TYPE_ANONYMOUS, - // NULL, RAPTOR_URI_SOURCE_BLANK_ID, (const unsigned char*)"genid1", NULL, NULL, NULL); + const RdfId connection_id = RdfId(RdfId::ANONYMOUS, + path_to_node_id(connection->src_port_path()).to_string() + + "-" + path_to_node_id(connection->dst_port_path()).to_string()); + /* const string src_port_rel_path = connection->src_port_path().substr(connection->patch_path().length()); const string dst_port_rel_path = connection->dst_port_path().substr(connection->patch_path().length()); - - serialize_resource_blank(node_id, NS_RDF("type"), NS_INGEN("Connection")); +*/ + _writer.write(connection_id, NS_RDF("type"), NS_INGEN("Connection")); - serialize_blank_blank(node_id, + _writer.write(connection_id, NS_INGEN("source"), - src_port_rel_path); + path_to_node_id(connection->src_port_path())); - serialize_blank_blank(node_id, + _writer.write(connection_id, NS_INGEN("destination"), - dst_port_rel_path); + path_to_node_id(connection->dst_port_path())); } diff --git a/src/libs/client/Serializer.h b/src/libs/client/Serializer.h index 4aa57be3..5fe00199 100644 --- a/src/libs/client/Serializer.h +++ b/src/libs/client/Serializer.h @@ -28,6 +28,7 @@ #include "raul/Path.h" #include "raul/Atom.h" #include "ObjectModel.h" +#include "RDFWriter.h" using std::string; using boost::optional; @@ -44,8 +45,8 @@ class ModelEngineInterface; /** Namespace prefix macros. */ -#define NS_RDF(x) "http://www.w3.org/1999/02/22-rdf-syntax-ns#" x -#define NS_INGEN(x) "http://codeson.net/ns/ingen#" x +#define NS_RDF(x) RdfId(RdfId::RESOURCE, "http://www.w3.org/1999/02/22-rdf-syntax-ns#" x) +#define NS_INGEN(x) RdfId(RdfId::RESOURCE, "http://codeson.net/ns/ingen#" x) /** Handles all patch saving and loading. @@ -72,59 +73,22 @@ public: optional engine_poly = optional()); - void start_to_filename(const string& filename) throw (std::logic_error); - void start_to_string() throw (std::logic_error); - void serialize(SharedPtr object) throw (std::logic_error); - void serialize_connection(SharedPtr c) throw (std::logic_error); - string finish() throw (std::logic_error); + void start_to_filename(const string& filename) throw (std::logic_error); + void start_to_string() throw (std::logic_error); + void serialize(SharedPtr object) throw (std::logic_error); + void serialize_connection(SharedPtr c) throw (std::logic_error); + string finish() throw (std::logic_error); private: - // Model -> RDF + void serialize_patch(SharedPtr p, unsigned depth); + void serialize_node(SharedPtr n, unsigned depth); + void serialize_port(SharedPtr p, unsigned depth); - void serialize_patch(SharedPtr p); + RdfId path_to_node_id(const Path& path); - void serialize_node(SharedPtr n, const string ns_prefix=""); - - void serialize_port(SharedPtr p, const string ns_prefix=""); - - - - - // Triple -> RDF - - void serialize_resource(const string& subject_uri, - const string& predicate_uri, - const string& object_uri); - - void serialize_resource_blank(const string& subject_node_id, - const string& predicate_uri, - const string& object_uri); - - void serialize_blank(const string& subject_uri, - const string& predicate_uri, - const string& object_node_id); - - void serialize_blank_blank(const string& subject_node_id, - const string& predicate_uri, - const string& object_node_id); - - void serialize_atom(const string& subject_uri, - const string& predicate_uri, - const Atom& atom); - - void serialize_atom_blank(const string& subject_node_id, - const string& predicate_uri, - const Atom& atom); - - void setup_prefixes(); - string expand_uri(const string& uri); - - - raptor_serializer* _serializer; - unsigned char* _string_output; - string _patch_search_path; - map _prefixes; + RDFWriter _writer; + string _patch_search_path; SharedPtr _engine; }; -- cgit v1.2.1