Fawkes API  Fawkes Development Version
clingo_access.h
1 /***************************************************************************
2  * clingo_access.h - Clingo access wrapper for the aspects
3  *
4  * Created: Mon Oct 31 13:41:07 2016
5  * Copyright 2016 Björn Schäpers
6  * 2018 Tim Niemueller [www.niemueller.org]
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. A runtime exception applies to
13  * this software (see LICENSE.GPL_WRE file mentioned below for details).
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
21  */
22 
23 #ifndef _PLUGINS_ASP_ASPECT_CLINGO_ACCESS_H_
24 #define _PLUGINS_ASP_ASPECT_CLINGO_ACCESS_H_
25 
26 #include <core/threading/mutex.h>
27 
28 #include <atomic>
29 #include <clingo.hh>
30 #include <functional>
31 #include <memory>
32 #include <vector>
33 
34 namespace fawkes {
35 
36 class Logger;
37 
38 class ClingoAccess : public Clingo::SolveEventHandler
39 {
40 public:
41  /** Debug levels, higher levels include the lower values. */
42  enum DebugLevel_t {
43  ASP_DBG_NONE = 0, ///< No debug output at all.
44  ASP_DBG_TIME = 10, ///< Print when starting/finishing grounding/solving for analysis.
45  ASP_DBG_PROGRAMS = 20, ///< Print which programs are grounded.
46  ASP_DBG_EXTERNALS = 30, ///< Print assignments and releases of externals.
47  ASP_DBG_MODELS = 40, ///< Print new models.
49  50, ///< Ignore '\#show' statements and print all symbols of a model.
50  ASP_DBG_ALL, ///< Print everything.
51  ASP_DBG_EVEN_CLINGO ///< Activates the --output-debug=text option for clingo.
52  };
53 
54  ClingoAccess(Logger *logger, const std::string &log_component);
55  ~ClingoAccess(void);
56 
57  void register_model_callback(std::shared_ptr<std::function<bool(void)>> callback);
58  void unregister_model_callback(std::shared_ptr<std::function<bool(void)>> callback);
59  void register_finish_callback(std::shared_ptr<std::function<void(Clingo::SolveResult)>> callback);
60  void
61  unregister_finish_callback(std::shared_ptr<std::function<void(Clingo::SolveResult)>> callback);
62 
63  void set_ground_callback(Clingo::GroundCallback &&callback);
64 
65  bool solving(void) const noexcept;
66  bool start_solving(void);
67  bool start_solving_blocking(void);
68  bool cancel_solving(void);
69 
70  bool reset(void);
71 
72  void set_num_threads(const int threads, const bool use_splitting = false);
73  int num_threads(void) const noexcept;
74 
75  Clingo::SymbolVector model_symbols(void) const;
76 
77  bool load_file(const std::string &path);
78 
79  bool ground(const Clingo::PartSpan &parts);
80 
81  inline bool
82  assign_external(const Clingo::Symbol &atom, const bool value)
83  {
84  return assign_external(atom, value ? Clingo::TruthValue::True : Clingo::TruthValue::False);
85  }
86 
87  inline bool
88  free_external(const Clingo::Symbol &atom)
89  {
90  return assign_external(atom, Clingo::TruthValue::Free);
91  }
92 
93  bool assign_external(const Clingo::Symbol &atom, const Clingo::TruthValue value);
94  bool release_external(const Clingo::Symbol &atom);
95 
96  DebugLevel_t debug_level() const;
97  void set_debug_level(DebugLevel_t log_level);
98 
99 private:
100  bool on_model(Clingo::Model &model) override;
101  void on_finish(Clingo::SolveResult result) override;
102 
103  void alloc_control(void);
104 
105 private:
106  Logger *const logger_;
107  const std::string log_comp_;
108 
109  std::atomic<DebugLevel_t> debug_level_;
110 
111  int num_threads_;
112  bool thread_mode_splitting_;
113 
114  Mutex control_mutex_;
115  bool control_is_locked_;
116  Clingo::Control *control_;
117 
118  mutable Mutex model_mutex_;
119  Clingo::SymbolVector model_symbols_, old_symbols_;
120  unsigned int model_counter_;
121 
122  std::atomic_bool solving_;
123  mutable Mutex callback_mutex_;
124  std::vector<std::shared_ptr<std::function<bool(void)>>> model_callbacks_;
125  std::vector<std::shared_ptr<std::function<void(Clingo::SolveResult)>>> finish_callbacks_;
126  Clingo::GroundCallback ground_callback_;
127  Clingo::SolveHandle async_handle_;
128 };
129 
130 } // end namespace fawkes
131 
132 #endif
fawkes::ClingoAccess::ASP_DBG_EVEN_CLINGO
@ ASP_DBG_EVEN_CLINGO
Activates the –output-debug=text option for clingo.
Definition: clingo_access.h:57
fawkes::ClingoAccess::cancel_solving
bool cancel_solving(void)
Stops the solving process, if it is running.
Definition: clingo_access.cpp:444
fawkes::ClingoAccess::assign_external
bool assign_external(const Clingo::Symbol &atom, const bool value)
Assign external value.
Definition: clingo_access.h:88
fawkes::ClingoAccess::ASP_DBG_ALL
@ ASP_DBG_ALL
Print everything.
Definition: clingo_access.h:56
fawkes::ClingoAccess::start_solving
bool start_solving(void)
Starts the solving process, if it isn't already running.
Definition: clingo_access.cpp:397
fawkes::ClingoAccess::load_file
bool load_file(const std::string &path)
Loads a file in the solver.
Definition: clingo_access.cpp:552
fawkes::Mutex
Definition: mutex.h:38
fawkes::ClingoAccess::set_debug_level
void set_debug_level(DebugLevel_t log_level)
Set debug level.
Definition: clingo_access.cpp:532
fawkes::ClingoAccess::reset
bool reset(void)
Tries to reset Clingo.
Definition: clingo_access.cpp:464
fawkes::ClingoAccess::ASP_DBG_NONE
@ ASP_DBG_NONE
No debug output at all.
Definition: clingo_access.h:49
fawkes::ClingoAccess::set_ground_callback
void set_ground_callback(Clingo::GroundCallback &&callback)
Sets the ground callback, to implement custom functions.
Definition: clingo_access.cpp:377
fawkes::ClingoAccess::model_symbols
Clingo::SymbolVector model_symbols(void) const
Returns a copy of the last symbols found.
Definition: clingo_access.cpp:541
fawkes::ClingoAccess::register_finish_callback
void register_finish_callback(std::shared_ptr< std::function< void(Clingo::SolveResult)>> callback)
Registers a callback for the event of finishing the solving process.
Definition: clingo_access.cpp:353
fawkes::ClingoAccess::start_solving_blocking
bool start_solving_blocking(void)
Starts the solving process.
Definition: clingo_access.cpp:421
fawkes::ClingoAccess::unregister_model_callback
void unregister_model_callback(std::shared_ptr< std::function< bool(void)>> callback)
Unregisters a callback for the event of a new model.
Definition: clingo_access.cpp:342
fawkes::ClingoAccess::unregister_finish_callback
void unregister_finish_callback(std::shared_ptr< std::function< void(Clingo::SolveResult)>> callback)
Unregisters a callback for the event of finishing the solving process.
Definition: clingo_access.cpp:365
fawkes::Logger
Definition: logger.h:41
fawkes
fawkes::ClingoAccess::release_external
bool release_external(const Clingo::Symbol &atom)
Releases an external value.
Definition: clingo_access.cpp:644
fawkes::ClingoAccess::ASP_DBG_PROGRAMS
@ ASP_DBG_PROGRAMS
Print which programs are grounded.
Definition: clingo_access.h:51
fawkes::ClingoAccess::debug_level
DebugLevel_t debug_level() const
Get current debug level.
Definition: clingo_access.cpp:523
fawkes::ClingoAccess::register_model_callback
void register_model_callback(std::shared_ptr< std::function< bool(void)>> callback)
Registers a callback for the event of a new model.
Definition: clingo_access.cpp:331
fawkes::ClingoAccess::ASP_DBG_ALL_MODEL_SYMBOLS
@ ASP_DBG_ALL_MODEL_SYMBOLS
Ignore '#show' statements and print all symbols of a model.
Definition: clingo_access.h:54
fawkes::ClingoAccess::ASP_DBG_MODELS
@ ASP_DBG_MODELS
Print new models.
Definition: clingo_access.h:53
fawkes::ClingoAccess::set_num_threads
void set_num_threads(const int threads, const bool use_splitting=false)
Sets the number of threads Clingo should use.
Definition: clingo_access.cpp:488
fawkes::ClingoAccess::DebugLevel_t
DebugLevel_t
Debug levels, higher levels include the lower values.
Definition: clingo_access.h:48
fawkes::ClingoAccess::ClingoAccess
ClingoAccess(Logger *logger, const std::string &log_component)
Constructor.
Definition: clingo_access.cpp:305
fawkes::ClingoAccess::~ClingoAccess
~ClingoAccess(void)
Destructor.
Definition: clingo_access.cpp:320
fawkes::ClingoAccess::ground
bool ground(const Clingo::PartSpan &parts)
Grounds a program part.
Definition: clingo_access.cpp:569
fawkes::ClingoAccess::solving
bool solving(void) const noexcept
Returns whether the solving process is running.
Definition: clingo_access.cpp:387
fawkes::ClingoAccess::ASP_DBG_EXTERNALS
@ ASP_DBG_EXTERNALS
Print assignments and releases of externals.
Definition: clingo_access.h:52
fawkes::ClingoAccess::free_external
bool free_external(const Clingo::Symbol &atom)
Release external value.
Definition: clingo_access.h:94
fawkes::ClingoAccess::ASP_DBG_TIME
@ ASP_DBG_TIME
Print when starting/finishing grounding/solving for analysis.
Definition: clingo_access.h:50
fawkes::ClingoAccess::num_threads
int num_threads(void) const noexcept
Returns how many threads Clingo should use.
Definition: clingo_access.cpp:514