Fawkes API  Fawkes Development Version
clips_rm_trigger.cpp
1 /***************************************************************************
2  * clips_rm_trigger.cpp - Class holding information and callback for trigger in CLIPS
3  *
4  *
5  * Created: 11:57:31 AM 2016
6  * Copyright 2016 Frederik Zwilling
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Library General Public License for more details.
18  *
19  * Read the full text in the LICENSE.GPL file in the doc directory.
20  */
21 
22 #include "clips_rm_trigger.h"
23 
24 #include <core/threading/mutex_locker.h>
25 
26 #include <clipsmm.h>
27 
28 using namespace fawkes;
29 using namespace mongo;
30 
31 /**
32  * Constructor with references to objects of the plugin
33  * @param assert_name String used to identify this trigger in resulting facts
34  * @param robot_memory Robot Memory
35  * @param clips Clips environment
36  * @param logger Logger
37  */
38 ClipsRmTrigger::ClipsRmTrigger(std::string assert_name,
39  RobotMemory * robot_memory,
41  fawkes::Logger * logger)
42 {
43  this->assert_name = assert_name;
44  this->robot_memory = robot_memory;
45  this->clips = clips;
46  this->logger = logger;
47 }
48 
49 ClipsRmTrigger::~ClipsRmTrigger()
50 {
51  if (trigger) {
52  robot_memory->remove_trigger(trigger);
53  }
54 }
55 
56 /**
57  * Set the trigger object given by the robot memory
58  * @param trigger Trigger
59  */
60 void
62 {
63  this->trigger = trigger;
64 }
65 
66 /**
67  * Callback function for the trigger. Asserts a fact about the update with the assert_name and updated object.
68  * When you retract the fact about the update, also call bson-destroy on the included pointer to avoid memory leaks.
69  * @param update updated object
70  */
71 void
72 ClipsRmTrigger::callback(mongo::BSONObj update)
73 {
74  MutexLocker locker(clips.objmutex_ptr());
75  clips->assert_fact_f("( %s)", assert_name.c_str());
76  CLIPS::Template::pointer temp = clips->get_template("robmem-trigger");
77  if (temp) {
78  struct timeval tv;
79  gettimeofday(&tv, 0);
80  CLIPS::Fact::pointer fact = CLIPS::Fact::create(**clips, temp);
81  fact->set_slot("name", assert_name.c_str());
82  CLIPS::Values rcvd_at(2, CLIPS::Value(CLIPS::TYPE_INTEGER));
83  rcvd_at[0] = tv.tv_sec;
84  rcvd_at[1] = tv.tv_usec;
85  fact->set_slot("rcvd-at", rcvd_at);
86  BSONObjBuilder *b = new BSONObjBuilder();
87  b->appendElements(update);
88  void *ptr = b;
89  fact->set_slot("ptr", CLIPS::Value(ptr));
90  CLIPS::Fact::pointer new_fact = clips->assert_fact(fact);
91 
92  if (!new_fact) {
93  logger->log_warn("CLIPS-RobotMemory", "Asserting robmem-trigger fact failed");
94  delete static_cast<BSONObjBuilder *>(ptr);
95  }
96  } else {
97  logger->log_warn("CLIPS-RobotMemory", "Did not get template, did you load robot-memory.clp?");
98  }
99 }
Fawkes library namespace.
Mutex locking helper.
Definition: mutex_locker.h:33
Class holding all information about an EventTrigger.
Definition: event_trigger.h:32
void callback(mongo::BSONObj update)
Callback function for the trigger.
ClipsRmTrigger(std::string assert_name, RobotMemory *robot_memory, fawkes::LockPtr< CLIPS::Environment > &clips, fawkes::Logger *logger)
Constructor with references to objects of the plugin.
virtual void log_warn(const char *component, const char *format,...)
Log warning message.
Definition: multi.cpp:216
void set_trigger(EventTrigger *trigger)
Set the trigger object given by the robot memory.
Access to the robot memory based on mongodb.
Definition: robot_memory.h:48
Interface for logging.
Definition: logger.h:41