From 3019b09099371b3fe568b7dcc3bb92203d800b1f Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 17 Aug 2008 03:20:42 +0000 Subject: Rename 'Loader' 'Parser'. git-svn-id: http://svn.drobilla.net/lad/ingen@1411 a436a847-0d15-0410-975c-d299462d15a1 --- src/libs/engine/HTTPEngineReceiver.cpp | 14 +- src/libs/gui/Configuration.cpp | 7 +- src/libs/gui/ThreadedLoader.cpp | 10 +- src/libs/gui/ThreadedLoader.hpp | 4 +- src/libs/module/World.hpp | 6 +- src/libs/serialisation/Loader.cpp | 426 ------------------------------- src/libs/serialisation/Loader.hpp | 56 ---- src/libs/serialisation/Makefile.am | 4 +- src/libs/serialisation/Parser.cpp | 426 +++++++++++++++++++++++++++++++ src/libs/serialisation/Parser.hpp | 56 ++++ src/libs/serialisation/serialisation.cpp | 8 +- src/libs/serialisation/serialisation.hpp | 4 +- 12 files changed, 509 insertions(+), 512 deletions(-) delete mode 100644 src/libs/serialisation/Loader.cpp delete mode 100644 src/libs/serialisation/Loader.hpp create mode 100644 src/libs/serialisation/Parser.cpp create mode 100644 src/libs/serialisation/Parser.hpp (limited to 'src/libs') diff --git a/src/libs/engine/HTTPEngineReceiver.cpp b/src/libs/engine/HTTPEngineReceiver.cpp index 7427f18d..4d57843b 100644 --- a/src/libs/engine/HTTPEngineReceiver.cpp +++ b/src/libs/engine/HTTPEngineReceiver.cpp @@ -26,7 +26,7 @@ #include "module/Module.hpp" #include "serialisation/serialisation.hpp" #include "serialisation/Serialiser.hpp" -#include "serialisation/Loader.hpp" +#include "serialisation/Parser.hpp" #include "engine/ThreadManager.hpp" #include "HTTPEngineReceiver.hpp" #include "QueuedEventSource.hpp" @@ -58,9 +58,9 @@ HTTPEngineReceiver::HTTPEngineReceiver(Engine& engine, uint16_t port) engine.world()->serialiser = SharedPtr( Ingen::Serialisation::new_serialiser(engine.world(), engine.engine_store())); - if (!engine.world()->loader) - engine.world()->loader = SharedPtr( - Ingen::Serialisation::new_loader()); + if (!engine.world()->parser) + engine.world()->parser = SharedPtr( + Ingen::Serialisation::new_parser()); } else { cerr << "WARNING: Failed to load ingen_serialisation module, HTTP disabled." << endl; } @@ -165,9 +165,9 @@ HTTPEngineReceiver::message_callback(SoupServer* server, SoupMessage* msg, const return; } - // Get loader - SharedPtr loader = me->_engine.world()->loader; - if (!loader) { + // Get parser + SharedPtr parser = me->_engine.world()->parser; + if (!parser) { soup_message_set_status (msg, SOUP_STATUS_INTERNAL_SERVER_ERROR); return; } diff --git a/src/libs/gui/Configuration.cpp b/src/libs/gui/Configuration.cpp index 36d5ad75..a442b37c 100644 --- a/src/libs/gui/Configuration.cpp +++ b/src/libs/gui/Configuration.cpp @@ -23,13 +23,10 @@ #include #include "client/PortModel.hpp" #include "client/PluginModel.hpp" -#include "client/PatchModel.hpp" -#include "serialisation/Loader.hpp" +#include "serialisation/Parser.hpp" #include "App.hpp" -using std::cerr; using std::cout; using std::endl; -using std::map; using std::string; -using Ingen::Client::PatchModel; +using namespace std; namespace Ingen { namespace GUI { diff --git a/src/libs/gui/ThreadedLoader.cpp b/src/libs/gui/ThreadedLoader.cpp index 0247529d..8c8af828 100644 --- a/src/libs/gui/ThreadedLoader.cpp +++ b/src/libs/gui/ThreadedLoader.cpp @@ -44,16 +44,16 @@ ThreadedLoader::ThreadedLoader(SharedPtr engine) world->serialisation_module = Ingen::Shared::load_module("ingen_serialisation"); if (world->serialisation_module) { - Loader* (*new_loader)() = NULL; + Parser* (*new_parser)() = NULL; bool found = App::instance().world()->serialisation_module->get_symbol( - "new_loader", (void*&)new_loader); + "new_parser", (void*&)new_parser); if (found) - _loader = SharedPtr(new_loader()); + _parser = SharedPtr(new_parser()); } - if (_loader) + if (_parser) start(); else cerr << "WARNING: Failed to load ingen_serialisation module, load disabled." << endl; @@ -99,7 +99,7 @@ ThreadedLoader::load_patch(bool merge, false))); } else { _events.push_back(sigc::hide_return(sigc::bind( - sigc::mem_fun(_loader.get(), &Ingen::Serialisation::Loader::load), + sigc::mem_fun(_parser.get(), &Ingen::Serialisation::Parser::parse), App::instance().world(), App::instance().world()->engine.get(), data_base_uri, diff --git a/src/libs/gui/ThreadedLoader.hpp b/src/libs/gui/ThreadedLoader.hpp index 6c095925..ea4f652d 100644 --- a/src/libs/gui/ThreadedLoader.hpp +++ b/src/libs/gui/ThreadedLoader.hpp @@ -29,7 +29,7 @@ #include "client/PatchModel.hpp" #include "client/DeprecatedLoader.hpp" #include "serialisation/Serialiser.hpp" -#include "serialisation/Loader.hpp" +#include "serialisation/Parser.hpp" using std::string; using std::list; using boost::optional; @@ -80,7 +80,7 @@ private: void _whipped(); SharedPtr _engine; - SharedPtr _loader; + SharedPtr _parser; DeprecatedLoader _deprecated_loader; Glib::Mutex _mutex; diff --git a/src/libs/module/World.hpp b/src/libs/module/World.hpp index a1a0d5c7..dbbac2bd 100644 --- a/src/libs/module/World.hpp +++ b/src/libs/module/World.hpp @@ -34,9 +34,9 @@ namespace Redland { class World; } namespace Ingen { class Engine; -namespace Serialisation { class Serialiser; class Loader; } +namespace Serialisation { class Serialiser; class Parser; } using Serialisation::Serialiser; -using Serialisation::Loader; +using Serialisation::Parser; namespace Shared { class EngineInterface; @@ -64,7 +64,7 @@ struct World { SharedPtr engine; SharedPtr local_engine; SharedPtr serialiser; - SharedPtr loader; + SharedPtr parser; SharedPtr store; SharedPtr serialisation_module; diff --git a/src/libs/serialisation/Loader.cpp b/src/libs/serialisation/Loader.cpp deleted file mode 100644 index b0d919e0..00000000 --- a/src/libs/serialisation/Loader.cpp +++ /dev/null @@ -1,426 +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 -#include -#include -#include -#include -#include -#include "interface/EngineInterface.hpp" -#include "Loader.hpp" - -using namespace std; -using namespace Raul; -using namespace Ingen::Shared; - -namespace Ingen { -namespace Serialisation { - - -/** Load (create) a patch from RDF into the engine. - * - * @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 -Loader::load(Ingen::Shared::World* world, - Ingen::Shared::CommonInterface* target, - const Glib::ustring& document_uri, - boost::optional parent, - std::string patch_name, - Glib::ustring patch_uri, - GraphObject::Variables data) -{ - setlocale(LC_NUMERIC, "C"); - - // FIXME: this whole thing is a mess - - std::set created; - - Redland::Model model(*world->rdf_world, document_uri, document_uri); - - if (patch_uri == "") - patch_uri = string("<") + document_uri + ">"; - else - patch_uri = string("<") + patch_uri + ">"; - - cout << "[Loader] Loading " << patch_uri << endl; - - size_t patch_poly = 1; - - /* Use parameter overridden polyphony, if given */ - GraphObject::Variables::iterator poly_param = data.find("ingen:polyphony"); - if (poly_param != data.end() && poly_param->second.type() == Atom::INT) { - patch_poly = poly_param->second.get_int32(); - - /* Get polyphony from file (mandatory if not specified in parameters) */ - } else { - // FIXME: polyphony datatype? - Redland::Query query(*world->rdf_world, Glib::ustring( - "SELECT DISTINCT ?poly WHERE {\n") + - patch_uri + " ingen:polyphony ?poly\n }"); - - Redland::Query::Results results = query.run(*world->rdf_world, model); - - if (results.size() == 0) { - cerr << "[Loader] ERROR: No polyphony found!" << endl; - return false; - } - - const Redland::Node& poly_node = (*results.begin())["poly"]; - assert(poly_node.is_int()); - patch_poly = static_cast(poly_node.to_int()); - } - - /* Get name (if available/necessary) */ - - if (patch_name == "") { - patch_name = string(document_uri.substr(document_uri.find_last_of("/")+1)); - if (patch_name.substr(patch_name.length()-10) == ".ingen.ttl") - patch_name = patch_name.substr(0, patch_name.length()-10); - - Redland::Query query(*world->rdf_world, Glib::ustring( - "SELECT DISTINCT ?name WHERE {\n") + - patch_uri + " ingen:symbol ?name\n}"); - - Redland::Query::Results results = query.run(*world->rdf_world, model); - - if (results.size() > 0) - patch_name = (*results.begin())["name"].to_string(); - } - - const Path patch_path = ( parent ? (parent.get().base() + patch_name) : Path("/") ); - - cout << " as " << patch_path << endl; - - if (patch_path != "/") - target->new_patch(patch_path, patch_poly); - - /* Set document metadata (so File->Save doesn't prompt) - * FIXME: This needs some thinking for multiple clients... */ - target->set_variable(patch_path, "ingen:document", Atom(document_uri.c_str())); - - /* Load (plugin) nodes */ - - Redland::Query query(*world->rdf_world, Glib::ustring( - "SELECT DISTINCT ?name ?plugin ?varkey ?varval ?poly WHERE {\n") + - patch_uri + " ingen:node ?node .\n" - "?node ingen:symbol ?name ;\n" - " ingen:plugin ?plugin ;\n" - " ingen:polyphonic ?poly .\n" - "OPTIONAL { ?node ingen:variable ?variable .\n" - " ?variable ingen:key ?varkey ;\n" - " ingen:value ?varval .\n" - " }" - "}"); - - Redland::Query::Results results = query.run(*world->rdf_world, model); - - map variable; - - 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 = world->rdf_world->qualify((*i)["plugin"].to_string()); - bool node_polyphonic = false; - - 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, node_polyphonic); - created.insert(node_path); - } - - const string key = world->rdf_world->prefixes().qualify((*i)["varkey"].to_string()); - 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 ?patch ?name WHERE {\n") + - patch_uri + " ingen:node ?patch .\n" - "?patch a ingen:Patch ;\n" - " ingen:symbol ?name .\n" - "}"); - - results = query.run(*world->rdf_world, model); - - for (Redland::Query::Results::iterator i = results.begin(); i != results.end(); ++i) { - - const string name = (*i)["name"].to_string(); - const string patch = (*i)["patch"].to_string(); - - const Path subpatch_path = patch_path.base() + (string)name; - - if (created.find(subpatch_path) == created.end()) { - created.insert(subpatch_path); - load(world, target, document_uri, patch_path, name, patch); - } - } - - //created.clear(); - - - /* Set node port control values */ - - query = Redland::Query(*world->rdf_world, Glib::ustring( - "SELECT DISTINCT ?nodename ?portname ?portval WHERE {\n") + - patch_uri + " ingen:node ?node .\n" - "?node ingen:symbol ?nodename ;\n" - " ingen:port ?port .\n" - "?port ingen:symbol ?portname ;\n" - " ingen:value ?portval .\n" - "FILTER ( datatype(?portval) = xsd:decimal )\n" - "}\n"); - - results = query.run(*world->rdf_world, model); - - 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") + - patch_uri + " ingen:port ?port .\n" - "?port a ?type ;\n" - " a ?datatype ;\n" - " ingen: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 ingen:variable ?variable .\n" - " ?variable ingen:key ?varkey ;\n" - " ingen:value ?varval .\n" - " }" - "}"); - - results = query.run(*world->rdf_world, model); - - 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)); - } - - created.clear(); - - - /* Node -> Node connections */ - - query = Redland::Query(*world->rdf_world, Glib::ustring( - "SELECT DISTINCT ?srcnodename ?srcname ?dstnodename ?dstname WHERE {\n") + - patch_uri + "ingen:node ?srcnode ;\n" - " ingen:node ?dstnode .\n" - "?srcnode ingen:port ?src ;\n" - " ingen:symbol ?srcnodename .\n" - "?dstnode ingen:port ?dst ;\n" - " ingen:symbol ?dstnodename .\n" - "?src ingen:symbol ?srcname .\n" - "?dst ingen:connectedTo ?src ;\n" - " ingen:symbol ?dstname .\n" - "}\n"); - - results = query.run(*world->rdf_world, model); - - for (Redland::Query::Results::iterator i = results.begin(); i != results.end(); ++i) { - Path src_node = patch_path.base() + Path::nameify((*i)["srcnodename"].to_string()); - Path src_port = src_node.base() + Path::nameify((*i)["srcname"].to_string()); - Path dst_node = patch_path.base() + Path::nameify((*i)["dstnodename"].to_string()); - Path dst_port = dst_node.base() + Path::nameify((*i)["dstname"].to_string()); - - //cerr << patch_path << " 1 CONNECTION: " << src_port << " -> " << dst_port << endl; - - target->connect(src_port, dst_port); - } - - - /* This Patch -> Node connections */ - - query = Redland::Query(*world->rdf_world, Glib::ustring( - "SELECT DISTINCT ?srcname ?dstnodename ?dstname WHERE {\n") + - patch_uri + " ingen:port ?src ;\n" - " ingen:node ?dstnode .\n" - "?dstnode ingen:port ?dst ;\n" - " ingen:symbol ?dstnodename .\n" - "?dst ingen:connectedTo ?src ;\n" - " ingen:symbol ?dstname .\n" - "?src ingen:symbol ?srcname .\n" - "}\n"); - - results = query.run(*world->rdf_world, model); - - for (Redland::Query::Results::iterator i = results.begin(); i != results.end(); ++i) { - Path src_port = patch_path.base() + Path::nameify((*i)["srcname"].to_string()); - Path dst_node = patch_path.base() + Path::nameify((*i)["dstnodename"].to_string()); - Path dst_port = dst_node.base() + Path::nameify((*i)["dstname"].to_string()); - - //cerr << patch_path << " 2 CONNECTION: " << src_port << " -> " << dst_port << endl; - - target->connect(src_port, dst_port); - } - - - /* Node -> This Patch connections */ - - query = Redland::Query(*world->rdf_world, Glib::ustring( - "SELECT DISTINCT ?srcnodename ?srcname ?dstname WHERE {\n") + - patch_uri + " ingen:port ?dst ;\n" - " ingen:node ?srcnode .\n" - "?srcnode ingen:port ?src ;\n" - " ingen:symbol ?srcnodename .\n" - "?dst ingen:connectedTo ?src ;\n" - " ingen:symbol ?dstname .\n" - "?src ingen:symbol ?srcname .\n" - "}\n"); - - results = query.run(*world->rdf_world, model); - - for (Redland::Query::Results::iterator i = results.begin(); i != results.end(); ++i) { - Path dst_port = patch_path.base() + Path::nameify((*i)["dstname"].to_string()); - Path src_node = patch_path.base() + Path::nameify((*i)["srcnodename"].to_string()); - Path src_port = src_node.base() + Path::nameify((*i)["srcname"].to_string()); - - //cerr << patch_path << " 3 CONNECTION: " << src_port << " -> " << dst_port << endl; - - target->connect(src_port, dst_port); - } - - - /* This Patch -> This Patch connections */ - - query = Redland::Query(*world->rdf_world, Glib::ustring( - "SELECT DISTINCT ?srcname ?dstname WHERE {\n") + - patch_uri + " ingen:port ?src ;\n" - " ingen:port ?dst .\n" - "?dst ingen:connectedTo ?src ;\n" - " ingen:symbol ?dstname .\n" - "?src ingen:symbol ?srcname .\n" - "}\n"); - - results = query.run(*world->rdf_world, model); - - for (Redland::Query::Results::iterator i = results.begin(); i != results.end(); ++i) { - Path src_port = patch_path.base() + Path::nameify((*i)["srcname"].to_string()); - Path dst_port = patch_path.base() + Path::nameify((*i)["dstname"].to_string()); - - //cerr << patch_path << " 4 CONNECTION: " << src_port << " -> " << dst_port << endl; - - target->connect(src_port, dst_port); - } - - - /* Load variables */ - - query = Redland::Query(*world->rdf_world, Glib::ustring( - "SELECT DISTINCT ?varkey ?varval WHERE {\n") + - patch_uri + " ingen:variable ?variable .\n" - "?variable ingen:key ?varkey ;\n" - " ingen:value ?varval .\n" - "}"); - - results = query.run(*world->rdf_world, model); - - for (Redland::Query::Results::iterator i = results.begin(); i != results.end(); ++i) { - - const string key = world->rdf_world->prefixes().qualify(string((*i)["varkey"])); - Redland::Node val_node = (*i)["varval"]; - - if (key != "") - target->set_variable(patch_path, key, AtomRDF::node_to_atom(val_node)); - } - - - // Set passed variables last to override any loaded values - for (GraphObject::Variables::const_iterator i = data.begin(); i != data.end(); ++i) - target->set_variable(patch_path, i->first, i->second); - - - /* Enable */ - - query = Redland::Query(*world->rdf_world, Glib::ustring( - "SELECT DISTINCT ?enabled WHERE {\n") + - patch_uri + " ingen:enabled ?enabled .\n" - "}"); - - results = query.run(*world->rdf_world, model); - - for (Redland::Query::Results::iterator i = results.begin(); i != results.end(); ++i) { - - 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; -} - -} // namespace Serialisation -} // namespace Ingen - diff --git a/src/libs/serialisation/Loader.hpp b/src/libs/serialisation/Loader.hpp deleted file mode 100644 index 9220e4c9..00000000 --- a/src/libs/serialisation/Loader.hpp +++ /dev/null @@ -1,56 +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 LOADER_H -#define LOADER_H - -#include -#include -#include -#include -#include -#include -#include "interface/GraphObject.hpp" -#include "module/World.hpp" - -namespace Redland { class World; } -namespace Ingen { namespace Shared { class CommonInterface; } } - -using namespace Ingen::Shared; - -namespace Ingen { -namespace Serialisation { - - -class Loader { -public: - virtual ~Loader() {} - - virtual bool load(Ingen::Shared::World* world, - Ingen::Shared::CommonInterface* target, - const Glib::ustring& uri, - boost::optional parent, - std::string patch_name, - Glib::ustring patch_uri = "", - GraphObject::Variables data = GraphObject::Variables()); -}; - - -} // namespace Serialisation -} // namespace Ingen - -#endif // LOADER_H diff --git a/src/libs/serialisation/Makefile.am b/src/libs/serialisation/Makefile.am index 3121cec3..57e99055 100644 --- a/src/libs/serialisation/Makefile.am +++ b/src/libs/serialisation/Makefile.am @@ -15,8 +15,8 @@ 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 = \ - Loader.cpp \ - Loader.hpp \ + Parser.cpp \ + Parser.hpp \ Serialiser.cpp \ Serialiser.hpp \ serialisation.cpp \ diff --git a/src/libs/serialisation/Parser.cpp b/src/libs/serialisation/Parser.cpp new file mode 100644 index 00000000..8bd9bcbb --- /dev/null +++ b/src/libs/serialisation/Parser.cpp @@ -0,0 +1,426 @@ +/* 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 +#include +#include +#include +#include +#include +#include "interface/EngineInterface.hpp" +#include "Parser.hpp" + +using namespace std; +using namespace Raul; +using namespace Ingen::Shared; + +namespace Ingen { +namespace Serialisation { + + +/** 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(Ingen::Shared::World* world, + Ingen::Shared::CommonInterface* target, + const Glib::ustring& document_uri, + boost::optional parent, + std::string patch_name, + Glib::ustring patch_uri, + GraphObject::Variables data) +{ + setlocale(LC_NUMERIC, "C"); + + // FIXME: this whole thing is a mess + + std::set created; + + Redland::Model model(*world->rdf_world, document_uri, document_uri); + + if (patch_uri == "") + patch_uri = string("<") + document_uri + ">"; + else + patch_uri = string("<") + patch_uri + ">"; + + cout << "[Parser] Loading " << patch_uri << endl; + + size_t patch_poly = 1; + + /* Use parameter overridden polyphony, if given */ + GraphObject::Variables::iterator poly_param = data.find("ingen:polyphony"); + if (poly_param != data.end() && poly_param->second.type() == Atom::INT) { + patch_poly = poly_param->second.get_int32(); + + /* Get polyphony from file (mandatory if not specified in parameters) */ + } else { + // FIXME: polyphony datatype? + Redland::Query query(*world->rdf_world, Glib::ustring( + "SELECT DISTINCT ?poly WHERE {\n") + + patch_uri + " ingen:polyphony ?poly\n }"); + + Redland::Query::Results results = query.run(*world->rdf_world, model); + + if (results.size() == 0) { + cerr << "[Parser] ERROR: No polyphony found!" << endl; + return false; + } + + const Redland::Node& poly_node = (*results.begin())["poly"]; + assert(poly_node.is_int()); + patch_poly = static_cast(poly_node.to_int()); + } + + /* Get name (if available/necessary) */ + + if (patch_name == "") { + patch_name = string(document_uri.substr(document_uri.find_last_of("/")+1)); + if (patch_name.substr(patch_name.length()-10) == ".ingen.ttl") + patch_name = patch_name.substr(0, patch_name.length()-10); + + Redland::Query query(*world->rdf_world, Glib::ustring( + "SELECT DISTINCT ?name WHERE {\n") + + patch_uri + " ingen:symbol ?name\n}"); + + Redland::Query::Results results = query.run(*world->rdf_world, model); + + if (results.size() > 0) + patch_name = (*results.begin())["name"].to_string(); + } + + const Path patch_path = ( parent ? (parent.get().base() + patch_name) : Path("/") ); + + cout << " as " << patch_path << endl; + + if (patch_path != "/") + target->new_patch(patch_path, patch_poly); + + /* Set document metadata (so File->Save doesn't prompt) + * FIXME: This needs some thinking for multiple clients... */ + target->set_variable(patch_path, "ingen:document", Atom(document_uri.c_str())); + + /* Load (plugin) nodes */ + + Redland::Query query(*world->rdf_world, Glib::ustring( + "SELECT DISTINCT ?name ?plugin ?varkey ?varval ?poly WHERE {\n") + + patch_uri + " ingen:node ?node .\n" + "?node ingen:symbol ?name ;\n" + " ingen:plugin ?plugin ;\n" + " ingen:polyphonic ?poly .\n" + "OPTIONAL { ?node ingen:variable ?variable .\n" + " ?variable ingen:key ?varkey ;\n" + " ingen:value ?varval .\n" + " }" + "}"); + + Redland::Query::Results results = query.run(*world->rdf_world, model); + + map variable; + + 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 = world->rdf_world->qualify((*i)["plugin"].to_string()); + bool node_polyphonic = false; + + 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, node_polyphonic); + created.insert(node_path); + } + + const string key = world->rdf_world->prefixes().qualify((*i)["varkey"].to_string()); + 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 ?patch ?name WHERE {\n") + + patch_uri + " ingen:node ?patch .\n" + "?patch a ingen:Patch ;\n" + " ingen:symbol ?name .\n" + "}"); + + results = query.run(*world->rdf_world, model); + + for (Redland::Query::Results::iterator i = results.begin(); i != results.end(); ++i) { + + const string name = (*i)["name"].to_string(); + const string patch = (*i)["patch"].to_string(); + + const Path subpatch_path = patch_path.base() + (string)name; + + if (created.find(subpatch_path) == created.end()) { + created.insert(subpatch_path); + parse(world, target, document_uri, patch_path, name, patch); + } + } + + //created.clear(); + + + /* Set node port control values */ + + query = Redland::Query(*world->rdf_world, Glib::ustring( + "SELECT DISTINCT ?nodename ?portname ?portval WHERE {\n") + + patch_uri + " ingen:node ?node .\n" + "?node ingen:symbol ?nodename ;\n" + " ingen:port ?port .\n" + "?port ingen:symbol ?portname ;\n" + " ingen:value ?portval .\n" + "FILTER ( datatype(?portval) = xsd:decimal )\n" + "}\n"); + + results = query.run(*world->rdf_world, model); + + 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") + + patch_uri + " ingen:port ?port .\n" + "?port a ?type ;\n" + " a ?datatype ;\n" + " ingen: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 ingen:variable ?variable .\n" + " ?variable ingen:key ?varkey ;\n" + " ingen:value ?varval .\n" + " }" + "}"); + + results = query.run(*world->rdf_world, model); + + 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)); + } + + created.clear(); + + + /* Node -> Node connections */ + + query = Redland::Query(*world->rdf_world, Glib::ustring( + "SELECT DISTINCT ?srcnodename ?srcname ?dstnodename ?dstname WHERE {\n") + + patch_uri + "ingen:node ?srcnode ;\n" + " ingen:node ?dstnode .\n" + "?srcnode ingen:port ?src ;\n" + " ingen:symbol ?srcnodename .\n" + "?dstnode ingen:port ?dst ;\n" + " ingen:symbol ?dstnodename .\n" + "?src ingen:symbol ?srcname .\n" + "?dst ingen:connectedTo ?src ;\n" + " ingen:symbol ?dstname .\n" + "}\n"); + + results = query.run(*world->rdf_world, model); + + for (Redland::Query::Results::iterator i = results.begin(); i != results.end(); ++i) { + Path src_node = patch_path.base() + Path::nameify((*i)["srcnodename"].to_string()); + Path src_port = src_node.base() + Path::nameify((*i)["srcname"].to_string()); + Path dst_node = patch_path.base() + Path::nameify((*i)["dstnodename"].to_string()); + Path dst_port = dst_node.base() + Path::nameify((*i)["dstname"].to_string()); + + //cerr << patch_path << " 1 CONNECTION: " << src_port << " -> " << dst_port << endl; + + target->connect(src_port, dst_port); + } + + + /* This Patch -> Node connections */ + + query = Redland::Query(*world->rdf_world, Glib::ustring( + "SELECT DISTINCT ?srcname ?dstnodename ?dstname WHERE {\n") + + patch_uri + " ingen:port ?src ;\n" + " ingen:node ?dstnode .\n" + "?dstnode ingen:port ?dst ;\n" + " ingen:symbol ?dstnodename .\n" + "?dst ingen:connectedTo ?src ;\n" + " ingen:symbol ?dstname .\n" + "?src ingen:symbol ?srcname .\n" + "}\n"); + + results = query.run(*world->rdf_world, model); + + for (Redland::Query::Results::iterator i = results.begin(); i != results.end(); ++i) { + Path src_port = patch_path.base() + Path::nameify((*i)["srcname"].to_string()); + Path dst_node = patch_path.base() + Path::nameify((*i)["dstnodename"].to_string()); + Path dst_port = dst_node.base() + Path::nameify((*i)["dstname"].to_string()); + + //cerr << patch_path << " 2 CONNECTION: " << src_port << " -> " << dst_port << endl; + + target->connect(src_port, dst_port); + } + + + /* Node -> This Patch connections */ + + query = Redland::Query(*world->rdf_world, Glib::ustring( + "SELECT DISTINCT ?srcnodename ?srcname ?dstname WHERE {\n") + + patch_uri + " ingen:port ?dst ;\n" + " ingen:node ?srcnode .\n" + "?srcnode ingen:port ?src ;\n" + " ingen:symbol ?srcnodename .\n" + "?dst ingen:connectedTo ?src ;\n" + " ingen:symbol ?dstname .\n" + "?src ingen:symbol ?srcname .\n" + "}\n"); + + results = query.run(*world->rdf_world, model); + + for (Redland::Query::Results::iterator i = results.begin(); i != results.end(); ++i) { + Path dst_port = patch_path.base() + Path::nameify((*i)["dstname"].to_string()); + Path src_node = patch_path.base() + Path::nameify((*i)["srcnodename"].to_string()); + Path src_port = src_node.base() + Path::nameify((*i)["srcname"].to_string()); + + //cerr << patch_path << " 3 CONNECTION: " << src_port << " -> " << dst_port << endl; + + target->connect(src_port, dst_port); + } + + + /* This Patch -> This Patch connections */ + + query = Redland::Query(*world->rdf_world, Glib::ustring( + "SELECT DISTINCT ?srcname ?dstname WHERE {\n") + + patch_uri + " ingen:port ?src ;\n" + " ingen:port ?dst .\n" + "?dst ingen:connectedTo ?src ;\n" + " ingen:symbol ?dstname .\n" + "?src ingen:symbol ?srcname .\n" + "}\n"); + + results = query.run(*world->rdf_world, model); + + for (Redland::Query::Results::iterator i = results.begin(); i != results.end(); ++i) { + Path src_port = patch_path.base() + Path::nameify((*i)["srcname"].to_string()); + Path dst_port = patch_path.base() + Path::nameify((*i)["dstname"].to_string()); + + //cerr << patch_path << " 4 CONNECTION: " << src_port << " -> " << dst_port << endl; + + target->connect(src_port, dst_port); + } + + + /* Load variables */ + + query = Redland::Query(*world->rdf_world, Glib::ustring( + "SELECT DISTINCT ?varkey ?varval WHERE {\n") + + patch_uri + " ingen:variable ?variable .\n" + "?variable ingen:key ?varkey ;\n" + " ingen:value ?varval .\n" + "}"); + + results = query.run(*world->rdf_world, model); + + for (Redland::Query::Results::iterator i = results.begin(); i != results.end(); ++i) { + + const string key = world->rdf_world->prefixes().qualify(string((*i)["varkey"])); + Redland::Node val_node = (*i)["varval"]; + + if (key != "") + target->set_variable(patch_path, key, AtomRDF::node_to_atom(val_node)); + } + + + // Set passed variables last to override any loaded values + for (GraphObject::Variables::const_iterator i = data.begin(); i != data.end(); ++i) + target->set_variable(patch_path, i->first, i->second); + + + /* Enable */ + + query = Redland::Query(*world->rdf_world, Glib::ustring( + "SELECT DISTINCT ?enabled WHERE {\n") + + patch_uri + " ingen:enabled ?enabled .\n" + "}"); + + results = query.run(*world->rdf_world, model); + + for (Redland::Query::Results::iterator i = results.begin(); i != results.end(); ++i) { + + 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; +} + +} // namespace Serialisation +} // namespace Ingen + diff --git a/src/libs/serialisation/Parser.hpp b/src/libs/serialisation/Parser.hpp new file mode 100644 index 00000000..18d59bf2 --- /dev/null +++ b/src/libs/serialisation/Parser.hpp @@ -0,0 +1,56 @@ +/* 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 LOADER_H +#define LOADER_H + +#include +#include +#include +#include +#include +#include +#include "interface/GraphObject.hpp" +#include "module/World.hpp" + +namespace Redland { class World; } +namespace Ingen { namespace Shared { class CommonInterface; } } + +using namespace Ingen::Shared; + +namespace Ingen { +namespace Serialisation { + + +class Parser { +public: + virtual ~Parser() {} + + virtual bool parse(Ingen::Shared::World* world, + Ingen::Shared::CommonInterface* target, + const Glib::ustring& uri, + boost::optional parent, + std::string patch_name, + Glib::ustring patch_uri = "", + GraphObject::Variables data = GraphObject::Variables()); +}; + + +} // namespace Serialisation +} // namespace Ingen + +#endif // LOADER_H diff --git a/src/libs/serialisation/serialisation.cpp b/src/libs/serialisation/serialisation.cpp index 3ece3772..1d08e76c 100644 --- a/src/libs/serialisation/serialisation.cpp +++ b/src/libs/serialisation/serialisation.cpp @@ -18,17 +18,17 @@ #include CONFIG_H_PATH #include "module/World.hpp" #include "serialisation.hpp" -#include "Loader.hpp" +#include "Parser.hpp" #include "Serialiser.hpp" namespace Ingen { namespace Serialisation { -Ingen::Serialisation::Loader* -new_loader() +Ingen::Serialisation::Parser* +new_parser() { - return new Loader(); + return new Parser(); } diff --git a/src/libs/serialisation/serialisation.hpp b/src/libs/serialisation/serialisation.hpp index abae159c..a250945b 100644 --- a/src/libs/serialisation/serialisation.hpp +++ b/src/libs/serialisation/serialisation.hpp @@ -24,13 +24,13 @@ namespace Shared { class World; class Store; } namespace Serialisation { -class Loader; +class Parser; class Serialiser; extern "C" { - extern Loader* new_loader(); + extern Parser* new_parser(); extern Serialiser* new_serialiser(Ingen::Shared::World* world, SharedPtr store); -- cgit v1.2.1