ClingoBackend#
Implements all basic clingo’s API functionality. This includes grounding, solving, cautious and brave resoning, use of extenrals and assuptions, and optimization
Public operations#
Can be used as OPERATION in the actions of the ui-state
- class ClingoBackend[source]#
Bases:
object
This backend contains the basic clingo functionality for a backend using clingo.
- add_assumption(atom, value='true')[source]#
Adds an atom a as an assumption.
If the value is “true”, the atom is assumed to be true. This assumption can be considered as an integrity constraint:
:- not a.
forcing the program to entail the given atom.If the value is “false”, the atom is assumed to be false: This assumption can be considered as an integrity constraint:
:- a.
forcing the program to never entail the given atom.Notice that the assumption must be generated by your domain encoding.
- Parameters
atom (str) – The clingo symbol to be added as a true assumption
value (str) – The value of the assumption either “true” or “false”
- add_atom(predicate)[source]#
Adds an atom, restarts the control and grounds
- Parameters
predicate (str) – The clingo symbol to be added
- clear_atoms()[source]#
Removes all atoms and resets the backend. and finally updates the model and returns the updated gui as a Json structure.
- download(show_prg=None, file_name='clinguin_download.lp')[source]#
Downloads the current model. It must be selected first via
select
.- Parameters
show_prg (_type_, optional) – Program to filter output using show statements. Defaults to None.
file_name (str, optional) – The name of the file for the download. Defaults to “clinguin_download.lp”.
- ground(program, arguments=None)[source]#
Grounds the given program. Notice that the base program is grounded when the server starts. This operation can be used for grounding encodings with multiple programs in a multi-shot setting.
- Parameters
program (str) – The name of the program to ground used in the #program directive
arguments (tuple, optional) – The list of arguments to ground the program. Defaults to an empty list.
instance (These are the arguments of your #program directive. For) –
step (in #program) –
- next_solution(opt_mode='ignore')[source]#
Obtains the next solution. If a no browsing has been started yet, then it calls solve, otherwise it iterates the models in the last call. To keep the atoms shown in the solution, use
select
.- Parameters
opt_mode – The clingo optimization mode, bu default is ‘ignore’, to browse only optimal models use ‘optN’
- remove_assumption(atom)[source]#
Removes an atom from the assumptions list regardless of its value. This will allow the atom to take any value in the solution.
- Parameters
atom (str) – The clingo symbol to be removed
- remove_assumption_signature(atom)[source]#
Removes from the list of assumptions those matching the given atom. Unlike function remove_assumption, this one allows for partial matches using the placeholder constant any. This will allow the atom to take any value in the solution.
- Parameters
atom (str) – The atom description as a symbol, where the reserver word any is used to state that anything can take part of that position. For instance, person(anna,any), will remove all assumptions of atom person, where the first argument is anna.
- remove_atom(predicate)[source]#
Removes an atom (if present), restarts the control and grounds again
- Parameters
predicate (str) – The clingo symbol to be added
- restart()[source]#
Restarts the backend. It will initialize all attributes, remove atoms, assumptions and externals, restart the control object by initializing all parameters, controls, ending the browsing and grounding.
- select(show_prg='')[source]#
Select the current solution during browsing. All atoms in the solution are added as assumptions in the backend.
- Parameters
show_program (str) – An optional show program to filter atoms
- set_constant(name, value)[source]#
Sets a constant value. Will reinitialize the control, ground and set arguments
- set_external(atom, value)[source]#
Sets the value of an external. Externals must be defined in the domain files using #external. The truth value of external atoms can then be provided by the user via this function.
- Parameters
atom (str) – The clingo symbol to be set
value (str) – The value (release, true or false)
Domain state constructors#
- property ClingoBackend._ds_context#
Adds context information from the client.
Includes predicate
_clinguin_context/2
indicating each key and value in the context.
- property ClingoBackend._ds_model#
Computes model and adds all atoms as facts. When the model is being iterated by the user, the current model is returned. It will use as optimality the mode set in the command line as default-opt-mode (ignore by default).
It uses a cache that is erased after an operation makes changes in the control.
- property ClingoBackend._ds_brave#
Computes brave consequences adds them as predicates
_any/1
. This are atoms that appear in some model. If it is not used in the UI files then the computation is not performed.It uses a cache that is erased after an operation makes changes in the control.
- property ClingoBackend._ds_cautious#
Computes cautious consequences adds them as predicates
_all/1
. This are atoms that appear in all models. If it is not used in the UI files then the computation is not performed.It uses a cache that is erased after an operation makes changes in the control.
- property ClingoBackend._ds_brave_optimal#
Computes brave consequences for only optimal solutions adds them as predicates
_any_opt/1
. This are atoms that appear in some optimal model. If it is not used in the UI files then the computation is not performed.It uses a cache that is erased after an operation makes changes in the control.
- property ClingoBackend._ds_cautious_optimal#
Computes cautious consequences of optimal models adds them as predicates
_all_opt/1
. This are atoms that appear in all optimal models. If it is not used in the UI files then the computation is not performed.It uses a cache that is erased after an operation makes changes in the control.
- property ClingoBackend._ds_assume#
Adds information about the assumptions.
Includes predicate
_clinguin_assume/2
for every atom that was assumed, where the second argument is either true or false.
- property ClingoBackend._ds_external#
Adds information about the external atoms
Includes predicate
_clinguin_external/2
for every external atom that has been set.
- property ClingoBackend._ds_opt#
Adds program to pass with optimality information.
- Includes predicates:
_clinguin_cost/1
: With a single tuple indicating the cost of the current model_clinguin_cost/2
: With the index and cost value, linearizing predicate_clinguin_cost/1
_clinguin_optimal/0
: If the solution is optimal_clinguin_optimizing/0
: If there is an optimization in the program
- property ClingoBackend._ds_unsat#
Adds information about the statisfiablity of the domain control
Includes predicate
_clinguin_unsat/0
if the domain control is unsat
- property ClingoBackend._ds_browsing#
Adds information about the browsing state
Includes predicate
_clinguin_browsing/0
if the user is browsing solutions
- property ClingoBackend._ds_constants#
Adds constants values.
Includes predicate
_clinguin_const/2
for each constant provided in the command line and used in the domain files.