forked from potassco/potassco.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_8c-example.html
95 lines (93 loc) · 21 KB
/
model_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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://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.11"/>
<title>Clingo C API: model.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>
<script type="text/javascript">
$(document).ready(function() { init_search(); });
</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.11 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main Page</span></a></li>
<li><a href="modules.html"><span>Modules</span></a></li>
<li><a href="annotated.html"><span>Classes</span></a></li>
<li><a href="files.html"><span>Files</span></a></li>
<li><a href="examples.html"><span>Examples</span></a></li>
<li>
<div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</li>
</ul>
</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">model.c</div> </div>
</div><!--header-->
<div class="contents">
<p>The example shows how to inspect a model.</p><h2>Output</h2>
<div class="fragment"><div class="line">$ ./model 0</div><div class="line">Stable model:</div><div class="line"> shown: c</div><div class="line"> atoms: b</div><div class="line"> terms: c</div><div class="line"> ~atoms: a</div><div class="line">Stable model:</div><div class="line"> shown: a</div><div class="line"> atoms: a</div><div class="line"> terms:</div><div class="line"> ~atoms: b</div></div><!-- fragment --><h2>Code</h2>
<div class="fragment"><div class="line"><span class="preprocessor">#define __STDC_FORMAT_MACROS</span></div><div class="line"><span class="preprocessor">#include <inttypes.h></span></div><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"></div><div class="line"><span class="keyword">typedef</span> <span class="keyword">struct </span>model_buffer {</div><div class="line"> <a class="code" href="group__Symbols.html#ga6c75c60fa57c3b97505265ff08f6f951">clingo_symbol_t</a> *symbols;</div><div class="line"> <span class="keywordtype">size_t</span> symbols_n;</div><div class="line"> <span class="keywordtype">char</span> *string;</div><div class="line"> <span class="keywordtype">size_t</span> string_n;</div><div class="line">} model_buffer_t;</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> free_model_buffer(model_buffer_t *buf) {</div><div class="line"> <span class="keywordflow">if</span> (buf->symbols) {</div><div class="line"> free(buf->symbols);</div><div class="line"> buf->symbols = NULL;</div><div class="line"> buf->symbols_n = 0;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keywordflow">if</span> (buf->string) {</div><div class="line"> free(buf->string);</div><div class="line"> buf->string = NULL;</div><div class="line"> buf->string_n = 0;</div><div class="line"> }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">bool</span> print_symbol(<a class="code" href="group__Symbols.html#ga6c75c60fa57c3b97505265ff08f6f951">clingo_symbol_t</a> symbol, model_buffer_t *buf) {</div><div class="line"> <span class="keywordtype">bool</span> ret = <span class="keyword">true</span>;</div><div class="line"> <span class="keywordtype">char</span> *string;</div><div class="line"> <span class="keywordtype">size_t</span> n;</div><div class="line"></div><div class="line"> <span class="comment">// determine size of the string representation of the next symbol in the model</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a0"></a><a class="code" href="group__Symbols.html#gafba05ee305c698c687390ed2ee4f1562">clingo_symbol_to_string_size</a>(symbol, &n)) { <span class="keywordflow">goto</span> error; }</div><div class="line"></div><div class="line"> <span class="keywordflow">if</span> (buf->string_n < n) {</div><div class="line"> <span class="comment">// allocate required memory to hold the symbol's string</span></div><div class="line"> <span class="keywordflow">if</span> (!(<span class="keywordtype">string</span> = (<span class="keywordtype">char</span>*)realloc(buf->string, <span class="keyword">sizeof</span>(*buf->string) * n))) {</div><div class="line"> <a name="a1"></a><a class="code" href="group__BasicTypes.html#ga861bc86ccee813d744920ac0de35ee11">clingo_set_error</a>(<a name="a2"></a><a class="code" href="group__BasicTypes.html#gga5adba1f5e366e7489ac4a3fb5266dba6aa14a0926eb3e653fcc13299b33d8d348">clingo_error_bad_alloc</a>, <span class="stringliteral">"could not allocate memory for symbol's string"</span>);</div><div class="line"> <span class="keywordflow">goto</span> error;</div><div class="line"> }</div><div class="line"></div><div class="line"> buf->string = string;</div><div class="line"> buf->string_n = n;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="comment">// retrieve the symbol's string</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a3"></a><a class="code" href="group__Symbols.html#ga7990b41b229320ac158fc428fa366000">clingo_symbol_to_string</a>(symbol, buf->string, n)) { <span class="keywordflow">goto</span> error; }</div><div class="line"> printf(<span class="stringliteral">"%s"</span>, buf->string);</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">bool</span> print_model(<a class="code" href="group__Model.html#gaaf9a93819f023f3cb8aa80598c46556b">clingo_model_t</a> *model, model_buffer_t *buf, <span class="keywordtype">char</span> <span class="keyword">const</span> *label, <a class="code" href="group__Model.html#gafe9df0b81b3b423f1c68dec3f64ad564">clingo_show_type_bitset_t</a> show) {</div><div class="line"> <span class="keywordtype">bool</span> ret = <span class="keyword">true</span>;</div><div class="line"> <a class="code" href="group__Symbols.html#ga6c75c60fa57c3b97505265ff08f6f951">clingo_symbol_t</a> *symbols;</div><div class="line"> <span class="keywordtype">size_t</span> n;</div><div class="line"> <a class="code" href="group__Symbols.html#ga6c75c60fa57c3b97505265ff08f6f951">clingo_symbol_t</a> <span class="keyword">const</span> *it, *ie;</div><div class="line"></div><div class="line"> <span class="comment">// determine the number of (shown) symbols in the model</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a4"></a><a class="code" href="group__Model.html#ga6fd012a9b6deb0d743701ceba35b65ec">clingo_model_symbols_size</a>(model, show, &n)) { <span class="keywordflow">goto</span> error; }</div><div class="line"></div><div class="line"> <span class="comment">// allocate required memory to hold all the symbols</span></div><div class="line"> <span class="keywordflow">if</span> (buf->symbols_n < n) {</div><div class="line"> <span class="keywordflow">if</span> (!(symbols = (<a class="code" href="group__Symbols.html#ga6c75c60fa57c3b97505265ff08f6f951">clingo_symbol_t</a>*)malloc(<span class="keyword">sizeof</span>(*buf->symbols) * n))) {</div><div class="line"> <a class="code" href="group__BasicTypes.html#ga861bc86ccee813d744920ac0de35ee11">clingo_set_error</a>(<a class="code" href="group__BasicTypes.html#gga5adba1f5e366e7489ac4a3fb5266dba6aa14a0926eb3e653fcc13299b33d8d348">clingo_error_bad_alloc</a>, <span class="stringliteral">"could not allocate memory for atoms"</span>);</div><div class="line"> <span class="keywordflow">goto</span> error;</div><div class="line"> }</div><div class="line"></div><div class="line"> buf->symbols = symbols;</div><div class="line"> buf->symbols_n = n;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="comment">// retrieve the symbols in the model</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a5"></a><a class="code" href="group__Model.html#gaa32137ad8fa747f5fa5dae3345ddf982">clingo_model_symbols</a>(model, show, buf->symbols, n)) { <span class="keywordflow">goto</span> error; }</div><div class="line"></div><div class="line"> printf(<span class="stringliteral">"%s:"</span>, label);</div><div class="line"></div><div class="line"> <span class="keywordflow">for</span> (it = buf->symbols, ie = buf->symbols + n; it != ie; ++it) {</div><div class="line"> printf(<span class="stringliteral">" "</span>);</div><div class="line"> <span class="keywordflow">if</span> (!print_symbol(*it, buf)) { <span class="keywordflow">goto</span> error; }</div><div class="line"> }</div><div class="line"></div><div class="line"> printf(<span class="stringliteral">"\n"</span>);</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">bool</span> on_model(<a class="code" href="group__Model.html#gaaf9a93819f023f3cb8aa80598c46556b">clingo_model_t</a> *model, model_buffer_t *data, <span class="keywordtype">bool</span> *goon) {</div><div class="line"> <span class="keywordtype">bool</span> ret = <span class="keyword">true</span>;</div><div class="line"> uint64_t number;</div><div class="line"> <a class="code" href="group__Model.html#ga13f511e3162055cd337c7f5e2ada31a1">clingo_model_type_t</a> type;</div><div class="line"> <span class="keywordtype">char</span> <span class="keyword">const</span> *type_string = <span class="stringliteral">""</span>;</div><div class="line"></div><div class="line"> <span class="comment">// get model type</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a6"></a><a class="code" href="group__Model.html#gad41ae442c905008f0dcd923c4ef3bc7c">clingo_model_type</a>(model, &type)) { <span class="keywordflow">goto</span> error; }</div><div class="line"></div><div class="line"> <span class="keywordflow">switch</span> ((<span class="keyword">enum</span> <a class="code" href="group__Model.html#gad41ae442c905008f0dcd923c4ef3bc7c">clingo_model_type</a>)type) {</div><div class="line"> <span class="keywordflow">case</span> <a name="a7"></a><a class="code" href="group__Model.html#ggad41ae442c905008f0dcd923c4ef3bc7ca26c979a76b39d052f9e4475b5479bbdf">clingo_model_type_stable_model</a>: { type_string = <span class="stringliteral">"Stable model"</span>; <span class="keywordflow">break</span>; }</div><div class="line"> <span class="keywordflow">case</span> <a name="a8"></a><a class="code" href="group__Model.html#ggad41ae442c905008f0dcd923c4ef3bc7ca6a84892300ae13401572c5b5a9bd69db">clingo_model_type_brave_consequences</a>: { type_string = <span class="stringliteral">"Brave consequences"</span>; <span class="keywordflow">break</span>; }</div><div class="line"> <span class="keywordflow">case</span> <a name="a9"></a><a class="code" href="group__Model.html#ggad41ae442c905008f0dcd923c4ef3bc7ca4ae425fae120bbeb34c5f983833b66d7">clingo_model_type_cautious_consequences</a>: { type_string = <span class="stringliteral">"Cautious consequences"</span>; <span class="keywordflow">break</span>; }</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="comment">// get running number of model</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a10"></a><a class="code" href="group__Model.html#gaea00ec08c9b1888b50b52a113eaa5cd6">clingo_model_number</a>(model, &number)) { <span class="keywordflow">goto</span> error; }</div><div class="line"></div><div class="line"> printf(<span class="stringliteral">"%s %"</span> PRIu64 <span class="stringliteral">":\n"</span>, type_string, number);</div><div class="line"></div><div class="line"> <span class="keywordflow">if</span> (!print_model(model, data, <span class="stringliteral">" shown"</span>, <a name="a11"></a><a class="code" href="group__Model.html#ggaa943fe81f0874ea63820b627bc19845ca82a698ef41cd919cb370d22fc0b44826">clingo_show_type_shown</a>)) { <span class="keywordflow">goto</span> error; }</div><div class="line"> <span class="keywordflow">if</span> (!print_model(model, data, <span class="stringliteral">" atoms"</span>, <a name="a12"></a><a class="code" href="group__Model.html#ggaa943fe81f0874ea63820b627bc19845cae4881b426be40132ce254553a1f7c78e">clingo_show_type_atoms</a>)) { <span class="keywordflow">goto</span> error; }</div><div class="line"> <span class="keywordflow">if</span> (!print_model(model, data, <span class="stringliteral">" terms"</span>, <a name="a13"></a><a class="code" href="group__Model.html#ggaa943fe81f0874ea63820b627bc19845ca25cc3101d9085b55be080510077c11cb">clingo_show_type_terms</a>)) { <span class="keywordflow">goto</span> error; }</div><div class="line"> <span class="keywordflow">if</span> (!print_model(model, data, <span class="stringliteral">" ~atoms"</span>, <a name="a14"></a><a class="code" href="group__Model.html#ggaa943fe81f0874ea63820b627bc19845ca764756fce5bfb41ba2d3bf23c4c85e09">clingo_show_type_complement</a></div><div class="line"> | <a class="code" href="group__Model.html#ggaa943fe81f0874ea63820b627bc19845cae4881b426be40132ce254553a1f7c78e">clingo_show_type_atoms</a>)) { <span class="keywordflow">goto</span> error; }</div><div class="line"></div><div class="line"> <span class="comment">// continue solving after a model has been reported</span></div><div class="line"> *goon = <span class="keyword">true</span>;</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"> <span class="keywordtype">char</span> <span class="keyword">const</span> *error_message;</div><div class="line"> <span class="keywordtype">int</span> ret = 0;</div><div class="line"> <a class="code" href="group__Control.html#gae917a23b0591d181004ec88c4e3291c1">clingo_solve_result_bitset_t</a> solve_ret;</div><div class="line"> <a class="code" href="group__Control.html#gaf008e9db9dbb37b0b7ef039bb9d582f0">clingo_control_t</a> *ctl = NULL;</div><div class="line"> <a name="_a15"></a><a class="code" href="structclingo__part.html">clingo_part_t</a> parts[] = {{ <span class="stringliteral">"base"</span>, NULL, 0 }};</div><div class="line"> model_buffer_t buf = {NULL, 0, NULL, 0};</div><div class="line"></div><div class="line"> <span class="comment">// create a control object and pass command line arguments</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a16"></a><a class="code" href="group__Control.html#ga46254f92ddf864a5214cf1572402e2bc">clingo_control_new</a>(argv+1, argc-1, NULL, NULL, 20, &ctl) != 0) { <span class="keywordflow">goto</span> error; }</div><div class="line"></div><div class="line"> <span class="comment">// add a logic program to the base part</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a17"></a><a class="code" href="group__Control.html#gab65cc073ff9dbf71e53b71fb14bc9ec7">clingo_control_add</a>(ctl, <span class="stringliteral">"base"</span>, NULL, 0, <span class="stringliteral">"1 {a; b} 1. #show c : b. #show a/0."</span>)) { <span class="keywordflow">goto</span> error; }</div><div class="line"></div><div class="line"> <span class="comment">// ground the base part</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a18"></a><a class="code" href="group__Control.html#gad0d95c5766649ff2f00e4ee3507a679f">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 using a model callback</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a19"></a><a class="code" href="group__Control.html#ga32ecc4f42257562dde343621597a596d">clingo_control_solve</a>(ctl, (<a name="a20"></a><a class="code" href="group__Control.html#gad666e6b4fba4d652351592937ccdc287">clingo_model_callback_t</a>*)on_model, &buf, NULL, 0, &solve_ret)) { <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"> <span class="keywordflow">if</span> (!(error_message = <a name="a21"></a><a class="code" href="group__BasicTypes.html#ga9732caf39396e15e7a2eb0e01d1a71b0">clingo_error_message</a>())) { error_message = <span class="stringliteral">"error"</span>; }</div><div class="line"></div><div class="line"> printf(<span class="stringliteral">"%s\n"</span>, error_message);</div><div class="line"> ret = <a name="a22"></a><a class="code" href="group__BasicTypes.html#ga94ce73fd010c0dbad708901a472f2c7a">clingo_error_code</a>();</div><div class="line"></div><div class="line">out:</div><div class="line"> free_model_buffer(&buf);</div><div class="line"> <span class="keywordflow">if</span> (ctl) { <a name="a23"></a><a class="code" href="group__Control.html#gad1c4e8f6be76e1d3d8e34d71363dd067">clingo_control_free</a>(ctl); }</div><div class="line"></div><div class="line"> <span class="keywordflow">return</span> ret;</div><div class="line">}</div><div class="line"></div></div><!-- fragment --> </div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Thu Aug 18 2016 13:19:52 for Clingo C API by  <a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.11
</small></address>
</body>
</html>