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 <bsoncxx/builder/basic/document.hpp>
27#include <clipsmm.h>
28
29using namespace fawkes;
30using namespace mongocxx;
31
32/**
33 * Constructor with references to objects of the plugin
34 * @param assert_name String used to identify this trigger in resulting facts
35 * @param robot_memory Robot Memory
36 * @param clips Clips environment
37 * @param logger Logger
38 */
39ClipsRmTrigger::ClipsRmTrigger(std::string assert_name,
40 RobotMemory * robot_memory,
42 fawkes::Logger * logger)
43{
44 this->assert_name = assert_name;
45 this->robot_memory = robot_memory;
46 this->clips = clips;
47 this->logger = logger;
48}
49
50ClipsRmTrigger::~ClipsRmTrigger()
51{
52 if (trigger) {
53 robot_memory->remove_trigger(trigger);
54 }
55}
56
57/**
58 * Set the trigger object given by the robot memory
59 * @param trigger Trigger
60 */
61void
63{
64 this->trigger = trigger;
65}
66
67/**
68 * Callback function for the trigger. Asserts a fact about the update with the assert_name and updated object.
69 * When you retract the fact about the update, also call bson-destroy on the included pointer to avoid memory leaks.
70 * @param update updated object
71 */
72void
73ClipsRmTrigger::callback(const bsoncxx::document::view &update)
74{
75 MutexLocker locker(clips.objmutex_ptr());
76 clips->assert_fact_f("( %s)", assert_name.c_str());
77 CLIPS::Template::pointer temp = clips->get_template("robmem-trigger");
78 if (temp) {
79 struct timeval tv;
80 gettimeofday(&tv, 0);
81 CLIPS::Fact::pointer fact = CLIPS::Fact::create(**clips, temp);
82 fact->set_slot("name", assert_name.c_str());
83 CLIPS::Values rcvd_at(2, CLIPS::Value(CLIPS::TYPE_INTEGER));
84 rcvd_at[0] = tv.tv_sec;
85 rcvd_at[1] = tv.tv_usec;
86 fact->set_slot("rcvd-at", rcvd_at);
87 using namespace bsoncxx::builder;
88 basic::document *b = new basic::document();
89 b->append(bsoncxx::builder::concatenate(update));
90 void *ptr = b;
91 fact->set_slot("ptr", CLIPS::Value(ptr));
92 CLIPS::Fact::pointer new_fact = clips->assert_fact(fact);
93
94 if (!new_fact) {
95 logger->log_warn("CLIPS-RobotMemory", "Asserting robmem-trigger fact failed");
96 delete static_cast<basic::document *>(ptr);
97 }
98 } else {
99 logger->log_warn("CLIPS-RobotMemory", "Did not get template, did you load robot-memory.clp?");
100 }
101}
ClipsRmTrigger(std::string assert_name, RobotMemory *robot_memory, fawkes::LockPtr< CLIPS::Environment > &clips, fawkes::Logger *logger)
Constructor with references to objects of the plugin.
void callback(const bsoncxx::document::view &update)
Callback function for the trigger.
void set_trigger(EventTrigger *trigger)
Set the trigger object given by the robot memory.
Class holding all information about an EventTrigger.
Definition: event_trigger.h:32
Access to the robot memory based on mongodb.
Definition: robot_memory.h:47
void remove_trigger(EventTrigger *trigger)
Remove a previously registered trigger.
Mutex * objmutex_ptr() const
Get object mutex.
Definition: lockptr.h:284
Interface for logging.
Definition: logger.h:42
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
Mutex locking helper.
Definition: mutex_locker.h:34
Fawkes library namespace.