← Back to team overview

wintermute-devel team mailing list archive

[Merge] lp:~gentoolx/wintermute/database-xml_serialization into lp:~wintermute-devel/wintermute/database

 

Adrian Borucki has proposed merging lp:~gentoolx/wintermute/database-xml_serialization into lp:~wintermute-devel/wintermute/database.

Requested reviews:
  Wintermute Developers (wintermute-devel)

For more details, see:
https://code.launchpad.net/~gentoolx/wintermute/database-xml_serialization/+merge/62083

This branch contains a lot of changes to the logic and code of WntrData library. The general change is erase of Node* classes and providing LocalLexical class as an equivalent of save/load usability. JSON handling is temporary disabled (code deleted) because proper wrapping into Boost archive must be done (boost doesn't provide its own JSON serialization archives). The logic change involves creating the set of abstract classes (models.hpp file) for better type management and easier code specification in the future. 

XML serialization is fully working now.
-- 
https://code.launchpad.net/~gentoolx/wintermute/database-xml_serialization/+merge/62083
Your team Wintermute Developers is requested to review the proposed merge of lp:~gentoolx/wintermute/database-xml_serialization into lp:~wintermute-devel/wintermute/database.
=== modified file 'CMakeLists.txt'
--- CMakeLists.txt	2011-05-24 05:06:27 +0000
+++ CMakeLists.txt	2011-05-24 07:35:57 +0000
@@ -13,7 +13,6 @@
 #
 # Dependency packages 
 #
-find_package(LibXML++ REQUIRED)
 find_package(Boost COMPONENTS
     filesystem
     signals
@@ -31,9 +30,7 @@
     ${Boost_INCLUDE_DIRS})
 set(WNTRDATA_CMAKE_DIR "${CMAKE_INSTALL_PREFIX}/share/cmake/WntrData")
 set(WNTRDATA_LIBRARIES 
-    ${LibXML++_LIBRARIES}
-    ${Boost_LIBRARIES}
-    jsoncpp)
+    ${Boost_LIBRARIES})
 #- Checking the build type
 if(CMAKE_BUILD_TYPE EQUAL Debug)
     set(DEBUG yes)

=== added file 'config.hpp.in.OTHER'
--- config.hpp.in.OTHER	1970-01-01 00:00:00 +0000
+++ config.hpp.in.OTHER	2011-05-24 07:35:57 +0000
@@ -0,0 +1,15 @@
+/** File providing compile-time data about runtime-configuration.
+ */
+#ifndef __CONFIG_HPP__
+#define __CONFIG_HPP__
+
+#cmakedefine DEBUG
+
+#define __WDATA_CONFIG_DIR__ "@WNTRDATA_CONFIG_DIR@"
+#define __WDATA_DATA_DIR__ "@WNTRDATA_DATA_DIR@"
+#define __WDATA_STORAGE_DIR__ "@WNTRDATA_STORAGE_DIR@"
+
+#define __WDATA_SERIAL_BACKEND__ "@WNTRDATA_SERIALIZATION_BACKEND@"
+
+#endif // __CONFIG_HPP__
+ 

=== modified file 'include/wntr/data/linguistics.hpp'
--- include/wntr/data/linguistics.hpp	2011-05-24 05:06:27 +0000
+++ include/wntr/data/linguistics.hpp	2011-05-24 07:35:57 +0000
@@ -23,16 +23,27 @@
 #ifndef LINGUISTICS_HPP
 #define	 LINGUISTICS_HPP
 
-#include <boost/signals.hpp>
+#include <boost/archive/xml_oarchive.hpp>
+#include <boost/archive/xml_iarchive.hpp>
+
+#include <boost/signals2.hpp>
 #include <boost/serialization/nvp.hpp>
+#include <boost/serialization/string.hpp>
+#include <boost/serialization/map.hpp>
 
+<<<<<<< TREE
 #include <jsoncpp/json.hpp>
+=======
+#include <boost/filesystem.hpp>
+>>>>>>> MERGE-SOURCE
 
 #include <string>
 #include <vector>
 #include <map>
 #include <stdexcept>
 
+#include "models.hpp"
+
 using namespace std;
 using std::string;
 using std::vector;
@@ -40,6 +51,7 @@
 using std::map;
 
 namespace Wintermute {
+<<<<<<< TREE
 	namespace Data {
 		namespace Linguistics {
 			class Configuration;
@@ -73,21 +85,61 @@
 			 */
 			typedef NodeIOExistsSignal::slot_type NodeIOExistsSlot;
 			/**
+=======
+    namespace Data {
+        namespace Linguistics {
+            class Configuration;
+
+            /**
+             * Signal type intended for checking data existence.
+             */
+            typedef boost::signals2::signal<bool (const string&, const string&)> NodeExistsSignal;
+            
+            /**
+             * Signal type intended for handling data.
+             */
+            typedef boost::signals2::signal<Lexical* (const string&, const string&)> NodeHandleSignal;
+            
+            /**
+             * Slot of the NodeExistsSignal.
+             * @see NodeExistsSignal
+             */
+            typedef NodeExistsSignal::slot_type NodeExistsSlot;
+            
+            /**
+>>>>>>> MERGE-SOURCE
 			 * Slot of the NodeIOHandleSignal.
 			 * @see NodeIOHandleSignal
+<<<<<<< TREE
 			 */
 			typedef NodeIOHandleSignal::slot_type NodeIOHandleSlot;
 			/**
+=======
+             */
+            typedef NodeHandleSignal::slot_type NodeHandleSlot;
+            
+            /**
+>>>>>>> MERGE-SOURCE
 			 * Pair of pointers to signals used to operating on Nodes.
 			 * @see Node
+<<<<<<< TREE
 			 * @see NodeIOExistsSignal
 			 * @see NodeIOHandleSignal
 			 */
 			typedef pair< NodeIOExistsSignal* , NodeIOHandleSignal* >
 					NodeInteroperingSignalPair;
 			/**
+=======
+			 * @see NodeExistsSignal
+			 * @see NodeHandleSignal
+             */
+            typedef pair<NodeExistsSignal*, NodeHandleSignal*> NodeInteroperingSignalPair;
+            
+            /**
+>>>>>>> MERGE-SOURCE
 			 * Vector of NodeInteroperingSignalPair.
 			 * @see NodeInteroperingSignalPair
+<<<<<<< TREE
 			 */
 			typedef vector< NodeInteroperingSignalPair >
 					NodeInteroperingSignalVector;
@@ -95,7 +147,15 @@
 
 			/**
 			 * Synonym for the LocalData.
+=======
+             */
+            typedef vector<NodeInteroperingSignalPair> NodeInteroperingSignalVector;
+			
+            /**
+			 * Synonym for the LocalModel.
+>>>>>>> MERGE-SOURCE
 			 * @see LocalNode
+<<<<<<< TREE
 			 */
 			typedef LocalNode LocalData;
 
@@ -645,5 +705,209 @@
 			};
 		}
 	}
+=======
+             */
+            typedef LocalModel LocalData;
+        
+            /**
+             * An abstract static class representing the current configuration of the linguistics data.
+             */
+            class Configuration 
+            {
+            private:
+                friend class boost::serialization::access;
+                
+                /* Configuration directory and locale variables */
+                static string _configDir;
+                static string _storageDir;
+                static string _locale;
+
+                /* Serialization backend variables */
+                static string _serialBackend;
+
+                template<class Archive>
+                void serialize(Archive& ar, unsigned int const version);
+                
+                Configuration();
+                ~Configuration();
+
+            public:
+                /**
+                 * Initializes the system by running prerequisite code.
+                 * @param configRoot The directory of which data should be loaded from.
+                 * @param storageRoot The directory where storage data are located.
+                 * @param locale The locale that should be initially loaded.
+                 */
+                static void Initialize(string const = _configDir, 
+                                       string const = _storageDir, string const = _locale);
+				
+                /**
+                 * Sets the default locale to use.
+                 * @param locale The locale to set.
+                 */
+                static void setLocale(string const = "");
+				
+                /**
+                 * Sets the root directory to load information from.
+                 * @param root The root directory.
+                 */
+                static void setConfigDirectory(string const = "");
+                
+                /**
+                 * Set the directory where system-wide storage data can be found.
+                 * @param storage Absolute path to the storage directory.
+                 */
+                static void setStorageDirectory(string const = _storageDir);
+				
+                /**
+                 * Gets current locale used.
+                 * @return String with the current locale.
+                 */
+                inline static string getLocale()
+                { return _locale; }
+                
+                /**
+                 * Gets the config directory for the specified locale.
+                 * @param locale String with the locale name.
+                 * @return String with the path to the locale root, relative to the main root.
+                 */
+                static string getConfigDirectory(string const = "");
+                
+                /**
+                 * Return the path to the storage directory.
+                 * @param locale Locale name.
+                 * @return String with the path to the storage directory.
+                 */
+                static string getStorageDirectory(string const = _locale);
+
+                /**
+                 * Returns information about currently used serialization backend (actually only info about Boost backends).
+                 * @return String containing the name of the current serialization backend.
+                 */
+                inline static string getSerializationBackend() 
+                { return _serialBackend; }
+                
+                /**
+                 * Initializes new Boost oarchive according to default archive used.
+                 * @param ofs Output file stream to use.
+                 * @return Pointer to the new Boost oarchive.
+                 */
+                template<class Archive>
+                Archive* newOArchive(ofstream& ofs);
+                
+                /**
+                 * Initializes new Boost iarchive according to default archive used.
+                 * @param ifs Input file stream to use.
+                 * @return Pointer to the new Boost iarchive.
+                 */
+                template<class Archive>
+                Archive* newIArchive(ifstream& ifs);
+            };
+
+            class Lexical : public StorageModel
+            {
+            public:
+                typedef map<string, string> LexicalMap;
+                
+                Lexical() : StorageModel() {}
+                
+                /**
+                 * Initialize Lexical object with ID and locale.
+                 * @param ID Lexical ID.
+                 * @param locale Lexical locale.
+                 */
+                Lexical(string const& ID, string const& locale)
+                : _ID(ID), _locale(locale) {}
+                
+                /**
+                 * Initialize Lexical object with full data set.
+                 * @param ID ID of the node represented by the Lexical object.
+                 * @param locale Locale of the node represented by the Lexical object.
+                 * @param symbol Symbol of the Lexical object.
+                 * @param flags Flags of the Lexical object.
+                 */
+                Lexical(string const& ID, string const& locale, 
+                        string const& symbol, LexicalMap const& flags) 
+                : _ID(ID), _locale(locale), _symbol(symbol), _flags(flags) {}
+                
+                string getID()
+                { return _ID; }
+                
+                string getLocale()
+                { return _locale; }
+                
+                string getSymbol()
+                { return _symbol; }
+                
+                LexicalMap getFlags()
+                { return _flags; }
+                
+                virtual void save() = 0;
+                virtual void load() = 0;
+                
+            protected:
+                string _ID;
+                string _locale;
+                
+                string _symbol;
+                LexicalMap _flags;
+            };
+            
+            class LocalLexical : public Lexical, public LocalModel
+            {
+                friend class boost::serialization::access;
+                
+            public:
+                LocalLexical() : Lexical(), LocalModel() {}
+                
+                /**
+                 * Initialize new LocalLexical object with explicit path.
+                 * @param path Path to the file represented by LocalLexical object.
+                 */
+                LocalLexical(const string& path) : Lexical(), LocalModel(path) {}
+                
+                /**
+                 * Initialize new LocalLexical object with ID and locale.
+                 * @param ID Lexical ID.
+                 * @param locale Lexical locale.
+                 */
+                LocalLexical(string const& ID, string const& locale)
+                : Lexical(ID, locale), LocalModel("./" + locale + "/" + ID) {}
+                
+                /**
+                 * Initialize new LocalLexical object with its ID, locale, and data.
+                 * @param ID LocalLexical ID.
+                 * @param locale LocalLexical locale.
+                 * @param symbol Symbol of the LocalLexical.
+                 * @param flags LocalLexical flags.
+                 */
+                LocalLexical(string const& ID, string const& locale,
+                             string const& symbol, LexicalMap const& flags)
+                : Lexical(ID, locale, symbol, flags), 
+                LocalModel(Configuration::getStorageDirectory(locale)) {}
+                
+                /**
+                 * Initialize new LocalLexical object with full data set.
+                 * @param ID ID of the node represented by the Lexical object.
+                 * @param locale Locale of the node represented by the Lexical object.
+                 * @param symbol Symbol of the Lexical object.
+                 * @param flags Flags of the Lexical object.
+                 * @param path Path to the file represented by LocalLexical object.
+                 */
+                LocalLexical(const string& ID, const string& locale, 
+                             const string& symbol, const LexicalMap& flags,
+                             string const& path)
+                : Lexical(ID, locale, symbol, flags), LocalModel(path) {}
+                
+                virtual void save();
+                virtual void load();
+                
+            private:
+                template<class Archive>
+                void serialize(Archive& ar, unsigned int const version);
+            };
+        } // namespaces
+    }
+>>>>>>> MERGE-SOURCE
 }
 #endif	/* LINGUISTICS_HPP */

=== added file 'include/wntr/data/models.hpp'
--- include/wntr/data/models.hpp	1970-01-01 00:00:00 +0000
+++ include/wntr/data/models.hpp	2011-05-24 07:35:57 +0000
@@ -0,0 +1,108 @@
+/**
+ * @file
+ * @author Adrian Borucki <gentoolx@xxxxxxxxx>
+ * @date Fri, 13 May 21:54:16
+ * 
+ * <license>
+ * This program 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 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program 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 more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * </license>
+ */
+
+#include <string>
+#include <map>
+#include <fstream>
+using namespace std;
+
+#include <boost/filesystem.hpp>
+
+#include <boost/archive/xml_oarchive.hpp>
+#include <boost/archive/xml_iarchive.hpp>
+
+#include <boost/serialization/nvp.hpp>
+#include <boost/serialization/string.hpp>
+#include <boost/serialization/map.hpp>
+
+using namespace std;
+namespace Wintermute 
+{
+    namespace Data
+    {
+        class SaveModel;
+        class LocalSaveModel;
+        class LoadModel;
+        class LocalLoadModel;
+        class StorageModel;
+        class LocalModel;
+        
+        class Lexical;
+        class LocalLexical;
+        
+/// Experimental ideas: the former is not necessearily needed, the latter will be reviewed later.	
+//         class XMLStorageModel;
+//         class RDFStorageModel;
+        
+        class LocalModel
+        {
+        public:
+            LocalModel() {}
+            
+            /**
+             * Constructor with path initialization.
+             * @param path Path of the represented local data.
+             */
+            LocalModel(string const& path) : _path(path) {}
+            
+            /**
+             * Return path of the represented local data.
+             * @return Path of the represented local data.
+             */
+            string getPath()
+            { return _path; }
+            
+        protected:
+            string _path;
+        };
+        
+        class SaveModel
+        {
+        public:
+            virtual void save() = 0;
+        };
+        
+        class LocalSaveModel : public SaveModel, public LocalModel
+        {
+        public:
+            virtual void save() = 0;
+        };
+        
+        class LoadModel
+        {
+        public:
+            virtual void load() = 0;
+        };
+        
+        class LocalLoadModel : public LoadModel, public LocalModel
+        {
+        public:
+            virtual void load() = 0;
+        };
+        
+        class StorageModel : public  SaveModel, public LoadModel
+        {
+        public:
+            virtual void save() = 0;
+            virtual void load() = 0;
+        };
+    }
+}
\ No newline at end of file

=== modified file 'src/linguistics.cpp'
--- src/linguistics.cpp	2011-05-24 05:06:27 +0000
+++ src/linguistics.cpp	2011-05-24 07:35:57 +0000
@@ -23,14 +23,21 @@
  * @todo Fix the JSON parsing; might be a syntax issue or just a malparsing. Whatever it may be, it needs to be fixed.
  */
 
+#include <boost/archive/xml_oarchive.hpp>
+#include <boost/archive/xml_iarchive.hpp>
+
+#include <boost/serialization/nvp.hpp>
 #include <boost/serialization/string.hpp>
 #include <boost/serialization/map.hpp>
 
 #include <boost/signals.hpp>
 #include <boost/filesystem.hpp>
 
+<<<<<<< TREE
 #include <jsoncpp/json.hpp>
 
+=======
+>>>>>>> MERGE-SOURCE
 #include <string>
 #include <fstream>
 #include <iostream>
@@ -41,7 +48,10 @@
 #include "config.hpp"
 
 using namespace std;
+<<<<<<< TREE
 using namespace Json;
+=======
+>>>>>>> MERGE-SOURCE
 using namespace Wintermute::Data;
 using std::string;
 using std::ios;
@@ -49,11 +59,15 @@
 using std::ifstream;
 using std::ostringstream;
 using std::istringstream;
+<<<<<<< TREE
 using Json::Value;
 using Json::Reader;
 using Json::FastWriter;
+=======
+>>>>>>> MERGE-SOURCE
 
 namespace Wintermute {
+<<<<<<< TREE
 	namespace Data {
 		namespace Linguistics {
 			NodeInteroperingSignalVector NodeReader::handles;
@@ -743,3 +757,136 @@
 		}
 	}
 }
+=======
+    namespace Data {
+        namespace Linguistics {
+            /* Configuration internals */
+            string Configuration::_configDir = __WDATA_DATA_DIR__; 
+            string Configuration::_storageDir = __WDATA_STORAGE_DIR__;
+            string Configuration::_locale = "en";
+
+            /* Backend variables */
+            string Configuration::_serialBackend = __WDATA_SERIAL_BACKEND__;
+
+            void Configuration::Initialize(const string configDir,
+                                           const string storageDir, const string locale) 
+            {
+#ifdef DEBUG
+                cout << "(database) Initializing Linguistics data.." << endl;
+#endif
+
+#ifdef DEBUG
+                cout << "(database) Parsing linguistics data... " << endl;
+#endif
+
+                Configuration::setConfigDirectory(configDir);
+                Configuration::setStorageDirectory(storageDir);
+                Configuration::setLocale(locale);
+            }
+
+            void Configuration::setLocale(string const locale) 
+            {
+                if (locale.size() == 0)
+                    return;
+                
+                Configuration::_locale = locale;
+            
+#ifdef DEBUG
+                cout << "(database) ## Global locale set to " << locale << endl;
+#endif
+            }
+
+            void Configuration::setConfigDirectory(string const configDir) 
+            {
+                if (configDir.size() == 0)
+                    return;
+
+                boost::filesystem::path newConfigDir(configDir);
+                Configuration::_configDir = newConfigDir.string();
+                
+#ifdef DEBUG
+                cout << "(database) ## Root set to " << configDir << endl;
+#endif
+            }
+
+            string Configuration::getConfigDirectory(const string locale) 
+            {
+                string _locale = locale;
+                if (_locale.empty())
+                    _locale = Configuration::getLocale();
+
+                string path = Configuration::_configDir + string("/locale/") + _locale;
+                boost::filesystem::path configPath(path);
+                return configPath.string();
+            }
+            
+            void Configuration::setStorageDirectory(const string storageDir)
+            {
+                boost::filesystem::path newStorageDir(storageDir);
+                Configuration::_storageDir = newStorageDir.string();
+            }
+            
+            string Configuration::getStorageDirectory(const string locale)
+            {
+                
+                string path = Configuration::_storageDir + string("/locale/") + locale;
+                boost::filesystem::path storagePath(path);
+                return storagePath.string();
+            }
+            
+            template<class Archive>
+            Archive* Configuration::newOArchive(ofstream& ofs)
+            {
+                if (Configuration::_serialBackend == "text")
+                    return new boost::archive::text_oarchive(ofs);
+                else 
+                if (Configuration::_serialBackend == "xml")
+                    return new boost::archive::xml_oarchive(ofs);
+                else {
+                    cout << "*** Unknown archive type!!! Done nothing. \n";
+                    return NULL;
+                }
+//                  throw ConfigException
+            }
+            
+            template<class Archive>
+            Archive* Configuration::newIArchive(ifstream& ifs)
+            {
+                if (Configuration::_serialBackend == "text")
+                    return new boost::archive::text_iarchive(ifs);
+                else 
+                if (Configuration::_serialBackend == "xml")
+                    return new boost::archive::xml_iarchive(ifs);
+                else {
+                    cout << "*** Unknown archive type!!! Done nothing. \n";
+                    return NULL;
+                }
+            }
+            
+            void LocalLexical::save()
+            {
+                ofstream ofNode(this->getPath());
+                {
+                    boost::archive::xml_oarchive ofNodeArchive(ofNode);
+                    ofNodeArchive << boost::serialization::make_nvp("LocalLexical", *this);
+                }
+            }
+            
+            void LocalLexical::load()
+            {
+                ifstream ifNode(this->getPath());
+                {
+                    boost::archive::xml_iarchive ifNodeArchive(ifNode);
+                    ifNodeArchive >> boost::serialization::make_nvp("LocalLexical", *this);
+                }
+            }
+            
+            template<class Archive>
+            void LocalLexical::serialize(Archive& ar, const unsigned int version)
+            {
+                ar & BOOST_SERIALIZATION_NVP(_symbol);
+                ar & BOOST_SERIALIZATION_NVP(_flags);
+            }
+        } // namespaces
+    }
+}>>>>>>> MERGE-SOURCE

=== modified file 'wntrdata.pc.in'
--- wntrdata.pc.in	2011-05-10 20:29:50 +0000
+++ wntrdata.pc.in	2011-05-24 07:35:57 +0000
@@ -7,7 +7,7 @@
 Description: Library and development files for Wintermute's database.
 Version: @GENERIC_LIB_VERSION@
 URL: https://www.launchpad.net/wintermute
-Requires: jsoncpp >= 0.6.0, libxml++-2.6
+Requires: jsoncpp >= 0.6.0
 Conflicts: wntrdata > @GENERIC_LIB_VERSION@
 Libs: -L${libdir} -lwntrdata
 Libs.private: -lm


Follow ups