sgpemv2/plugins/xmlsave/src/xml_visitor.cc

257 lines
7.8 KiB
C++
Raw Normal View History

// 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 "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;
/*
#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(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)
{
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);
// cout << "PROCESS: " << obj.get_name() << endl;
// 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)
{
const Thread* t = *iter;
// cout << "THREAD: " << t->get_name() << endl;
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(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
{
throw SerializerError("Error trying to add thread to empty XML node.");
}
}
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.");
}
}