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

nasa / trick / 25456501308

06 May 2026 07:29PM UTC coverage: 55.935% (-0.8%) from 56.7%
25456501308

Pull #2011

github

web-flow
Merge 7ad262960 into 7054e405e
Pull Request #2011: Single-file CI and code style adoption

14612 of 26123 relevant lines covered (55.94%)

462107.16 hits per line

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

76.81
/trick_source/sim_services/MonteCarloGeneration/mc_variable.cc
1
/*******************************TRICK HEADER******************************
2
PURPOSE: ( Base class for the MonteCarloVariable type)
3

4
PROGRAMMERS:
5
  (((Gary Turner) (OSR) (October 2019) (Antares) (Initial)))
6
  (((Isaac Reaves) (NASA) (November 2022) (Integration into Trick Core)))
7
**********************************************************************/
8

9
#include "trick/mc_variable.hh"
10
#include "trick/message_proto.h"
11
#include "trick/message_type.h"
12
#include "trick/exec_proto.h"
13
#include <sstream> // ostringstream
14

15
/*****************************************************************************
16
Constructor
17
*****************************************************************************/
18
MonteCarloVariable::MonteCarloVariable(
519✔
19
    const std::string & var_name)
519✔
20
  :
21
  units(),
519✔
22
  include_in_summary(true),
519✔
23
  variable_name(var_name),
519✔
24
  assignment(),
519✔
25
  command(),
519✔
26
  type(Undefined)
519✔
27
{}
519✔
28

29
/*****************************************************************************
30
insert_units
31
Purpose:(Provides a unit-conversion insertion into an established command)
32
*****************************************************************************/
33
void
34
MonteCarloVariable::insert_units()
337✔
35
{
36
  // If no units specified, nothing to do here.
37
  if (units.empty()) {
337✔
38
    return;
334✔
39
  }
40

41
  // Unreachable code in current implementation.  insert_units is only ever
42
  // called immediately after generate_command, which cannot possibly generate
43
  // an empty string.
44
  if (command.empty()) {
3✔
45
    std::string message = 
46
      std::string("File: ") + __FILE__ + ", Line: " + 
×
47
      std::to_string(__LINE__) + ", Sequencing error\nVariable " + 
×
48
      variable_name.c_str() + "  has units specified (" + units.c_str() + 
×
49
      ") but no command generated.\nThe command must be generated before " + 
50
      "applying units.Will attempt to generate the command to "
51
      "avoid terminal fault but this\nmay not be what was intended.\n";
×
52
    message_publish(MSG_ERROR, message.c_str());
×
53
  }
×
54
  // parse the command
55
  size_t pos_equ = command.find("=");
3✔
56
  if (pos_equ == std::string::npos) {
3✔
57
      // Unreachable code in current implementation.  insert_units is only ever
58
      // called immediately after generate_command, and even if all else fails,
59
      // generate_command produces a command with an = symbol in it, So an =
60
      // will always be found.
61
    std::string message = 
62
      std::string("File: ") + __FILE__ + ", Line: " + 
×
63
      std::to_string(__LINE__) + " Invalid command\nFor variable " + 
×
64
      variable_name.c_str() + ", the command is poorly formed.\nCannot " +
×
65
      "apply units to this command.\n";
×
66
    message_publish(MSG_ERROR, message.c_str());
×
67
    exec_terminate_with_return(1, __FILE__, __LINE__, message.c_str());
×
68
    return;
×
69
  }
×
70

71
  // TODO: Pick a unit-conversion mechanism
72
  //       Right now, the only one available is Trick:
73
  trick_units( pos_equ+1);
3✔
74
}
75

76
/*****************************************************************************
77
trick_units
78
Purpose:(inserts the Trick unit conversion mechanism into an established
79
         command string.)
80
*****************************************************************************/
81
void
82
MonteCarloVariable::trick_units(
3✔
83
    size_t insertion_pt)
84
{
85
  command.insert(insertion_pt, " trick.attach_units(\"" + units + "\",");
3✔
86
  command.append(")");
3✔
87
}
3✔
88

89
/*****************************************************************************
90
generate_assignment_internal
91
Purpose:()
92
*****************************************************************************/
93
void
94
MonteCarloVariable::assign_double(
230✔
95
  double value)
96
{
97
  std::ostringstream ostring;
230✔
98
  ostring.precision(16);
230✔
99
  ostring << value;
230✔
100
  assignment = ostring.str();
230✔
101
  generate_command();
230✔
102
  insert_units();
230✔
103
}
230✔
104
/****************************************************************************/
105
void
106
MonteCarloVariable::assign_int(
17✔
107
  int value)
108
{
109
  assignment = std::to_string(value);
17✔
110
  generate_command();
17✔
111
  insert_units();
17✔
112
}
17✔
113
/****************************************************************************/
114
void
115
MonteCarloVariable::generate_command()
345✔
116
{
117
  command = "\n" + variable_name + " = " + assignment;
345✔
118
}
345✔
119

120
/*****************************************************************************
121
get_type_str
122
Purpose:(Return the type of this variable as string. Meant to be overridden
123
  by derived classes)
124
*****************************************************************************/
125
std::string MonteCarloVariable::get_type_str() const
64✔
126
{
127
  switch (get_type()) {
64✔
128
  // Unreachable case in current implementation.
129
  // All current variable classes have been given a "type"
130
  default:
×
131
    return(std::string("Undefined_type"));
×
132
    break;
133
  case MonteCarloVariable::Calculated:
3✔
134
    return(std::string("Calculated"));
6✔
135
    break;
136
  case MonteCarloVariable::Constant:
12✔
137
    return(std::string("Constant"));
24✔
138
    break;
139
  case MonteCarloVariable::Execute:
6✔
140
    return(std::string("Execute"));
12✔
141
    break;
142
  case MonteCarloVariable::Prescribed:
9✔
143
    return(std::string("Prescribed"));
18✔
144
    break;
145
  case MonteCarloVariable::Random:
34✔
146
    return(std::string("Random"));
68✔
147
    break;
148
  }
149
}
150

151

152
/*****************************************************************************
153
summarize_variable
154
Purpose:(Provide a string summarizing the attributes of this MonteCarloVariable)
155
*****************************************************************************/
156
std::string MonteCarloVariable::summarize_variable() const
64✔
157
{
158
  std::ostringstream ss;
64✔
159
  ss << variable_name << std::string(": type=") << get_type_str();
128✔
160
  return (ss.str());
128✔
161
}
64✔
162

163

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