/*
This file is part of Ingen.
Copyright 2007-2016 David Robillard
Ingen is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License as published by the Free
Software Foundation, either version 3 of the License, or 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 Affero General Public License for details.
You should have received a copy of the GNU Affero General Public License
along with Ingen. If not, see .
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "ingen/Arc.hpp"
#include "ingen/Interface.hpp"
#include "ingen/Log.hpp"
#include "ingen/Node.hpp"
#include "ingen/Resource.hpp"
#include "ingen/Serialiser.hpp"
#include "ingen/Store.hpp"
#include "ingen/URIMap.hpp"
#include "ingen/URIs.hpp"
#include "ingen/World.hpp"
#include "lv2/lv2plug.in/ns/ext/state/state.h"
#include "lv2/lv2plug.in/ns/extensions/ui/ui.h"
#include "raul/Path.hpp"
#include "sord/sordmm.hpp"
#include "sratom/sratom.h"
using namespace std;
using namespace Sord;
using namespace Ingen;
namespace Ingen {
struct Serialiser::Impl {
explicit Impl(World& world)
: _root_path("/")
, _world(world)
, _model(NULL)
, _sratom(sratom_new(&_world.uri_map().urid_map_feature()->urid_map))
{}
~Impl() {
sratom_free(_sratom);
}
enum class Mode { TO_FILE, TO_STRING };
void start_to_file(const Raul::Path& root, const std::string& filename);
std::set serialise_graph(SPtr p,
const Sord::Node& id);
void serialise_block(SPtr n,
const Sord::Node& class_id,
const Sord::Node& id);
void serialise_port(SPtr p,
Resource::Graph context,
const Sord::Node& id);
void serialise_properties(Sord::Node id,
const Properties& props);
void write_bundle(SPtr graph,
const std::string& uri);
Sord::Node path_rdf_node(const Raul::Path& path) const;
Sord::Node rdf_node(SPtr node) const;
void write_manifest(const std::string& bundle_path,
SPtr graph);
void write_plugins(const std::string& bundle_path,
const std::set plugins);
void serialise_arc(const Sord::Node& parent,
SPtr a)
throw (std::logic_error);
std::string finish();
Raul::Path _root_path;
Mode _mode;
std::string _base_uri;
std::string _basename;
World& _world;
Sord::Model* _model;
Sratom* _sratom;
};
Serialiser::Serialiser(World& world)
: me(new Impl(world))
{}
Serialiser::~Serialiser()
{
delete me;
}
void
Serialiser::Impl::write_manifest(const std::string& bundle_path,
SPtr graph)
{
const string manifest_path(Glib::build_filename(bundle_path, "manifest.ttl"));
const string binary_path(Glib::Module::build_path("", "ingen_lv2"));
start_to_file(Raul::Path("/"), manifest_path);
Sord::World& world = _model->world();
const URIs& uris = _world.uris();
const string filename("main.ttl");
const Sord::URI subject(world, filename, _base_uri);
_model->add_statement(subject,
Sord::URI(world, uris.rdf_type),
Sord::URI(world, uris.ingen_Graph));
_model->add_statement(subject,
Sord::URI(world, uris.rdf_type),
Sord::URI(world, uris.lv2_Plugin));
_model->add_statement(subject,
Sord::URI(world, uris.rdfs_seeAlso),
Sord::URI(world, filename, _base_uri));
_model->add_statement(subject,
Sord::URI(world, uris.lv2_prototype),
Sord::URI(world, uris.ingen_GraphPrototype));
finish();
}
void
Serialiser::Impl::write_plugins(const std::string& bundle_path,
const std::set plugins)
{
const string plugins_path(Glib::build_filename(bundle_path, "plugins.ttl"));
start_to_file(Raul::Path("/"), plugins_path);
Sord::World& world = _model->world();
const URIs& uris = _world.uris();
for (const auto& p : plugins) {
const Atom& minor = p->get_property(uris.lv2_minorVersion);
const Atom& micro = p->get_property(uris.lv2_microVersion);
_model->add_statement(Sord::URI(world, p->uri()),
Sord::URI(world, uris.rdf_type),
Sord::URI(world, uris.lv2_Plugin));
if (minor.is_valid() && micro.is_valid()) {
_model->add_statement(Sord::URI(world, p->uri()),
Sord::URI(world, uris.lv2_minorVersion),
Sord::Literal::integer(world, minor.get()));
_model->add_statement(Sord::URI(world, p->uri()),
Sord::URI(world, uris.lv2_microVersion),
Sord::Literal::integer(world, micro.get()));
}
}
finish();
}
void
Serialiser::write_bundle(SPtr graph,
const std::string& path)
{
me->write_bundle(graph, path);
}
void
Serialiser::Impl::write_bundle(SPtr graph,
const std::string& a_path)
{
std::string path = Glib::filename_from_uri(a_path);
if (Glib::file_test(path, Glib::FILE_TEST_EXISTS)
&& !Glib::file_test(path, Glib::FILE_TEST_IS_DIR)) {
path = Glib::path_get_dirname(path);
}
_world.log().info(fmt("Writing bundle %1%\n") % path);
if (path.back() != '/')
path.append("/");
g_mkdir_with_parents(path.c_str(), 0744);
const string main_file = Glib::build_filename(path, "main.ttl");
const Raul::Path old_root_path = _root_path;
start_to_file(graph->path(), main_file);
std::set plugins = serialise_graph(
graph, Sord::URI(_model->world(), main_file, _base_uri));
finish();
write_manifest(path, graph);
write_plugins(path, plugins);
_root_path = old_root_path;
}
/** Begin a serialization to a file.
*
* This must be called before any serializing methods.
*/
void
Serialiser::Impl::start_to_file(const Raul::Path& root, const string& filename)
{
// Set Base URI
assert(filename.find(":") == string::npos || filename.substr(0, 5) == "file:");
if (filename.find(":") == string::npos) {
_base_uri = "file://" + filename;
} else {
_base_uri = filename;
}
// Find graph basename to use as symbol / fallback name
_basename = Glib::path_get_basename(filename);
if (_basename == "main.ttl") {
_basename = Glib::path_get_basename(Glib::path_get_dirname(filename));
}
_model = new Sord::Model(*_world.rdf_world(), _base_uri);
_mode = Mode::TO_FILE;
_root_path = root;
}
void
Serialiser::start_to_string(const Raul::Path& root, const string& base_uri)
{
me->_root_path = root;
me->_base_uri = base_uri;
me->_model = new Sord::Model(*me->_world.rdf_world(), base_uri);
me->_mode = Impl::Mode::TO_STRING;
}
void
Serialiser::start_to_file(const Raul::Path& root, const string& filename)
{
me->start_to_file(root, filename);
}
std::string
Serialiser::finish()
{
return me->finish();
}
std::string
Serialiser::Impl::finish()
{
string ret = "";
if (_mode == Mode::TO_FILE) {
SerdStatus st = _model->write_to_file(_base_uri, SERD_TURTLE);
if (st) {
_world.log().error(fmt("Error writing file %1% (%2%)\n")
% _base_uri % serd_strerror(st));
}
} else {
ret = _model->write_to_string(_base_uri, SERD_TURTLE);
}
delete _model;
_model = NULL;
_base_uri = "";
return ret;
}
Sord::Node
Serialiser::Impl::path_rdf_node(const Raul::Path& path) const
{
assert(_model);
assert(path == _root_path || path.is_child_of(_root_path));
return Sord::URI(_model->world(),
path.substr(_root_path.base().length()),
_base_uri);
}
Sord::Node
Serialiser::Impl::rdf_node(const SPtr node) const
{
return path_rdf_node(node->path());
}
void
Serialiser::serialise(SPtr object) throw (std::logic_error)
{
if (!me->_model)
throw std::logic_error("serialise called without serialisation in progress");
const URIs& uris = me->_world.uris();
if (object->has_property(uris.rdf_type, uris.ingen_Graph)) {
me->serialise_graph(object, me->rdf_node(object));
} else if (object->has_property(uris.rdf_type, uris.ingen_Block)) {
const Sord::URI plugin_id(me->_model->world(), object->plugin()->uri());
me->serialise_block(object, plugin_id, me->rdf_node(object));
} else if (object->has_property(uris.rdf_type, uris.lv2_InputPort) ||
object->has_property(uris.rdf_type, uris.lv2_OutputPort)) {
me->serialise_port(object,
Resource::Graph::DEFAULT,
me->rdf_node(object));
} else {
me->serialise_properties(me->rdf_node(object),
object->properties());
}
}
std::set
Serialiser::Impl::serialise_graph(SPtr graph,
const Sord::Node& graph_id)
{
Sord::World& world = _model->world();
const URIs& uris = _world.uris();
_model->add_statement(graph_id,
Sord::URI(world, uris.rdf_type),
Sord::URI(world, uris.ingen_Graph));
_model->add_statement(graph_id,
Sord::URI(world, uris.rdf_type),
Sord::URI(world, uris.lv2_Plugin));
_model->add_statement(graph_id,
Sord::URI(world, uris.lv2_extensionData),
Sord::URI(world, LV2_STATE__interface));
_model->add_statement(graph_id,
Sord::URI(world, LV2_UI__ui),
Sord::URI(world, "http://drobilla.net/ns/ingen#GraphUIGtk2"));
// If the graph has no doap:name (required by LV2), use the basename
if (graph->properties().find(uris.doap_name) == graph->properties().end()) {
const std::string sym = _basename.substr(0, _basename.find('.'));
_model->add_statement(graph_id,
Sord::URI(world, uris.doap_name),
Sord::Literal(world, sym));
}
const Properties props = graph->properties(Resource::Graph::INTERNAL);
serialise_properties(graph_id, props);
std::set plugins;
for (Store::const_iterator n = _world.store()->find_first_child(graph);
n != _world.store()->end() && n->first.is_child_of(graph->uri());
++n) {
if (uri_to_path(n->first).parent() != graph->path()) {
continue;
}
if (n->second->has_property(uris.rdf_type, uris.ingen_Graph)) {
const SPtr subgraph = n->second;
SerdURI base_uri;
serd_uri_parse((const uint8_t*)_base_uri.c_str(), &base_uri);
const string sub_bundle_path = subgraph->path().substr(1) + ".ingen";
SerdURI subgraph_uri;
SerdNode subgraph_node = serd_node_new_uri_from_string(
(const uint8_t*)sub_bundle_path.c_str(),
&base_uri,
&subgraph_uri);
const Sord::URI subgraph_id(world, (const char*)subgraph_node.buf);
// Save our state
std::string my_base_uri = _base_uri;
Sord::Model* my_model = _model;
// Write child bundle within this bundle
write_bundle(subgraph, subgraph_id.to_string());
// Restore our state
_base_uri = my_base_uri;
_model = my_model;
// Serialise reference to graph block
const Sord::Node block_id(rdf_node(subgraph));
_model->add_statement(graph_id,
Sord::URI(world, uris.ingen_block),
block_id);
serialise_block(subgraph, subgraph_id, block_id);
} else if (n->second->has_property(uris.rdf_type, uris.ingen_Block)) {
const SPtr block = n->second;
const Sord::URI class_id(world, block->plugin()->uri());
const Sord::Node block_id(rdf_node(n->second));
_model->add_statement(graph_id,
Sord::URI(world, uris.ingen_block),
block_id);
serialise_block(block, class_id, block_id);
plugins.insert(block->plugin());
} else if (n->second->has_property(uris.rdf_type, uris.lv2_InputPort) ||
n->second->has_property(uris.rdf_type, uris.lv2_OutputPort)) {
const SPtr p = n->second;
// Ensure lv2:name always exists so Graph is a valid LV2 plugin
const Sord::Node port_id(rdf_node(p));
if (p->properties().find(uris.lv2_name) == p->properties().end())
_model->add_statement(port_id,
Sord::URI(world, uris.lv2_name),
Sord::Literal(world, p->symbol()));
_model->add_statement(graph_id,
Sord::URI(world, LV2_CORE__port),
port_id);
serialise_port(p, Resource::Graph::DEFAULT, port_id);
serialise_port(p, Resource::Graph::INTERNAL, port_id);
} else {
_world.log().warn(fmt("<%1%> has no known type\n")
% n->second->uri());
}
}
for (const auto& a : graph->arcs()) {
serialise_arc(graph_id, a.second);
}
return plugins;
}
void
Serialiser::Impl::serialise_block(SPtr block,
const Sord::Node& class_id,
const Sord::Node& block_id)
{
const URIs& uris = _world.uris();
_model->add_statement(block_id,
Sord::URI(_model->world(), uris.rdf_type),
Sord::URI(_model->world(), uris.ingen_Block));
_model->add_statement(block_id,
Sord::URI(_model->world(), uris.lv2_prototype),
class_id);
// Serialise properties, but remove possibly stale state:state (set again below)
Properties props = block->properties();
props.erase(uris.state_state);
serialise_properties(block_id, props);
if (_base_uri.substr(0, 5) == "file:") {
const std::string base = Glib::filename_from_uri(_base_uri);
const std::string graph_dir = Glib::path_get_dirname(base);
const std::string state_dir = Glib::build_filename(graph_dir, block->symbol());
const std::string state_file = Glib::build_filename(state_dir, "state.ttl");
if (block->save_state(state_dir)) {
_model->add_statement(block_id,
Sord::URI(_model->world(), uris.state_state),
Sord::URI(_model->world(), Glib::filename_to_uri(state_file)));
}
}
for (Store::const_iterator n = _world.store()->find_first_child(block);
n != _world.store()->end() && n->first.is_child_of(block->uri());
++n) {
if (uri_to_path(n->first).parent() != block->path()) {
continue;
}
if (n->second->has_property(uris.rdf_type, uris.lv2_InputPort) ||
n->second->has_property(uris.rdf_type, uris.lv2_InputPort)) {
const SPtr p = n->second;
const Sord::Node port_id = rdf_node(p);
serialise_port(p, Resource::Graph::DEFAULT, port_id);
_model->add_statement(block_id,
Sord::URI(_model->world(), uris.lv2_port),
port_id);
}
}
}
void
Serialiser::Impl::serialise_port(SPtr port,
Resource::Graph context,
const Sord::Node& port_id)
{
URIs& uris = _world.uris();
Sord::World& world = _model->world();
Properties props = port->properties(context);
if (context == Resource::Graph::INTERNAL) {
// Always write lv2:symbol for Graph ports (required for lv2:Plugin)
_model->add_statement(port_id,
Sord::URI(world, uris.lv2_symbol),
Sord::Literal(world, port->path().symbol()));
} else if (context == Resource::Graph::EXTERNAL) {
// Never write lv2:index for plugin instances (not persistent/stable)
props.erase(uris.lv2_index);
}
if (context == Resource::Graph::INTERNAL &&
port->has_property(uris.rdf_type, uris.lv2_ControlPort) &&
port->has_property(uris.rdf_type, uris.lv2_InputPort))
{
const Atom& val = port->get_property(uris.ingen_value);
if (val.is_valid()) {
props.erase(uris.lv2_default);
props.insert(make_pair(uris.lv2_default, val));
} else {
_world.log().warn("Control input has no value, lv2:default omitted.\n");
}
} else if (context != Resource::Graph::INTERNAL &&
!port->has_property(uris.rdf_type, uris.lv2_InputPort)) {
props.erase(uris.ingen_value);
}
serialise_properties(port_id, props);
}
void
Serialiser::serialise_arc(const Sord::Node& parent,
SPtr arc)
throw (std::logic_error)
{
return me->serialise_arc(parent, arc);
}
void
Serialiser::Impl::serialise_arc(const Sord::Node& parent,
SPtr arc)
throw (std::logic_error)
{
if (!_model)
throw std::logic_error(
"serialise_arc called without serialisation in progress");
Sord::World& world = _model->world();
const URIs& uris = _world.uris();
const Sord::Node src = path_rdf_node(arc->tail_path());
const Sord::Node dst = path_rdf_node(arc->head_path());
const Sord::Node arc_id = Sord::Node::blank_id(*_world.rdf_world());
_model->add_statement(arc_id,
Sord::URI(world, uris.ingen_tail),
src);
_model->add_statement(arc_id,
Sord::URI(world, uris.ingen_head),
dst);
if (parent.is_valid()) {
_model->add_statement(parent,
Sord::URI(world, uris.ingen_arc),
arc_id);
} else {
_model->add_statement(arc_id,
Sord::URI(world, uris.rdf_type),
Sord::URI(world, uris.ingen_Arc));
}
}
static bool
skip_property(Ingen::URIs& uris, const Sord::Node& predicate)
{
return (predicate.to_string() == INGEN__file ||
predicate.to_string() == uris.ingen_arc ||
predicate.to_string() == uris.ingen_block ||
predicate.to_string() == uris.lv2_port);
}
void
Serialiser::Impl::serialise_properties(Sord::Node id,
const Properties& props)
{
LV2_URID_Unmap* unmap = &_world.uri_map().urid_unmap_feature()->urid_unmap;
SerdNode base = serd_node_from_string(SERD_URI,
(const uint8_t*)_base_uri.c_str());
SerdEnv* env = serd_env_new(&base);
SordInserter* inserter = sord_inserter_new(_model->c_obj(), env);
sratom_set_sink(_sratom, _base_uri.c_str(),
(SerdStatementSink)sord_inserter_write_statement, NULL,
inserter);
sratom_set_pretty_numbers(_sratom, true);
for (const auto& p : props) {
const Sord::URI key(_model->world(), p.first);
if (!skip_property(_world.uris(), key)) {
if (p.second.type() == _world.uris().atom_URI &&
!strncmp((const char*)p.second.get_body(), "ingen:/main/", 13)) {
/* Value is a graph URI relative to the running engine.
Chop the prefix and save the path relative to the graph file.
This allows saving references to bundle resources. */
sratom_write(_sratom, unmap, 0,
sord_node_to_serd_node(id.c_obj()),
sord_node_to_serd_node(key.c_obj()),
p.second.type(), p.second.size(),
(const char*)p.second.get_body() + 13);
} else {
sratom_write(_sratom, unmap, 0,
sord_node_to_serd_node(id.c_obj()),
sord_node_to_serd_node(key.c_obj()),
p.second.type(), p.second.size(), p.second.get_body());
}
}
}
sord_inserter_free(inserter);
serd_env_free(env);
}
} // namespace Ingen