Fawkes API  Fawkes Development Version
clips_feature_inifin.cpp
1 
2 /***************************************************************************
3  * clips_feature_inifin.cpp - CLIPSFeatureAspect initializer/finalizer
4  *
5  * Created: Fri Aug 16 13:15:27 2013
6  * Copyright 2006-2013 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <plugins/clips/aspect/clips_feature_inifin.h>
25 #include <plugins/clips/aspect/clips_env_manager.h>
26 #include <core/threading/thread_finalizer.h>
27 
28 #include <clipsmm.h>
29 
30 namespace fawkes {
31 #if 0 /* just to make Emacs auto-indent happy */
32 }
33 #endif
34 
35 /** @class CLIPSFeatureAspectIniFin <plugins/clips/aspect/clips_feature_inifin.h>
36  * CLIPSFeatureAspect initializer/finalizer.
37  * @author Tim Niemueller
38  */
39 
40 /** Constructor. */
42  : AspectIniFin("CLIPSFeatureAspect")
43 {
44 }
45 
46 /** Destructor. */
48 {
49 }
50 
51 
52 
53 void
55 {
56  CLIPSFeatureAspect *clips_thread;
57  clips_thread = dynamic_cast<CLIPSFeatureAspect *>(thread);
58  if (clips_thread == NULL) {
59  throw CannotInitializeThreadException("Thread '%s' claims to have the "
60  "CLIPSFeatureAspect, but RTTI says it "
61  "has not. ", thread->name());
62  }
63 
64  clips_env_mgr_->add_features(clips_thread->clips_features_);
65 }
66 
67 
68 bool
70 {
71  CLIPSFeatureAspect *clips_thread;
72  clips_thread = dynamic_cast<CLIPSFeatureAspect *>(thread);
73  if (clips_thread == NULL) return true;
74 
75  try {
76  clips_env_mgr_->assert_can_remove_features(clips_thread->clips_features_);
77  return true;
78  } catch (Exception &e) {
79  return false;
80  }
81 }
82 
83 void
85 {
86  CLIPSFeatureAspect *clips_thread;
87  clips_thread = dynamic_cast<CLIPSFeatureAspect *>(thread);
88  if (clips_thread == NULL) {
89  throw CannotFinalizeThreadException("Thread '%s' claims to have the "
90  "CLIPSFeatureAspect, but RTTI says it "
91  "has not. ", thread->name());
92  }
93 
94  clips_env_mgr_->remove_features(clips_thread->clips_features_);
95 }
96 
97 
98 
99 /** Set CLIPS environment manger.
100  * @param clips_env_mgr CLIPS environment manager
101  */
102 void
104 {
105  clips_env_mgr_ = clips_env_mgr;
106 }
107 
108 } // end namespace fawkes
Thread aspect to provide a feature to CLIPS environments.
Definition: clips_feature.h:57
Fawkes library namespace.
virtual void finalize(Thread *thread)
Finalize thread.
Thread class encapsulation of pthreads.
Definition: thread.h:42
virtual bool prepare_finalize(Thread *thread)
Default finalize preparation.
LockPtr<> is a reference-counting shared lockable smartpointer.
Definition: lockptr.h:57
virtual void init(Thread *thread)
Initialize thread.
Thread cannot be initialized.
Base class for exceptions in Fawkes.
Definition: exception.h:36
void set_manager(LockPtr< CLIPSEnvManager > &clips_env_mgr)
Set CLIPS environment manger.
const char * name() const
Get name of thread.
Definition: thread.h:95
Thread cannot be finalized.
Aspect initializer/finalizer base class.
Definition: inifin.h:36