COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
agent_calc.cpp File Reference

Demonstration calculator Agent. More...

#include "support/configCosmos.h"
#include <stdio.h>
#include <iostream>
#include "agent/agentclass.h"
#include "physics/physicslib.h"
#include "support/jsonlib.h"
#include "support/jsondef.h"
Include dependency graph for agent_calc.cpp:

Macros

#define MAXBUFFERSIZE   100000
 

Functions

int myagent ()
 
int32_t request_get_value (string &request, string &response, Agent *agent)
 
int32_t request_add (string &request, string &response, Agent *agent)
 
int32_t request_sub (string &request, string &response, Agent *agent)
 
int32_t request_mul (string &request, string &response, Agent *agent)
 
int32_t request_div (string &request, string &response, Agent *agent)
 
int32_t request_change_node_name (string &request, string &response, Agent *agent)
 
void replace (std::string &str, const std::string &from, const std::string &to)
 
int main (int argc, char *argv[])
 

Variables

static char nodename [COSMOS_MAX_NAME+1] = "sat_001"
 
static char agentname [COSMOS_MAX_NAME+1] = "calc"
 
static Agentagent
 

Detailed Description

Demonstration calculator Agent.

Macro Definition Documentation

#define MAXBUFFERSIZE   100000

Function Documentation

int myagent ( )
379 {
380  // Start performing the body of the agent
381  while(agent->running())
382  {
383  COSMOS_SLEEP(0.1); // no support in win
384  }
385 
386  return 0;
387 }
uint16_t running()
Check if we&#39;re supposed to be running.
Definition: agentclass.cpp:391
static Agent * agent
Definition: agent_calc.cpp:75
int32_t request_get_value ( string &  request,
string &  response,
Agent agent 
)
int32_t request_add ( string &  request,
string &  response,
Agent agent 
)
391 {
392  float a,b;
393  cout<<"request.size() = "<<request.size()<<endl;
394  cout<<"request.length() = "<<request.length()<<endl;
395  sscanf(request.c_str(),"%*s %f %f",&a,&b);
396  response = std::to_string(a + b);
397 
398  return 0;
399 }
string to_string(char *value)
Definition: stringlib.cpp:220
long b
Definition: jpegint.h:371
Definition: eci2kep_test.cpp:33
int32_t request_sub ( string &  request,
string &  response,
Agent agent 
)
403 {
404  float a,b;
405 
406  sscanf(request.c_str(),"%*s %f %f",&a,&b);
407  response = std::to_string(a - b);
408 
409  return 0;
410 }
string to_string(char *value)
Definition: stringlib.cpp:220
long b
Definition: jpegint.h:371
Definition: eci2kep_test.cpp:33
int32_t request_mul ( string &  request,
string &  response,
Agent agent 
)
413 {
414  float a,b;
415 
416  sscanf(request.c_str(),"%*s %f %f", &a, &b);
417  response = std::to_string(a * b);
418 
419  return 0;
420 }
string to_string(char *value)
Definition: stringlib.cpp:220
long b
Definition: jpegint.h:371
Definition: eci2kep_test.cpp:33
int32_t request_div ( string &  request,
string &  response,
Agent agent 
)
423 {
424  float a,b;
425 
426  sscanf(request.c_str(),"%*s %f %f", &a, &b);
427  response = std::to_string(a / b);
428 
429  return 0;
430 }
string to_string(char *value)
Definition: stringlib.cpp:220
long b
Definition: jpegint.h:371
Definition: eci2kep_test.cpp:33
int32_t request_change_node_name ( string &  request,
string &  response,
Agent agent 
)
433 {
434  char new_name[41];
435  sscanf(request.c_str(),"%*s %40s", new_name);
436 
437  strcpy(agent->cinfo->node.name, new_name);
438  cout << "The new node name is <" << agent->cinfo->node.name << ">" << endl;
439 
440  return 0;
441 }
nodestruc node
Structure for summary information in node.
Definition: jsondef.h:4220
char name[40+1]
Node Name.
Definition: jsondef.h:3556
cosmosstruc * cinfo
Definition: agentclass.h:346
void replace ( std::string &  str,
const std::string &  from,
const std::string &  to 
)
126  {
127  if(from.empty()) return;
128  size_t start_pos = 0;
129  while((start_pos = str.find(from, start_pos)) != std::string::npos) {
130  str.replace(start_pos, from.length(), to);
131  start_pos += to.length();
132  }
133  return;
134 }
const string & str
Definition: json11.cpp:360
int main ( int  argc,
char *  argv[] 
)
139 {
140  int iretn;
141 /*
142  // Make node_name = 1st argument
143  if (argc == 2)
144  strcpy(nodename, argv[1]);
145  // Make agent_proc = 2st argument
146  if (argc == 3) {
147  strcpy(agentname, argv[2]);
148  strcpy(nodename, argv[1]);
149  }
150 */
151  // Initialize agents. Set nodename if provided through command line args
152  if (argc > 1)
153  {
154  // Initialize agent instance with specified node and agent names
155  agent = new Agent(nodename, agentname);
156 
157  // Check if agent was successfully constructed.
158  if ((iretn = agent->wait()) < 0)
159  {
160  fprintf(agent->get_debug_fd(), "%16.10f %s Failed to start Agent %s on Node %s Dated %s : %s\n",currentmjd(), mjd2iso8601(currentmjd()).c_str(), agent->getAgent().c_str(), agent->getNode().c_str(), utc2iso8601(data_ctime(argv[0])).c_str(), cosmos_error_string(iretn).c_str());
161  exit(iretn);
162  }
163  else
164  {
165  fprintf(agent->get_debug_fd(), "%16.10f %s Started Agent %s on Node %s Dated %s\n",currentmjd(), mjd2iso8601(currentmjd()).c_str(), agent->getAgent().c_str(), agent->getNode().c_str(), utc2iso8601(data_ctime(argv[0])).c_str());
166  }
167 
168 
169  cout << "Hello, I am an agent. My name is [" << nodename << ":" << agentname << "]" << endl << endl;
170  }
171  else
172  {
173  // Initialize agent instance with unspecified node name but specified agent name
174  agent = new Agent("otb", agentname);
175 
176  // Check if agent was successfully constructed.
177  if ((iretn = agent->wait()) < 0)
178  {
179  fprintf(agent->get_debug_fd(), "%16.10f %s Failed to start Agent %s on Node %s Dated %s : %s\n",currentmjd(), mjd2iso8601(currentmjd()).c_str(), agent->getAgent().c_str(), agent->getNode().c_str(), utc2iso8601(data_ctime(argv[0])).c_str(), cosmos_error_string(iretn).c_str());
180  exit(iretn);
181  }
182  else
183  {
184  fprintf(agent->get_debug_fd(), "%16.10f %s Started Agent %s on Node %s Dated %s\n",currentmjd(), mjd2iso8601(currentmjd()).c_str(), agent->getAgent().c_str(), agent->getNode().c_str(), utc2iso8601(data_ctime(argv[0])).c_str());
185  }
186 
187 
188  cout << "Hello, I am an agent. My name is [null:" << agentname << "]" << endl << endl;
189  }
190 
191 // hijack agent_calc to demonstrate namespace 2.0
192 
193  // verify the counts
194  size_t n1 = 0;
195  for (uint16_t i=0; i<agent->cinfo->jmap.size(); ++i) {
196  for (uint16_t j=0; j<agent->cinfo->jmap[i].size(); ++j) {
197  n1++;
198  }
199  }
200  cout<<"There were "<<n1<<" entries in namespace 1.0 and "<<agent->cinfo->jmapped<<" is the value of jmapped."<<endl;
201 
202  cout<<"\nWould you like to see the names from Namespace 1.0? (y/n) : ";
203  char see;
204  cin>>see;
205  if(see == 'Y' || see == 'y') {
206  cout<<endl;
207  // printout all namespace 1.0 names
208  for (uint16_t i=0; i<agent->cinfo->jmap.size(); ++i) {
209  for (uint16_t j=0; j<agent->cinfo->jmap[i].size(); ++j) {
210  cout << "jmap[" << i << "]["<<j<<"] :\t" << agent->cinfo->jmap[i][j].name << endl;
211  }
212  }
213  }
214 
215 /* old namespace 1.0 way of getting values with json_out(..)
216  cout << agent->cinfo->node.name << endl;
217 
218  string jsp;
219  json_out(jsp, "node_name", agent->cinfo);
220  cout << "<" << jsp << ">" << endl;
221  jsp.clear();
222  json_out(jsp, "event_name", agent->cinfo);
223  cout << "before <" << jsp << ">" << endl;
224 
225  //set_json_value("event_name", "EVENT NAME");
226  strcpy(agent->cinfo->event[0].name, "NEW NAME");
227  jsp.clear();
228  json_out(jsp, "event_name", agent->cinfo);
229 
230  cout << " after <" << jsp << ">" << endl;
231 
232  agent->cinfo->equation.push_back(equationstruc{ "string1", "string2" });
233 */
234 
235 
236 // see if we can print out entire cosmosstruc in JSON
237 
238  //agent->cinfo->add_name("Entire COSMOSSTRUC", agent->cinfo);
239  //cout<<"Output:\n\t<"<<agent->cinfo->get_json<cosmosstruc>("Entire COSMOSSTRUC")<<">"<<endl;
240 
241  //cout<<"all done";
242  //cout<<"The size of the namespace for cosmosstruc using namespace 2.0 is "<<agent->cinfo->size()<<"."<<endl;
243 
244  cout<<"\nFor my next trick... I will add all the default names supported by Namespace 2.0"<<endl;
245 
247 
248  cout<<"\nThe size of the namespace for cosmosstruc using namespace 2.0 is "<<agent->cinfo->size()<<"."<<endl;
249 
250  cout<<"\nWould you like to see the names from Namespace 2.0? (y/n) : ";
251  cin>>see;
252  if(see == 'Y' || see == 'y') {
253  // printout all namespace 2.0 names
254  cout<<endl;
256  }
257 
258  cout<<"\nWould you like to see the names and types from Namespace 2.0? (y/n) : ";
259  cin>>see;
260  if(see == 'Y' || see == 'y') {
261  // printout all namespace 2.0 names and values
262  cout<<endl;
264  }
265 
266  cout<<"\nWould you like to see the names, types, and values from Namespace 2.0? (y/n) : ";
267  cin>>see;
268  if(see == 'Y' || see == 'y') {
269  // printout all namespace 2.0 names and values
270  cout<<endl;
272  }
273 
274 
275 
276 // need support for decimal points DONE!
277 // need support for negative numbers?
278 // need to convert contents of output into a double stack-wise DONE!
279 // need to work without leading 0 before decimal?
280 
281  cout<<"\nFor my next trick I will try to implement to concept of equations for Namespace 2.0"<<endl;
282  // given a string with parenthesis (), algebraic operators +-*/^, numbers (treated as doubles), and name from the namespace whose value is numbers
283 
284  // all results are doubles
285  // ignore whitespace
286 
287 
288 
289  //string test_equation = "1.23456 + 4.4 * 0.000000000000000000002 / ( 12345678901234567890 - 555.4321 ) ^ 2 ^ 3";
290  //string test_equation = "3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3";
291  //string test_equation = "9 - 5 / (8 - 3) * 2 + 6" ;
292  //string test_equation = "+0.5 * 8 + .6 / 6 - 12 * 2 " ;
293  //string test_equation = "(5) * (8) + (6) / (6) - (12) * (2) " ;
294 
295 // this dumps cuz * - need parenthesis around negatives
296  string test_equation = "0.5 * (-8) + .6 / 6 - 12 * 2 " ;
297 //wrong answer cuz + -
298  //string test_equation = "(-1) * (-8 + -2)";
299  //string test_equation = "(-1) * (-8 -2)";
300 // first need to be able to read tokens, which is either operator or number (or, later) variable names
301 
302 
303  string str = test_equation;
304 
305 cout<<"but seriously... let's calculate already"<<endl;
306 
307 //cout<<std::setprecision(std::numeric_limits<double>::digits10)<<"ANSWER == <"<<equationator(str)<<">"<<endl;
308 cin.clear();
309 cin.ignore(100000,'\n');
310 
311 double answer = 0;
312 while(answer != -1) {
313  cout<<"Please enter an equation:\t";
314  string eq;
315  getline(cin,eq);
316  answer = agent->cinfo->equationator(eq);
317  cout<<"The answer is "<<std::setprecision(std::numeric_limits<double>::digits10)<<answer<<endl;
318 }
319 
320 /*
321 
322  string js = agent->cinfo->get_json<cosmosstruc>("Entire COSMOSSTRUC");
323  cout<<"Output:<\n"<<js<<"\n>"<<endl;
324 
325  devicestruc d;
326  agent->cinfo->device.push_back(d);
327 
328  js = agent->cinfo->get_json_pretty<cosmosstruc>("Entire COSMOSSTRUC");
329  cout<<"Output:<\n"<<js<<"\n>"<<endl;
330 
331  agent->cinfo->add_name("Entire COSMOSSTRUC Equations", &agent->cinfo->equation);
332  cout<<"Output:\n\t<\n"<<agent->cinfo->get_json<vector<equationstruc>>("Entire COSMOSSTRUC Equations")<<">"<<endl;
333  cout<<"Output:\n\t<\n"<<agent->cinfo->get_json_pretty<vector<equationstruc>>("Entire COSMOSSTRUC Equations")<<">"<<endl;
334 
335  agent->cinfo->add_default_names();
336  agent->cinfo->print_all_names();
337  //cout<<agent->cinfo->get_json_pretty<locstruc>("node.loc")<<endl;
338 
339  // try to set some shit out of bounds
340  agent->cinfo->set_value<string>("user[9].tool", "OUT OF BOUNDS!!!!");
341  cout<<agent->cinfo->get_json_pretty<vector<userstruc>>("user")<<endl;
342  cout<<agent->cinfo->get_json_pretty<userstruc>("user[9]")<<endl;
343 
344 
345 */
346 /*
347  agent->cinfo->add_name("Entire COSMOSSTRUC Unit", &agent->cinfo->unit);
348  cout<<"Output:\n\t<"<<agent->cinfo->get_json<vector<vector<unitstruc>>>("Entire COSMOSSTRUC Unit")<<">"<<endl;
349 
350  equationstruc eq = { "Test Name", "Test Value" };
351  vector<equationstruc> eqs;
352  eqs.push_back(eq);
353  agent->cinfo->set_value<vector<equationstruc>>("Entire COSMOSSTRUC Equations", eqs);
354  cout<<"Output:\n\t<"<<agent->cinfo->get_json<vector<equationstruc>>("Entire COSMOSSTRUC Equations")<<">"<<endl;
355 
356 */
357 
358  // Define the requests that we need for this agent
359  if ((iretn=agent->add_request("add",request_add)))
360  exit (iretn);
361 
362  if ((iretn=agent->add_request("sub",request_sub)))
363  exit (iretn);
364 
365  if ((iretn=agent->add_request("mul",request_mul)))
366  exit (iretn);
367 
368  if ((iretn=agent->add_request("div",request_div)))
369  exit (iretn);
370 
371  if ((iretn=agent->add_request("node",request_change_node_name)))
372  exit (iretn);
373 
374  // Start our own thread
375  iretn = myagent();
376 }
FILE * get_debug_fd(double mjd=0.)
Definition: agentclass.cpp:2645
uint16_t jmapped
Whether JSON map has been created.
Definition: jsondef.h:4205
int i
Definition: rw_test.cpp:37
int32_t request_mul(string &request, string &response, Agent *agent)
Definition: agent_calc.cpp:412
string getNode()
Listen for heartbeat.
Definition: agentclass.cpp:2607
void print_all_names() const
Prints every name within Namespace 2.0.
Definition: jsondef.h:4520
int iretn
Definition: rw_test.cpp:37
int32_t wait(State state=State::RUN, double waitsec=10.)
Definition: agentclass.cpp:398
void add_default_names()
Add default names for every accessible memory location within the COSMOS Data Structure (cosmosstruc)...
Definition: jsondef.h:4671
int myagent()
Definition: agent_calc.cpp:378
const string & str
Definition: json11.cpp:360
int32_t request_change_node_name(string &request, string &response, Agent *agent)
Definition: agent_calc.cpp:432
void print_all_names_types_values() const
Prints every name and the associated value and type within Namespace 2.0.
Definition: jsondef.h:4547
string cosmos_error_string(int32_t cosmos_errno)
Definition: cosmos-errno.cpp:45
int32_t request_sub(string &request, string &response, Agent *agent)
Definition: agent_calc.cpp:402
string getAgent()
Definition: agentclass.cpp:2609
int32_t add_request(string token, external_request_function function, string synopsis="", string description="")
Add internal request to Agent request list with description and synopsis.
Definition: agentclass.cpp:312
Definition: agentclass.h:139
void print_all_names_types() const
Prints every name and the associated type within Namespace 2.0.
Definition: jsondef.h:4529
double data_ctime(string path)
Definition: datalib.cpp:1910
double equationator(const string &str)
Evaluate the expression of a python?Matlab? equation.
Definition: jsondef.h:7825
static char nodename[COSMOS_MAX_NAME+1]
Definition: agent_calc.cpp:60
static Agent * agent
Definition: agent_calc.cpp:75
double currentmjd(double offset)
Current UTC in Modified Julian Days.
Definition: timelib.cpp:65
int32_t request_add(string &request, string &response, Agent *agent)
Definition: agent_calc.cpp:390
string utc2iso8601(double utc)
ISO 8601 version of time.
Definition: timelib.cpp:1286
cosmosstruc * cinfo
Definition: agentclass.h:346
size_t size()
Returns the length of the map used to represent Namespace 2.0.
Definition: jsondef.h:4514
vector< vector< jsonentry > > jmap
JSON Namespace Map matrix. first entry hash, second is items with that hash.
Definition: jsondef.h:4208
string mjd2iso8601(double mjd)
Definition: timelib.cpp:1316
int32_t request_div(string &request, string &response, Agent *agent)
Definition: agent_calc.cpp:422
static char agentname[COSMOS_MAX_NAME+1]
Definition: agent_calc.cpp:61

Variable Documentation

char nodename[COSMOS_MAX_NAME+1] = "sat_001"
static
char agentname[COSMOS_MAX_NAME+1] = "calc"
static
Agent* agent
static