-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapplication_8c-example.html
221 lines (219 loc) · 21.4 KB
/
application_8c-example.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.17"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Clingo C API: application.c</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="clingo.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">Clingo C API
</div>
<div id="projectbrief">C API for clingo providing high level functions to control grounding and solving.</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.17 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
$(function() {
initMenu('',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="header">
<div class="headertitle">
<div class="title">application.c</div> </div>
</div><!--header-->
<div class="contents">
<p>The example shows how to extend the clingo application.It behaves like a normal normal clingo but adds one option to override the default program part to ground. </p>
<h2><a class="anchor" id="autotoc_md24"></a>
Example calls</h2>
<div class="fragment"><div class="line">$ cat example.lp</div>
<div class="line">b.</div>
<div class="line">#program test.</div>
<div class="line">t.</div>
<div class="line"> </div>
<div class="line">$ ./application --program test example.lp</div>
<div class="line">example version 1.0.0</div>
<div class="line">Reading from example.lp</div>
<div class="line">Solving...</div>
<div class="line">Answer: 1</div>
<div class="line">t</div>
<div class="line">SATISFIABLE</div>
<div class="line"> </div>
<div class="line">Models : 1+</div>
<div class="line">Calls : 1</div>
<div class="line">Time : 0.004s (Solving: 0.00s 1st Model: 0.00s Unsat: 0.00s)</div>
<div class="line">CPU Time : 0.004s</div>
</div><!-- fragment --><h2><a class="anchor" id="autotoc_md25"></a>
Code</h2>
<div class="fragment"><div class="line"><span class="preprocessor">#include <<a class="code" href="clingo_8h.html">clingo.h</a>></span></div>
<div class="line"><span class="preprocessor">#include <stdlib.h></span></div>
<div class="line"><span class="preprocessor">#include <stdio.h></span></div>
<div class="line"><span class="preprocessor">#include <string.h></span></div>
<div class="line"> </div>
<div class="line"><span class="comment">// struct to store parsed command line arguments</span></div>
<div class="line"><span class="keyword">typedef</span> <span class="keyword">struct </span>options {</div>
<div class="line"> <span class="keywordtype">char</span> <span class="keyword">const</span> *program;</div>
<div class="line">} options_t;</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">char</span> <span class="keyword">const</span> *name(<span class="keywordtype">void</span> *data) {</div>
<div class="line"> (void)data;</div>
<div class="line"> <span class="comment">// the name of the program printed in its help output</span></div>
<div class="line"> <span class="keywordflow">return</span> <span class="stringliteral">"example"</span>;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">char</span> <span class="keyword">const</span> *version(<span class="keywordtype">void</span> *data) {</div>
<div class="line"> (void)data;</div>
<div class="line"> <span class="comment">// the version of the program printed in its help output</span></div>
<div class="line"> <span class="keywordflow">return</span> <span class="stringliteral">"1.0.0"</span>;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">bool</span> solve(<a class="code" href="group__Control.html#gaf008e9db9dbb37b0b7ef039bb9d582f0">clingo_control_t</a> *ctl) {</div>
<div class="line"> <span class="keywordtype">bool</span> ret = <span class="keyword">true</span>;</div>
<div class="line"> <a class="code" href="group__SolveHandle.html#ga023c1084a4c01ea6e121a8310efba045">clingo_solve_handle_t</a> *handle;</div>
<div class="line"> <a class="code" href="group__Model.html#gaaf9a93819f023f3cb8aa80598c46556b">clingo_model_t</a> <span class="keyword">const</span> *model;</div>
<div class="line"> <a class="code" href="group__Control.html#gae917a23b0591d181004ec88c4e3291c1">clingo_solve_result_bitset_t</a> result;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// get a solve handle</span></div>
<div class="line"> <span class="keywordflow">if</span> (!<a name="a0"></a><a class="code" href="group__Control.html#ga4b3b7388e75ace676481f6021c4dc66d">clingo_control_solve</a>(ctl, <a name="a1"></a><a class="code" href="group__SolveHandle.html#gga63eb87834deebecdf9c799d64a3c65a2a60a9c133be636e68a7d02680c8b9d47e">clingo_solve_mode_yield</a>, NULL, 0, NULL, NULL, &handle)) { <span class="keywordflow">goto</span> error; }</div>
<div class="line"> <span class="comment">// loop over all models</span></div>
<div class="line"> <span class="keywordflow">while</span> (<span class="keyword">true</span>) {</div>
<div class="line"> <span class="keywordflow">if</span> (!<a name="a2"></a><a class="code" href="group__SolveHandle.html#ga73d71df7adeee92f1d515f52cdcbbac0">clingo_solve_handle_resume</a>(handle)) { <span class="keywordflow">goto</span> error; }</div>
<div class="line"> <span class="keywordflow">if</span> (!<a name="a3"></a><a class="code" href="group__SolveHandle.html#gaf9353c14149a2a16adf0474796871ae1">clingo_solve_handle_model</a>(handle, &model)) { <span class="keywordflow">goto</span> error; }</div>
<div class="line"> </div>
<div class="line"> <span class="keywordflow">if</span> (!model) { <span class="keywordflow">break</span>; }</div>
<div class="line"> }</div>
<div class="line"> <span class="comment">// close the solve handle</span></div>
<div class="line"> <span class="keywordflow">if</span> (!<a name="a4"></a><a class="code" href="group__SolveHandle.html#gae4270b7d4d0174a479307438680007c5">clingo_solve_handle_get</a>(handle, &result)) { <span class="keywordflow">goto</span> error; }</div>
<div class="line"> </div>
<div class="line"> <span class="keywordflow">goto</span> out;</div>
<div class="line"> </div>
<div class="line">error:</div>
<div class="line"> ret = <span class="keyword">false</span>;</div>
<div class="line"> </div>
<div class="line">out:</div>
<div class="line"> <span class="comment">// free the solve handle</span></div>
<div class="line"> <span class="keywordflow">return</span> <a name="a5"></a><a class="code" href="group__SolveHandle.html#ga488b76e7240d625bd4066e68a33ab23e">clingo_solve_handle_close</a>(handle) && ret;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">bool</span> parse_option(<span class="keywordtype">char</span> <span class="keyword">const</span> *value, <span class="keywordtype">void</span> *data) {</div>
<div class="line"> <span class="keywordtype">char</span> **program = (<span class="keywordtype">char</span> **)data;</div>
<div class="line"> <span class="comment">// allocate memory for program name</span></div>
<div class="line"> <span class="comment">// note that we forgo freeing memory for the example</span></div>
<div class="line"> <span class="comment">// (it could be done early in the main loop or after clingo_main finished)</span></div>
<div class="line"> <span class="keywordflow">if</span> (!(*program = (<span class="keywordtype">char</span> *)malloc(strlen(value) + 1))) {</div>
<div class="line"> <span class="keywordflow">return</span> <span class="keyword">false</span>;</div>
<div class="line"> }</div>
<div class="line"> strcpy(*program, value);</div>
<div class="line"> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">bool</span> register_options(<a class="code" href="group__ExtendingClingo.html#ga2b159cfef1bc326a9e25617095be39cf">clingo_options_t</a> *options, <span class="keywordtype">void</span> *data) {</div>
<div class="line"> options_t *options_ = (options_t*)data;</div>
<div class="line"> <span class="comment">// register an option to overwrite which program part to ground</span></div>
<div class="line"> <span class="keywordflow">return</span> <a name="a6"></a><a class="code" href="group__ExtendingClingo.html#gad2404aa611652a0f16624bd97066a6fa">clingo_options_add</a>(options, <span class="stringliteral">"Example"</span>, <span class="stringliteral">"program"</span>, <span class="stringliteral">"Override the default program part to ground"</span>, parse_option, &options_->program, <span class="keyword">false</span>, <span class="stringliteral">"<prog>"</span>);</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">bool</span> main_loop(<a class="code" href="group__Control.html#gaf008e9db9dbb37b0b7ef039bb9d582f0">clingo_control_t</a> *ctl, <span class="keywordtype">char</span> <span class="keyword">const</span> *<span class="keyword">const</span> *files, <span class="keywordtype">size_t</span> size, <span class="keywordtype">void</span> *data) {</div>
<div class="line"> options_t *options = (options_t*)data;</div>
<div class="line"> <span class="keywordtype">bool</span> ret = <span class="keyword">true</span>;</div>
<div class="line"> <a name="_a7"></a><a class="code" href="structclingo__part.html">clingo_part_t</a> parts[] = {{ options->program ? options->program : <span class="stringliteral">"base"</span>, NULL, 0 }};</div>
<div class="line"> <span class="keywordtype">char</span> <span class="keyword">const</span> *<span class="keyword">const</span> *file;</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// load files into the control object</span></div>
<div class="line"> <span class="keywordflow">for</span> (file = files; file != files + size; ++file) {</div>
<div class="line"> <span class="keywordflow">if</span> (!<a name="a8"></a><a class="code" href="group__Control.html#gacd916338afc7dd07cb33ae3eca50353d">clingo_control_load</a>(ctl, *file)) { <span class="keywordflow">goto</span> error; }</div>
<div class="line"> }</div>
<div class="line"> <span class="comment">// if no files are given read from stdin</span></div>
<div class="line"> <span class="keywordflow">if</span> (size == 0) {</div>
<div class="line"> <span class="keywordflow">if</span> (!<a class="code" href="group__Control.html#gacd916338afc7dd07cb33ae3eca50353d">clingo_control_load</a>(ctl, <span class="stringliteral">"-"</span>)) { <span class="keywordflow">goto</span> error; }</div>
<div class="line"> }</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// ground</span></div>
<div class="line"> <span class="keywordflow">if</span> (!<a name="a9"></a><a class="code" href="group__Control.html#gaf85b77055668171e6a85f9d729719b57">clingo_control_ground</a>(ctl, parts, 1, NULL, NULL)) { <span class="keywordflow">goto</span> error; }</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// solve</span></div>
<div class="line"> <span class="keywordflow">if</span> (!solve(ctl)) { <span class="keywordflow">goto</span> error; }</div>
<div class="line"> </div>
<div class="line"> <span class="keywordflow">goto</span> out;</div>
<div class="line"> </div>
<div class="line">error:</div>
<div class="line"> ret = <span class="keyword">false</span>;</div>
<div class="line"> </div>
<div class="line">out:</div>
<div class="line"> <span class="keywordflow">return</span> ret;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> <span class="keyword">const</span> **argv) {</div>
<div class="line"> options_t options = { NULL };</div>
<div class="line"> <a name="_a10"></a><a class="code" href="structclingo__application.html">clingo_application_t</a> app = { name, version, NULL, main_loop, NULL, NULL, register_options, NULL };</div>
<div class="line"> <span class="keywordflow">return</span> <a name="a11"></a><a class="code" href="group__ExtendingClingo.html#ga8a8f258e5484315d8f86d4832df41d67">clingo_main</a>(&app, argv+1, argc-1, &options);</div>
<div class="line">}</div>
</div><!-- fragment --> </div><!-- contents -->
<div class="ttc" id="astructclingo__application_html"><div class="ttname"><a href="structclingo__application.html">clingo_application</a></div><div class="ttdoc">This struct contains a set of functions to customize the clingo application.</div><div class="ttdef"><b>Definition:</b> clingo.h:3835</div></div>
<div class="ttc" id="agroup__ExtendingClingo_html_ga8a8f258e5484315d8f86d4832df41d67"><div class="ttname"><a href="group__ExtendingClingo.html#ga8a8f258e5484315d8f86d4832df41d67">clingo_main</a></div><div class="ttdeci">CLINGO_VISIBILITY_DEFAULT int clingo_main(clingo_application_t *application, char const *const *arguments, size_t size, void *data)</div><div class="ttdoc">Run clingo with a customized main function (similar to python and lua embedding).</div></div>
<div class="ttc" id="agroup__ExtendingClingo_html_ga2b159cfef1bc326a9e25617095be39cf"><div class="ttname"><a href="group__ExtendingClingo.html#ga2b159cfef1bc326a9e25617095be39cf">clingo_options_t</a></div><div class="ttdeci">struct clingo_options clingo_options_t</div><div class="ttdoc">Object to add command-line options.</div><div class="ttdef"><b>Definition:</b> clingo.h:3805</div></div>
<div class="ttc" id="agroup__SolveHandle_html_gga63eb87834deebecdf9c799d64a3c65a2a60a9c133be636e68a7d02680c8b9d47e"><div class="ttname"><a href="group__SolveHandle.html#gga63eb87834deebecdf9c799d64a3c65a2a60a9c133be636e68a7d02680c8b9d47e">clingo_solve_mode_yield</a></div><div class="ttdeci">@ clingo_solve_mode_yield</div><div class="ttdoc">Yield models in calls to clingo_solve_handle_model.</div><div class="ttdef"><b>Definition:</b> clingo.h:2288</div></div>
<div class="ttc" id="agroup__Control_html_gaf008e9db9dbb37b0b7ef039bb9d582f0"><div class="ttname"><a href="group__Control.html#gaf008e9db9dbb37b0b7ef039bb9d582f0">clingo_control_t</a></div><div class="ttdeci">struct clingo_control clingo_control_t</div><div class="ttdoc">Control object holding grounding and solving state.</div><div class="ttdef"><b>Definition:</b> clingo.h:2693</div></div>
<div class="ttc" id="agroup__ExtendingClingo_html_gad2404aa611652a0f16624bd97066a6fa"><div class="ttname"><a href="group__ExtendingClingo.html#gad2404aa611652a0f16624bd97066a6fa">clingo_options_add</a></div><div class="ttdeci">CLINGO_VISIBILITY_DEFAULT bool clingo_options_add(clingo_options_t *options, char const *group, char const *option, char const *description, bool(*parse)(char const *value, void *data), void *data, bool multi, char const *argument)</div><div class="ttdoc">Add an option that is processed with a custom parser.</div></div>
<div class="ttc" id="aclingo_8h_html"><div class="ttname"><a href="clingo_8h.html">clingo.h</a></div></div>
<div class="ttc" id="astructclingo__part_html"><div class="ttname"><a href="structclingo__part.html">clingo_part</a></div><div class="ttdoc">Struct used to specify the program parts that have to be grounded.</div><div class="ttdef"><b>Definition:</b> clingo.h:2645</div></div>
<div class="ttc" id="agroup__SolveHandle_html_ga488b76e7240d625bd4066e68a33ab23e"><div class="ttname"><a href="group__SolveHandle.html#ga488b76e7240d625bd4066e68a33ab23e">clingo_solve_handle_close</a></div><div class="ttdeci">CLINGO_VISIBILITY_DEFAULT bool clingo_solve_handle_close(clingo_solve_handle_t *handle)</div><div class="ttdoc">Stops the running search and releases the handle.</div></div>
<div class="ttc" id="agroup__Control_html_gaf85b77055668171e6a85f9d729719b57"><div class="ttname"><a href="group__Control.html#gaf85b77055668171e6a85f9d729719b57">clingo_control_ground</a></div><div class="ttdeci">CLINGO_VISIBILITY_DEFAULT bool clingo_control_ground(clingo_control_t *control, clingo_part_t const *parts, size_t parts_size, clingo_ground_callback_t ground_callback, void *ground_callback_data)</div><div class="ttdoc">Ground the selected parts of the current (non-ground) logic program.</div></div>
<div class="ttc" id="agroup__SolveHandle_html_gae4270b7d4d0174a479307438680007c5"><div class="ttname"><a href="group__SolveHandle.html#gae4270b7d4d0174a479307438680007c5">clingo_solve_handle_get</a></div><div class="ttdeci">CLINGO_VISIBILITY_DEFAULT bool clingo_solve_handle_get(clingo_solve_handle_t *handle, clingo_solve_result_bitset_t *result)</div><div class="ttdoc">Get the next solve result.</div></div>
<div class="ttc" id="agroup__Control_html_gacd916338afc7dd07cb33ae3eca50353d"><div class="ttname"><a href="group__Control.html#gacd916338afc7dd07cb33ae3eca50353d">clingo_control_load</a></div><div class="ttdeci">CLINGO_VISIBILITY_DEFAULT bool clingo_control_load(clingo_control_t *control, char const *file)</div><div class="ttdoc">Extend the logic program with a program in a file.</div></div>
<div class="ttc" id="agroup__Control_html_ga4b3b7388e75ace676481f6021c4dc66d"><div class="ttname"><a href="group__Control.html#ga4b3b7388e75ace676481f6021c4dc66d">clingo_control_solve</a></div><div class="ttdeci">CLINGO_VISIBILITY_DEFAULT bool clingo_control_solve(clingo_control_t *control, clingo_solve_mode_bitset_t mode, clingo_literal_t const *assumptions, size_t assumptions_size, clingo_solve_event_callback_t notify, void *data, clingo_solve_handle_t **handle)</div><div class="ttdoc">Solve the currently grounded logic program enumerating its models.</div></div>
<div class="ttc" id="agroup__Model_html_gaaf9a93819f023f3cb8aa80598c46556b"><div class="ttname"><a href="group__Model.html#gaaf9a93819f023f3cb8aa80598c46556b">clingo_model_t</a></div><div class="ttdeci">struct clingo_model clingo_model_t</div><div class="ttdoc">Object representing a model.</div><div class="ttdef"><b>Definition:</b> clingo.h:2085</div></div>
<div class="ttc" id="agroup__SolveHandle_html_ga73d71df7adeee92f1d515f52cdcbbac0"><div class="ttname"><a href="group__SolveHandle.html#ga73d71df7adeee92f1d515f52cdcbbac0">clingo_solve_handle_resume</a></div><div class="ttdeci">CLINGO_VISIBILITY_DEFAULT bool clingo_solve_handle_resume(clingo_solve_handle_t *handle)</div><div class="ttdoc">Discards the last model and starts the search for the next one.</div></div>
<div class="ttc" id="agroup__SolveHandle_html_ga023c1084a4c01ea6e121a8310efba045"><div class="ttname"><a href="group__SolveHandle.html#ga023c1084a4c01ea6e121a8310efba045">clingo_solve_handle_t</a></div><div class="ttdeci">struct clingo_solve_handle clingo_solve_handle_t</div><div class="ttdoc">Search handle to a solve call.</div><div class="ttdef"><b>Definition:</b> clingo.h:2322</div></div>
<div class="ttc" id="agroup__Control_html_gae917a23b0591d181004ec88c4e3291c1"><div class="ttname"><a href="group__Control.html#gae917a23b0591d181004ec88c4e3291c1">clingo_solve_result_bitset_t</a></div><div class="ttdeci">unsigned clingo_solve_result_bitset_t</div><div class="ttdef"><b>Definition:</b> clingo.h:2248</div></div>
<div class="ttc" id="agroup__SolveHandle_html_gaf9353c14149a2a16adf0474796871ae1"><div class="ttname"><a href="group__SolveHandle.html#gaf9353c14149a2a16adf0474796871ae1">clingo_solve_handle_model</a></div><div class="ttdeci">CLINGO_VISIBILITY_DEFAULT bool clingo_solve_handle_model(clingo_solve_handle_t *handle, clingo_model_t const **model)</div><div class="ttdoc">Get the next model (or zero if there are no more models).</div></div>
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Sep 16 2022 19:52:36 for Clingo C API by  <a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.17
</small></address>
</body>
</html>