aboutsummaryrefslogtreecommitdiff
path: root/src/rdf_publisher.cc
blob: a2566688893e818d7a0f138939b1c79004e9ee94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include <algorithm>
#include <cassert>
#include <cctype>
#include <iostream>
#include <map>
#include <set>
#include <string>

#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 *,
                                const char *thorn,
                                const char *parameter,
                                const char *new_value)
{
  DECLARE_CCTK_PARAMETERS;

  // should parameter changes be logged at all ?
  if (nr_of_parameter_changes_to_be_logged == 0) return;

  // reparse the "steered_parameters_log_exclusion_list" if it has changed
  int timesSet =
    CCTK_ParameterQueryTimesSet("steered_parameters_log_exclusion_list",
                                CCTK_THORNSTRING);
  static int lastTimesSet = -1;
  static std::set<std::string> exclusionList;
  if (lastTimesSet != timesSet) {
    exclusionList.clear();
    std::string parseString(steered_parameters_log_exclusion_list);
    const std::string whitespaces(" \t\n");
    while (1) {
      std::string::size_type start = parseString.find_first_not_of(whitespaces);
      if (start == std::string::npos) break;
      std::string::size_type end = parseString.find_first_of(whitespaces,start);
      if (end == std::string::npos) end = parseString.length();
      std::string parameter(parseString, start, end - start);
      std::transform(parameter.begin(), parameter.end(), parameter.begin(),
                     tolower);
      exclusionList.insert(parameter);
      parseString.erase(0, end);
    }
    lastTimesSet = timesSet;
  }


  const char *name = "Steer parameter";
  std::string key(thorn); key.append ("::"); key.append (parameter);

  // // do not log steering events for parameters in the log exclusion list
  std::string lcKey(key);
  std::transform(lcKey.begin(), lcKey.end(), lcKey.begin(), tolower);
  if (exclusionList.find(lcKey) != exclusionList.end()) return;

  // check if the maximum number of parameter change logs has been reached
  static std::map<std::string, int> logList;
  logList[lcKey]++;
  if (nr_of_parameter_changes_to_be_logged > 0 and
      logList[lcKey] >= nr_of_parameter_changes_to_be_logged) return;

  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 (CCTK_ARGUMENTS)
{
  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