From bade64c9aca86225fa37950c17aabc02a3f4a041 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Mon, 8 Oct 2007 16:41:15 +0000 Subject: Serializer -> Serialiser. Ingen is a god-fearing proper Queen's english speaking upstanding gentleman of a project, cherrio, pip-pip, indeed, etc. git-svn-id: http://svn.drobilla.net/lad/ingen@849 a436a847-0d15-0410-975c-d299462d15a1 --- src/libs/gui/PatchCanvas.cpp | 6 +- src/libs/gui/ThreadedLoader.hpp | 6 +- src/libs/gui/UploadPatchWindow.cpp | 4 +- src/libs/serialisation/Makefile.am | 4 +- src/libs/serialisation/Serialiser.cpp | 434 ++++++++++++++++++++++++++++++++++ src/libs/serialisation/Serialiser.hpp | 96 ++++++++ src/libs/serialisation/Serializer.cpp | 434 ---------------------------------- src/libs/serialisation/Serializer.hpp | 96 -------- 8 files changed, 540 insertions(+), 540 deletions(-) create mode 100644 src/libs/serialisation/Serialiser.cpp create mode 100644 src/libs/serialisation/Serialiser.hpp delete mode 100644 src/libs/serialisation/Serializer.cpp delete mode 100644 src/libs/serialisation/Serializer.hpp diff --git a/src/libs/gui/PatchCanvas.cpp b/src/libs/gui/PatchCanvas.cpp index f991df8e..a02235bf 100644 --- a/src/libs/gui/PatchCanvas.cpp +++ b/src/libs/gui/PatchCanvas.cpp @@ -22,7 +22,7 @@ #include #include #include "interface/EngineInterface.hpp" -#include "serialisation/Serializer.hpp" +#include "serialisation/Serialiser.hpp" #include "client/PluginModel.hpp" #include "client/PatchModel.hpp" #include "client/NodeModel.hpp" @@ -41,7 +41,7 @@ #include "GladeFactory.hpp" #include "WindowFactory.hpp" using Ingen::Client::Store; -using Ingen::Serialisation::Serializer; +using Ingen::Serialisation::Serialiser; using Ingen::Client::PluginModel; namespace Ingen { @@ -458,7 +458,7 @@ PatchCanvas::destroy_selection() void PatchCanvas::copy_selection() { - Serializer serializer(*App::instance().world()->rdf_world); + Serialiser serializer(*App::instance().world()->rdf_world); serializer.start_to_string(); for (list >::iterator m = _selected_items.begin(); m != _selected_items.end(); ++m) { diff --git a/src/libs/gui/ThreadedLoader.hpp b/src/libs/gui/ThreadedLoader.hpp index 8d140c26..d0f171af 100644 --- a/src/libs/gui/ThreadedLoader.hpp +++ b/src/libs/gui/ThreadedLoader.hpp @@ -28,7 +28,7 @@ #include "interface/EngineInterface.hpp" #include "client/PatchModel.hpp" #include "client/DeprecatedLoader.hpp" -#include "serialisation/Serializer.hpp" +#include "serialisation/Serialiser.hpp" #include "serialisation/Loader.hpp" using std::string; using std::list; @@ -59,7 +59,7 @@ public: ~ThreadedLoader(); // FIXME: there's a pattern here.... - // (same core interface as Loader/Serializer) + // (same core interface as Loader/Serialiser) void load_patch(bool merge, const string& data_base_uri, @@ -85,7 +85,7 @@ private: SharedPtr _loader; DeprecatedLoader _deprecated_loader; - Serializer _serializer; + Serialiser _serializer; Glib::Mutex _mutex; list _events; }; diff --git a/src/libs/gui/UploadPatchWindow.cpp b/src/libs/gui/UploadPatchWindow.cpp index b804796f..a31f0cf0 100644 --- a/src/libs/gui/UploadPatchWindow.cpp +++ b/src/libs/gui/UploadPatchWindow.cpp @@ -24,7 +24,7 @@ #include #include "module/module.h" #include "interface/EngineInterface.hpp" -#include "serialisation/Serializer.hpp" +#include "serialisation/Serialiser.hpp" #include "client/PatchModel.hpp" #include "UploadPatchWindow.hpp" #include "App.hpp" @@ -248,7 +248,7 @@ UploadPatchWindow::upload_clicked() _upload_progress->set_fraction(0.0); _upload_progress->set_text(""); - Serializer s(*App::instance().world()->rdf_world); + Serialiser s(*App::instance().world()->rdf_world); s.start_to_string(); s.serialize(_patch); const string str = s.finish(); diff --git a/src/libs/serialisation/Makefile.am b/src/libs/serialisation/Makefile.am index eff5a7a4..0a914222 100644 --- a/src/libs/serialisation/Makefile.am +++ b/src/libs/serialisation/Makefile.am @@ -11,8 +11,8 @@ libingen_serialisation_la_LIBADD = @RAUL_LIBS@ @GLIBMM_LIBS@ libingen_serialisation_la_SOURCES = \ Loader.cpp \ Loader.hpp \ - Serializer.cpp \ - Serializer.hpp \ + Serialiser.cpp \ + Serialiser.hpp \ serialisation.cpp \ serialisation.hpp diff --git a/src/libs/serialisation/Serialiser.cpp b/src/libs/serialisation/Serialiser.cpp new file mode 100644 index 00000000..15b7f753 --- /dev/null +++ b/src/libs/serialisation/Serialiser.cpp @@ -0,0 +1,434 @@ +/* This file is part of Ingen. + * Copyright (C) 2007 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 +#include +#include +#include // atof +#include +#include +#include +#include +#include +#include +#include // pair, make_pair +#include +#include +#include +#include +#include +#include +#include +#include +#include "interface/EngineInterface.hpp" +#include "interface/Plugin.hpp" +#include "interface/Patch.hpp" +#include "interface/Node.hpp" +#include "interface/Port.hpp" +#include "interface/Connection.hpp" +#include "Serialiser.hpp" + +using namespace std; +using namespace Raul; +using namespace Raul::RDF; +using namespace Ingen; +using namespace Ingen::Shared; + +namespace Ingen { +namespace Serialisation { + + +Serialiser::Serialiser(Raul::RDF::World& world) + : _world(world) +{ +} + +void +Serialiser::to_file(SharedPtr object, const string& filename) +{ + _root_object = object; + start_to_filename(filename); + serialize(object); + finish(); +} + + +string +Serialiser::to_string(SharedPtr object) +{ + _root_object = object; + start_to_string(); + serialize(object); + return finish(); +} + + +/** Begin a serialization to a file. + * + * This must be called before any serializing methods. + */ +void +Serialiser::start_to_filename(const string& filename) +{ + setlocale(LC_NUMERIC, "C"); + + _base_uri = "file://" + filename; + _model = new RDF::Model(_world); + _mode = TO_FILE; +} + + +/** Begin a serialization to a string. + * + * This must be called before any serializing methods. + * + * The results of the serialization will be returned by the finish() method after + * the desired objects have been serialized. + */ +void +Serialiser::start_to_string() +{ + setlocale(LC_NUMERIC, "C"); + + _base_uri = ""; + _model = new RDF::Model(_world); + _mode = TO_STRING; +} + + +/** Finish a serialization. + * + * If this was a serialization to a string, the serialization output + * will be returned, otherwise the empty string is returned. + */ +string +Serialiser::finish() +{ + string ret = ""; + + if (_mode == TO_FILE) + _model->serialize_to_file(_base_uri); + else + ret = _model->serialize_to_string(); + + _base_uri = ""; + _node_map.clear(); + + return ret; +} + + +/** Convert a path to an RDF blank node ID for serializing. + */ +RDF::Node +Serialiser::path_to_node_id(const Path& path) +{ + assert(_model); + /*string ret = path.substr(1); + + for (size_t i=0; i < ret.length(); ++i) { + if (ret[i] == '/') + ret[i] = '_'; + } + + return RDF::Node(Node::BLANK, ret); + */ + + NodeMap::iterator i = _node_map.find(path); + if (i != _node_map.end()) { + assert(i->second); + assert(i->second.get_node()); + return i->second; + } else { + RDF::Node id = _world.blank_id(); + assert(id); + _node_map[path] = id; + return id; + } +} + + +#if 0 +/** Searches for the filename passed in the path, returning the full + * path of the file, or the empty string if not found. + * + * This function tries to be as friendly a black box as possible - if the path + * passed is an absolute path and the file is found there, it will return + * that path, etc. + * + * additional_path is a list (colon delimeted as usual) of additional + * directories to look in. ie the directory the parent patch resides in would + * be a good idea to pass as additional_path, in the case of a subpatch. + */ +string +Serialiser::find_file(const string& filename, const string& additional_path) +{ + string search_path = additional_path + ":" + _patch_search_path; + + // Try to open the raw filename first + std::ifstream is(filename.c_str(), std::ios::in); + if (is.good()) { + is.close(); + return filename; + } + + string directory; + string full_patch_path = ""; + + while (search_path != "") { + directory = search_path.substr(0, search_path.find(':')); + if (search_path.find(':') != string::npos) + search_path = search_path.substr(search_path.find(':')+1); + else + search_path = ""; + + full_patch_path = directory +"/"+ filename; + + std::ifstream is; + is.open(full_patch_path.c_str(), std::ios::in); + + if (is.good()) { + is.close(); + return full_patch_path; + } else { + cerr << "[Serialiser] Could not find patch file " << full_patch_path << endl; + } + } + + return ""; +} +#endif + +void +Serialiser::serialize(SharedPtr object) throw (std::logic_error) +{ + if (!_model) + throw std::logic_error("serialize called without serialization in progress"); + + SharedPtr patch = PtrCast(object); + if (patch) { + serialize_patch(patch); + return; + } + + SharedPtr node = PtrCast(object); + if (node) { + serialize_node(node, path_to_node_id(node->path())); + return; + } + + SharedPtr port = PtrCast(object); + if (port) { + serialize_port(port.get(), path_to_node_id(port->path())); + return; + } + + cerr << "[Serialiser] WARNING: Unsupported object type, " + << object->path() << " not serialized." << endl; +} + + +RDF::Node +Serialiser::patch_path_to_rdf_id(const Path& path) +{ + if (path == _root_object->path()) { + return RDF::Node(_model->world(), RDF::Node::RESOURCE, _base_uri); + } else { + assert(path.length() > _root_object->path().length()); + return RDF::Node(_model->world(), RDF::Node::RESOURCE, + _base_uri + string("#") + path.substr(_root_object->path().length())); + } +} + + +void +Serialiser::serialize_patch(SharedPtr patch) +{ + assert(_model); + + const RDF::Node patch_id = patch_path_to_rdf_id(patch->path()); + + _model->add_statement( + patch_id, + "rdf:type", + RDF::Node(_model->world(), RDF::Node::RESOURCE, "http://drobilla.net/ns/ingen#Patch")); + + if (patch->path().name().length() > 0) { + _model->add_statement( + patch_id, "ingen:name", + Atom(patch->path().name().c_str())); + } + + _model->add_statement( + patch_id, + "ingen:polyphony", + Atom((int)patch->internal_polyphony())); + + _model->add_statement( + patch_id, + "ingen:enabled", + Atom(patch->enabled())); + + for (GraphObject::MetadataMap::const_iterator m = patch->metadata().begin(); + m != patch->metadata().end(); ++m) { + if (m->first.find(":") != string::npos) { + _model->add_statement( + patch_id, + m->first.c_str(), + m->second); + } + } + + for (GraphObject::const_iterator n = patch->children_begin(); n != patch->children_end(); ++n) { + SharedPtr patch = PtrCast(n->second); + SharedPtr node = PtrCast(n->second); + if (patch) { + _model->add_statement(patch_id, "ingen:node", patch_path_to_rdf_id(patch->path())); + serialize_patch(patch); + } else if (node) { + const RDF::Node node_id = path_to_node_id(n->second->path()); + _model->add_statement(patch_id, "ingen:node", node_id); + serialize_node(node, node_id); + } + } + + for (uint32_t i=0; i < patch->num_ports(); ++i) { + Port* p = patch->port(i); + const RDF::Node port_id = path_to_node_id(p->path()); + _model->add_statement(patch_id, "ingen:port", port_id); + serialize_port(p, port_id); + } + + for (Shared::Patch::Connections::const_iterator c = patch->connections().begin(); + c != patch->connections().end(); ++c) { + serialize_connection(*c); + } +} + + +void +Serialiser::serialize_plugin(SharedPtr plugin) +{ + assert(_model); + + const RDF::Node plugin_id = RDF::Node(_model->world(), RDF::Node::RESOURCE, plugin->uri()); + + _model->add_statement( + plugin_id, + "rdf:type", + RDF::Node(_model->world(), RDF::Node::RESOURCE, plugin->type_uri())); +} + + +void +Serialiser::serialize_node(SharedPtr node, const RDF::Node& node_id) +{ + const RDF::Node plugin_id + = RDF::Node(_model->world(), RDF::Node::RESOURCE, node->plugin()->uri()); + + _model->add_statement( + node_id, + "rdf:type", + RDF::Node(_model->world(), RDF::Node::RESOURCE, "ingen:Node")); + + _model->add_statement( + node_id, + "ingen:name", + node->path().name()); + + _model->add_statement( + node_id, + "ingen:plugin", + plugin_id); + + _model->add_statement( + node_id, + "ingen:polyphonic", + node->polyphonic()); + + //serialize_plugin(node->plugin()); + + for (uint32_t i=0; i < node->num_ports(); ++i) { + Port* p = node->port(i); + assert(p); + const RDF::Node port_id = path_to_node_id(p->path()); + serialize_port(p, port_id); + _model->add_statement(node_id, "ingen:port", port_id); + } + + for (GraphObject::MetadataMap::const_iterator m = node->metadata().begin(); + m != node->metadata().end(); ++m) { + if (m->first.find(":") != string::npos) { + _model->add_statement( + node_id, + m->first, + m->second); + } + } +} + +/** Writes a port subject with various information only if there are some + * predicate/object pairs to go with it (eg if the port has metadata, or a value, or..). + * Audio output ports with no metadata will not be written, for example. + */ +void +Serialiser::serialize_port(const Port* port, const RDF::Node& port_id) +{ + if (port->is_input()) + _model->add_statement(port_id, "rdf:type", + RDF::Node(_model->world(), RDF::Node::RESOURCE, "ingen:InputPort")); + else + _model->add_statement(port_id, "rdf:type", + RDF::Node(_model->world(), RDF::Node::RESOURCE, "ingen:OutputPort")); + + _model->add_statement(port_id, "ingen:name", Atom(port->path().name().c_str())); + + _model->add_statement(port_id, "rdf:type", + RDF::Node(_model->world(), RDF::Node::RESOURCE, port->type().uri())); + + if (port->type() == DataType::CONTROL && port->is_input()) + _model->add_statement(port_id, "ingen:value", port->value()); + + if (port->metadata().size() > 0) { + for (GraphObject::MetadataMap::const_iterator m = port->metadata().begin(); + m != port->metadata().end(); ++m) { + if (m->first.find(":") != string::npos) { + _model->add_statement( + port_id, + m->first, + m->second); + } + } + } +} + + +void +Serialiser::serialize_connection(SharedPtr connection) throw (std::logic_error) +{ + if (!_model) + throw std::logic_error("serialize_connection called without serialization in progress"); + + const RDF::Node src_node = path_to_node_id(connection->src_port_path()); + const RDF::Node dst_node = path_to_node_id(connection->dst_port_path()); + + _model->add_statement(dst_node, "ingen:connectedTo", src_node); +} + + +} // namespace Serialisation +} // namespace Ingen diff --git a/src/libs/serialisation/Serialiser.hpp b/src/libs/serialisation/Serialiser.hpp new file mode 100644 index 00000000..23d10c82 --- /dev/null +++ b/src/libs/serialisation/Serialiser.hpp @@ -0,0 +1,96 @@ +/* This file is part of Ingen. + * Copyright (C) 2007 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 SERIALISER_H +#define SERIALISER_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace Raul; +using namespace Ingen::Shared; + +namespace Ingen { + +namespace Shared { + class Plugin; + class GraphObject; + class Patch; + class Node; + class Port; + class Connection; +} + +namespace Serialisation { + + +/** Serializes Ingen objects (patches, nodes, etc) to RDF. + * + * \ingroup IngenClient + */ +class Serialiser +{ +public: + Serialiser(Raul::RDF::World& world); + + void to_file(SharedPtr object, const string& filename); + string to_string(SharedPtr object); + + void start_to_string(); + void serialize(SharedPtr object) throw (std::logic_error); + void serialize_connection(SharedPtr c) throw (std::logic_error); + string finish(); + +private: + enum Mode { TO_FILE, TO_STRING }; + + void start_to_filename(const string& filename); + + void setup_prefixes(); + + void serialize_plugin(SharedPtr p); + + void serialize_patch(SharedPtr p); + void serialize_node(SharedPtr n, const Raul::RDF::Node& id); + void serialize_port(const Shared::Port* p, const Raul::RDF::Node& id); + + Raul::RDF::Node path_to_node_id(const Path& path); + Raul::RDF::Node patch_path_to_rdf_id(const Path& path); + + typedef std::map NodeMap; + + SharedPtr _root_object; + Mode _mode; + NodeMap _node_map; + string _base_uri; + Raul::RDF::World& _world; + Raul::RDF::Model* _model; +}; + + +} // namespace Serialisation +} // namespace Ingen + +#endif // SERIALISER_H diff --git a/src/libs/serialisation/Serializer.cpp b/src/libs/serialisation/Serializer.cpp deleted file mode 100644 index 7483ae5e..00000000 --- a/src/libs/serialisation/Serializer.cpp +++ /dev/null @@ -1,434 +0,0 @@ -/* This file is part of Ingen. - * Copyright (C) 2007 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 -#include -#include -#include // atof -#include -#include -#include -#include -#include -#include -#include // pair, make_pair -#include -#include -#include -#include -#include -#include -#include -#include -#include "interface/EngineInterface.hpp" -#include "interface/Plugin.hpp" -#include "interface/Patch.hpp" -#include "interface/Node.hpp" -#include "interface/Port.hpp" -#include "interface/Connection.hpp" -#include "Serializer.hpp" - -using namespace std; -using namespace Raul; -using namespace Raul::RDF; -using namespace Ingen; -using namespace Ingen::Shared; - -namespace Ingen { -namespace Serialisation { - - -Serializer::Serializer(Raul::RDF::World& world) - : _world(world) -{ -} - -void -Serializer::to_file(SharedPtr object, const string& filename) -{ - _root_object = object; - start_to_filename(filename); - serialize(object); - finish(); -} - - -string -Serializer::to_string(SharedPtr object) -{ - _root_object = object; - start_to_string(); - serialize(object); - return finish(); -} - - -/** Begin a serialization to a file. - * - * This must be called before any serializing methods. - */ -void -Serializer::start_to_filename(const string& filename) -{ - setlocale(LC_NUMERIC, "C"); - - _base_uri = "file://" + filename; - _model = new RDF::Model(_world); - _mode = TO_FILE; -} - - -/** Begin a serialization to a string. - * - * This must be called before any serializing methods. - * - * The results of the serialization will be returned by the finish() method after - * the desired objects have been serialized. - */ -void -Serializer::start_to_string() -{ - setlocale(LC_NUMERIC, "C"); - - _base_uri = ""; - _model = new RDF::Model(_world); - _mode = TO_STRING; -} - - -/** Finish a serialization. - * - * If this was a serialization to a string, the serialization output - * will be returned, otherwise the empty string is returned. - */ -string -Serializer::finish() -{ - string ret = ""; - - if (_mode == TO_FILE) - _model->serialize_to_file(_base_uri); - else - ret = _model->serialize_to_string(); - - _base_uri = ""; - _node_map.clear(); - - return ret; -} - - -/** Convert a path to an RDF blank node ID for serializing. - */ -RDF::Node -Serializer::path_to_node_id(const Path& path) -{ - assert(_model); - /*string ret = path.substr(1); - - for (size_t i=0; i < ret.length(); ++i) { - if (ret[i] == '/') - ret[i] = '_'; - } - - return RDF::Node(Node::BLANK, ret); - */ - - NodeMap::iterator i = _node_map.find(path); - if (i != _node_map.end()) { - assert(i->second); - assert(i->second.get_node()); - return i->second; - } else { - RDF::Node id = _world.blank_id(); - assert(id); - _node_map[path] = id; - return id; - } -} - - -#if 0 -/** Searches for the filename passed in the path, returning the full - * path of the file, or the empty string if not found. - * - * This function tries to be as friendly a black box as possible - if the path - * passed is an absolute path and the file is found there, it will return - * that path, etc. - * - * additional_path is a list (colon delimeted as usual) of additional - * directories to look in. ie the directory the parent patch resides in would - * be a good idea to pass as additional_path, in the case of a subpatch. - */ -string -Serializer::find_file(const string& filename, const string& additional_path) -{ - string search_path = additional_path + ":" + _patch_search_path; - - // Try to open the raw filename first - std::ifstream is(filename.c_str(), std::ios::in); - if (is.good()) { - is.close(); - return filename; - } - - string directory; - string full_patch_path = ""; - - while (search_path != "") { - directory = search_path.substr(0, search_path.find(':')); - if (search_path.find(':') != string::npos) - search_path = search_path.substr(search_path.find(':')+1); - else - search_path = ""; - - full_patch_path = directory +"/"+ filename; - - std::ifstream is; - is.open(full_patch_path.c_str(), std::ios::in); - - if (is.good()) { - is.close(); - return full_patch_path; - } else { - cerr << "[Serializer] Could not find patch file " << full_patch_path << endl; - } - } - - return ""; -} -#endif - -void -Serializer::serialize(SharedPtr object) throw (std::logic_error) -{ - if (!_model) - throw std::logic_error("serialize called without serialization in progress"); - - SharedPtr patch = PtrCast(object); - if (patch) { - serialize_patch(patch); - return; - } - - SharedPtr node = PtrCast(object); - if (node) { - serialize_node(node, path_to_node_id(node->path())); - return; - } - - SharedPtr port = PtrCast(object); - if (port) { - serialize_port(port.get(), path_to_node_id(port->path())); - return; - } - - cerr << "[Serializer] WARNING: Unsupported object type, " - << object->path() << " not serialized." << endl; -} - - -RDF::Node -Serializer::patch_path_to_rdf_id(const Path& path) -{ - if (path == _root_object->path()) { - return RDF::Node(_model->world(), RDF::Node::RESOURCE, _base_uri); - } else { - assert(path.length() > _root_object->path().length()); - return RDF::Node(_model->world(), RDF::Node::RESOURCE, - _base_uri + string("#") + path.substr(_root_object->path().length())); - } -} - - -void -Serializer::serialize_patch(SharedPtr patch) -{ - assert(_model); - - const RDF::Node patch_id = patch_path_to_rdf_id(patch->path()); - - _model->add_statement( - patch_id, - "rdf:type", - RDF::Node(_model->world(), RDF::Node::RESOURCE, "http://drobilla.net/ns/ingen#Patch")); - - if (patch->path().name().length() > 0) { - _model->add_statement( - patch_id, "ingen:name", - Atom(patch->path().name().c_str())); - } - - _model->add_statement( - patch_id, - "ingen:polyphony", - Atom((int)patch->internal_polyphony())); - - _model->add_statement( - patch_id, - "ingen:enabled", - Atom(patch->enabled())); - - for (GraphObject::MetadataMap::const_iterator m = patch->metadata().begin(); - m != patch->metadata().end(); ++m) { - if (m->first.find(":") != string::npos) { - _model->add_statement( - patch_id, - m->first.c_str(), - m->second); - } - } - - for (GraphObject::const_iterator n = patch->children_begin(); n != patch->children_end(); ++n) { - SharedPtr patch = PtrCast(n->second); - SharedPtr node = PtrCast(n->second); - if (patch) { - _model->add_statement(patch_id, "ingen:node", patch_path_to_rdf_id(patch->path())); - serialize_patch(patch); - } else if (node) { - const RDF::Node node_id = path_to_node_id(n->second->path()); - _model->add_statement(patch_id, "ingen:node", node_id); - serialize_node(node, node_id); - } - } - - for (uint32_t i=0; i < patch->num_ports(); ++i) { - Port* p = patch->port(i); - const RDF::Node port_id = path_to_node_id(p->path()); - _model->add_statement(patch_id, "ingen:port", port_id); - serialize_port(p, port_id); - } - - for (Shared::Patch::Connections::const_iterator c = patch->connections().begin(); - c != patch->connections().end(); ++c) { - serialize_connection(*c); - } -} - - -void -Serializer::serialize_plugin(SharedPtr plugin) -{ - assert(_model); - - const RDF::Node plugin_id = RDF::Node(_model->world(), RDF::Node::RESOURCE, plugin->uri()); - - _model->add_statement( - plugin_id, - "rdf:type", - RDF::Node(_model->world(), RDF::Node::RESOURCE, plugin->type_uri())); -} - - -void -Serializer::serialize_node(SharedPtr node, const RDF::Node& node_id) -{ - const RDF::Node plugin_id - = RDF::Node(_model->world(), RDF::Node::RESOURCE, node->plugin()->uri()); - - _model->add_statement( - node_id, - "rdf:type", - RDF::Node(_model->world(), RDF::Node::RESOURCE, "ingen:Node")); - - _model->add_statement( - node_id, - "ingen:name", - node->path().name()); - - _model->add_statement( - node_id, - "ingen:plugin", - plugin_id); - - _model->add_statement( - node_id, - "ingen:polyphonic", - node->polyphonic()); - - //serialize_plugin(node->plugin()); - - for (uint32_t i=0; i < node->num_ports(); ++i) { - Port* p = node->port(i); - assert(p); - const RDF::Node port_id = path_to_node_id(p->path()); - serialize_port(p, port_id); - _model->add_statement(node_id, "ingen:port", port_id); - } - - for (GraphObject::MetadataMap::const_iterator m = node->metadata().begin(); - m != node->metadata().end(); ++m) { - if (m->first.find(":") != string::npos) { - _model->add_statement( - node_id, - m->first, - m->second); - } - } -} - -/** Writes a port subject with various information only if there are some - * predicate/object pairs to go with it (eg if the port has metadata, or a value, or..). - * Audio output ports with no metadata will not be written, for example. - */ -void -Serializer::serialize_port(const Port* port, const RDF::Node& port_id) -{ - if (port->is_input()) - _model->add_statement(port_id, "rdf:type", - RDF::Node(_model->world(), RDF::Node::RESOURCE, "ingen:InputPort")); - else - _model->add_statement(port_id, "rdf:type", - RDF::Node(_model->world(), RDF::Node::RESOURCE, "ingen:OutputPort")); - - _model->add_statement(port_id, "ingen:name", Atom(port->path().name().c_str())); - - _model->add_statement(port_id, "rdf:type", - RDF::Node(_model->world(), RDF::Node::RESOURCE, port->type().uri())); - - if (port->type() == DataType::CONTROL && port->is_input()) - _model->add_statement(port_id, "ingen:value", port->value()); - - if (port->metadata().size() > 0) { - for (GraphObject::MetadataMap::const_iterator m = port->metadata().begin(); - m != port->metadata().end(); ++m) { - if (m->first.find(":") != string::npos) { - _model->add_statement( - port_id, - m->first, - m->second); - } - } - } -} - - -void -Serializer::serialize_connection(SharedPtr connection) throw (std::logic_error) -{ - if (!_model) - throw std::logic_error("serialize_connection called without serialization in progress"); - - const RDF::Node src_node = path_to_node_id(connection->src_port_path()); - const RDF::Node dst_node = path_to_node_id(connection->dst_port_path()); - - _model->add_statement(dst_node, "ingen:connectedTo", src_node); -} - - -} // namespace Serialisation -} // namespace Ingen diff --git a/src/libs/serialisation/Serializer.hpp b/src/libs/serialisation/Serializer.hpp deleted file mode 100644 index ead392bc..00000000 --- a/src/libs/serialisation/Serializer.hpp +++ /dev/null @@ -1,96 +0,0 @@ -/* This file is part of Ingen. - * Copyright (C) 2007 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 SERIALIZER_H -#define SERIALIZER_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace Raul; -using namespace Ingen::Shared; - -namespace Ingen { - -namespace Shared { - class Plugin; - class GraphObject; - class Patch; - class Node; - class Port; - class Connection; -} - -namespace Serialisation { - - -/** Serializes Ingen objects (patches, nodes, etc) to RDF. - * - * \ingroup IngenClient - */ -class Serializer -{ -public: - Serializer(Raul::RDF::World& world); - - void to_file(SharedPtr object, const string& filename); - string to_string(SharedPtr object); - - void start_to_string(); - void serialize(SharedPtr object) throw (std::logic_error); - void serialize_connection(SharedPtr c) throw (std::logic_error); - string finish(); - -private: - enum Mode { TO_FILE, TO_STRING }; - - void start_to_filename(const string& filename); - - void setup_prefixes(); - - void serialize_plugin(SharedPtr p); - - void serialize_patch(SharedPtr p); - void serialize_node(SharedPtr n, const Raul::RDF::Node& id); - void serialize_port(const Shared::Port* p, const Raul::RDF::Node& id); - - Raul::RDF::Node path_to_node_id(const Path& path); - Raul::RDF::Node patch_path_to_rdf_id(const Path& path); - - typedef std::map NodeMap; - - SharedPtr _root_object; - Mode _mode; - NodeMap _node_map; - string _base_uri; - Raul::RDF::World& _world; - Raul::RDF::Model* _model; -}; - - -} // namespace Serialisation -} // namespace Ingen - -#endif // SERIALIZER_H -- cgit v1.2.1