summaryrefslogtreecommitdiffstats
path: root/src/log/RBACoverageLog.cpp
blob: ca64f1414b251f1b71a4723bd5386d9c2d65787a (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
351
352
353
354
355
356
357
/**
 * Copyright (c) 2019 DENSO CORPORATION.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Coverage Log class definition file
 */

#include <algorithm>
#include <sstream>
#include "RBACoverageLog.hpp"

#include "RBAExpression.hpp"
#include "RBALog.hpp"
#include "RBAAbstractConstraint.hpp"
#include "RBAConstraintImpl.hpp"
#include "RBAILogCollector.hpp"
#include "RBAExpressionType.hpp"
#include "RBAConstraintInfo.hpp"

namespace rba
{

#ifdef RBA_USE_LOG
const std::string RBACoverageLog::SEP = ",";
const std::string RBACoverageLog::EXP_SEP = "\t";
const std::string RBACoverageLog::START = "START";
const std::string RBACoverageLog::END = "END";
const std::string RBACoverageLog::EXPRESSION = "EXPRESSION";
const std::string RBACoverageLog::CONSTRAINT = "Constraint";
const std::string RBACoverageLog::RULE = "Rule";

/**
 * Add request log in one line
 *
 * @param message log message
 */
void
RBACoverageLog::addRequestLogLine(const std::string& message)
{
  notify("#Request#" + message);
}

/**
 * Add allocate setting log in one line
 *
 * @param message log 
 */
void
RBACoverageLog::addPrevResultLogLine(const std::string& message)
{
  notify("#PrevResult#" + message);
}

/**
 * Add result log in one line
 *
 * @param message
 */
void
RBACoverageLog::addResultLogLine(const std::string& message)
{
  notify("#Result#" + message);
}

void
RBACoverageLog::addStartLogLine(const std::string& log)
{
  notify(log);
}

void
RBACoverageLog::addCoverageLogCollector(RBAILogCollector* collector)
{
  collectors_.insert(collector);
}

void
RBACoverageLog::removeCoverageLogCollector(RBAILogCollector* collector)
{
  auto it = std::find(collectors_.begin(), collectors_.end(), collector);
  if(it != collectors_.end()) {
    collectors_.erase(it);
  }
}

/**
 * Add "Constraint" log in one line
 *
 * @param message
 */
void
RBACoverageLog::addConstraintLogLine(const std::string& message)
{
  notify("#Constraint#" + message);
}

/**
 * Add "Constraint" start log
 *
 * @param constraint
 */
void
RBACoverageLog::addConstraintStartLog(const RBAAbstractConstraint* element)
{
  std::string isRuntime = "t";
  std::string eleName = element->getElementName();
  if(element->isConstraint()) {
    const RBAConstraintImpl* constraint
      = dynamic_cast<const RBAConstraintImpl*>(element);
    if(!constraint->isRuntime()) {
      isRuntime = "f";
    }
  }
  addConstraintLogLine(START + SEP + eleName + SEP + isRuntime);
}

/**
 * Add "Constraint" finish log
 *
 * @param constraint
 */
void
RBACoverageLog::addConstraintEndLog(const RBAAbstractConstraint* element)
{
  addConstraintLogLine(END);
}

/**
 * Add "Constraint expression" execution log
 *
 * @param expressionText
 * @param result
 */
void
RBACoverageLog::addConstraintExpressionLog(
    const std::string& expressionText, RBAExecuteResult result)
{
  std::string resultMsg;
  if (RBAExecuteResult::TRUE == result) {
    resultMsg = "t";
  } else if (RBAExecuteResult::FALSE == result) {
    resultMsg = "f";
  } else if (RBAExecuteResult::EXE == result) {
    resultMsg = "e";
  } else if (RBAExecuteResult::NOTEXE == result) {
    resultMsg = "ne";
  } else {
    resultMsg = "skip";
  }
  addConstraintLogLine(
      EXPRESSION + EXP_SEP + getHierarchy() + EXP_SEP + expressionText + EXP_SEP
          + resultMsg);
}

/**
 * Add "Constraint expression" execution log (For Action)
 *
 * @param expressionText
 */
void
RBACoverageLog::addConstraintExpressionLog(const std::string& expressionText)
{
  addConstraintLogLine(EXPRESSION + EXP_SEP +
		       getHierarchy() + EXP_SEP +
		       expressionText);
}

/**
 * Add request cancellation log in one line
 *
 * @param message
 */
void
RBACoverageLog::addCanceledRequestLogLine(const std::string& message)
{
  notify("#CanceledRequest#" + message);
}

/**
 * Add constraint structure log in one line
 *
 * @param message
 */
void
RBACoverageLog::addHierarchyOfConstraintLogLine(const std::string& message)
{
  notify("#HierarchyOfConstraint#" + message);
}

/**
 * Add constraint structure start log in one line
 *
 * @param message
 */
void

RBACoverageLog::
addHierarchyOfConstraintStartLog(const RBAAbstractConstraint* element)
{
  std::string isRuntime = "t";
  std::string type = CONSTRAINT;
  std::string eleName = element->getElementName();
  if(element->isConstraint()) {
    const RBAConstraintImpl* constraint
      = dynamic_cast<const RBAConstraintImpl*>(element);
    if(!constraint->isRuntime()) {
      isRuntime = "f";
    }
  }
  else if(element->isRule()) {
    type = RULE;
  }
  addHierarchyOfConstraintLogLine(START + SEP +
                                  eleName + SEP +
                                  isRuntime + SEP + type);
}

/**
 * Add constraint structure finish log 
 *
 * @param constraint
 */
void
RBACoverageLog::addHierarchyOfConstraintEndLog(const RBAAbstractConstraint* element)
{
  std::string eleName = element->getElementName();
  addHierarchyOfConstraintLogLine(END + SEP + eleName);
}

/**
 * Add constraint structure execution log
 *
 * @param expressionText
 * @param result
 */
void
RBACoverageLog::
addHierarchyOfConstraintExpressionLog(const std::string& expressionText,
				      const RBAExpression* expression)
{
  addHierarchyOfConstraintLogLine(EXPRESSION + EXP_SEP +
				  getHierarchy() + EXP_SEP +
				  expressionText + EXP_SEP +
				  getExpressionType(expression));
}

// ---------------------------------------------------------
// Hierarchical information of constraint log for coverage
// ---------------------------------------------------------

/**
 * Initialize the constraint hierarchy
 */
void
RBACoverageLog::initConstraintHierarchy()
{
  hierarchys_.clear();
}

/**
 * Returns the constraint hierarchy as a string
 *
 * @return
 */
std::string
RBACoverageLog::getHierarchy() const
{
  std::ostringstream oss;

  std::string pre;
  int32_t i=0;
  for(std::string now : hierarchys_) {
    if((i != 0) && (pre[0] != '#')) {
      // If it is the first time and it is not treated as multiple operators,
      // add split identifier
      oss << "#";
    }
    pre = now;
    if(now[0] == '#') {
      now = now.substr(1, now.length());
    }
    oss << now;
    i++;
  }

  return oss.str();
}

/**
 * Add constraint hierarchy
 *
 * @param data
 * @return
 */
bool
RBACoverageLog::addHierarchy(const std::string& data)
{
  hierarchys_.push_back(data);
  return true;
}

/**
 * Remove constraint hierarchy
 *
 * @param data
 * @return
 */
void
RBACoverageLog::removeHierarchy()
{
  hierarchys_.pop_back();
}

/**
 * Returns the type string of the expression 
 * to be output to the hierarchical log
 *
 * @param expression
 * @return
 */
std::string
RBACoverageLog::getExpressionType(const RBAExpression* expression) const
{
  switch(expression->getUnderlyingType()) {
  case RBAExpressionType::BOOLEAN:
    return "boolean";
  case RBAExpressionType::ACTION:
  case RBAExpressionType::SET_OF_ACTION:
    return "action";
  default:
    return "unknown";
  }
}

void
RBACoverageLog::notify(const std::string& log)
{
  for(RBAILogCollector* collector : collectors_) {
    collector->log(log);
  }
}
#endif

}