diff options
Diffstat (limited to 'src/libs/client')
-rw-r--r-- | src/libs/client/Serializer.cpp | 44 | ||||
-rw-r--r-- | src/libs/client/Serializer.hpp | 26 |
2 files changed, 53 insertions, 17 deletions
diff --git a/src/libs/client/Serializer.cpp b/src/libs/client/Serializer.cpp index d662b83b..be468202 100644 --- a/src/libs/client/Serializer.cpp +++ b/src/libs/client/Serializer.cpp @@ -57,6 +57,25 @@ Serializer::Serializer(Raul::RDF::World& world) : _world(world) { } + +void +Serializer::to_file(SharedPtr<ObjectModel> object, const string& filename) +{ + _root_object = object; + start_to_filename(filename); + serialize(object); + finish(); +} + + +string +Serializer::to_string(SharedPtr<ObjectModel> object) +{ + _root_object = object; + start_to_string(); + serialize(object); + return finish(); +} /** Begin a serialization to a file. @@ -200,7 +219,7 @@ Serializer::serialize(SharedPtr<ObjectModel> object) throw (std::logic_error) SharedPtr<PatchModel> patch = PtrCast<PatchModel>(object); if (patch) { - serialize_patch(patch, Node(_model->world(), Node::RESOURCE, _base_uri)); + serialize_patch(patch); return; } @@ -221,11 +240,26 @@ Serializer::serialize(SharedPtr<ObjectModel> object) throw (std::logic_error) } +Node +Serializer::patch_path_to_rdf_id(const Path& path) +{ + if (path == _root_object->path()) { + return Node(_model->world(), Node::RESOURCE, _base_uri); + } else { + assert(path.length() > _root_object->path().length()); + return Node(_model->world(), Node::RESOURCE, + _base_uri + string("#") + path.substr(_root_object->path().length() + 1)); + } +} + + void -Serializer::serialize_patch(SharedPtr<PatchModel> patch, const Node& patch_id) +Serializer::serialize_patch(SharedPtr<PatchModel> patch) { assert(_model); + const Node patch_id = patch_path_to_rdf_id(patch->path()); + _model->add_statement( patch_id, "rdf:type", @@ -255,10 +289,8 @@ Serializer::serialize_patch(SharedPtr<PatchModel> patch, const Node& patch_id) SharedPtr<PatchModel> patch = PtrCast<PatchModel>(n->second); SharedPtr<NodeModel> node = PtrCast<NodeModel>(n->second); if (patch) { - const Node subpatch_id = Node(_model->world(), Node::RESOURCE, - patch_id.to_string() + "#" + patch->path().substr(1)); - _model->add_statement(patch_id, "ingen:node", subpatch_id); - serialize_patch(patch, subpatch_id); + _model->add_statement(patch_id, "ingen:node", patch_path_to_rdf_id(patch->path())); + serialize_patch(patch); } else if (node) { const Node node_id = path_to_node_id(n->second->path()); _model->add_statement(patch_id, "ingen:node", node_id); diff --git a/src/libs/client/Serializer.hpp b/src/libs/client/Serializer.hpp index 23f93df6..bb3b0e8a 100644 --- a/src/libs/client/Serializer.hpp +++ b/src/libs/client/Serializer.hpp @@ -51,34 +51,38 @@ class Serializer public: Serializer(Raul::RDF::World& world); - void start_to_filename(const string& filename); + void to_file(SharedPtr<ObjectModel> object, const string& filename); + string to_string(SharedPtr<ObjectModel> object); + void start_to_string(); - - void serialize(SharedPtr<ObjectModel> object) throw (std::logic_error); + void serialize(SharedPtr<ObjectModel> object) throw (std::logic_error); void serialize_connection(SharedPtr<ConnectionModel> c) throw (std::logic_error); - string finish(); - + private: enum Mode { TO_FILE, TO_STRING }; + + void start_to_filename(const string& filename); void setup_prefixes(); void serialize_plugin(SharedPtr<PluginModel> p); - void serialize_patch(SharedPtr<PatchModel> p, const Raul::RDF::Node& id); + void serialize_patch(SharedPtr<PatchModel> p); void serialize_node(SharedPtr<NodeModel> n, const Raul::RDF::Node& id); void serialize_port(SharedPtr<PortModel> p, const Raul::RDF::Node& id); Raul::RDF::Node path_to_node_id(const Path& path); + Raul::RDF::Node patch_path_to_rdf_id(const Path& path); typedef Raul::PathTable<Raul::RDF::Node> NodeMap; - Mode _mode; - NodeMap _node_map; - string _base_uri; - Raul::RDF::World& _world; - Raul::RDF::Model* _model; + SharedPtr<ObjectModel> _root_object; + Mode _mode; + NodeMap _node_map; + string _base_uri; + Raul::RDF::World& _world; + Raul::RDF::Model* _model; }; |