summaryrefslogtreecommitdiffstats
path: root/src/serialisation
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2008-09-30 16:50:21 +0000
committerDavid Robillard <d@drobilla.net>2008-09-30 16:50:21 +0000
commit93850c202de8b073a1ce1dd8bd246d407bce4e2f (patch)
tree6910b135bf4eff12de1af116cef73f6e9c107cd0 /src/serialisation
parenta8bf5272d096de73507d2eab47f282c345f4ca8a (diff)
downloadingen-93850c202de8b073a1ce1dd8bd246d407bce4e2f.tar.gz
ingen-93850c202de8b073a1ce1dd8bd246d407bce4e2f.tar.bz2
ingen-93850c202de8b073a1ce1dd8bd246d407bce4e2f.zip
Flatten ingen source directory heirarchy a bit.
git-svn-id: http://svn.drobilla.net/lad/trunk/ingen@1551 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'src/serialisation')
-rw-r--r--src/serialisation/Makefile.am24
-rw-r--r--src/serialisation/Parser.cpp587
-rw-r--r--src/serialisation/Parser.hpp126
-rw-r--r--src/serialisation/Serialiser.cpp501
-rw-r--r--src/serialisation/Serialiser.hpp109
-rw-r--r--src/serialisation/serialisation.cpp46
-rw-r--r--src/serialisation/serialisation.hpp44
-rw-r--r--src/serialisation/wscript16
8 files changed, 1453 insertions, 0 deletions
diff --git a/src/serialisation/Makefile.am b/src/serialisation/Makefile.am
new file mode 100644
index 00000000..57e99055
--- /dev/null
+++ b/src/serialisation/Makefile.am
@@ -0,0 +1,24 @@
+MAINTAINERCLEANFILES = Makefile.in
+
+moduledir = $(libdir)/ingen
+
+module_LTLIBRARIES = libingen_serialisation.la
+
+libingen_serialisation_la_CXXFLAGS = \
+ @INGEN_CFLAGS@ \
+ @REDLANDMM_CFLAGS@ \
+ @RAUL_CFLAGS@ \
+ @GLIBMM_CFLAGS@ \
+ @SLV2_CFLAGS@
+
+libingen_serialisation_la_LDFLAGS = -no-undefined -module -avoid-version
+libingen_serialisation_la_LIBADD = @RAUL_LIBS@ @REDLANDMM_LIBS@ @GLIBMM_LIBS@ @SLV2_LIBS@
+
+libingen_serialisation_la_SOURCES = \
+ Parser.cpp \
+ Parser.hpp \
+ Serialiser.cpp \
+ Serialiser.hpp \
+ serialisation.cpp \
+ serialisation.hpp
+
diff --git a/src/serialisation/Parser.cpp b/src/serialisation/Parser.cpp
new file mode 100644
index 00000000..aed49a38
--- /dev/null
+++ b/src/serialisation/Parser.cpp
@@ -0,0 +1,587 @@
+/* This file is part of Ingen.
+ * Copyright (C) 2007 Dave Robillard <http://drobilla.net>
+ *
+ * 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 <iostream>
+#include <set>
+#include <locale.h>
+#include <glibmm/ustring.h>
+#include <redlandmm/Model.hpp>
+#include <redlandmm/Node.hpp>
+#include <redlandmm/Query.hpp>
+#include <raul/TableImpl.hpp>
+#include <raul/Atom.hpp>
+#include <raul/AtomRDF.hpp>
+#include "interface/EngineInterface.hpp"
+#include "Parser.hpp"
+
+using namespace std;
+using namespace Raul;
+using namespace Ingen::Shared;
+
+namespace Ingen {
+namespace Serialisation {
+
+#define NS_INGEN "http://drobilla.net/ns/ingen#"
+
+
+Glib::ustring
+Parser::uri_relative_to_base(Glib::ustring base, const Glib::ustring uri)
+{
+ base = base.substr(0, base.find_last_of("/")+1);
+ Glib::ustring ret;
+ if (uri.length() > base.length() && uri.substr(0, base.length()) == base)
+ ret = uri.substr(base.length());
+ else
+ ret = uri;
+ return ret;
+}
+
+
+/** Parse a patch from RDF into a CommonInterface (engine or client).
+ *
+ * @param document_uri URI of file to load objects from.
+ * @param parent Path of parent under which to load objects.
+ * @return whether or not load was successful.
+ */
+bool
+Parser::parse_document(
+ Ingen::Shared::World* world,
+ Ingen::Shared::CommonInterface* target,
+ const Glib::ustring& document_uri,
+ Glib::ustring object_uri,
+ Glib::ustring engine_base,
+ boost::optional<Raul::Symbol> symbol,
+ boost::optional<GraphObject::Variables> data)
+{
+ Redland::Model model(*world->rdf_world, document_uri, document_uri);
+
+ if (object_uri == document_uri || object_uri == "")
+ cout << "Parsing document " << object_uri << " (base " << document_uri << ")" << endl;
+ else
+ cout << "Parsing " << object_uri << " from " << document_uri << endl;
+
+ return parse(world, target, model, document_uri, engine_base, object_uri, symbol, data);;
+}
+
+
+bool
+Parser::parse_string(
+ Ingen::Shared::World* world,
+ Ingen::Shared::CommonInterface* target,
+ const Glib::ustring& str,
+ const Glib::ustring& base_uri,
+ Glib::ustring engine_base,
+ boost::optional<Glib::ustring> object_uri,
+ boost::optional<Raul::Symbol> symbol,
+ boost::optional<GraphObject::Variables> data)
+{
+ Redland::Model model(*world->rdf_world, str.c_str(), str.length(), base_uri);
+
+ if (object_uri)
+ cout << "Parsing " << object_uri.get() << " (base " << base_uri << ")" << endl;
+ else
+ cout << "Parsing all objects found in string (base " << base_uri << ")" << endl;
+
+ bool ret = parse(world, target, model, base_uri, engine_base, object_uri, symbol, data);
+ if (ret) {
+ const Glib::ustring subject = Glib::ustring("<") + base_uri + Glib::ustring(">");
+ parse_connections(world, target, model, base_uri, subject,
+ Path((engine_base == "") ? "/" : engine_base));
+ }
+
+ return ret;
+}
+
+
+bool
+Parser::parse(
+ Ingen::Shared::World* world,
+ Ingen::Shared::CommonInterface* target,
+ Redland::Model& model,
+ Glib::ustring base_uri,
+ Glib::ustring engine_base,
+ boost::optional<Glib::ustring> object_uri,
+ boost::optional<Raul::Symbol> symbol,
+ boost::optional<GraphObject::Variables> data)
+{
+ const Redland::Node::Type res = Redland::Node::RESOURCE;
+ Glib::ustring query_str;
+ if (object_uri && object_uri.get()[0] == '/')
+ object_uri = object_uri.get().substr(1);
+
+ if (object_uri)
+ query_str = Glib::ustring("SELECT DISTINCT ?class WHERE { <") + object_uri.get() + "> a ?class . }";
+ else
+ query_str = Glib::ustring("SELECT DISTINCT ?subject ?class WHERE { ?subject a ?class . }");
+
+ Redland::Query query(*world->rdf_world, query_str);
+ Redland::Query::Results results = query.run(*world->rdf_world, model, base_uri);
+
+ const Redland::Node patch_class(*world->rdf_world, res, NS_INGEN "Patch");
+ const Redland::Node node_class(*world->rdf_world, res, NS_INGEN "Node");
+ const Redland::Node in_port_class(*world->rdf_world, res, NS_INGEN "InputPort");
+ const Redland::Node out_port_class(*world->rdf_world, res, NS_INGEN "OutputPort");
+
+ string subject_str = ((object_uri && object_uri.get() != "") ? object_uri.get() : base_uri);
+ if (subject_str[0] == '/')
+ subject_str = subject_str.substr(1);
+ if (subject_str == "")
+ subject_str = base_uri;
+
+ const Redland::Node subject_uri(*world->rdf_world, res, subject_str);
+
+ bool ret = false;
+
+ for (Redland::Query::Results::iterator i = results.begin(); i != results.end(); ++i) {
+ const Redland::Node& subject = (object_uri ? subject_uri : (*i)["subject"]);
+ const Redland::Node& rdf_class = (*i)["class"];
+ if (!object_uri) {
+ std::string path_str = uri_relative_to_base(base_uri, subject.to_c_string());
+ if (path_str[0] != '/')
+ path_str = string("/").append(path_str);
+ if (Path(path_str).parent() != "/")
+ continue;
+ }
+
+ if (rdf_class == patch_class || rdf_class == node_class ||
+ rdf_class == in_port_class || rdf_class == out_port_class) {
+ Raul::Path path("/");
+ if (base_uri != subject.to_c_string()) {
+ string path_str = (string)uri_relative_to_base(base_uri, subject.to_c_string());
+ if (path_str[0] != '/')
+ path_str = string("/").append(path_str);
+ if (Path::is_valid(path_str)) {
+ path = path_str;
+ } else {
+ cerr << "[Parser] ERROR: Invalid path '" << path << "'" << endl;
+ continue;
+ }
+ }
+
+ if (path.parent() != "/")
+ continue;
+
+ if (rdf_class == patch_class) {
+ ret = parse_patch(world, target, model, base_uri, engine_base,
+ subject.to_c_string(), data);
+ if (ret)
+ target->set_variable(path, "ingen:document", Atom(base_uri.c_str()));
+ } else if (rdf_class == node_class) {
+ ret = parse_node(world, target, model,
+ base_uri, Glib::ustring("<") + subject.to_c_string() + ">", path, data);
+ } else if (rdf_class == in_port_class || rdf_class == out_port_class) {
+ ret = parse_port(world, target, model,
+ base_uri, Glib::ustring("<") + subject.to_c_string() + ">", path, data);
+ }
+ if (ret == false) {
+ cerr << "Failed to parse object " << object_uri << endl;
+ return ret;
+ }
+ }
+
+ }
+
+ return ret;
+}
+
+
+bool
+Parser::parse_patch(
+ Ingen::Shared::World* world,
+ Ingen::Shared::CommonInterface* target,
+ Redland::Model& model,
+ const Glib::ustring& base_uri,
+ Glib::ustring engine_base,
+ const Glib::ustring& object_uri,
+ boost::optional<GraphObject::Variables> data=boost::optional<GraphObject::Variables>())
+{
+ std::set<Path> created;
+ uint32_t patch_poly = 0;
+
+ /* Use parameter overridden polyphony, if given */
+ if (data) {
+ GraphObject::Variables::iterator poly_param = data.get().find("ingen:polyphony");
+ if (poly_param != data.get().end() && poly_param->second.type() == Atom::INT)
+ patch_poly = poly_param->second.get_int32();
+ }
+
+ Glib::ustring subject = ((object_uri[0] == '<')
+ ? object_uri : Glib::ustring("<") + object_uri + ">");
+
+ if (subject[0] == '<' && subject[1] == '/')
+ subject = string("<").append(subject.substr(2));
+
+ //cout << "**** LOADING PATCH URI " << object_uri << ", SUBJ " << subject
+ // << ", ENG BASE " << engine_base << endl;
+
+ /* Get polyphony from file (mandatory if not specified in parameters) */
+ if (patch_poly == 0) {
+ Redland::Query query(*world->rdf_world, Glib::ustring(
+ "SELECT DISTINCT ?poly WHERE { ") + subject + " ingen:polyphony ?poly\n }");
+
+ Redland::Query::Results results = query.run(*world->rdf_world, model, base_uri);
+
+ if (results.size() == 0) {
+ cerr << "[Parser] ERROR: No polyphony found!" << endl;
+ cerr << "Query was:" << endl << query.string() << endl;
+ return false;
+ }
+
+ const Redland::Node& poly_node = (*results.begin())["poly"];
+ assert(poly_node.is_int());
+ patch_poly = static_cast<uint32_t>(poly_node.to_int());
+ }
+
+ string symbol = uri_relative_to_base(base_uri, object_uri);
+ symbol = symbol.substr(0, symbol.find("."));
+ Path patch_path("/");
+ if (engine_base == "")
+ patch_path = "/";
+ else if (engine_base[engine_base.length()-1] == '/')
+ patch_path = Path(engine_base + symbol);
+ else if (Path::is_valid(engine_base))
+ patch_path = (Path)engine_base;
+ else if (Path::is_valid(string("/").append(engine_base)))
+ patch_path = (Path)(string("/").append(engine_base));
+ else
+ cerr << "WARNING: Illegal engine base path '" << engine_base << "', loading patch to root" << endl;
+
+ //if (patch_path != "/")
+ target->new_patch(patch_path, patch_poly);
+
+ /* Plugin nodes */
+ Redland::Query query(*world->rdf_world, Glib::ustring(
+ "SELECT DISTINCT ?name ?plugin ?varkey ?varval ?poly WHERE {\n") +
+ subject + " ingen:node ?node .\n"
+ "?node lv2:symbol ?name ;\n"
+ " ingen:plugin ?plugin ;\n"
+ " ingen:polyphonic ?poly .\n"
+ "OPTIONAL { ?node lv2var:variable ?variable .\n"
+ " ?variable rdf:predicate ?varkey ;\n"
+ " rdf:value ?varval .\n"
+ " }"
+ "}");
+
+ Redland::Query::Results results = query.run(*world->rdf_world, model, base_uri);
+ world->rdf_world->mutex().lock();
+ for (Redland::Query::Results::iterator i = results.begin(); i != results.end(); ++i) {
+ const string node_name = (*i)["name"].to_string();
+ const Path node_path = patch_path.base() + node_name;
+
+ if (created.find(node_path) == created.end()) {
+ const string node_plugin = (*i)["plugin"].to_string();
+ bool node_polyphonic = false;
+
+ const Redland::Node& poly_node = (*i)["poly"];
+ if (poly_node.is_bool() && poly_node.to_bool() == true)
+ node_polyphonic = true;
+
+ target->new_node(node_path, node_plugin);
+ target->set_property(node_path, "ingen:polyphonic", node_polyphonic);
+ created.insert(node_path);
+ }
+
+ const string key = world->rdf_world->prefixes().qualify((*i)["varkey"].to_string());
+ const Redland::Node& val_node = (*i)["varval"];
+
+ if (key != "")
+ target->set_variable(node_path, key, AtomRDF::node_to_atom(val_node));
+ }
+ world->rdf_world->mutex().unlock();
+
+
+ /* Load subpatches */
+ query = Redland::Query(*world->rdf_world, Glib::ustring(
+ "SELECT DISTINCT ?subpatch ?symbol WHERE {\n") +
+ subject + " ingen:node ?subpatch .\n"
+ "?subpatch a ingen:Patch ;\n"
+ " lv2:symbol ?symbol .\n"
+ "}");
+
+ results = query.run(*world->rdf_world, model, base_uri);
+ for (Redland::Query::Results::iterator i = results.begin(); i != results.end(); ++i) {
+ const string symbol = (*i)["symbol"].to_string();
+ const string subpatch = (*i)["subpatch"].to_string();
+
+ const Path subpatch_path = patch_path.base() + symbol;
+
+ if (created.find(subpatch_path) == created.end()) {
+ string subpatch_rel = uri_relative_to_base(base_uri, subpatch);
+ string sub_base = engine_base;
+ if (sub_base[sub_base.length()-1] == '/')
+ sub_base = sub_base.substr(sub_base.length()-1);
+ sub_base.append("/").append(symbol);
+ created.insert(subpatch_path);
+ parse_patch(world, target, model, base_uri, subpatch_rel, sub_base);
+ }
+ }
+
+
+ /* Set node port control values */
+ query = Redland::Query(*world->rdf_world, Glib::ustring(
+ "SELECT DISTINCT ?nodename ?portname ?portval WHERE {\n") +
+ subject + " ingen:node ?node .\n"
+ "?node lv2:symbol ?nodename ;\n"
+ " ingen:port ?port .\n"
+ "?port lv2:symbol ?portname ;\n"
+ " ingen:value ?portval .\n"
+ "FILTER ( datatype(?portval) = xsd:decimal )\n"
+ "}");
+
+ results = query.run(*world->rdf_world, model, base_uri);
+ for (Redland::Query::Results::iterator i = results.begin(); i != results.end(); ++i) {
+ const string node_name = (*i)["nodename"].to_string();
+ const string port_name = (*i)["portname"].to_string();
+
+ assert(Path::is_valid_name(node_name));
+ assert(Path::is_valid_name(port_name));
+ const Path port_path = patch_path.base() + node_name + "/" + port_name;
+
+ target->set_port_value(port_path, AtomRDF::node_to_atom((*i)["portval"]));
+ }
+
+
+ /* Load this patch's ports */
+ query = Redland::Query(*world->rdf_world, Glib::ustring(
+ "SELECT DISTINCT ?port ?type ?name ?datatype ?varkey ?varval ?portval WHERE {\n") +
+ subject + " ingen:port ?port .\n"
+ "?port a ?type ;\n"
+ " a ?datatype ;\n"
+ " lv2:symbol ?name .\n"
+ " FILTER (?type != ?datatype && ((?type = ingen:InputPort) || (?type = ingen:OutputPort)))\n"
+ "OPTIONAL { ?port ingen:value ?portval . \n"
+ " FILTER ( datatype(?portval) = xsd:decimal ) }\n"
+ "OPTIONAL { ?port lv2var:variable ?variable .\n"
+ " ?variable rdf:predicate ?varkey ;\n"
+ " rdf:value ?varval .\n"
+ " }"
+ "}");
+
+ results = query.run(*world->rdf_world, model, base_uri);
+ world->rdf_world->mutex().lock();
+ for (Redland::Query::Results::iterator i = results.begin(); i != results.end(); ++i) {
+ const string name = (*i)["name"].to_string();
+ const string type = world->rdf_world->qualify((*i)["type"].to_string());
+ const string datatype = world->rdf_world->qualify((*i)["datatype"].to_string());
+
+ assert(Path::is_valid_name(name));
+ const Path port_path = patch_path.base() + name;
+
+ if (created.find(port_path) == created.end()) {
+ bool is_output = (type == "ingen:OutputPort"); // FIXME: check validity
+ // FIXME: read index
+ target->new_port(port_path, 0, datatype, is_output);
+ created.insert(port_path);
+ }
+
+ const Redland::Node& val_node = (*i)["portval"];
+ target->set_port_value(patch_path.base() + name, AtomRDF::node_to_atom(val_node));
+
+ const string key = world->rdf_world->prefixes().qualify((*i)["varkey"].to_string());
+ const Redland::Node& var_val_node = (*i)["varval"];
+
+ if (key != "")
+ target->set_variable(patch_path.base() + name, key, AtomRDF::node_to_atom(var_val_node));
+ }
+ world->rdf_world->mutex().unlock();
+
+ created.clear();
+
+ parse_connections(world, target, model, base_uri, subject, patch_path);
+ parse_variables(world, target, model, base_uri, subject, patch_path, data);
+
+ /* Enable */
+ query = Redland::Query(*world->rdf_world, Glib::ustring(
+ "SELECT DISTINCT ?enabled WHERE { ") + subject + " ingen:enabled ?enabled }");
+
+ results = query.run(*world->rdf_world, model, base_uri);
+ for (Redland::Query::Results::iterator i = results.begin(); i != results.end(); ++i) {
+ const Redland::Node& enabled_node = (*i)["enabled"];
+ if (enabled_node.is_bool() && enabled_node) {
+ target->set_property(patch_path, "ingen:enabled", (bool)true);
+ break;
+ } else {
+ cerr << "WARNING: Unknown type for property ingen:enabled" << endl;
+ }
+ }
+
+ return true;
+}
+
+
+bool
+Parser::parse_node(
+ Ingen::Shared::World* world,
+ Ingen::Shared::CommonInterface* target,
+ Redland::Model& model,
+ const Glib::ustring& base_uri,
+ const Glib::ustring& subject,
+ const Raul::Path& path,
+ boost::optional<GraphObject::Variables> data=boost::optional<GraphObject::Variables>())
+{
+ /* Get plugin */
+ Redland::Query query(*world->rdf_world, Glib::ustring(
+ "SELECT DISTINCT ?plug WHERE { ") + subject + " ingen:plugin ?plug }");
+
+ Redland::Query::Results results = query.run(*world->rdf_world, model, base_uri);
+
+ if (results.size() == 0) {
+ cerr << "[Parser] ERROR: Node missing mandatory ingen:plugin property" << endl;
+ return false;
+ }
+
+ const Redland::Node& plugin_node = (*results.begin())["plug"];
+ if (plugin_node.type() != Redland::Node::RESOURCE) {
+ cerr << "[Parser] ERROR: node's ingen:plugin property is not a resource" << endl;
+ return false;
+ }
+
+ target->new_node(path, world->rdf_world->expand_uri(plugin_node.to_c_string()));
+ parse_variables(world, target, model, base_uri, subject, path, data);
+
+ return true;
+}
+
+
+bool
+Parser::parse_port(
+ Ingen::Shared::World* world,
+ Ingen::Shared::CommonInterface* target,
+ Redland::Model& model,
+ const Glib::ustring& base_uri,
+ const Glib::ustring& subject,
+ const Raul::Path& path,
+ boost::optional<GraphObject::Variables> data)
+{
+ Redland::Query query(*world->rdf_world, Glib::ustring(
+ "SELECT DISTINCT ?type ?datatype ?value WHERE {\n") +
+ subject + " a ?type ;\n"
+ " a ?datatype .\n"
+ " FILTER (?type != ?datatype && ((?type = ingen:InputPort) || (?type = ingen:OutputPort)))\n"
+ "OPTIONAL { " + subject + " ingen:value ?value . }\n"
+ "}");
+
+ Redland::Query::Results results = query.run(*world->rdf_world, model, base_uri);
+ world->rdf_world->mutex().lock();
+
+ for (Redland::Query::Results::iterator i = results.begin(); i != results.end(); ++i) {
+ const string type = world->rdf_world->qualify((*i)["type"].to_string());
+ const string datatype = world->rdf_world->qualify((*i)["datatype"].to_string());
+
+ bool is_output = (type == "ingen:OutputPort");
+ // FIXME: read index
+ target->new_port(path, 0, datatype, is_output);
+
+ const Redland::Node& val_node = (*i)["value"];
+ if (val_node.to_string() != "")
+ target->set_port_value(path, AtomRDF::node_to_atom(val_node));
+ }
+ world->rdf_world->mutex().unlock();
+
+ return parse_variables(world, target, model, base_uri, subject, path, data);
+}
+
+
+bool
+Parser::parse_connections(
+ Ingen::Shared::World* world,
+ Ingen::Shared::CommonInterface* target,
+ Redland::Model& model,
+ const Glib::ustring& base_uri,
+ const Glib::ustring& subject,
+ const Raul::Path& parent)
+{
+ Redland::Query query(*world->rdf_world, Glib::ustring(
+ "SELECT DISTINCT ?src ?dst WHERE {\n")
+ /*+ subject*/ + /*"?foo ingen:connection ?connection .\n"*/
+ "?connection ingen:source ?src ;\n"
+ " ingen:destination ?dst .\n"
+ "}");
+
+ Redland::Query::Results results = query.run(*world->rdf_world, model, base_uri);
+ for (Redland::Query::Results::iterator i = results.begin(); i != results.end(); ++i) {
+ string src_path = parent.base() + uri_relative_to_base(base_uri, (*i)["src"].to_string());
+ if (!Path::is_valid(src_path)) {
+ cerr << "ERROR: Invalid path in connection: " << src_path << endl;
+ continue;
+ }
+
+ string dst_path = parent.base() + uri_relative_to_base(base_uri, (*i)["dst"].to_string());
+ if (!Path::is_valid(dst_path)) {
+ cerr << "ERROR: Invalid path in connection: " << dst_path << endl;
+ continue;
+ }
+
+ target->connect(src_path, dst_path);
+ }
+
+ return true;
+}
+
+
+bool
+Parser::parse_variables(
+ Ingen::Shared::World* world,
+ Ingen::Shared::CommonInterface* target,
+ Redland::Model& model,
+ const Glib::ustring& base_uri,
+ const Glib::ustring& subject,
+ const Raul::Path& path,
+ boost::optional<GraphObject::Variables> data=boost::optional<GraphObject::Variables>())
+{
+ Redland::Query query(*world->rdf_world, Glib::ustring(
+ "SELECT DISTINCT ?varkey ?varval WHERE {\n") +
+ subject + " lv2var:variable ?variable .\n"
+ "?variable rdf:predicate ?varkey ;\n"
+ " rdf:value ?varval .\n"
+ "}");
+
+ Redland::Query::Results results = query.run(*world->rdf_world, model, base_uri);
+ for (Redland::Query::Results::iterator i = results.begin(); i != results.end(); ++i) {
+ const string key = world->rdf_world->prefixes().qualify(string((*i)["varkey"]));
+ const Redland::Node& val_node = (*i)["varval"];
+ if (key != "")
+ target->set_variable(path, key, AtomRDF::node_to_atom(val_node));
+ }
+
+ query = Redland::Query(*world->rdf_world, Glib::ustring(
+ "SELECT DISTINCT ?key ?val WHERE {\n") +
+ subject + " ingen:property ?property .\n"
+ "?property rdf:predicate ?key ;\n"
+ " rdf:value ?val .\n"
+ "}");
+
+ results = query.run(*world->rdf_world, model, base_uri);
+ for (Redland::Query::Results::iterator i = results.begin(); i != results.end(); ++i) {
+ const string key = world->rdf_world->prefixes().qualify(string((*i)["key"]));
+ const Redland::Node& val_node = (*i)["val"];
+ if (key != "")
+ target->set_property(path, key, AtomRDF::node_to_atom(val_node));
+ }
+
+ // Set passed variables last to override any loaded values
+ if (data)
+ for (GraphObject::Variables::const_iterator i = data.get().begin(); i != data.get().end(); ++i)
+ target->set_variable(path, i->first, i->second);
+
+ return true;
+}
+
+
+} // namespace Serialisation
+} // namespace Ingen
+
diff --git a/src/serialisation/Parser.hpp b/src/serialisation/Parser.hpp
new file mode 100644
index 00000000..7b8a35eb
--- /dev/null
+++ b/src/serialisation/Parser.hpp
@@ -0,0 +1,126 @@
+/* This file is part of Ingen.
+ * Copyright (C) 2007 Dave Robillard <http://drobilla.net>
+ *
+ * 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 LOADER_H
+#define LOADER_H
+
+#include <string>
+#include <glibmm/ustring.h>
+#include <boost/optional.hpp>
+#include <raul/SharedPtr.hpp>
+#include <raul/Path.hpp>
+#include <raul/Table.hpp>
+#include "interface/GraphObject.hpp"
+#include "module/World.hpp"
+
+namespace Redland { class World; class Model; }
+namespace Ingen { namespace Shared { class CommonInterface; } }
+
+using namespace Ingen::Shared;
+
+namespace Ingen {
+namespace Serialisation {
+
+
+class Parser {
+public:
+ virtual ~Parser() {}
+
+ virtual bool parse_document(
+ Ingen::Shared::World* world,
+ Shared::CommonInterface* target,
+ const Glib::ustring& document_uri,
+ Glib::ustring engine_base,
+ Glib::ustring object_uri,
+ boost::optional<Raul::Symbol> symbol=boost::optional<Raul::Symbol>(),
+ boost::optional<GraphObject::Variables> data=boost::optional<GraphObject::Variables>());
+
+ virtual bool parse_string(
+ Ingen::Shared::World* world,
+ Shared::CommonInterface* target,
+ const Glib::ustring& str,
+ const Glib::ustring& base_uri,
+ Glib::ustring engine_base,
+ boost::optional<Glib::ustring> object_uri=boost::optional<Glib::ustring>(),
+ boost::optional<Raul::Symbol> symbol=boost::optional<Raul::Symbol>(),
+ boost::optional<GraphObject::Variables> data=boost::optional<GraphObject::Variables>());
+
+private:
+
+ Glib::ustring uri_relative_to_base(Glib::ustring base, const Glib::ustring uri);
+
+ bool parse(
+ Ingen::Shared::World* world,
+ Shared::CommonInterface* target,
+ Redland::Model& model,
+ Glib::ustring base_uri,
+ Glib::ustring engine_base,
+ boost::optional<Glib::ustring> object_uri=boost::optional<Glib::ustring>(),
+ boost::optional<Raul::Symbol> symbol=boost::optional<Raul::Symbol>(),
+ boost::optional<GraphObject::Variables> data=boost::optional<GraphObject::Variables>());
+
+ bool parse_patch(
+ Ingen::Shared::World* world,
+ Ingen::Shared::CommonInterface* target,
+ Redland::Model& model,
+ const Glib::ustring& base_uri,
+ Glib::ustring engine_base,
+ const Glib::ustring& object_uri,
+ boost::optional<GraphObject::Variables> data);
+
+ bool parse_node(
+ Ingen::Shared::World* world,
+ Ingen::Shared::CommonInterface* target,
+ Redland::Model& model,
+ const Glib::ustring& base_uri,
+ const Glib::ustring& subject,
+ const Raul::Path& path,
+ boost::optional<GraphObject::Variables> data);
+
+ bool parse_port(
+ Ingen::Shared::World* world,
+ Ingen::Shared::CommonInterface* target,
+ Redland::Model& model,
+ const Glib::ustring& base_uri,
+ const Glib::ustring& subject,
+ const Raul::Path& path,
+ boost::optional<GraphObject::Variables> data=boost::optional<GraphObject::Variables>());
+
+ bool parse_variables(
+ Ingen::Shared::World* world,
+ Ingen::Shared::CommonInterface* target,
+ Redland::Model& model,
+ const Glib::ustring& base_uri,
+ const Glib::ustring& subject,
+ const Raul::Path& path,
+ boost::optional<GraphObject::Variables> data);
+
+ bool parse_connections(
+ Ingen::Shared::World* world,
+ Ingen::Shared::CommonInterface* target,
+ Redland::Model& model,
+ const Glib::ustring& base_uri,
+ const Glib::ustring& subject,
+ const Raul::Path& parent);
+
+};
+
+
+} // namespace Serialisation
+} // namespace Ingen
+
+#endif // LOADER_H
diff --git a/src/serialisation/Serialiser.cpp b/src/serialisation/Serialiser.cpp
new file mode 100644
index 00000000..2f0d9877
--- /dev/null
+++ b/src/serialisation/Serialiser.cpp
@@ -0,0 +1,501 @@
+/* This file is part of Ingen.
+ * Copyright (C) 2007 Dave Robillard <http://drobilla.net>
+ *
+ * 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 <algorithm>
+#include <cassert>
+#include <cmath>
+#include <cstdlib> // atof
+#include <cstring>
+#include <fstream>
+#include <iostream>
+#include <locale.h>
+#include <stdexcept>
+#include <string>
+#include <utility> // pair, make_pair
+#include <vector>
+#include <raul/Atom.hpp>
+#include <raul/AtomRDF.hpp>
+#include <raul/Path.hpp>
+#include <raul/TableImpl.hpp>
+#include <redlandmm/Model.hpp>
+#include <redlandmm/Node.hpp>
+#include <redlandmm/World.hpp>
+#include "module/World.hpp"
+#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 Redland;
+using namespace Ingen;
+using namespace Ingen::Shared;
+
+namespace Ingen {
+namespace Serialisation {
+
+
+Serialiser::Serialiser(Shared::World& world, SharedPtr<Shared::Store> store)
+ : _root_path("/")
+ , _store(store)
+ , _world(*world.rdf_world)
+{
+}
+
+
+void
+Serialiser::to_file(SharedPtr<GraphObject> object, const string& filename)
+{
+ _root_path = object->path();
+ start_to_filename(filename);
+ serialise(object);
+ finish();
+}
+
+
+
+string
+Serialiser::to_string(SharedPtr<GraphObject> object,
+ const string& base_uri,
+ const GraphObject::Variables& extra_rdf)
+{
+ start_to_string(object->path(), base_uri);
+ serialise(object);
+
+ Redland::Node base_rdf_node(_model->world(), Redland::Node::RESOURCE, base_uri);
+ for (GraphObject::Variables::const_iterator v = extra_rdf.begin(); v != extra_rdf.end(); ++v) {
+ if (v->first.find(":") != string::npos) {
+ _model->add_statement(base_rdf_node, v->first,
+ AtomRDF::atom_to_node(_model->world(), v->second));
+ } else {
+ cerr << "Warning: not serialising extra RDF with key '" << v->first << "'" << endl;
+ }
+ }
+
+ 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");
+
+ assert(filename.find(":") == string::npos || filename.substr(0, 5) == "file:");
+ if (filename.find(":") == string::npos)
+ _base_uri = "file://" + filename;
+ else
+ _base_uri = filename;
+ _model = new Redland::Model(_world);
+ _model->set_base_uri(_base_uri);
+ _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 serialised.
+ *
+ * All serialized paths will have the root path chopped from their prefix
+ * (therefore all serialized paths must be descendants of the root)
+ */
+void
+Serialiser::start_to_string(const Raul::Path& root, const string& base_uri)
+{
+ setlocale(LC_NUMERIC, "C");
+
+ _root_path = root;
+ _base_uri = base_uri;
+ _model = new Redland::Model(_world);
+ _model->set_base_uri(base_uri);
+ _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->serialise_to_file(_base_uri);
+ } else {
+ char* c_str = _model->serialise_to_string();
+ ret = c_str;
+ free(c_str);
+ }
+
+ _base_uri = "";
+#ifdef USE_BLANK_NODES
+ _node_map.clear();
+#endif
+ return ret;
+}
+
+
+Redland::Node
+Serialiser::patch_path_to_rdf_node(const Path& path)
+{
+#ifdef USE_BLANK_NODES
+ if (path == _root_path) {
+ return Redland::Node(_model->world(), Redland::Node::RESOURCE, _base_uri);
+ } else {
+ assert(path.length() > _root_path.length());
+ return Redland::Node(_model->world(), Redland::Node::RESOURCE,
+ _base_uri + string("#") + path.substr(_root_path.length()));
+ }
+#else
+ return path_to_rdf_node(path);
+#endif
+}
+
+
+
+/** Convert a path to an RDF blank node ID for serializing.
+ */
+Redland::Node
+Serialiser::path_to_rdf_node(const Path& path)
+{
+#if USE_BLANK_NODES
+ NodeMap::iterator i = _node_map.find(path);
+ if (i != _node_map.end()) {
+ assert(i->second);
+ assert(i->second.get_node());
+ return i->second;
+ } else {
+ Redland::Node id = _world.blank_id(Path::nameify(path.substr(1)));
+ assert(id);
+ _node_map[path] = id;
+ return id;
+ }
+#else
+ assert(_model);
+ assert(path.substr(0, _root_path.length()) == _root_path);
+
+ if (path == _root_path)
+ return Redland::Node(_model->world(), Redland::Node::RESOURCE, _base_uri);
+ else
+ return Redland::Node(_model->world(), Redland::Node::RESOURCE,
+ path.substr(_root_path.base().length()));
+#endif
+}
+
+
+#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::serialise(SharedPtr<GraphObject> object) throw (std::logic_error)
+{
+ if (!_model)
+ throw std::logic_error("serialise called without serialization in progress");
+
+ SharedPtr<Shared::Patch> patch = PtrCast<Shared::Patch>(object);
+ if (patch) {
+ serialise_patch(patch);
+ return;
+ }
+
+ SharedPtr<Shared::Node> node = PtrCast<Shared::Node>(object);
+ if (node) {
+ serialise_node(node, path_to_rdf_node(node->path()));
+ return;
+ }
+
+ SharedPtr<Shared::Port> port = PtrCast<Shared::Port>(object);
+ if (port) {
+ serialise_port(port.get(), path_to_rdf_node(port->path()));
+ return;
+ }
+
+ cerr << "[Serialiser] WARNING: Unsupported object type, "
+ << object->path() << " not serialised." << endl;
+}
+
+
+void
+Serialiser::serialise_patch(SharedPtr<Shared::Patch> patch)
+{
+ assert(_model);
+
+ const Redland::Node patch_id = patch_path_to_rdf_node(patch->path());
+
+ _model->add_statement(
+ patch_id,
+ "rdf:type",
+ Redland::Node(_model->world(), Redland::Node::RESOURCE, "http://drobilla.net/ns/ingen#Patch"));
+
+ GraphObject::Variables::const_iterator s = patch->variables().find("lv2:symbol");
+ // If symbol is stored as a variable, write that
+ if (s != patch->variables().end()) {
+ _model->add_statement(patch_id, "lv2:symbol",
+ Redland::Node(_model->world(), Redland::Node::LITERAL, s->second.get_string()));
+ // Otherwise take the one from our path (if possible)
+ } else if (patch->path() != "/") {
+ _model->add_statement(
+ patch_id, "lv2:symbol",
+ Redland::Node(_model->world(), Redland::Node::LITERAL, patch->path().name()));
+ }
+
+ _model->add_statement(
+ patch_id,
+ "ingen:polyphony",
+ AtomRDF::atom_to_node(_model->world(), Atom((int)patch->internal_polyphony())));
+
+ _model->add_statement(
+ patch_id,
+ "ingen:enabled",
+ AtomRDF::atom_to_node(_model->world(), Atom((bool)patch->enabled())));
+
+ serialise_variables(patch_id, patch->variables());
+
+ for (GraphObject::const_iterator n = _store->children_begin(patch);
+ n != _store->children_end(patch); ++n) {
+
+ if (n->second->graph_parent() != patch.get())
+ continue;
+
+ SharedPtr<Shared::Patch> patch = PtrCast<Shared::Patch>(n->second);
+ SharedPtr<Shared::Node> node = PtrCast<Shared::Node>(n->second);
+ if (patch) {
+ _model->add_statement(patch_id, "ingen:node", patch_path_to_rdf_node(patch->path()));
+ serialise_patch(patch);
+ } else if (node) {
+ const Redland::Node node_id = path_to_rdf_node(n->second->path());
+ _model->add_statement(patch_id, "ingen:node", node_id);
+ serialise_node(node, node_id);
+ }
+ }
+
+ for (uint32_t i=0; i < patch->num_ports(); ++i) {
+ Port* p = patch->port(i);
+ const Redland::Node port_id = path_to_rdf_node(p->path());
+ _model->add_statement(patch_id, "ingen:port", port_id);
+ serialise_port(p, port_id);
+ }
+
+ for (Shared::Patch::Connections::const_iterator c = patch->connections().begin();
+ c != patch->connections().end(); ++c) {
+ serialise_connection(patch, *c);
+ }
+}
+
+
+void
+Serialiser::serialise_plugin(SharedPtr<Shared::Plugin> plugin)
+{
+ assert(_model);
+
+ const Redland::Node plugin_id = Redland::Node(_model->world(), Redland::Node::RESOURCE, plugin->uri());
+
+ _model->add_statement(
+ plugin_id,
+ "rdf:type",
+ Redland::Node(_model->world(), Redland::Node::RESOURCE, plugin->type_uri()));
+}
+
+
+void
+Serialiser::serialise_node(SharedPtr<Shared::Node> node, const Redland::Node& node_id)
+{
+ const Redland::Node plugin_id
+ = Redland::Node(_model->world(), Redland::Node::RESOURCE, node->plugin()->uri());
+
+ _model->add_statement(
+ node_id,
+ "rdf:type",
+ Redland::Node(_model->world(), Redland::Node::RESOURCE, "ingen:Node"));
+
+ _model->add_statement(
+ node_id,
+ "lv2:symbol",
+ Redland::Node(_model->world(), Redland::Node::LITERAL, node->path().name()));
+
+ _model->add_statement(
+ node_id,
+ "ingen:plugin",
+ plugin_id);
+
+ _model->add_statement(
+ node_id,
+ "ingen:polyphonic",
+ AtomRDF::atom_to_node(_model->world(), Atom(node->polyphonic())));
+
+ //serialise_plugin(node->plugin());
+
+ for (uint32_t i=0; i < node->num_ports(); ++i) {
+ Port* p = node->port(i);
+ assert(p);
+ const Redland::Node port_id = path_to_rdf_node(p->path());
+ serialise_port(p, port_id);
+ _model->add_statement(node_id, "ingen:port", port_id);
+ }
+
+ serialise_variables(node_id, node->variables());
+}
+
+
+/** Writes a port subject with various information only if there are some
+ * predicate/object pairs to go with it (eg if the port has variable, or a value, or..).
+ * Audio output ports with no variable will not be written, for example.
+ */
+void
+Serialiser::serialise_port(const Port* port, const Redland::Node& port_id)
+{
+ if (port->is_input())
+ _model->add_statement(port_id, "rdf:type",
+ Redland::Node(_model->world(), Redland::Node::RESOURCE, "ingen:InputPort"));
+ else
+ _model->add_statement(port_id, "rdf:type",
+ Redland::Node(_model->world(), Redland::Node::RESOURCE, "ingen:OutputPort"));
+
+ _model->add_statement(port_id, "lv2:index",
+ AtomRDF::atom_to_node(_model->world(), Atom((int)port->index())));
+
+ _model->add_statement(port_id, "lv2:symbol",
+ Redland::Node(_model->world(), Redland::Node::LITERAL, port->path().name()));
+
+ _model->add_statement(port_id, "rdf:type",
+ Redland::Node(_model->world(), Redland::Node::RESOURCE, port->type().uri()));
+
+ if (port->type() == DataType::CONTROL && port->is_input())
+ _model->add_statement(port_id, "ingen:value",
+ AtomRDF::atom_to_node(_model->world(), Atom(port->value())));
+
+ serialise_variables(port_id, port->variables());
+}
+
+
+void
+Serialiser::serialise_connection(SharedPtr<GraphObject> parent,
+ SharedPtr<Connection> connection) throw (std::logic_error)
+{
+ if (!_model)
+ throw std::logic_error("serialise_connection called without serialization in progress");
+
+ const Redland::Node src_node = path_to_rdf_node(connection->src_port_path());
+ const Redland::Node dst_node = path_to_rdf_node(connection->dst_port_path());
+
+ /* This would allow associating data with the connection... */
+ const Redland::Node connection_node = _world.blank_id();
+ _model->add_statement(connection_node, "ingen:source", src_node);
+ _model->add_statement(connection_node, "ingen:destination", dst_node);
+ if (parent) {
+ const Redland::Node parent_node = path_to_rdf_node(parent->path());
+ _model->add_statement(parent_node, "ingen:connection", connection_node);
+ } else {
+ _model->add_statement(connection_node, "rdf:type",
+ Redland::Node(_model->world(), Redland::Node::RESOURCE, "ingen:Connection"));
+ }
+
+ /* ... but this is cleaner */
+ //_model->add_statement(dst_node, "ingen:connectedTo", src_node);
+}
+
+
+void
+Serialiser::serialise_variables(Redland::Node subject, const GraphObject::Variables& variables)
+{
+ for (GraphObject::Variables::const_iterator v = variables.begin(); v != variables.end(); ++v) {
+ if (v->first.find(":") != string::npos && v->first != "ingen:document") {
+ if (v->second.is_valid()) {
+ const Redland::Node var_id = _world.blank_id();
+ const Redland::Node key(_model->world(), Redland::Node::RESOURCE, v->first);
+ const Redland::Node value = AtomRDF::atom_to_node(_model->world(), v->second);
+ if (value) {
+ _model->add_statement(subject, "lv2var:variable", var_id);
+ _model->add_statement(var_id, "rdf:predicate", key);
+ _model->add_statement(var_id, "rdf:value", value);
+ } else {
+ cerr << "Warning: can not serialise value: key '" << v->first << "', type "
+ << (int)v->second.type() << endl;
+ }
+ } else {
+ cerr << "Warning: variable with no value: key '" << v->first << "'" << endl;
+ }
+ } else {
+ cerr << "Warning: not serialising variable with invalid key '" << v->first << "'" << endl;
+ }
+ }
+}
+
+
+} // namespace Serialisation
+} // namespace Ingen
diff --git a/src/serialisation/Serialiser.hpp b/src/serialisation/Serialiser.hpp
new file mode 100644
index 00000000..f27cad83
--- /dev/null
+++ b/src/serialisation/Serialiser.hpp
@@ -0,0 +1,109 @@
+/* This file is part of Ingen.
+ * Copyright (C) 2007 Dave Robillard <http://drobilla.net>
+ *
+ * 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 <map>
+#include <utility>
+#include <string>
+#include <stdexcept>
+#include <cassert>
+#include <raul/SharedPtr.hpp>
+#include <raul/Path.hpp>
+#include <raul/Atom.hpp>
+#include <redlandmm/World.hpp>
+#include <redlandmm/Model.hpp>
+#include "interface/GraphObject.hpp"
+#include "shared/Store.hpp"
+
+using namespace Raul;
+using namespace Ingen::Shared;
+
+namespace Ingen {
+
+namespace Shared {
+ class Plugin;
+ class GraphObject;
+ class Patch;
+ class Node;
+ class Port;
+ class Connection;
+ class World;
+}
+
+namespace Serialisation {
+
+
+/** Serialises Ingen objects (patches, nodes, etc) to RDF.
+ *
+ * \ingroup IngenClient
+ */
+class Serialiser
+{
+public:
+ Serialiser(Shared::World& world, SharedPtr<Shared::Store> store);
+
+ void to_file(SharedPtr<GraphObject> object, const std::string& filename);
+
+ std::string to_string(SharedPtr<GraphObject> object,
+ const std::string& base_uri,
+ const GraphObject::Variables& extra_rdf);
+
+ void start_to_string(const Raul::Path& root, const std::string& base_uri);
+ void serialise(SharedPtr<GraphObject> object) throw (std::logic_error);
+ void serialise_connection(SharedPtr<GraphObject> parent,
+ SharedPtr<Shared::Connection> c) throw (std::logic_error);
+
+ std::string finish();
+
+private:
+ enum Mode { TO_FILE, TO_STRING };
+
+ void start_to_filename(const std::string& filename);
+
+ void setup_prefixes();
+
+ void serialise_plugin(SharedPtr<Shared::Plugin> p);
+
+ void serialise_patch(SharedPtr<Shared::Patch> p);
+ void serialise_node(SharedPtr<Shared::Node> n, const Redland::Node& id);
+ void serialise_port(const Shared::Port* p, const Redland::Node& id);
+
+ void serialise_variables(Redland::Node subject, const GraphObject::Variables& variables);
+
+ Redland::Node path_to_rdf_node(const Path& path);
+ Redland::Node patch_path_to_rdf_node(const Path& path);
+
+ Raul::Path _root_path;
+ SharedPtr<Shared::Store> _store;
+ Mode _mode;
+ std::string _base_uri;
+ Redland::World& _world;
+ Redland::Model* _model;
+
+#ifdef USE_BLANK_NODES
+ typedef std::map<Raul::Path, Redland::Node> NodeMap;
+ NodeMap _node_map;
+#endif
+};
+
+
+} // namespace Serialisation
+} // namespace Ingen
+
+#endif // SERIALISER_H
diff --git a/src/serialisation/serialisation.cpp b/src/serialisation/serialisation.cpp
new file mode 100644
index 00000000..1d08e76c
--- /dev/null
+++ b/src/serialisation/serialisation.cpp
@@ -0,0 +1,46 @@
+/* This file is part of Ingen.
+ * Copyright (C) 2007 Dave Robillard <http://drobilla.net>
+ *
+ * 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 CONFIG_H_PATH
+#include "module/World.hpp"
+#include "serialisation.hpp"
+#include "Parser.hpp"
+#include "Serialiser.hpp"
+
+namespace Ingen {
+namespace Serialisation {
+
+
+Ingen::Serialisation::Parser*
+new_parser()
+{
+ return new Parser();
+}
+
+
+Ingen::Serialisation::Serialiser*
+new_serialiser(Ingen::Shared::World* world, SharedPtr<Store> store)
+{
+ assert(world->rdf_world);
+ return new Serialiser(*world, store);
+}
+
+
+
+} // namespace Serialisation
+} // namespace Ingen
+
diff --git a/src/serialisation/serialisation.hpp b/src/serialisation/serialisation.hpp
new file mode 100644
index 00000000..a250945b
--- /dev/null
+++ b/src/serialisation/serialisation.hpp
@@ -0,0 +1,44 @@
+/* This file is part of Ingen.
+ * Copyright (C) 2007 Dave Robillard <http://drobilla.net>
+ *
+ * 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 INGEN_SERIALISATION_H
+#define INGEN_SERIALISATION_H
+
+namespace Ingen {
+
+namespace Shared { class World; class Store; }
+
+namespace Serialisation {
+
+class Parser;
+class Serialiser;
+
+
+extern "C" {
+
+ extern Parser* new_parser();
+ extern Serialiser* new_serialiser(Ingen::Shared::World* world,
+ SharedPtr<Shared::Store> store);
+
+}
+
+
+} // namespace Serialisation
+} // namespace Ingen
+
+#endif // INGEN_SERIALISATION_H
+
diff --git a/src/serialisation/wscript b/src/serialisation/wscript
new file mode 100644
index 00000000..c3e98b81
--- /dev/null
+++ b/src/serialisation/wscript
@@ -0,0 +1,16 @@
+#!/usr/bin/env python
+import Params
+
+def build(bld):
+ obj = bld.create_obj('cpp', 'shlib')
+ obj.source = '''
+ Parser.cpp
+ Serialiser.cpp
+ serialisation.cpp
+ '''
+ obj.includes = ['..', '../../common', '../..']
+ obj.name = 'libingen_serialisation'
+ obj.target = 'ingen_serialisation'
+ obj.uselib = 'GLIBMM SLV2 RAUL REDLANDMM'
+ obj.vnum = '0.0.0'
+