Fawkes API Fawkes Development Version
clingo_control_manager.cpp
1/***************************************************************************
2 * clingo_control_manager.cpp - Clingo control manager
3 *
4 * Created: Thu Oct 27 16:23:32 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#include <core/exception.h>
24#include <logging/logger.h>
25#include <plugins/asp/aspect/clingo_access.h>
26#include <plugins/asp/aspect/clingo_control_manager.h>
27
28namespace fawkes {
29
30/** @class ClingoControlManager <plugins/asp/aspect/clingo_control_manager.h>
31 * The Clingo Control Manager creates and maintains Clingo Controls.
32 * @author Björn Schäpers
33 */
34
35/** Constructor. */
37{
38}
39
40/** Destructor. */
42{
43}
44
45/**
46 * @brief Sets the logger for all Clingo Controls.
47 * @param[in] logger The logger.
48 */
49void
51{
52 logger_ = logger;
53}
54
55/** Create a new control.
56 * The control is registered internally under the specified name. It
57 * must be destroyed when done with it. Only a single control can be
58 * created for a particular control name.
59 * @param[in] ctrl_name The Name by which to register the control.
60 * @param[in] log_component_name The Prefix for log entries. If empty it will be set to "Clingo".
61 * @return A new plain Clingo Control.
62 */
64ClingoControlManager::create_control(const std::string &ctrl_name,
65 const std::string &log_component_name)
66{
67 if (controls_.count(ctrl_name) != 0) {
68 throw Exception("Clingo Control '%s' already exists!", ctrl_name.c_str());
69 }
70
71 Clingo::SymbolSpan s;
72 LockPtr<ClingoAccess> ctrl(new ClingoAccess(logger_, log_component_name));
73
74 controls_.emplace(ctrl_name, ctrl);
75
76 return ctrl;
77}
78
79/**
80 * "Destroys" the named control. Only ever destroy controls which you have created yourself.
81 * It will be unregistered, but live as long as there is a LockPtr reference to it.
82 * @param[in] ctrl_name The name of the control to destroy.
83 */
84void
85ClingoControlManager::destroy_control(const std::string &ctrl_name)
86{
87 controls_.erase(ctrl_name);
88 return;
89}
90
91/**
92 * Get map of controls.
93 * @return The map from control name to control lock ptr.
94 */
95const std::unordered_map<std::string, LockPtr<ClingoAccess>> &
97{
98 return controls_;
99}
100
101} // end namespace fawkes
A wrapper around the clingo control, to control the solving process.
Definition: clingo_access.h:39
LockPtr< ClingoAccess > create_control(const std::string &ctrl_name, const std::string &log_component_name)
Create a new control.
void destroy_control(const std::string &ctrl_name)
"Destroys" the named control.
virtual ~ClingoControlManager(void)
Destructor.
const std::unordered_map< std::string, LockPtr< ClingoAccess > > & controls(void) const
Get map of controls.
void set_logger(Logger *logger)
Sets the logger for all Clingo Controls.
Base class for exceptions in Fawkes.
Definition: exception.h:36
LockPtr<> is a reference-counting shared lockable smartpointer.
Definition: lockptr.h:55
Interface for logging.
Definition: logger.h:42
Fawkes library namespace.