- updated xml_serializer.??, xml_visitor.??

- added xml_serializer_factory.??



git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@750 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
paolo 2006-07-11 00:06:40 +00:00
parent 864e71b82e
commit b6b303c4e3
6 changed files with 568 additions and 32 deletions

View File

@ -20,17 +20,28 @@
#include "xml_serializer.hh"
#include "xml_visitor.hh"
#include "xml_serializer_factory.hh"
#include "backend/string_utils.hh"
#include "backend/environment.hh"
#include "backend/history.hh"
#include "string_utils.hh"
#include "environment.hh"
#include "history.hh"
/*
#include "backend/concrete_environment.hh"
#include "backend/concrete_history.hh"
*/
#include "backend/process.hh"
#include "backend/serializer_error.hh"
using namespace sgpem;
#include <iostream>
using namespace std;
XMLSerializer::~XMLSerializer()
{
}
@ -70,9 +81,44 @@ void XMLSerializer::restore_snapshot(const Glib::ustring& filename, History& his
{
// TODO - all to do!!
// DEBUG - remove me when finished
XMLSerializerFactory fact(hist);
#ifdef LIBXML_SAX1_ENABLED
xmlDocPtr doc;
LIBXML_TEST_VERSION
xmlKeepBlanksDefault(0);
/*
* build an XML tree from a the file;
*/
doc = xmlParseFile(filename.c_str());
if (doc == NULL) {
xmlCleanupParser();
throw SerializerError("Parsing Error: doc is invalid.");
}
// read all elements and fill hist
read_doc(doc, fact);
// frees all xml related data
xmlFreeDoc(doc);
/* Clean up everything else before quitting. */
xmlCleanupParser();
#else
/*
* the library has been compiled without some of the old interfaces
*/
throw SerializerError("ERROR: Compilation with SAX1 must be enabled! (?)");
#endif /* LIBXML_SAX1_ENABLED */
}
const Glib::ustring XMLSerializer::get_filename_extension()
{
return Glib::ustring("ocio");
@ -112,11 +158,28 @@ void XMLSerializer::fill_doc(xmlDocPtr doc, const History& hist)
fill_schedulables(schedulables_node, hist);
}
void XMLSerializer::fill_resources(xmlNodePtr resources, const History& hist)
void XMLSerializer::fill_resources(xmlNodePtr resources_node, const History& hist)
{
const Environment& env = hist.get_last_environment();
const Environment::Resources& rvect = env.get_resources();
typedef Environment::Resources::const_iterator res_iterator;
res_iterator iter = rvect.begin();
res_iterator end = rvect.end();
while(iter!=end)
{
XMLVisitor xvisit(resources_node);
Glib::ustring key;
int_to_string((int)(*iter).first, key);
xvisit.from_resource(*((*iter).second), key);
iter++;
}
}
void XMLSerializer::fill_schedulables(xmlNodePtr schedulables, const History& hist)
void XMLSerializer::fill_schedulables(xmlNodePtr schedulables_node, const History& hist)
{
@ -129,20 +192,130 @@ void XMLSerializer::fill_schedulables(xmlNodePtr schedulables, const History& hi
proc_iterator end = pvect.end();
while(iter!=end)
{
XMLVisitor xvisit(schedulables);
XMLVisitor xvisit(schedulables_node);
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++;
}
}
void XMLSerializer::read_doc(xmlDocPtr doc, XMLSerializerFactory& fact)
{
/*
* Check the document is of the right kind
*/
xmlNodePtr root;
root = xmlDocGetRootElement(doc);
if (root == NULL) {
xmlFreeDoc(doc);
xmlCleanupParser();
throw SerializerError("Reading Error: xml doc is empty.");
}
cout << "ROOT: " << root->name << endl;
xmlNodePtr cur;
cur = root->children;
while(cur!=NULL)
{
cout << "NODE: " << cur->name << endl;
Glib::ustring name((const char *)cur->name);
if(name=="resources")
{
read_resources(cur, fact);
}
if(name=="schedulables")
{
read_schedulables(cur, fact);
}
cur = cur->next;
}
}
XMLSerializerFactory::Parameters* read_properties(xmlAttrPtr prop)
{
if(prop==NULL)
return NULL;
XMLSerializerFactory::Parameters* par=new XMLSerializerFactory::Parameters();
while (prop != NULL) {
// cout << "PROP: " << prop->name;
if(prop->children && xmlNodeIsText(prop->children)){
xmlChar *key = xmlNodeGetContent (prop->children);
// xmlChar *key = xmlNodeListGetString(doc, prop->children, 1);
if(key!=NULL)
{
// cout << " VALUE: " << key;
std::pair<Glib::ustring, Glib::ustring> key_value(Glib::ustring((const char *)prop->name), Glib::ustring((const char *)key));
par->insert(key_value);
// c.insert(typename Cont::value_type(new_key, pos->second));
cout << " pair PROP: " << key_value.first << " VALUE: " << key_value.second << endl;
xmlFree(key);
}
else
cout << " !VALUE IS NULL! ";
}
cout << endl;
prop = prop->next;
}
return par;
}
void XMLSerializer::read_resources(xmlNodePtr resources_node, XMLSerializerFactory& fact)
{
xmlNodePtr cur;
cur = resources_node->children;
while(cur!=NULL)
{
Glib::ustring node_name((const char *)cur->name);
if(node_name=="resource")
{
cout << "NODE: " << cur->name << endl;
xmlAttrPtr prop = cur->properties;
XMLSerializerFactory::Parameters* par=read_properties(prop);
if(par!=NULL)
{
fact.factory_method(Glib::ustring("Resource"), *par);
}
// fact.create_resource(*par);
}
cur = cur->next;
}
}
void XMLSerializer::read_schedulables(xmlNodePtr schedulables_node, XMLSerializerFactory& fact)
{
xmlNodePtr cur;
cur = schedulables_node->children;
while(cur!=NULL)
{
cout << "NODE: " << cur->name << endl;
xmlAttrPtr prop = cur->properties;
while (prop != NULL) {
cout << "PROP: " << prop->name;
if(prop->children && xmlNodeIsText(prop->children)){
xmlChar *key = xmlNodeGetContent (prop->children);
// xmlChar *key = xmlNodeListGetString(doc, prop->children, 1);
if(key!=NULL)
{
cout << " VALUE: " << key;
xmlFree(key);
}
else
cout << " !VALUE IS NULL! ";
}
cout << endl;
prop = prop->next;
}
cur = cur->next;
}
}
/* comment */

View File

@ -22,11 +22,15 @@
#define XML_SERIALIZER_HH 1
#include "config.h"
#include "backend/serializer.hh"
#include "serializer.hh"
#include <glibmm/ustring.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
namespace sgpem
{
class XMLSerializerFactory;
}
namespace sgpem
{
@ -45,8 +49,12 @@ namespace sgpem
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);
void fill_resources(xmlNodePtr resources_node, const History& hist);
void fill_schedulables(xmlNodePtr schedulables_node, const History& hist);
void read_doc(xmlDocPtr doc, XMLSerializerFactory& fact);
void read_resources(xmlNodePtr resources_node, XMLSerializerFactory& fact);
void read_schedulables(xmlNodePtr schedulables_node, XMLSerializerFactory& fact);
};
}

View File

@ -0,0 +1,176 @@
// src/xml_serializer_factory.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_factory.hh"
#include "string_utils.hh"
// #include "environment.hh"
#include "history.hh"
#include "resource.hh"
// #include "backend/process.hh"
#include "backend/serializer_error.hh"
using namespace sgpem;
#include <iostream>
using namespace std;
XMLSerializerFactory::XMLSerializerFactory(History& hist)
: _hist(&hist)
{
}
XMLSerializerFactory::~XMLSerializerFactory()
{
}
History* XMLSerializerFactory::get_history()
{
return _hist;
}
void
XMLSerializerFactory::factory_method(const Glib::ustring& class_name, Parameters& parameters)
{
if(class_name == "Resource")
{
create_resource(parameters);
}
else if(class_name == "Process")
{
}
else if(class_name == "Thread")
{
}
else if(class_name == "Request")
{
}
else if(class_name == "SubRequest")
{
}
}
XMLSerializerFactory::ResourcePair
XMLSerializerFactory::create_resource(Parameters& parameters)
{
// if(_hist!=NULL)
{
Glib::ustring name;
Glib::ustring key("0");
int arrival_time=0;
int how_many=1;
bool preemptable=false;
Parameters::iterator pos;
// read "name" property
pos = parameters.find(Glib::ustring("name"));
if (pos != parameters.end()) {
name = pos->second;
}
// read "key" property
pos = parameters.find(Glib::ustring("key"));
if (pos != parameters.end()) {
key = pos->second;
}
// read "preemptable" property
pos = parameters.find(Glib::ustring("preemptable"));
if (pos != parameters.end()) {
preemptable = (pos->second == "true");
}
// read "arrival-time" property
pos = parameters.find(Glib::ustring("arrival-time"));
if (pos != parameters.end()) {
string_to_int(pos->second, arrival_time);
}
// read "how-many" property
pos = parameters.find(Glib::ustring("how-many"));
if (pos != parameters.end()) {
string_to_int(pos->second, how_many);
}
History::ResourcePair respair = _hist->add_resource(name, preemptable, how_many, arrival_time);
return respair;
}
}
Process&
XMLSerializerFactory::create_process(Parameters& parameters)
{
// if(_hist!=NULL)
{
Glib::ustring name;
int arrival_time=0;
int priority=1;
Parameters::iterator pos;
// read "name" property
pos = parameters.find(Glib::ustring("name"));
if (pos != parameters.end()) {
name = pos->second;
}
// read "arrival-time" property
pos = parameters.find(Glib::ustring("arrival-time"));
if (pos != parameters.end()) {
string_to_int(pos->second, arrival_time);
}
// read "priority" property
pos = parameters.find(Glib::ustring("priority"));
if (pos != parameters.end()) {
string_to_int(pos->second, priority);
}
return _hist->add_process(Glib::ustring(name, arrival_time, priority);;
}
}
Thread&
XMLSerializerFactory::create_thread(Parameters& parameters)
{
}
Request&
XMLSerializerFactory::create_request(Parameters& parameters)
{
}
Subrequest&
XMLSerializerFactory::create_subrequest(Parameters& parameters)
{
}
/* comment */

View File

@ -0,0 +1,71 @@
// src/xml_serializer_factory.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_FACTORY_HH
#define XML_SERIALIZER_FACTORY_HH 1
#include "config.h"
// #include "history.hh"
#include "environment.hh"
#include <glibmm/ustring.h>
#include <map>
namespace sgpem
{
class History;
class Resource;
class Process;
class Thread;
class Request;
class Subrequest;
}
namespace sgpem
{
class XMLSerializerFactory;
class XMLSerializerFactory
{
public:
XMLSerializerFactory(History& hist);
~XMLSerializerFactory();
History* get_history();
typedef Environment::resource_key_t resource_key_t;
typedef const std::pair<resource_key_t, Resource*> ResourcePair;
//typedef const std::map<const Glib::ustring&, const Glib::ustring&> Parameters;
typedef std::map<Glib::ustring, Glib::ustring> Parameters;
void factory_method(const Glib::ustring& class_name, Parameters& parameters);
protected:
private:
ResourcePair create_resource(Parameters& parameters);
Process& create_process(Parameters& parameters);
Thread& create_thread(Parameters& parameters);
Request& create_request(Parameters& parameters);
Subrequest& create_subrequest(Parameters& parameters);
History* _hist;
};
}
#endif

View File

@ -20,13 +20,16 @@
#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"
#include "string_utils.hh"
#include "resource.hh"
#include "process.hh"
#include "thread.hh"
#include "request.hh"
#include "sub_request.hh"
#include "serializer_error.hh"
#include <iostream>
using namespace std;
using namespace sgpem;
@ -72,11 +75,43 @@ void XMLVisitor::from_subrequest(const SubRequest& obj)
{
}
void XMLVisitor::from_resource(const Resource& obj, const Glib::ustring& key)
{
from_resource(_current, obj, key);
}
/*
void XMLVisitor::from_resource(xmlNodePtr parent, const Resource& obj)
{
}
*/
void XMLVisitor::from_resource(xmlNodePtr parent, const Resource& obj, const Glib::ustring& key)
{
if(parent!=NULL)
{
Glib::ustring strPreemptible("false"); // fixed??
Glib::ustring strArrivalTime("0"); // fixed??
Glib::ustring strPlaces;
int_to_string((int)obj.get_places(), strPlaces);
xmlNodePtr process_node = xmlNewChild(parent, NULL, (const xmlChar *) "resource", NULL);
xmlNewProp(process_node, (const xmlChar *) "name", (const xmlChar *) obj.get_name().c_str());
xmlNewProp(process_node, (const xmlChar *) "key", (const xmlChar *) key.c_str());
xmlNewProp(process_node, (const xmlChar *) "arrival-time", (const xmlChar *) strArrivalTime.c_str());
xmlNewProp(process_node, (const xmlChar *) "how-many", (const xmlChar *) strPlaces.c_str());
xmlNewProp(process_node, (const xmlChar *) "pre-emptible", (const xmlChar *) strPreemptible.c_str());
}
else
{
throw SerializerError("Error trying to add resource to empty XML node.");
}
}
void XMLVisitor::from_process(xmlNodePtr parent, const Process& obj)
@ -96,6 +131,7 @@ void XMLVisitor::from_process(xmlNodePtr parent, const Process& obj)
// make a threads subnode
xmlNodePtr threads_node = xmlNewChild(process_node, NULL, (const xmlChar *) "threads", NULL);
// cout << "PROCESS: " << obj.get_name() << endl;
// iterate on threads
typedef std::vector<Thread*> Threads;
typedef std::vector<Thread*>::const_iterator thr_iterator;
@ -104,6 +140,9 @@ void XMLVisitor::from_process(xmlNodePtr parent, const Process& obj)
thr_iterator end = tvect.end();
while(iter!=end)
{
const Thread* t = *iter;
// cout << "THREAD: " << t->get_name() << endl;
from_thread(threads_node, *(*iter));
iter++;
}
@ -127,11 +166,29 @@ void XMLVisitor::from_thread(xmlNodePtr parent, const Thread& obj)
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);
xmlNodePtr thread_node = xmlNewChild(parent, 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());
// make a requests subnode
xmlNodePtr requests_node = xmlNewChild(thread_node, NULL, (const xmlChar *) "requests", NULL);
// cout << "PROCESS: " << obj.get_name() << endl;
// iterate on requests
typedef std::vector<Request*> Requests;
typedef std::vector<Request*>::const_iterator req_iterator;
const Requests& rvect = ((Thread&)obj).get_requests();
req_iterator iter = rvect.begin();
req_iterator end = rvect.end();
while(iter!=end)
{
const Request* r = *iter;
// cout << "REQUEST: " << r->get_instant() << endl;
from_request(requests_node, *(*iter));
iter++;
}
}
else
{
@ -142,9 +199,58 @@ void XMLVisitor::from_thread(xmlNodePtr parent, const Thread& obj)
void XMLVisitor::from_request(xmlNodePtr parent, const Request& obj)
{
if(parent!=NULL)
{
Glib::ustring strArrivalTime;
int_to_string(obj.get_instant(), strArrivalTime);
xmlNodePtr request_node = xmlNewChild(parent, NULL, (const xmlChar *) "request", NULL);
xmlNewProp(request_node, (const xmlChar *) "arrival-time", (const xmlChar *) strArrivalTime.c_str());
// make a requests subnode
// xmlNodePtr subrequests_node = xmlNewChild(thread_node, NULL, (const xmlChar *) "requests", NULL);
// cout << "PROCESS: " << obj.get_name() << endl;
// iterate on subrequests
typedef std::vector<SubRequest*> SubRequests;
typedef std::vector<SubRequest*>::const_iterator subreq_iterator;
const SubRequests& srvect = ((Request&)obj).get_subrequests();
subreq_iterator iter = srvect.begin();
subreq_iterator end = srvect.end();
while(iter!=end)
{
const SubRequest* sr = *iter;
cout << "SUB REQUEST: " << sr->get_resource_key() << " places: " << sr->get_places() << " length: " << sr->get_length() << endl;
from_subrequest(request_node, *(*iter));
iter++;
}
}
else
{
throw SerializerError("Error trying to add request to empty XML node.");
}
}
void XMLVisitor::from_subrequest(xmlNodePtr parent, const SubRequest& obj)
{
if(parent!=NULL)
{
Glib::ustring strResource;
Glib::ustring strHowMany;
Glib::ustring strLastsFor;
int_to_string(obj.get_resource_key(), strResource);
int_to_string(obj.get_places(), strHowMany);
int_to_string(obj.get_length(), strLastsFor);
xmlNodePtr subrequest_node = xmlNewChild(parent, NULL, (const xmlChar *) "subrequest", NULL);
xmlNewProp(subrequest_node, (const xmlChar *) "resource", (const xmlChar *) strResource.c_str());
xmlNewProp(subrequest_node, (const xmlChar *) "how-many", (const xmlChar *) strHowMany.c_str());
xmlNewProp(subrequest_node, (const xmlChar *) "lasts-for", (const xmlChar *) strLastsFor.c_str());
}
else
{
throw SerializerError("Error trying to add subrequest to empty XML node.");
}
}

View File

@ -31,7 +31,7 @@ namespace sgpem
}
#include "config.h"
#include "backend/serializer_visitor.hh"
#include "serializer_visitor.hh"
#include <glibmm/ustring.h>
#include <libxml/xmlmemory.h>
@ -52,8 +52,10 @@ namespace sgpem
virtual void from_thread(const Thread& obj);
virtual void from_request(const Request& obj);
virtual void from_subrequest(const SubRequest& obj);
virtual void from_resource(const Resource& obj, const Glib::ustring& key);
private:
void from_resource(xmlNodePtr parent, const Resource& obj);
void from_resource(xmlNodePtr parent, const Resource& obj, const Glib::ustring& key);
void from_process(xmlNodePtr parent, const Process& obj);
void from_thread(xmlNodePtr parent, const Thread& obj);
void from_request(xmlNodePtr parent, const Request& obj);