• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

cyclus / cyclus / 16384544356

19 Jul 2025 03:38AM UTC coverage: 36.53% (-4.7%) from 41.231%
16384544356

push

github

web-flow
Merge pull request #1881 from dean-krueger/clang-format

Clang Format Cyclus src

1517 of 26176 new or added lines in 94 files covered. (5.8%)

309 existing lines in 19 files now uncovered.

51830 of 141883 relevant lines covered (36.53%)

14764.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.75
/src/agent.cc
1
// Implements the Agent Class
2
#include "agent.h"
3

4
#include <algorithm>
5
#include <iostream>
6
#include <sstream>
7
#include <string>
8

9
#include "context.h"
10
#include "error.h"
11
#include "logger.h"
12
#include "resource.h"
13

14
namespace cyclus {
15

16
// static members
17
int Agent::next_id_ = 0;
18

19
void Agent::InitFrom(Agent* m) {
17,537✔
20
  prototype_ = m->prototype_;
17,537✔
21
  kind_ = m->kind_;
17,537✔
22
  spec_ = m->spec_;
17,537✔
23
  lifetime_ = m->lifetime_;
17,537✔
24
  ctx_ = m->ctx_;
17,537✔
25
}
17,537✔
26

27
std::string Agent::InformErrorMsg(std::string msg) {
×
28
  std::stringstream ret;
×
29
  ret << "A(n) " << spec_ << " named " << prototype_ << " at time "
NEW
30
      << context()->time() << " received the following error:\n"
×
31
      << msg;
×
32
  return ret.str();
×
33
}
×
34

35
void Agent::InfileToDb(InfileTree* qe, DbInit di) {
675✔
36
  std::string proto = qe->GetString("name");
675✔
37
  int lifetime = OptionalQuery<int>(qe, "lifetime", -1);
675✔
38
  di.NewDatum("Agent")
39
      ->AddVal("Prototype", proto)
40
      ->AddVal("Lifetime", lifetime)
41
      ->Record();
2,025✔
42
}
675✔
43

44
void Agent::InitFrom(QueryableBackend* b) {
3,025✔
45
  QueryResult qr = b->Query("Agent", NULL);
3,025✔
46
  prototype_ = qr.GetVal<std::string>("Prototype");
6,050✔
47
  id_ = qr.GetVal<int>("AgentId");
3,025✔
48
  lifetime_ = qr.GetVal<int>("Lifetime");
3,025✔
49
}
3,025✔
50

51
void Agent::Snapshot(DbInit di) {
8,985✔
52
  di.NewDatum("Agent")
53
      ->AddVal("Prototype", prototype_)
8,985✔
54
      ->AddVal("Lifetime", lifetime_)
55
      ->Record();
26,955✔
56
}
8,985✔
57

58
Agent::Agent(Context* ctx)
22,581✔
59
    : ctx_(ctx),
22,581✔
60
      id_(next_id_++),
22,581✔
61
      kind_("Agent"),
×
62
      parent_id_(-1),
22,581✔
63
      enter_time_(-1),
22,581✔
64
      lifetime_(-1),
22,581✔
65
      parent_(NULL),
22,581✔
66
      spec_("UNSPECIFIED") {
22,581✔
67
  ctx_->agent_list_.insert(this);
22,581✔
68
  MLOG(LEV_DEBUG3) << "Agent ID=" << id_ << ", ptr=" << this << " created.";
22,581✔
69
}
22,581✔
70

71
Agent::~Agent() {
20,607✔
72
  MLOG(LEV_DEBUG3) << "Deleting agent '" << prototype() << "' ID=" << id_;
20,607✔
73
  context()->agent_list_.erase(this);
20,607✔
74

75
  std::set<Agent*>::iterator it;
76
  if (parent_ != NULL) {
20,607✔
77
    CLOG(LEV_DEBUG2) << "Agent '" << parent_->prototype()
838✔
NEW
78
                     << "' ID=" << parent_->id() << " has removed child '"
×
NEW
79
                     << prototype() << "' ID=" << id()
×
NEW
80
                     << " from its list of children.";
×
81
    it = find(parent_->children_.begin(), parent_->children_.end(), this);
838✔
82
    if (it != parent_->children_.end()) {
838✔
83
      parent_->children_.erase(it);
84
    }
85
  }
86

87
  // set children's parents to NULL
88
  for (it = children_.begin(); it != children_.end(); ++it) {
20,979✔
89
    Agent* child = *it;
372✔
90
    child->parent_ = NULL;
372✔
91
    child->parent_id_ = -1;
372✔
92
  }
93
}
20,607✔
94

95
std::string Agent::str() {
49✔
96
  std::stringstream ss;
49✔
97
  ss << kind_ << "_" << prototype_ << " ( " << "ID=" << id_
98
     << ", implementation=" << spec_ << ",  name=" << prototype_
99
     << ",  parentID=" << parent_id_ << " ) ";
98✔
100
  return ss.str();
49✔
101
}
49✔
102

103
void Agent::lifetime(int n_timesteps) {
2✔
104
  if (enter_time_ != -1) {
2✔
105
    throw ValueError("cannot set the lifetime of an already-built facility");
2✔
106
  }
107
  lifetime_ = n_timesteps;
1✔
108
}
1✔
109

110
void Agent::lifetime_force(int n_timesteps) {
×
111
  try {
112
    lifetime(n_timesteps);
×
NEW
113
  } catch (ValueError e) {
×
NEW
114
    if (enter_time_ + n_timesteps <= context()->time()) {
×
UNCOV
115
      lifetime(context()->time() - enter_time_ + 1);
×
116
    } else {
NEW
117
      lifetime_ = n_timesteps;
×
118
    }
119
  }
×
120
}
×
121

122
bool Agent::AncestorOf(Agent* other) {
62✔
123
  other = other->parent();
124
  while (other != NULL) {
69✔
125
    if (this == other) return true;
13✔
126
    other = other->parent();
127
  }
128
  return false;
129
}
130

131
bool Agent::DecendentOf(Agent* other) {
81✔
132
  const std::set<Agent*>& children = other->children();
133
  std::set<Agent*>::const_iterator it = children.begin();
134
  for (; it != children.end(); ++it) {
101✔
135
    if (this == *(it) || this->DecendentOf(*(it))) return true;
44✔
136
  }
137
  return false;
138
}
139

140
bool Agent::InFamilyTree(Agent* other) {
34✔
141
  return (this == other) || AncestorOf(other) || DecendentOf(other);
34✔
142
}
143

144
void Agent::Build(Agent* parent) {
17,579✔
145
  CLOG(LEV_DEBUG1) << "Agent '" << prototype()
17,579✔
146
                   << "' is entering the simulation.";
×
147
  CLOG(LEV_DEBUG3) << "It has:";
17,579✔
148
  CLOG(LEV_DEBUG3) << " * Spec: " << spec_;
17,579✔
149
  CLOG(LEV_DEBUG3) << " * ID: " << id();
17,579✔
150

151
  Connect(parent);
17,579✔
152
  enter_time_ = ctx_->time();
17,579✔
153
  EnterNotify();
17,579✔
154
  this->AddToTable();
17,579✔
155
}
17,579✔
156

157
void Agent::EnterNotify() {
18,448✔
158
  ctx_->RegisterAgent(this);
18,448✔
159
}
18,448✔
160

161
void Agent::Connect(Agent* parent) {
19,206✔
162
  if (parent == this) {
19,206✔
163
    throw KeyError("Agent " + prototype() +
×
164
                   "is trying to add itself as its own child.");
×
165
  }
166
  if (parent != NULL) {
19,206✔
167
    parent_ = parent;
2,802✔
168
    parent_id_ = parent->id();
2,802✔
169
    parent->children_.insert(this);
2,802✔
170
  }
171
}
19,206✔
172

173
void Agent::Decommission() {
11,668✔
174
  CLOG(LEV_INFO3) << prototype() << "(" << this << ")"
11,668✔
175
                  << " is being decommissioned";
×
176
  ctx_->NewDatum("AgentExit")
11,668✔
177
      ->AddVal("AgentId", id())
11,668✔
178
      ->AddVal("ExitTime", ctx_->time())
11,668✔
179
      ->Record();
46,672✔
180
  ctx_->UnregisterAgent(this);
11,668✔
181
  ctx_->DelAgent(this);
11,668✔
182
}
11,668✔
183

184
std::string Agent::PrintChildren() {
×
185
  std::stringstream ss("");
×
186
  ss << "Children of " << prototype() << ":" << std::endl;
×
187

188
  std::set<Agent*>::iterator it;
189
  for (it = children_.begin(); it != children_.end(); ++it) {
×
190
    Agent* child = *it;
×
191
    std::vector<std::string> print_outs = GetTreePrintOuts(child);
×
192
    for (int j = 0; j < print_outs.size(); j++) {
×
193
      ss << "\t" << print_outs.at(j);
194
    }
195
  }
×
196
  return ss.str();
×
197
}
×
198

199
std::vector<std::string> Agent::GetTreePrintOuts(Agent* m) {
×
200
  std::vector<std::string> ret;
201
  std::stringstream ss("");
×
202
  ss << m->prototype() << std::endl;
×
203
  ret.push_back(ss.str());
×
204
  std::set<Agent*>::iterator it;
205
  for (it = children_.begin(); it != children_.end(); ++it) {
×
206
    Agent* child = *it;
×
207
    std::vector<std::string> outs = GetTreePrintOuts(child);
×
208
    for (int j = 0; j < outs.size(); j++) {
×
209
      ss.str("");
×
210
      ss << "\t" << outs.at(j) << std::endl;
211
      ret.push_back(ss.str());
×
212
    }
213
  }
×
214
  return ret;
×
215
}
×
216

217
void Agent::AddToTable() {
17,579✔
218
  ctx_->NewDatum("AgentEntry")
17,579✔
219
      ->AddVal("AgentId", id_)
220
      ->AddVal("Kind", kind_)
17,579✔
221
      ->AddVal("Spec", spec_)
17,579✔
222
      ->AddVal("Prototype", prototype_)
17,579✔
223
      ->AddVal("ParentId", parent_id_)
224
      ->AddVal("Lifetime", lifetime_)
225
      ->AddVal("EnterTime", enter_time_)
226
      ->Record();
87,895✔
227
}
17,579✔
228
}  // namespace cyclus
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc