diff options
author | tradke <tradke@83718e91-0e4f-0410-abf4-91180603181f> | 2007-08-29 12:34:48 +0000 |
---|---|---|
committer | tradke <tradke@83718e91-0e4f-0410-abf4-91180603181f> | 2007-08-29 12:34:48 +0000 |
commit | fd620c91b16ec633407d347b40315546464a45d4 (patch) | |
tree | 29182b72fe9301397439b2a32850d6281969e34d /src/rdf_publisher.cc | |
parent | f087ca19300df86166e298c305d42143ad67704d (diff) |
- register callbacks for the Publish API
these callbacks collect published information and send it to an external
RDF information service
- use the Publish API to periodically announce simulation metadata runtime
information
git-svn-id: http://svn.cactuscode.org/arrangements/CactusUtils/Formaline/trunk@132 83718e91-0e4f-0410-abf4-91180603181f
Diffstat (limited to 'src/rdf_publisher.cc')
-rw-r--r-- | src/rdf_publisher.cc | 309 |
1 files changed, 309 insertions, 0 deletions
diff --git a/src/rdf_publisher.cc b/src/rdf_publisher.cc new file mode 100644 index 0000000..fd17707 --- /dev/null +++ b/src/rdf_publisher.cc @@ -0,0 +1,309 @@ +// $Header$ + +#include <cassert> +#include <string> +#include <iostream> + +#include "util_Table.h" +#include "cctk.h" +#include "cctk_Arguments.h" +#include "cctk_Parameters.h" + +#include "Publish.h" + +#include "rdf.hh" + + +using std::endl; + +namespace Formaline +{ + +// buffer of published RDF metadata since the last Update() call +std::vector<rdfPublishItem> rdfPublishList; + + +static rdfPublishItem* CreateNewItem (CCTK_POINTER_TO_CONST cctkGH, + CCTK_POINTER data, + CCTK_INT level, + CCTK_STRING name, + CCTK_STRING key, + bool isTable) +{ + DECLARE_CCTK_PARAMETERS; + + // prevent compiler warnings about unused parameters + data = &data; + + assert (key and *key); + assert (level >= 0); + rdfPublishItem* item = NULL; + if (level <= publish_level) { + rdfPublishItem newItem; + rdfPublishList.push_back (newItem); + item = &rdfPublishList.back(); + char* currentdate = Util_CurrentDateTime (); + item->datetime = clean (currentdate); + free (currentdate); + item->hasCCTKinfo = cctkGH != NULL; + if (item->hasCCTKinfo) { + const cGH* _cctkGH = (const cGH*) cctkGH; + item->cctk_time = _cctkGH->cctk_time; + item->cctk_iteration = _cctkGH->cctk_iteration; + } + if (name and *name) item->name = clean (name); + item->key = clean (key); + item->isTable = isTable; + } + return item; +} + +static CCTK_INT PublishBooleanAsRDF (CCTK_POINTER_TO_CONST cctkGH, + CCTK_POINTER data, + CCTK_INT level, + CCTK_INT value, + CCTK_STRING key, + CCTK_STRING name) +{ + rdfPublishItem* item = CreateNewItem (cctkGH, data, level, name, key, false); + if (item) { + item->scalar.value = value ? "true" : "false"; + item->scalar.type = "boolean"; + } + + return (item ? 1 : 0); +} + + +static CCTK_INT PublishIntAsRDF (CCTK_POINTER_TO_CONST cctkGH, + CCTK_POINTER data, + CCTK_INT level, + CCTK_INT value, + CCTK_STRING key, + CCTK_STRING name) +{ + rdfPublishItem* item = CreateNewItem (cctkGH, data, level, name, key, false); + if (item) { + std::ostringstream buf; + buf << value; + item->scalar.value = buf.str(); + item->scalar.type = "int"; + } + + return (item ? 1 : 0); +} + + +static CCTK_INT PublishRealAsRDF (CCTK_POINTER_TO_CONST cctkGH, + CCTK_POINTER data, + CCTK_INT level, + CCTK_REAL value, + CCTK_STRING key, + CCTK_STRING name) +{ + rdfPublishItem* item = CreateNewItem (cctkGH, data, level, name, key, false); + if (item) { + std::ostringstream buf; + buf << value; + item->scalar.value = buf.str(); + item->scalar.type = "double"; + } + + return (item ? 1 : 0); +} + + +static CCTK_INT PublishStringAsRDF (CCTK_POINTER_TO_CONST cctkGH, + CCTK_POINTER data, + CCTK_INT level, + CCTK_STRING value, + CCTK_STRING key, + CCTK_STRING name) +{ + rdfPublishItem* item = CreateNewItem (cctkGH, data, level, name, key, false); + if (item) { + item->scalar.value = clean (value); + item->scalar.type = "string"; + } + + return (item ? 1 : 0); +} + + +static CCTK_INT PublishTableAsRDF (CCTK_POINTER_TO_CONST cctkGH, + CCTK_POINTER data, + CCTK_INT level, + CCTK_INT table, + CCTK_STRING key, + CCTK_STRING name) +{ + rdfPublishItem* item = CreateNewItem (cctkGH, data, level, name, key, true); + if (not item) return 0; + + const int maxkeylen = Util_TableQueryMaxKeyLength (table); + if (maxkeylen <= 0) return (-1); + char* userkey = new char[maxkeylen + 1]; + + int iterator; + for (iterator = Util_TableItCreate (table); + Util_TableItQueryIsNonNull (iterator) > 0; + Util_TableItAdvance (iterator)) { + CCTK_INT typecode, n_elements = -1; + + Util_TableItQueryKeyValueInfo (iterator, maxkeylen + 1, userkey, + &typecode, &n_elements); + if (n_elements <= 0) { + CCTK_VWarn (3, __LINE__, __FILE__, CCTK_THORNSTRING, + "No value provided for user key '%s' in table to be " + "published with key '%s'", userkey, key); + continue; + } + if (n_elements > 1 and + (typecode == CCTK_VARIABLE_INT or typecode == CCTK_VARIABLE_REAL)) { + CCTK_VWarn (3, __LINE__, __FILE__, CCTK_THORNSTRING, + "Array value not supported for user key '%s' in table to " + "be published with key '%s'", userkey, key); + continue; + } else if (typecode != CCTK_VARIABLE_CHAR and + typecode != CCTK_VARIABLE_INT and + typecode != CCTK_VARIABLE_REAL) { + CCTK_VWarn (3, __LINE__, __FILE__, CCTK_THORNSTRING, + "Unsupported datatype '%s' for value with user key '%s' " + "in table to be published with key '%s'", + CCTK_VarTypeName (typecode), userkey, key); + continue; + } + + rdfTableEntry newEntry; + item->table.push_back (newEntry); + rdfTableEntry* entry = &item->table.back(); + entry->key = clean (userkey); + std::ostringstream buf; + if (typecode == CCTK_VARIABLE_CHAR) { + char *buffer = new char[n_elements + 1]; + Util_TableGetString (table, n_elements + 1, buffer, userkey); + entry->value.value = buffer; + delete[] buffer; + } else if (typecode == CCTK_VARIABLE_INT) { + CCTK_INT int_value; + Util_TableGetInt (table, &int_value, userkey); + buf << int_value; + entry->value.value = buf.str(); + entry->value.type = "int"; + } else if (typecode == CCTK_VARIABLE_REAL) { + CCTK_REAL real_value; + Util_TableGetReal (table, &real_value, userkey); + buf << real_value; + entry->value.value = buf.str(); + entry->value.type = "double"; + } else { + assert (0); + } + } + Util_TableItDestroy (iterator); + delete[] userkey; + + return 1; +} + + +static void ParameterSetNotify (void *unused, + const char *thorn, + const char *parameter, + const char *new_value) +{ + unused = &unused; + + const char *name = "Set Parameter"; + std::string key(thorn); key.append ("::"); key.append (parameter); + + int type; + const void* data = CCTK_ParameterGet (parameter, thorn, &type); + + if (type == PARAMETER_KEYWORD || type == PARAMETER_STRING || + type == PARAMETER_SENTENCE) + { + if (CCTK_IsFunctionAliased ("PublishString")) + { + PublishString (NULL, 0, *(const char *const *) data, key.c_str(), name); + } + } + else if (type == PARAMETER_BOOLEAN) + { + if (CCTK_IsFunctionAliased ("PublishBoolean")) + { + PublishBoolean (NULL, 0, *(const CCTK_INT *) data, key.c_str(), name); + } + } + else if (type == PARAMETER_INT) + { + if (CCTK_IsFunctionAliased ("PublishInt")) + { + PublishInt (NULL, 0, *(const CCTK_INT *) data, key.c_str(), name); + } + } + else if (type == PARAMETER_REAL) + { + if (CCTK_IsFunctionAliased ("PublishReal")) + { + PublishReal (NULL, 0, *(const CCTK_REAL *) data, key.c_str(), name); + } + } else { + assert (0 and "invalid parameter type"); + } +} + + +extern "C" +void Formaline_RegisterPublishRDF_Callbacks (void) +{ + int registered = 0; + +#define REGISTER_RDF_CALLBACKS(type) \ + if (CCTK_IsFunctionAliased ("Publish" #type "_Register")) { \ + if (Publish##type##_Register (Publish##type##AsRDF, NULL, \ + "Publish as RDF")) { \ + CCTK_WARN (0, "Failed to register Publish" #type " callback"); \ + } \ + registered++; \ + } + // we don't have a valid cctkGH yet + if (CCTK_MyProc (NULL) == 0) { + REGISTER_RDF_CALLBACKS (Boolean); + REGISTER_RDF_CALLBACKS (Int); + REGISTER_RDF_CALLBACKS (Real); + REGISTER_RDF_CALLBACKS (String); + REGISTER_RDF_CALLBACKS (Table); + if (registered) { + if (CCTK_ParameterSetNotifyRegister (ParameterSetNotify, NULL, + CCTK_THORNSTRING, NULL, NULL)) { + CCTK_VWarn (0, __LINE__, __FILE__, CCTK_THORNSTRING, + "Couldn't register parameter set notify callback"); + } + } else { + CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING, + "Couldn't register Publish RDF callbacks because no thorn " + "provides Publish register API aliases functions"); + } + } +} + +extern "C" +void Formaline_UnregisterPublishRDF_Callbacks (CCTK_ARGUMENTS) +{ +#define UNREGISTER_RDF_CALLBACKS(type) \ + if (CCTK_IsFunctionAliased ("Publish" #type "_Unregister")) { \ + Publish##type##_Unregister ("Publish as RDF"); \ + } + if (CCTK_MyProc (cctkGH) == 0) { + CCTK_ParameterSetNotifyUnregister (CCTK_THORNSTRING); + UNREGISTER_RDF_CALLBACKS (Boolean); + UNREGISTER_RDF_CALLBACKS (Int); + UNREGISTER_RDF_CALLBACKS (Real); + UNREGISTER_RDF_CALLBACKS (String); + UNREGISTER_RDF_CALLBACKS (Table); + } +} + + +} // namespace Formaline |