- added xml serialization classes (partial implementation)

- xml_serializer
  - xml_visitor
  - testsuite/test-xml_visitor



git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@743 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
paolo 2006-07-08 05:54:21 +00:00
parent 4d862a3c2f
commit 519e516314
5 changed files with 550 additions and 0 deletions

View File

@ -0,0 +1,132 @@
// src/testsuite/test-history.cc - Copyright 2005, 2006, University
// of Padova, dept. of Pure and Applied
// Mathematics
//
// This file is part of SGPEMv2.
//
// This 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.
//
// SGPEMv2 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 SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
/* This executable tests for workingness of the whole History services,
* and nothing else. */
#include "config.h"
#include "gettext.h"
#include <glibmm/ustring.h>
#include <cassert>
#include <iostream>
#include "backend/concrete_environment.hh"
#include "backend/concrete_history.hh"
#include "backend/history_observer.hh"
#include "xml_serializer.hh"
using namespace sgpem;
using namespace std;
void fillHistory(History &hist)
{
cout << "filling history..." << endl << endl;
// add a resource - name, preemptable, places, availability
History::ResourcePair respair = hist.add_resource(Glib::ustring("Resource 1"), false, 1, 2);
// and print his values
cout << "resource name: " << respair.second->get_name() << " key: " << respair.first << " places: " << respair.second->get_places() << endl;
// add a process - name, arrival time, priority
Process& p1 = hist.add_process(Glib::ustring("Process 1"), 5, 2); // name, arrival time, priority
// and print his values
cout << "process name: " << p1.get_name() << " arrival_time: " << p1.get_arrival_time() << " base_priority: " << p1.get_base_priority() << endl;
// add a process - name, arrival time, priority
Process& p2 = hist.add_process(Glib::ustring("Process 2"), 7, 3); // name, arrival time, priority
// and print his values
cout << "process name: " << p2.get_name() << " arrival_time: " << p2.get_arrival_time() << " base_priority: " << p2.get_base_priority() << endl;
// add a process - name, arrival time, priority
Process& p3 = hist.add_process(Glib::ustring("Process 3"), 9, 1); // name, arrival time, priority
// and print his values
cout << "process name: " << p3.get_name() << " arrival_time: " << p3.get_arrival_time() << " base_priority: " << p3.get_base_priority() << endl;
// add a thread - name, parent, cpu time, arrival time, priority
Thread& p1_t1 = hist.add_thread(Glib::ustring("Process 1 - Thread 1"), p1, 3, 2, 6);
// and print his values
cout << "thread name: " << p1_t1.get_name() << " total_cpu_time: " << p1_t1.get_total_cpu_time() << " arrival_time: " << p1_t1.get_arrival_time() << " base_priority: " << p1_t1.get_base_priority() << endl;
// add a request - Thread, time
Request& req1 = hist.add_request(p1_t1, 7);
// and print his values
cout << "request arrival_time: " << req1.get_instant() << endl;
// add a sub request - Request, resource_key, duration, places
SubRequest& req1_sub1 = hist.add_subrequest(req1, respair.first, 5, 2);
// and print his values
cout << "sub request resource_key: " << req1_sub1.get_resource_key() << " places: " << req1_sub1.get_places() << " length: " << req1_sub1.get_length() << endl;
cout << endl;
}
void dumpEnvironment(const Environment& env)
{
const Environment::Processes& pvect = env.get_processes();
typedef std::vector<Process*>::const_iterator proc_iterator;
proc_iterator iter = pvect.begin();
proc_iterator end = pvect.end();
while(iter!=end)
{
Process* p = (*iter);
cout << "process name: " << p->get_name() << " arrival_time: " << p->get_arrival_time() << " base_priority: " << p->get_base_priority() << endl;
iter++;
typedef std::vector<Thread*> Threads;
typedef std::vector<Thread*>::const_iterator thr_iterator;
const Threads& tvect = p->get_threads();
thr_iterator iter1 = tvect.begin();
thr_iterator end1 = tvect.end();
while(iter1!=end1)
{
Thread* p = (*iter1);
cout << "Thread name: " << p->get_name() << " arrival_time: " << p->get_arrival_time() << " base_priority: " << p->get_base_priority() << endl;
iter1++;
}
}
}
int
main(int argc, char** argv)
{
// using namespace sgpem;
ConcreteHistory hist;
fillHistory(hist);
const Environment& env = hist.get_last_environment();
dumpEnvironment(env);
XMLSerializer xmlser;
xmlser.save_snapshot(Glib::ustring("xml-visit.xml"), hist);
// typedef std::vector<Process*> Processes;
return 0;
}

View File

@ -0,0 +1,148 @@
// src/backend/serialize_visitor.cc - Copyright 2005, 2006, University
// of Padova, dept. of Pure and Applied
// Mathematics
//
// This file is part of SGPEMv2.
//
// This 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.
//
// SGPEMv2 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 SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "xml_serializer.hh"
#include "xml_visitor.hh"
#include "backend/string_utils.hh"
#include "backend/environment.hh"
#include "backend/history.hh"
#include "backend/concrete_environment.hh"
#include "backend/concrete_history.hh"
#include "backend/process.hh"
#include "backend/serializer_error.hh"
using namespace sgpem;
XMLSerializer::~XMLSerializer()
{
}
XMLSerializer::XMLSerializer()
{
}
void XMLSerializer::save_snapshot(const Glib::ustring& filename, const History& hist)
{
/* COMPAT: Do not genrate nodes for formatting spaces */
LIBXML_TEST_VERSION
xmlKeepBlanksDefault(0);
xmlDocPtr doc;
doc = xmlNewDoc((const xmlChar *)"1.0");
if(doc!=NULL){
fill_doc(doc, hist);
int nwritten = xmlSaveFormatFile (filename.c_str(), doc, 1);
if(nwritten<0)
{
throw SerializerError("Error writing xml doc to output stream.");
}
}
else
{
throw SerializerError("Internal Error creating xml doc.");
}
/* Clean up everything else before quitting. */
xmlCleanupParser();
}
void XMLSerializer::restore_snapshot(const Glib::ustring& filename, History& hist)
{
// TODO - all to do!!
// DEBUG - remove me when finished
}
const Glib::ustring XMLSerializer::get_filename_extension()
{
return Glib::ustring("ocio");
}
const Glib::ustring XMLSerializer::get_filename_description()
{
return Glib::ustring("SGPEMv2 XML formatted snapshot save file");
}
void XMLSerializer::fill_doc(xmlDocPtr doc, const History& hist)
{
xmlNodePtr root_node = NULL;/* node pointers */
/*
* Creates a new document, a node and set it as a root node
*/
root_node = xmlNewNode(NULL, (const xmlChar *) "sgpem");
xmlDocSetRootElement(doc, root_node);
/*
* Creates a DTD declaration. Isn't mandatory.
*/
// dtd = xmlCreateIntSubset(doc, BAD_CAST "root", NULL, BAD_CAST "tree2.dtd");
/*
* xmlNewChild() creates a new node, which is "attached" as child node
* of root_node node.
*/
xmlNodePtr resources_node = xmlNewChild(root_node, NULL, (const xmlChar *) "resources", NULL);
/*
* The same as above, but the new child node doesn't have a content
*/
xmlNodePtr schedulables_node = xmlNewChild(root_node, NULL, (const xmlChar *) "schedulables", NULL);
fill_resources(resources_node, hist);
fill_schedulables(schedulables_node, hist);
}
void XMLSerializer::fill_resources(xmlNodePtr resources, const History& hist)
{
}
void XMLSerializer::fill_schedulables(xmlNodePtr schedulables, const History& hist)
{
const Environment& env = hist.get_last_environment();
const Environment::Processes& pvect = env.get_processes();
typedef std::vector<Process*>::const_iterator proc_iterator;
proc_iterator iter = pvect.begin();
proc_iterator end = pvect.end();
while(iter!=end)
{
XMLVisitor xvisit(schedulables);
xvisit.from_process(*(*iter));
/*
Glib::ustring strPriority;
Glib::ustring strArrivalTime;
Process* p = (*iter);
int_to_string(p->get_base_priority(), strPriority);
int_to_string(p->get_arrival_time(), strArrivalTime);
xmlNodePtr process_node = xmlNewChild(schedulables, NULL, (const xmlChar *) p->get_name().c_str(), NULL);
xmlNewProp(process_node, (const xmlChar *) "name", (const xmlChar *) p->get_name().c_str());
xmlNewProp(process_node, (const xmlChar *) "priority", (const xmlChar *) strPriority.c_str());
xmlNewProp(process_node, (const xmlChar *) "arrival-time", (const xmlChar *) strArrivalTime.c_str());
*/
iter++;
}
}

View File

@ -0,0 +1,53 @@
// src/backend/serializer.hh - Copyright 2005, 2006, University
// of Padova, dept. of Pure and Applied
// Mathematics
//
// This file is part of SGPEMv2.
//
// This 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.
//
// SGPEMv2 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 SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#ifndef XML_SERIALIZER_HH
#define XML_SERIALIZER_HH 1
#include "config.h"
#include "backend/serializer.hh"
#include <glibmm/ustring.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
namespace sgpem
{
class XMLSerializer;
class XMLSerializer : public Serializer
{
public:
XMLSerializer();
virtual ~XMLSerializer();
virtual void save_snapshot(const Glib::ustring& filename, const History& hist);
virtual void restore_snapshot(const Glib::ustring& filename, History& hist);
virtual const Glib::ustring get_filename_extension();
virtual const Glib::ustring get_filename_description();
protected:
private:
void fill_doc(xmlDocPtr doc, const History& hist);
void fill_resources(xmlNodePtr doc, const History& hist);
void fill_schedulables(xmlNodePtr doc, const History& hist);
};
}
#endif

View File

@ -0,0 +1,150 @@
// src/backend/serialize_visitor.cc - Copyright 2005, 2006, University
// of Padova, dept. of Pure and Applied
// Mathematics
//
// This file is part of SGPEMv2.
//
// This 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.
//
// SGPEMv2 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 SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "xml_visitor.hh"
#include "backend/string_utils.hh"
#include "backend/resource.hh"
#include "backend/process.hh"
#include "backend/thread.hh"
#include "backend/request.hh"
#include "backend/sub_request.hh"
#include "backend/serializer_error.hh"
using namespace sgpem;
/*
#include <iostream>
using namespace std;
*/
XMLVisitor::XMLVisitor(xmlNodePtr current)
: _current(current)
{
}
XMLVisitor::~XMLVisitor()
{
}
void XMLVisitor::from_resource(const Resource& obj)
{
}
void XMLVisitor::from_process(const Process& obj)
{
from_process(_current, obj);
}
void XMLVisitor::from_thread(const Thread& obj)
{
from_thread(_current, obj);
}
void XMLVisitor::from_request(const Request& obj)
{
}
void XMLVisitor::from_subrequest(const SubRequest& obj)
{
}
void XMLVisitor::from_resource(xmlNodePtr parent, const Resource& obj)
{
}
void XMLVisitor::from_process(xmlNodePtr parent, const Process& obj)
{
if(parent!=NULL)
{
Glib::ustring strPriority;
Glib::ustring strArrivalTime;
int_to_string(obj.get_base_priority(), strPriority);
int_to_string(obj.get_arrival_time(), strArrivalTime);
xmlNodePtr process_node = xmlNewChild(parent, NULL, (const xmlChar *) "process", NULL);
xmlNewProp(process_node, (const xmlChar *) "name", (const xmlChar *) obj.get_name().c_str());
xmlNewProp(process_node, (const xmlChar *) "priority", (const xmlChar *) strPriority.c_str());
xmlNewProp(process_node, (const xmlChar *) "arrival-time", (const xmlChar *) strArrivalTime.c_str());
// make a threads subnode
xmlNodePtr threads_node = xmlNewChild(process_node, NULL, (const xmlChar *) "threads", NULL);
// iterate on threads
typedef std::vector<Thread*> Threads;
typedef std::vector<Thread*>::const_iterator thr_iterator;
const Threads& tvect = ((Process&)obj).get_threads();
thr_iterator iter = tvect.begin();
thr_iterator end = tvect.end();
while(iter!=end)
{
from_thread(threads_node, *(*iter));
iter++;
}
}
else
{
throw SerializerError("Error trying to add process to empty XML node.");
}
}
void XMLVisitor::from_thread(xmlNodePtr parent, const Thread& obj)
{
if(parent!=NULL)
{
Glib::ustring strPriority;
Glib::ustring strArrivalTime;
Glib::ustring strLastsTime;
int_to_string(obj.get_base_priority(), strPriority);
int_to_string(obj.get_arrival_time(), strArrivalTime);
int_to_string(obj.get_elapsed_time(), strLastsTime);
// get_elapsed_time() or get_total_cpu_time() ???
xmlNodePtr thread_node = xmlNewChild(_current, NULL, (const xmlChar *) "thread", NULL);
xmlNewProp(thread_node, (const xmlChar *) "name", (const xmlChar *) obj.get_name().c_str());
xmlNewProp(thread_node, (const xmlChar *) "priority", (const xmlChar *) strPriority.c_str());
xmlNewProp(thread_node, (const xmlChar *) "arrival-delta", (const xmlChar *) strArrivalTime.c_str());
xmlNewProp(thread_node, (const xmlChar *) "lasts-for", (const xmlChar *) strLastsTime.c_str());
}
else
{
throw SerializerError("Error trying to add thread to empty XML node.");
}
}
void XMLVisitor::from_request(xmlNodePtr parent, const Request& obj)
{
}
void XMLVisitor::from_subrequest(xmlNodePtr parent, const SubRequest& obj)
{
}

View File

@ -0,0 +1,67 @@
// src/backend/serialize_visitor.hh - Copyright 2005, 2006, University
// of Padova, dept. of Pure and Applied
// Mathematics
//
// This file is part of SGPEMv2.
//
// This 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.
//
// SGPEMv2 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 SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#ifndef XML_VISITOR_HH
#define XML_VISITOR_HH 1
namespace sgpem
{
class Resource;
class Process;
class Thread;
class Request;
class SubRequest;
}
#include "config.h"
#include "backend/serializer_visitor.hh"
#include <glibmm/ustring.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
namespace sgpem
{
class XMLVisitor;
class XMLVisitor : public SerializerVisitor
{
public:
XMLVisitor(xmlNodePtr current);
virtual ~XMLVisitor();
virtual void from_resource(const Resource& obj);
virtual void from_process(const Process& obj);
virtual void from_thread(const Thread& obj);
virtual void from_request(const Request& obj);
virtual void from_subrequest(const SubRequest& obj);
private:
void from_resource(xmlNodePtr parent, const Resource& obj);
void from_process(xmlNodePtr parent, const Process& obj);
void from_thread(xmlNodePtr parent, const Thread& obj);
void from_request(xmlNodePtr parent, const Request& obj);
void from_subrequest(xmlNodePtr parent, const SubRequest& obj);
xmlNodePtr _current;
};
}
#endif