diff options
Diffstat (limited to 'src/serialisation/Parser.cpp')
-rw-r--r-- | src/serialisation/Parser.cpp | 341 |
1 files changed, 190 insertions, 151 deletions
diff --git a/src/serialisation/Parser.cpp b/src/serialisation/Parser.cpp index 06922653..059a9bfd 100644 --- a/src/serialisation/Parser.cpp +++ b/src/serialisation/Parser.cpp @@ -86,17 +86,37 @@ relative_uri(Glib::ustring base, const Glib::ustring uri, bool leading_slash) return ret; } +static std::string +get_basename(const std::string& uri) +{ + std::string ret = Glib::path_get_basename(uri); + ret = ret.substr(0, ret.find_last_of('.')); + return ret; +} + +static bool +skip_property(const Sord::Node& predicate) +{ + return (predicate.to_string() == "http://drobilla.net/ns/ingen#node" + || predicate.to_string() == "http://drobilla.net/ns/ingen#connection" + || predicate.to_string() == "http://lv2plug.in/ns/lv2core#port"); +} + +Parser::Parser(Ingen::Shared::World& world) +{ +} + Parser::PatchRecords Parser::find_patches(Ingen::Shared::World* world, const Glib::ustring& manifest_uri) { + const Sord::URI ingen_Patch (*world->rdf_world(), NS_INGEN "Patch"); + const Sord::URI rdf_type (*world->rdf_world(), NS_RDF "type"); + const Sord::URI rdfs_seeAlso(*world->rdf_world(), NS_RDFS "seeAlso"); + Sord::Model model(*world->rdf_world(), manifest_uri); model.load_file(manifest_uri); - Sord::URI rdf_type(*world->rdf_world(), NS_RDF "type"); - Sord::URI rdfs_seeAlso(*world->rdf_world(), NS_RDFS "seeAlso"); - Sord::URI ingen_Patch(*world->rdf_world(), NS_INGEN "Patch"); - std::list<PatchRecord> records; for (Sord::Iter i = model.find(nil, rdf_type, ingen_Patch); !i.end(); ++i) { const Sord::Node patch = i.get_subject(); @@ -204,13 +224,14 @@ Parser::parse(Ingen::Shared::World* world, boost::optional<Raul::Symbol> symbol, boost::optional<GraphObject::Properties> data) { - const Sord::URI rdf_type (*world->rdf_world(), NS_RDF "type"); - const Sord::URI patch_class (*world->rdf_world(), NS_INGEN "Patch"); - const Sord::URI node_class (*world->rdf_world(), NS_INGEN "URI"); - const Sord::URI internal_class (*world->rdf_world(), NS_INGEN "Internal"); - const Sord::URI in_port_class (*world->rdf_world(), NS_LV2 "InputPort"); - const Sord::URI out_port_class (*world->rdf_world(), NS_LV2 "OutputPort"); - const Sord::URI lv2_class (*world->rdf_world(), NS_LV2 "Plugin"); + const Sord::URI patch_class (*world->rdf_world(), NS_INGEN "Patch"); + const Sord::URI node_class (*world->rdf_world(), NS_INGEN "Node"); + const Sord::URI port_class (*world->rdf_world(), NS_INGEN "Port"); + const Sord::URI internal_class(*world->rdf_world(), NS_INGEN "Internal"); + const Sord::URI in_port_class (*world->rdf_world(), NS_LV2 "InputPort"); + const Sord::URI out_port_class(*world->rdf_world(), NS_LV2 "OutputPort"); + const Sord::URI lv2_class (*world->rdf_world(), NS_LV2 "Plugin"); + const Sord::URI rdf_type (*world->rdf_world(), NS_RDF "type"); Sord::Node subject = nil; if (data_path && data_path->is_root()) { @@ -228,23 +249,40 @@ Parser::parse(Ingen::Shared::World* world, path = parent->child(*symbol); } - boost::optional<Path> ret; - boost::optional<Path> root_path; + // Get all subjects and their types (?subject a ?type) + typedef std::map< Sord::Node, std::set<Sord::Node> > Subjects; + Subjects subjects; for (Sord::Iter i = model.find(subject, rdf_type, nil); !i.end(); ++i) { const Sord::Node& subject = i.get_subject(); const Sord::Node& rdf_class = i.get_object(); - if (rdf_class == patch_class) { + Subjects::iterator s = subjects.find(subject); + if (s == subjects.end()) { + std::set<Sord::Node> types; + types.insert(rdf_class); + subjects.insert(make_pair(subject, types)); + } else { + s->second.insert(rdf_class); + } + } + + // Parse and create each subject + boost::optional<Path> ret; + boost::optional<Path> root_path; + for (Subjects::const_iterator i = subjects.begin(); i != subjects.end(); ++i) { + const Sord::Node& subject = i->first; + const std::set<Sord::Node>& types = i->second; + if (types.find(patch_class) != types.end()) { ret = parse_patch(world, target, model, subject, parent, symbol, data); - } else if (rdf_class == node_class) { + } else if (types.find(node_class) != types.end()) { ret = parse_node(world, target, model, subject, path, data); - } else if (rdf_class == in_port_class || rdf_class == out_port_class) { + } else if (types.find(port_class) != types.end()) { parse_properties(world, target, model, subject, path, data); ret = path; } - + if (!ret) { - LOG(error) << "Failed to parse object " << path << endl; + LOG(error) << "Failed to parse " << path << endl; return boost::optional<Path>(); } @@ -256,6 +294,60 @@ Parser::parse(Ingen::Shared::World* world, return path; } +static Resource::Properties +get_properties(Sord::Model& model, + const Sord::Node& subject) +{ + Resource::Properties props; + for (Sord::Iter i = model.find(subject, nil, nil); !i.end(); ++i) { + const Sord::Node& predicate = i.get_predicate(); + const Sord::Node& object = i.get_object(); + if (!skip_property(predicate)) { + props.insert( + make_pair(predicate.to_string(), + AtomRDF::node_to_atom(model, object))); + } + } + return props; +} + +typedef std::pair<Path, Resource::Properties> PortRecord; + +static int +get_port(Ingen::Shared::World* world, + Sord::Model& model, + const Sord::Node& subject, + const Raul::Path& parent, + PortRecord& record) +{ + const LV2URIMap& uris = *world->uris().get(); + + // Get all properties + Resource::Properties props = get_properties(model, subject); + + // Get index + Resource::Properties::const_iterator i = props.find(uris.lv2_index); + if (i == props.end() + || i->second.type() != Atom::INT + || i->second.get_int32() < 0) { + LOG(error) << "Port " << subject << " has no valid lv2:index" << endl; + return -1; + } + const uint32_t index = i->second.get_int32(); + + // Get symbol + Resource::Properties::const_iterator s = props.find(uris.lv2_symbol); + if (s == props.end()) { + LOG(error) << "Port " << subject << " has no symbol" << endl; + return -1; + } + const Symbol port_sym = s->second.get_string(); + const Path port_path = parent.child(port_sym); + + record = make_pair(port_path, props); + return index; +} + boost::optional<Path> Parser::parse_patch(Ingen::Shared::World* world, Ingen::Shared::CommonInterface* target, @@ -265,24 +357,23 @@ Parser::parse_patch(Ingen::Shared::World* world, boost::optional<Raul::Symbol> a_symbol, boost::optional<GraphObject::Properties> data) { - const LV2URIMap& uris = *world->uris().get(); - - Sord::URI ingen_polyphony(*world->rdf_world(), NS_INGEN "polyphony"); - Sord::URI lv2_port(*world->rdf_world(), NS_LV2 "port"); - Sord::URI lv2_symbol(*world->rdf_world(), NS_LV2 "symbol"); + const Sord::URI ingen_node(*world->rdf_world(), NS_INGEN "node"); + const Sord::URI ingen_polyphony(*world->rdf_world(), NS_INGEN "polyphony"); + const Sord::URI lv2_port(*world->rdf_world(), NS_LV2 "port"); + const LV2URIMap& uris = *world->uris().get(); const Sord::Node& patch = subject_node; uint32_t patch_poly = 0; - /* Use parameter overridden polyphony, if given */ + // Use parameter overridden polyphony, if given if (data) { GraphObject::Properties::iterator poly_param = data.get().find(uris.ingen_polyphony); if (poly_param != data.get().end() && poly_param->second.type() == Atom::INT) patch_poly = poly_param->second.get_int32(); } - /* Load polyphony from file if necessary */ + // Load polyphony from file if necessary if (patch_poly == 0) { Sord::Iter i = model.find(subject_node, ingen_polyphony, nil); if (!i.end()) { @@ -294,7 +385,7 @@ Parser::parse_patch(Ingen::Shared::World* world, } } - /* No polyphony found anywhere, use 1 */ + // No polyphony found anywhere, use 1 if (patch_poly == 0) patch_poly = 1; @@ -304,8 +395,7 @@ Parser::parse_patch(Ingen::Shared::World* world, if (a_symbol) { symbol = *a_symbol; } else { - const std::string basename = Glib::path_get_basename(base_uri); - symbol = Raul::Symbol::symbolify(basename.substr(0, basename.find('.'))); + const std::string basename = get_basename(base_uri); } string patch_path_str = relative_uri(base_uri, subject_node.to_string(), true); @@ -317,132 +407,63 @@ Parser::parse_patch(Ingen::Shared::World* world, return boost::optional<Raul::Path>(); } - /* Create patch */ + // Create patch Path patch_path(patch_path_str); - Resource::Properties props; - props.insert(make_pair(uris.rdf_type, Raul::URI(uris.ingen_Patch))); - props.insert(make_pair(uris.ingen_polyphony, Raul::Atom(int32_t(patch_poly)))); + Resource::Properties props = get_properties(model, subject_node); target->put(patch_path, props); - Sord::URI rdf_type(*world->rdf_world(), NS_RDF "type"); - Sord::URI ingen_Patch(*world->rdf_world(), NS_INGEN "Patch"); - Sord::URI ingen_node(*world->rdf_world(), NS_INGEN "node"); - - typedef multimap<Raul::URI, Raul::Atom> Properties; - typedef map<string, Sord::Node> Resources; - typedef map<string, Properties> Objects; - typedef map<string, string> Types; - - Objects patch_nodes; - Objects plugin_nodes; - Resources resources; - Types types; - - /* For each node in this patch */ - typedef map<Sord::Node, Properties> Nodes; - Nodes nodes; + // For each node in this patch for (Sord::Iter n = model.find(subject_node, ingen_node, nil); !n.end(); ++n) { - Sord::Node node = n.get_object(); - - /* Get all node properties */ - Properties node_properties; - for (Sord::Iter np = model.find(node, nil, nil); !np.end(); ++np) { - const Sord::Node& predicate = np.get_predicate(); - const Sord::Node& object = np.get_object(); - if (!skip_property(predicate)) { - node_properties.insert( - make_pair(predicate.to_string(), - AtomRDF::node_to_atom(model, object))); - } - } + Sord::Node node = n.get_object(); + const Path node_path = patch_path.child(get_basename(node.to_string())); - /* Create node */ - const Path node_path = patch_path.child( - relative_uri(base_uri, node.to_string(), false)); - target->put(node_path, node_properties); + // Parse and create node + parse_node(world, target, model, node, node_path, + boost::optional<GraphObject::Properties>()); - /* For each port on this node */ + // For each port on this node for (Sord::Iter p = model.find(node, lv2_port, nil); !p.end(); ++p) { Sord::Node port = p.get_object(); - /* Get all port properties */ - Properties port_properties; - for (Sord::Iter pp = model.find(port, nil, nil); !pp.end(); ++pp) { - const Sord::Node& predicate = pp.get_predicate(); - const Sord::Node& object = pp.get_object(); - if (!skip_property(predicate)) { - port_properties.insert( - make_pair(predicate.to_string(), - AtomRDF::node_to_atom(model, object))); - } - } - - /* Set port properties */ - Properties::const_iterator s = port_properties.find(uris.lv2_symbol); - if (s == port_properties.end()) { - LOG(error) << "Port on " << node_path << " has no symbol" << endl; + // Get all properties + PortRecord port_record; + const int index = get_port(world, model, port, node_path, port_record); + if (index < 0) { + LOG(error) << "Invalid port " << port << endl; return boost::optional<Path>(); } - const Symbol port_sym = s->second.get_string(); - const Path port_path = node_path.child(port_sym); - target->put(port_path, port_properties); + // Create port and/or set all port properties + target->put(port_record.first, port_record.second); } } - /* For each port on this patch */ + // For each port on this patch + typedef std::map<uint32_t, PortRecord> PortRecords; + PortRecords ports; for (Sord::Iter p = model.find(patch, lv2_port, nil); !p.end(); ++p) { Sord::Node port = p.get_object(); - /* Get all port properties */ - Properties port_properties; - for (Sord::Iter pp = model.find(port, nil, nil); !pp.end(); ++pp) { - const Sord::Node& predicate = pp.get_predicate(); - const Sord::Node& object = pp.get_object(); - if (!skip_property(predicate)) { - port_properties.insert( - make_pair(predicate.to_string(), - AtomRDF::node_to_atom(model, object))); - } - } - - /* Set port properties */ - Properties::const_iterator s = port_properties.find(uris.lv2_symbol); - if (s == port_properties.end()) { - LOG(error) << "Port on " << patch_path << " has no symbol" << endl; + // Get all properties + PortRecord port_record; + const int index = get_port(world, model, port, patch_path, port_record); + if (index < 0) { + LOG(error) << "Invalid port " << port << endl; return boost::optional<Path>(); } - const Symbol port_sym = s->second.get_string(); - const Path port_path = patch_path.child(port_sym); - target->put(port_path, port_properties); + // Store port information in ports map + ports[index] = port_record; } - parse_properties(world, target, model, subject_node, patch_path, data); + // Create ports in order by index + for (PortRecords::const_iterator i = ports.begin(); i != ports.end(); ++i) { + target->put(i->second.first, i->second.second); + } + + //parse_properties(world, target, model, subject_node, patch_path, data); parse_connections(world, target, model, subject_node, patch_path); - cerr << "FIXME: enable patch" << endl; - target->set_property(patch_path, uris.ingen_enabled, (bool)true); -#if 0 - /* Enable */ - query = Sord::Query(*world->rdf_world(), Glib::ustring( - "SELECT DISTINCT ?enabled WHERE {\n") - + subject + " ingen:enabled ?enabled .\n" - "}"); - - results = query.run(*world->rdf_world(), model, base_uri); - for (; !results->finished(); results->next()) { - Glib::Mutex::Lock lock(world->rdf_world()->mutex()); - const Sord::Node& enabled_node = results->get("enabled"); - if (enabled_node.is_bool() && enabled_node) { - target->set_property(patch_path, uris.ingen_enabled, (bool)true); - break; - } else { - LOG(warn) << "Unknown type for ingen:enabled" << endl; - } - } -#endif - return patch_path; } @@ -458,27 +479,52 @@ Parser::parse_node(Ingen::Shared::World* world, Sord::URI rdf_instanceOf(*world->rdf_world(), NS_RDF "instanceOf"); - /* Get plugin */ Sord::Iter i = model.find(subject, rdf_instanceOf, nil); - if (i.end()) { - LOG(error) << "Node missing mandatory rdf:instanceOf property" << endl; + if (i.end() || i.get_object().type() != Sord::Node::URI) { + LOG(error) << "Node missing mandatory rdf:instanceOf" << endl; return boost::optional<Path>(); } - const Sord::Node& plugin_node = i.get_object(); - if (plugin_node.type() != Sord::Node::URI) { - LOG(error) << "Node's rdf:instanceOf property is not a resource" << endl; - return boost::optional<Path>(); - } + const std::string type_uri = relative_uri( + model.base_uri().to_string(), + i.get_object().to_string(), + false); - Resource::Properties props; - props.insert(make_pair(uris.rdf_type, - Raul::URI(uris.ingen_Node))); - props.insert(make_pair(uris.rdf_instanceOf, - AtomRDF::node_to_atom(model, plugin_node))); - target->put(path, props); + if (!serd_uri_string_has_scheme((const uint8_t*)type_uri.c_str())) { + SerdURI base_uri; + serd_uri_parse(model.base_uri().to_u_string(), &base_uri); + + SerdURI ignored; + SerdNode sub_uri = serd_node_new_uri_from_string( + i.get_object().to_u_string(), + &base_uri, + &ignored); + + const std::string sub_uri_str((const char*)sub_uri.buf); + std::string basename = get_basename(sub_uri_str); + + const std::string sub_file = + string((const char*)sub_uri.buf) + "/" + basename + ".ttl"; + + Sord::Model sub_model(*world->rdf_world(), sub_file); + sub_model.load_file(sub_file); + + Sord::URI sub_node(*world->rdf_world(), sub_file); + parse_patch(world, target, sub_model, sub_node, + path.parent(), Raul::Symbol(path.symbol())); + + cout << "DONE PARSING SUB PATCH FILE" << endl; - parse_properties(world, target, model, subject, path, data); + parse_patch(world, target, model, subject, + path.parent(), Raul::Symbol(path.symbol())); + + cout << "} SUB PATCH" << endl; + } else { + Resource::Properties props = get_properties(model, subject); + props.insert(make_pair(uris.rdf_type, + Raul::URI(uris.ingen_Node))); + target->put(path, props); + } return path; } @@ -558,13 +604,6 @@ Parser::parse_properties(Ingen::Shared::World* world, return true; } -bool -Parser::skip_property(const Sord::Node& predicate) -{ - return (predicate.to_string() == "http://drobilla.net/ns/ingen#node" - || predicate.to_string() == "http://lv2plug.in/ns/lv2core#port"); -} - } // namespace Serialisation } // namespace Ingen |