Fawkes API  Fawkes Development Version
cedar_thread.cpp
1 
2 /***************************************************************************
3  * cedar_thread.cpp - CLIPS-based agent main thread
4  *
5  * Created: Fri Aug 16 18:00:32 2013 +0200
6  * Copyright 2006-2013 Tim Niemueller [www.niemueller.de]
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 "cedar_thread.h"
23 
24 #include "plugin_director_thread.h"
25 
26 #include <core/threading/mutex_locker.h>
27 
28 using namespace fawkes;
29 
30 /** @class CedarThread "cedar_thread.h"
31  * Main thread of CEDAR error analysis plugin.
32  * @author Tim Niemueller
33  */
34 
35 /** Constructor.
36  * @param pdt plugin director thread to use for Fawkes info
37  */
39 : Thread("CedarThread", Thread::OPMODE_WAITFORWAKEUP),
40  BlockedTimingAspect(BlockedTimingAspect::WAKEUP_HOOK_THINK),
41  CLIPSAspect("cedar", "CEDAR")
42 {
43  pdt_ = pdt;
44 }
45 
46 /** Destructor. */
48 {
49 }
50 
51 void
53 {
54  clips->evaluate(std::string("(path-add-subst \"@BASEDIR@\" \"") + BASEDIR + "\")");
55  clips->evaluate(std::string("(path-add-subst \"@FAWKES_BASEDIR@\" \"") + FAWKES_BASEDIR + "\")");
56  clips->evaluate(std::string("(path-add-subst \"@RESDIR@\" \"") + RESDIR + "\")");
57  clips->evaluate(std::string("(path-add-subst \"@CONFDIR@\" \"") + CONFDIR + "\")");
58 
59  clips->evaluate(std::string("(path-add \"") + SRCDIR + "/clips/\")");
60  clips->evaluate(std::string("(path-add \"") + CONFDIR + "/cedar/\")");
61 
62  clips->evaluate("(ff-feature-request \"config\")");
63 
64  bool use_fawkes = false;
65  try {
66  use_fawkes = config->get_bool("/cedar/use-fawkes");
67  } catch (Exception &e) {
68  } // ignored, use default
69 
70  if (use_fawkes) {
71  clips->add_function("fawkes-get-plugin-info",
72  sigc::slot<void>(
73  sigc::mem_fun(*this, &CedarThread::clips_get_plugin_info)));
74  }
75 
76  clips->batch_evaluate(SRCDIR "/clips/cedar.clp");
77  clips->assert_fact("(cedar-init)");
78  clips->refresh_agenda();
79  clips->run();
80 }
81 
82 void
84 {
85 }
86 
87 void
89 {
91  clips->assert_fact("(time (now))");
92  clips->refresh_agenda();
93  clips->run();
94 }
95 
96 void
97 CedarThread::clips_get_plugin_info()
98 {
99  std::list<std::string> loaded = pdt_->get_loaded_plugins();
100  std::list<std::pair<std::string, std::string>> available = pdt_->get_available_plugins();
101 
103 
104  for (auto p : available) {
105  bool is_loaded = (std::find(loaded.begin(), loaded.end(), p.first) != loaded.end());
106 
107  clips->assert_fact_f("(fawkes-plugin (name \"%s\") (state %s))",
108  p.first.c_str(),
109  is_loaded ? "LOADED" : "AVAILABLE");
110  }
111 }
CedarThread::init
virtual void init()
Initialize the thread.
Definition: cedar_thread.cpp:52
CedarThread::CedarThread
CedarThread(CedarPluginDirectorThread *pdt)
Constructor.
Definition: cedar_thread.cpp:38
CedarThread::loop
virtual void loop()
Code to execute in the thread.
Definition: cedar_thread.cpp:88
fawkes::Configuration::get_bool
virtual bool get_bool(const char *path)=0
Get value from configuration which is of type bool.
fawkes::MutexLocker
Mutex locking helper.
Definition: mutex_locker.h:34
fawkes::LockPtr::objmutex_ptr
Mutex * objmutex_ptr() const
Get object mutex.
Definition: lockptr.h:284
fawkes::BlockedTimingAspect
Thread aspect to use blocked timing.
Definition: blocked_timing.h:51
CedarThread::finalize
virtual void finalize()
Finalize the thread.
Definition: cedar_thread.cpp:83
fawkes::CLIPSAspect::clips
LockPtr< CLIPS::Environment > clips
CLIPS environment for exclusive usage.
Definition: clips.h:50
fawkes
Fawkes library namespace.
CedarThread::~CedarThread
virtual ~CedarThread()
Destructor.
Definition: cedar_thread.cpp:47
fawkes::ConfigurableAspect::config
Configuration * config
This is the Configuration member used to access the configuration.
Definition: configurable.h:41
fawkes::CLIPSAspect
Thread aspect to get access to a CLIPS environment.
Definition: clips.h:41
fawkes::Thread
Thread class encapsulation of pthreads.
Definition: thread.h:46
CedarPluginDirectorThread
Plugin manager access for CEDAR.
Definition: plugin_director_thread.h:32
fawkes::Exception
Base class for exceptions in Fawkes.
Definition: exception.h:36