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
34namespace fawkes {
35
36class Logger;
37
38class ClingoAccess : public Clingo::SolveEventHandler
39{
40public:
41 /** Debug levels, higher levels include the lower values. */
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
97 void set_debug_level(DebugLevel_t log_level);
98
99private:
100 bool on_model(Clingo::Model &model) override;
101 void on_finish(Clingo::SolveResult result) override;
102
103 void alloc_control(void);
104
105private:
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
A wrapper around the clingo control, to control the solving process.
Definition: clingo_access.h:39
bool cancel_solving(void)
Stops the solving process, if it is running.
bool release_external(const Clingo::Symbol &atom)
Releases an external value.
Clingo::SymbolVector model_symbols(void) const
Returns a copy of the last symbols found.
int num_threads(void) const noexcept
Returns how many threads Clingo should use.
DebugLevel_t
Debug levels, higher levels include the lower values.
Definition: clingo_access.h:42
@ ASP_DBG_MODELS
Print new models.
Definition: clingo_access.h:47
@ ASP_DBG_NONE
No debug output at all.
Definition: clingo_access.h:43
@ ASP_DBG_ALL_MODEL_SYMBOLS
Ignore '#show' statements and print all symbols of a model.
Definition: clingo_access.h:48
@ ASP_DBG_EVEN_CLINGO
Activates the –output-debug=text option for clingo.
Definition: clingo_access.h:51
@ ASP_DBG_PROGRAMS
Print which programs are grounded.
Definition: clingo_access.h:45
@ ASP_DBG_EXTERNALS
Print assignments and releases of externals.
Definition: clingo_access.h:46
@ ASP_DBG_ALL
Print everything.
Definition: clingo_access.h:50
@ ASP_DBG_TIME
Print when starting/finishing grounding/solving for analysis.
Definition: clingo_access.h:44
DebugLevel_t debug_level() const
Get current debug level.
void set_ground_callback(Clingo::GroundCallback &&callback)
Sets the ground callback, to implement custom functions.
bool start_solving(void)
Starts the solving process, if it isn't already running.
bool assign_external(const Clingo::Symbol &atom, const bool value)
Assign external value.
Definition: clingo_access.h:82
void register_model_callback(std::shared_ptr< std::function< bool(void)> > callback)
Registers a callback for the event of a new model.
ClingoAccess(Logger *logger, const std::string &log_component)
Constructor.
void set_num_threads(const int threads, const bool use_splitting=false)
Sets the number of threads Clingo should use.
bool load_file(const std::string &path)
Loads a file in the solver.
void unregister_model_callback(std::shared_ptr< std::function< bool(void)> > callback)
Unregisters a callback for the event of a new model.
~ClingoAccess(void)
Destructor.
bool reset(void)
Tries to reset Clingo.
bool free_external(const Clingo::Symbol &atom)
Release external value.
Definition: clingo_access.h:88
bool ground(const Clingo::PartSpan &parts)
Grounds a program part.
bool solving(void) const noexcept
Returns whether the solving process is running.
void set_debug_level(DebugLevel_t log_level)
Set debug level.
void unregister_finish_callback(std::shared_ptr< std::function< void(Clingo::SolveResult)> > callback)
Unregisters a callback for the event of finishing the solving process.
void register_finish_callback(std::shared_ptr< std::function< void(Clingo::SolveResult)> > callback)
Registers a callback for the event of finishing the solving process.
bool start_solving_blocking(void)
Starts the solving process.
Interface for logging.
Definition: logger.h:42
Mutex mutual exclusion lock.
Definition: mutex.h:33
Fawkes library namespace.