aboutsummaryrefslogtreecommitdiff
path: root/src/rdf.cc
diff options
context:
space:
mode:
authortradke <tradke@83718e91-0e4f-0410-abf4-91180603181f>2006-07-05 16:15:31 +0000
committertradke <tradke@83718e91-0e4f-0410-abf4-91180603181f>2006-07-05 16:15:31 +0000
commit416857507c662b6fcb7ee295146118991afe8b1c (patch)
treed4d16c76e1eb090dcb3a0abb5d2c0961c426cc4e /src/rdf.cc
parentffa04af8063d3a7f32ef6e155a284d2095b15c6c (diff)
Revised version of RDF generation: it follows Dylan Stark's vocabulary
(as suggested on http://www.cct.lsu.edu/~dstark/cctk/0.1/) as much as possible and produces valid RDF/XML documents which can be queried with SPARQL. git-svn-id: http://svn.cactuscode.org/arrangements/CactusUtils/Formaline/trunk@92 83718e91-0e4f-0410-abf4-91180603181f
Diffstat (limited to 'src/rdf.cc')
-rw-r--r--src/rdf.cc408
1 files changed, 330 insertions, 78 deletions
diff --git a/src/rdf.cc b/src/rdf.cc
index 3335adc..82cda95 100644
--- a/src/rdf.cc
+++ b/src/rdf.cc
@@ -18,6 +18,8 @@
#include "cctk.h"
#include "cctk_Parameters.h"
+#include "cctk_Version.h"
+#include "util_String.h"
#include "util_Network.h"
#include "rdf.hh"
@@ -30,48 +32,292 @@ namespace Formaline
{
using namespace std;
-
-
+
static bool
is_clean_for_shell (char const * str);
-
+
+#if 0
static list<string>
parse (char const * const key, string& node);
+#endif
+
-
-
rdf::
rdf (char const * const id,
enum state const st)
: storage (st)
{
+ //
+ // This code was copied over from function Formaline_AnnounceInitial()
+ // in announce.cc and modified here to create a valid RDF/XML (rather then
+ // an unstructured plain XML document).
+ //
+ DECLARE_CCTK_PARAMETERS;
+
+ if (verbose) CCTK_INFO ("Announcing initial RDF metadata information");
+
+ //
+ // RDF/XML document header with some namespace definitions
+ //
msgbuf
<< "<?xml version=\"1.0\" encoding=\"utf-8\"?>" << endl
<< "<!DOCTYPE owl [" << endl
-// << " <!ENTITY dc 'http://purl.org/dc/elements/1.1/'>" << endl
-// << " <!ENTITY doap 'http://usefulinc.com/ns/doap#'>" << endl
-// << " <!ENTITY foaf 'http://xmlns.com/foaf/0.1/'>" << endl
-<< " <!ENTITY rdf 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'>" << endl
-<< " <!ENTITY xsd 'http://www.w3.org/2001/XMLSchema#'>" << endl
-// << " <!ENTITY rdfs 'http://www.w3.org/2000/01/rdf-schema#'>" << endl
-<< "" << endl
-// << " <!ENTITY cctk 'http://www.cct.lsu.edu/~dstark/cctk/0.1/'>" << endl
-<< " <!ENTITY form 'http://www.aei.mpg.de/form#'>" << endl
+// << "\t<!ENTITY dc 'http://purl.org/dc/elements/1.1/'>" << endl
+// << "\t<!ENTITY doap 'http://usefulinc.com/ns/doap#'>" << endl
+// << "\t<!ENTITY foaf 'http://xmlns.com/foaf/0.1/'>" << endl
+<< "\t<!ENTITY rdf 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'>" << endl
+<< "\t<!ENTITY xsd 'http://www.w3.org/2001/XMLSchema#'>" << endl
+// << "\t<!ENTITY rdfs 'http://www.w3.org/2000/01/rdf-schema#'>" << endl
+<< "\t<!ENTITY cctk 'http://www.cct.lsu.edu/~dstark/cctk/0.1/'>" << endl
+// << "\t<!ENTITY form 'http://www.aei.mpg.de/form#'>" << endl
<< "]>" << endl
-<< "<rdf:RDF" << endl
-// << " xmlns:dc=\"&dc;\"" << endl
-// << " xmlns:doap=\"&doap;\"" << endl
-// << " xmlns:foaf=\"&foaf;\"" << endl
-<< " xmlns:rdf=\"&rdf;\"" << endl
-<< " xmlns:xsd=\"&xsd;\"" << endl
-// << " xmlns:rdfs=\"&rdfs;\"" << endl
-// << " xmlns:cctk=\"&cctk;\"" << endl
-<< " xmlns:form=\"&form;\"" << endl
-<< ">" << endl
-<< endl
-<< "<form:Simulation>" << endl
-<< " <form:jobid>" << clean (string (id)) << "</form:jobid>" << endl;
+<< "<rdf:RDF xmlns:rdf=\"&rdf;\""
+<< endl << "\txmlns:xsd=\"&xsd;\""
+// << endl << "\txmlns:dc=\"&dc;\""
+// << endl << "\txmlns:doap=\"&doap;\""
+// << endl << "\txmlns:foaf=\"&foaf;\""
+// << endl << "\txmlns:rdfs=\"&rdfs;\""
+<< endl << "\txmlns:cctk=\"&cctk;\""
+// << endl << "\txmlns:form=\"&form;\""
+<< endl << ">" << endl << endl;
+
+ //
+ // general metadata with inlined attribute values
+ //
+ const string jobID = clean (string (id));
+ char hostbuf[512] = "";
+ Util_GetHostName (hostbuf, sizeof (hostbuf));
+ const string host = clean (hostbuf);
+ const cGH* const cctkGH = NULL;
+ const int nprocs = CCTK_nProcs (cctkGH);
+#if 0
+ const string user = clean (CCTK_RunUser());
+#else
+ const string user = clean (getenv ("USER"));
+#endif
+ char** argv;
+ CCTK_CommandLine (&argv);
+ const string executable = clean (argv[0]);
+ char parfilebuf[512] = "";
+ CCTK_ParameterFilename (sizeof (parfilebuf), parfilebuf);
+ const string parfile = clean (parfilebuf);
+ const string version = clean (CCTK_FullVersion ());
+ ostringstream compiled_at_buf;
+ compiled_at_buf << CCTK_CompileDate () << " " << CCTK_CompileTime ();
+ const string compiled_at (clean (compiled_at_buf.str()));
+ char rundatebuf[32] = "";
+ char runtimebuf[32] = "";
+ Util_CurrentDate (sizeof (rundatebuf), rundatebuf);
+ Util_CurrentTime (sizeof (runtimebuf), runtimebuf);
+ ostringstream started_at_buf;
+ started_at_buf << rundatebuf << " " << runtimebuf;
+ const string started_at (clean (started_at_buf.str()));
+ char cwdbuf[512];
+ getcwd (cwdbuf, sizeof (cwdbuf));
+ const string cwd (clean (cwdbuf));
+
+
+ msgbuf
+<< "<cctk:Simulation rdf:about=\"#" << jobID << "\">" << endl
+<< "\t<cctk:simulationID>" << jobID << "</cctk:simulationID>" << endl
+<< "\t<cctk:host>" << host << "</cctk:host>" << endl
+<< "\t<cctk:nProcs>" << nprocs << "</cctk:nProcs>" << endl
+<< "\t<cctk:user>" << user << "</cctk:user>" << endl
+<< "\t<cctk:executable>" << executable << "</cctk:executable>" << endl
+<< "\t<cctk:parameterFile>" << parfile << "</cctk:parameterFile>" << endl
+<< "\t<cctk:hasVersion>" << version << "</cctk:hasVersion>" << endl
+<< "\t<cctk:compiledAt>" << compiled_at << "</cctk:compiledAt>" << endl
+<< "\t<cctk:startedAt>" << started_at << "</cctk:startedAt>" << endl
+<< "\t<cctk:cwd>" << cwd << "</cctk:cwd>" << endl
+<< "</cctk:Simulation>" << endl << endl;
+
+
+ //
+ // metadata as references to other nodes
+ //
+ msgbuf << "<cctk:Simulation rdf:about=\"#" << jobID << "\">" << endl
+ << "\t<cctk:hasThornList rdf:resource=\"#ThornList\"/>" << endl
+ << "\t<cctk:hasParameterFile rdf:resource=\"#ParameterFile\"/>" << endl
+ << "</cctk:Simulation>" << endl << endl;
+
+ // store thorn list
+ msgbuf << "<cctk:ThornList rdf:about=\"#ThornList\">" << endl;
+ const int numthorns = CCTK_NumCompiledThorns ();
+ for (int thorn = 0; thorn < numthorns; ++ thorn) {
+ const char* const thornname = CCTK_CompiledThorn (thorn);
+
+ msgbuf << "\t<cctk:containsThorn rdf:resource=\"#Thorns/"
+ << thornname << "\"/>" << endl;
+ }
+ msgbuf << "</cctk:ThornList>" << endl << endl;
+
+ // store parameter file contents
+ msgbuf << "<cctk:ParameterFile rdf:about=\"#ParameterFile\">" << endl
+ << "\t<rdf:value>";
+ ifstream file (parfile.c_str());
+ char c;
+ ostringstream filebuf;
+ while (filebuf and file.get (c)) filebuf.put (c);
+ file.close();
+ msgbuf << clean (filebuf.str());
+ filebuf.clear();
+ msgbuf << "</rdf:value>" << endl
+ << "</cctk:ParameterFile>" << endl << endl;
+
+ // store all parameters which have been set in the parfile
+ msgbuf
+<< "<!-- ============================================================ -->" << endl
+<< "<!-- thorn graphs with their parameters -->" << endl
+<< "<!-- ============================================================ -->" << endl;
+ ostringstream parambuf;
+ parambuf
+<< "<!-- ============================================================ -->" << endl
+<< "<!-- list of parameters and their values -->" << endl
+<< "<!-- ============================================================ -->" << endl;
+
+ for (int thorn = 0; thorn < numthorns; ++ thorn) {
+ const char* const thornname = CCTK_CompiledThorn (thorn);
+
+ msgbuf << "<cctk:Thorn rdf:about=\"#Thorns/"
+ << thornname << "\">" << endl;
+ msgbuf << "\t<cctk:hasName>" << thornname << "</cctk:hasName>" << endl;
+
+ // skip parameters that belong to inactive thorns
+ const bool is_active = CCTK_IsThornActive (thornname);
+ msgbuf << "\t<cctk:isActive>" << (is_active ? "true" : "false")
+ << "</cctk:isActive>" << endl;
+
+ // loop over all parameters of this thorn (if it is active)
+ if (is_active) {
+ for (int first = 1; ; first = 0) {
+ char* fullname = NULL;
+ const cParamData* pdata = NULL;
+
+ // get the first/next parameter
+ const int ierr = CCTK_ParameterWalk (first, thornname,&fullname,&pdata);
+ assert (ierr >= 0);
+ if (ierr > 0) break;
+
+ msgbuf << "\t<cctk:hasParameter rdf:resource=\"#Parameters/"
+ << pdata->thorn << "/" << pdata->name << "\"/>" << endl;
+
+ // get its value
+ const void* const pvalue
+ = CCTK_ParameterGet (pdata->name, pdata->thorn, NULL);
+ assert (pvalue);
+
+ switch (pdata->type) {
+ case PARAMETER_BOOLEAN:
+ {
+// CCTK_INT default_value;
+// const int ierr = CCTK_SetBoolean (&default_value, pdata->defval);
+// assert (! ierr);
+ const CCTK_INT value = * static_cast<const CCTK_INT*> (pvalue);
+// if (pdata->n_set > 0 or value != default_value) {
+ parambuf << "<cctk:BooleanParameter rdf:about=\"#Parameters/"
+ << pdata->thorn << "/" << pdata->name << "\">" << endl
+ << "\t<cctk:hasName>" << fullname
+ << "</cctk:hasName>" << endl
+ << "\t<cctk:hasValue>" << (value ? "true" : "false")
+ << "</cctk:hasValue>" << endl
+ << "</cctk:BooleanParameter>" << endl;
+ }
+// }
+ break;
+
+ case PARAMETER_INT:
+ {
+// const CCTK_INT default_value = strtol (pdata->defval, 0, 0);
+ const CCTK_INT value = *static_cast<const CCTK_INT*> (pvalue);
+// if (pdata->n_set > 0 or value != default_value) {
+ parambuf << "<cctk:IntegerParameter rdf:about=\"#Parameters/"
+ << pdata->thorn << "/" << pdata->name << "\">" << endl
+ << "\t<cctk:hasName>" << fullname
+ << "</cctk:hasName>" << endl
+ << "\t<cctk:hasValue>" << value
+ << "</cctk:hasValue>" << endl
+ << "</cctk:IntegerParameter>" << endl;
+// }
+ }
+ break;
+
+ case PARAMETER_REAL:
+ {
+// char* const default_string = strdup (pdata->defval);
+// assert (default_string);
+ // Convert "d" and "D" to "e" and "E", because this is what
+ // strtod() expects
+// for (char* p = default_string; *p; ++p) {
+// switch (*p) {
+// case 'd': *p = 'e'; break;
+// case 'D': *p = 'E'; break;
+// }
+// }
+// CCTK_REAL const default_value = strtod (default_string, 0);
+// free (default_string);
+ CCTK_REAL const value = *static_cast<CCTK_REAL const *> (pvalue);
+// if (pdata->n_set > 0 or value != default_value) {
+ parambuf << "<cctk:RealParameter rdf:about=\"#Parameters/"
+ << pdata->thorn << "/" << pdata->name << "\">" << endl
+ << "\t<cctk:hasName>" << fullname
+ << "</cctk:hasName>" << endl
+ << "\t<cctk:hasValue>" << value
+ << "</cctk:hasValue>" << endl
+ << "</cctk:RealParameter>" << endl;
+// }
+ }
+ break;
+
+ case PARAMETER_KEYWORD:
+ {
+ const char* const value = *static_cast<const char*const*>(pvalue);
+// if (pdata->n_set > 0 or Util_StrCmpi (pdata->defval, value)) {
+ parambuf << "<cctk:KeywordParameter rdf:about=\"#Parameters/"
+ << pdata->thorn << "/" << pdata->name << "\">" << endl
+ << "\t<cctk:hasName>" << fullname
+ << "</cctk:hasName>" << endl
+ << "\t<cctk:hasValue>" << clean (value)
+ << "</cctk:hasValue>" << endl
+ << "</cctk:KeywordParameter>" << endl;
+// }
+ }
+ break;
+
+ case PARAMETER_STRING:
+ {
+ const char* const value = *static_cast<const char*const*>(pvalue);
+// if (pdata->n_set > 0 or strcmp (pdata->defval, value)) {
+ parambuf << "<cctk:StringParameter rdf:about=\"#Parameters/"
+ << pdata->thorn << "/" << pdata->name << "\">" << endl
+ << "\t<cctk:hasName>" << fullname
+ << "</cctk:hasName>" << endl
+ << "\t<cctk:hasValue>" << clean (value)
+ << "</cctk:hasValue>" << endl
+ << "</cctk:StringParameter>" << endl;
+// }
+ }
+ break;
+
+ default: assert (0 and "invalid parameter type");
+
+ } // switch (pdata->type)
+
+ free (fullname);
+ } // loop over all parameters of this thorn
+ } // if (is_active)
+
+ msgbuf << "</cctk:Thorn>" << endl;
+
+ } // loop over all thorns
+
+ // FIXME: is there some better method for concatenation ??
+ msgbuf << endl << parambuf.str();
+
+ //
+ // close the RDF/XML document
+ //
+ msgbuf << endl << "</rdf:RDF>" << endl;
}
@@ -80,19 +326,15 @@ namespace Formaline
~ rdf ()
{
DECLARE_CCTK_PARAMETERS;
-
+
string const socket_script = "socket-client.pl";
string const socket_data = "socket-data";
-
-
-
+
+
+
// Write the data
- msgbuf
-<< "</form:Simulation>" << endl
-<< endl
-<< "</rdf:RDF>" << endl;
string const msgstr = msgbuf.str();
-
+
ostringstream databuf;
databuf << "POST HTTP/1.0 200\r\n"
<< "Content-Type: text/xml\r\n"
@@ -102,19 +344,19 @@ namespace Formaline
<< "\r\n"
<< "\r\n";
string const datastr = databuf.str();
-
+
ostringstream datafilenamebuf;
datafilenamebuf << out_dir << "/" << socket_data;
string const datafilenamestr = datafilenamebuf.str();
char const * const datafilename = datafilenamestr.c_str();
-
+
ofstream datafile;
datafile.open (datafilename, ios::out);
datafile << datastr;
datafile.close ();
-
-
-
+
+
+
// Write the script
ostringstream scriptbuf;
scriptbuf
@@ -178,19 +420,19 @@ namespace Formaline
<< "}" << endl
<< endl;
string const scriptstr = scriptbuf.str();
-
+
ostringstream scriptfilenamebuf;
scriptfilenamebuf << out_dir << "/" << socket_script;
string const scriptfilenamestr = scriptfilenamebuf.str();
char const * const scriptfilename = scriptfilenamestr.c_str();
-
+
ofstream scriptfile;
scriptfile.open (scriptfilename, ios::out);
scriptfile << scriptstr;
scriptfile.close ();
-
-
-
+
+
+
// Check that the file name is sane
if (! is_clean_for_shell (scriptfilename))
{
@@ -202,9 +444,9 @@ namespace Formaline
return;
}
}
-
-
-
+
+
+
// Make the script executable
ostringstream chmodbuf;
chmodbuf << "chmod a+x " << scriptfilenamestr
@@ -212,9 +454,9 @@ namespace Formaline
string const chmodstr = chmodbuf.str();
char const * const chmod = chmodstr.c_str();
system (chmod);
-
-
-
+
+
+
bool my_use_relay_host = use_relay_host;
char const * my_relay_host = 0;
if (my_use_relay_host)
@@ -250,7 +492,7 @@ namespace Formaline
// Don't know a good relay host; try without
my_use_relay_host = false;
}
-
+
if (verbose)
{
if (my_use_relay_host)
@@ -265,7 +507,7 @@ namespace Formaline
}
}
}
-
+
if (my_use_relay_host)
{
// Check that the relay host name is sane
@@ -280,9 +522,9 @@ namespace Formaline
}
}
}
-
-
-
+
+
+
char cwd[10000];
if (my_use_relay_host)
{
@@ -297,7 +539,7 @@ namespace Formaline
return;
}
}
-
+
// Check that the current directory name is sane
if (! is_clean_for_shell (cwd))
{
@@ -314,9 +556,9 @@ namespace Formaline
{
cwd[0] = '\0';
}
-
-
-
+
+
+
// Send the data
ostringstream cmdbuf;
if (my_use_relay_host)
@@ -331,7 +573,7 @@ namespace Formaline
}
string const cmdstr = cmdbuf.str();
char const * const cmd = cmdstr.c_str();
-
+
int const ierr = system (cmd);
if (ierr != 0)
{
@@ -342,20 +584,20 @@ namespace Formaline
CCTK_WARN (1, "Failed to send data to the rdf");
}
}
-
- remove (datafilename);
+
+// remove (datafilename);
remove (scriptfilename);
}
-
void rdf::
store (char const * const key,
bool const value)
{
+#if 0
ostringstream valuebuf;
valuebuf << (value ? "true" : "false");
-
+
string node;
list<string> const keys = parse (key, node);
string indent_string (NUM_INDENT_SPACES, ' ');
@@ -378,6 +620,7 @@ namespace Formaline
msgbuf << indent_string << "</form:" << * lsi << ">" << endl;
}
msgbuf << endl;
+#endif
}
@@ -386,9 +629,10 @@ namespace Formaline
store (char const * const key,
CCTK_INT const value)
{
+#if 0
ostringstream valuebuf;
valuebuf << value;
-
+
string node;
list<string> const keys = parse (key, node);
string indent_string (NUM_INDENT_SPACES, ' ');
@@ -411,6 +655,7 @@ namespace Formaline
msgbuf << indent_string << "</form:" << * lsi << ">" << endl;
}
msgbuf << endl;
+#endif
}
@@ -419,10 +664,11 @@ namespace Formaline
store (char const * const key,
CCTK_REAL const value)
{
+#if 0
int const prec = numeric_limits<CCTK_REAL>::digits10;
ostringstream valuebuf;
valuebuf << setprecision(prec) << value;
-
+
string node;
list<string> const keys = parse (key, node);
string indent_string (NUM_INDENT_SPACES, ' ');
@@ -445,6 +691,7 @@ namespace Formaline
msgbuf << indent_string << "</form:" << * lsi << ">" << endl;
}
msgbuf << endl;
+#endif
}
@@ -453,9 +700,13 @@ namespace Formaline
store (char const * const key,
char const * const value)
{
+#if 0
+ // don't store keys with empty string values
+ if (not *value) return;
+
ostringstream valuebuf;
valuebuf << value;
-
+
string node;
list<string> const keys = parse (key, node);
string indent_string (NUM_INDENT_SPACES, ' ');
@@ -479,6 +730,7 @@ namespace Formaline
msgbuf << indent_string << "</form:" << * lsi << ">" << endl;
}
msgbuf << endl;
+#endif
}
@@ -488,7 +740,7 @@ namespace Formaline
const
{
ostringstream buf;
-
+
for (string::const_iterator p = txt.begin(); p != txt.end(); ++ p)
{
switch (* p)
@@ -498,12 +750,12 @@ namespace Formaline
default: buf << * p;
}
}
-
+
return buf.str();
}
-
+
static bool
is_clean_for_shell (char const * const str)
{
@@ -531,9 +783,9 @@ namespace Formaline
}
return true;
}
-
-
-
+
+
+#if 0
static list<string>
parse (char const * const key, string& node)
{
@@ -550,7 +802,7 @@ namespace Formaline
node = str.substr (p);
return strs;
}
-
-
-
+#endif
+
+
} // namespace Formaline