-
Notifications
You must be signed in to change notification settings - Fork 0
/
Manual.tex
3000 lines (2732 loc) · 125 KB
/
Manual.tex
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\documentclass[a4paper, titlepage]{article}
\usepackage{graphicx}
\usepackage{url}
\usepackage{hyperref}
\usepackage{listings}
\usepackage{amsmath}
\usepackage{array}
\usepackage{longtable}
\usepackage{multirow}
\usepackage{paralist}
\usepackage{color}
\usepackage{amsthm}
\usepackage{tikz}
\usetikzlibrary{arrows}
\usepackage{csquotes}
\newcommand{\mi}[1]{\mathit{#1}}
\newcommand{\amp}[1]{\ensuremath{\&{\mathit{#1}}}}
\newcommand{\ext}[3]{\ensuremath{\&{\mathit{#1}}[#2](#3)}}
\DeclareMathOperator{\leftimpl}{:-}
\DeclareMathOperator{\weakimpl}{:\sim}
\DeclareMathOperator{\clasneg}{\text{-}}
\DeclareMathOperator{\nott}{\mathit{not}}
\DeclareMathOperator{\noteq}{!=}
\DeclareMathOperator{\noteqq}{<>}
\DeclareMathOperator{\lesseq}{<=}
\DeclareMathOperator{\geeq}{>=}
\newcommand{\lts}{\,{<}\,}
\newcommand{\gts}{\,{>}\,}
\newcommand{\les}{\,{\le}\,}
\newcommand{\ges}{\,{\ge}\,}
\newcommand{\examplelink}[1]{\url{https://github.com/hexhex/manual/tree/master/#1}}
\newcommand{\exampledownloadlink}[2]{\href{https://github.com/hexhex/manual/raw/master/#1}{#2}}
\newcommand\mycenterline[1]{\par\smallskip\centerline{#1} \smallskip}
\newcommand\leftaligned[1]{\par \smallskip \noindent \qquad #1 \smallskip \par}
%\newcommand\mycenterline[1]{\smallskip #1 \smallskip\\}
% for commenting out stuff
\newcommand\nop[1]{}
\setlength{\tabcolsep}{1.5pt}
\lstset{
literate={~} {$\sim$}{1},
basicstyle=\footnotesize,
language=Python,
showstringspaces=false,
frame=single
}
\newcommand{\row}[1]{%
\ensuremath{\color{black}\ensuremath{\mathbf{#1}} \color{black}}%
}
\newcommand{\rowprefix}[1]{%
\ensuremath{\color{black}\mathbf{#1{:}}\ }%
}
\newcommand{\rowprefixprime}[1]{%
\ensuremath{\color{black}\mathbf{#1\prime{:}}\ }%
}
\newcommand{\rowprefixprimeprime}[1]{%
\ensuremath{\color{black}\mathbf{#1\prime\prime{:}}\ }%
}
\newcommand{\tobewritten}[0]{This section to be written.}
\allowdisplaybreaks
\begin{document}
\setcounter{page}{3}
\newcommand{\dlvhex}{{\sc dlvhex}}
\newcommand{\clasp}{{\sf Clasp}}
\newcommand{\gringo}{{\sf Gringo}}
\newcommand{\hex}{{\sc hex}}
\newcommand{\dlv}{{\sc dlv}}
\newcommand{\dlvdb}{{\sc dlvdb}}
\newcommand{\libdlv}{{\sc libdlv}}
\newcommand{\libclingo}{{\sc libclingo}}
\newcommand{\genuineii}{{\sc genuineii}}
\newcommand{\genuinegi}{{\sc genuinegi}}
\newcommand{\genuineic}{{\sc genuineic}}
\newcommand{\genuinegc}{{\sc genuinegc}}
\setcounter{secnumdepth}{4} % how many sectioning levels to assign numbers to
\setcounter{tocdepth}{4} % how many sectioning levels to show in ToC
\theoremstyle{definition}
\newtheorem{exmpl}{Example}[section]
% add \qed to mark end of example
\newenvironment{exmp}{\begin{exmpl}}{\qed\end{exmpl}}
\begin{titlepage}
\centering
%\vfill
\includegraphics[width=1.0\textwidth]{biglogo_whitebg.png}
\vskip2cm
{\bfseries\Huge User Guide} \\[1em]
{\bfseries\Huge \dlvhex\ 2.X} \\
\vskip1.0cm
{\sc\Large INFSYS Research Report 1843-15-05} \\
\medskip
{\Large \today}
\vskip2cm
{\bfseries\Large
Thomas Eiter$^1$\\[2mm]
Mustafa Mehuljic$^2$ \\[2mm] %\'{c}$^2$ \\[2mm]
Christoph Redl$^1$ \\[2mm]
Peter Sch\"{u}ller$^2$ \\[2mm]
}
\vspace{1cm}
\begin{tabular}{r@{}l}
{\Large$^1$}
& Institut f\"ur Informationssysteme,\\
& Abteilung Wissensbasierte Systems,\\
& Technische Universit\"at Wien, Austria \\
& {\tt\{eiter,redl\}@kr.tuwien.ac.at} \\[1ex]
{\Large$^2$}
& Department of Computer Engineering,\\
& Faculty of Engineering, \\
& Marmara University, Turkey \\
& {\tt [email protected]}\\
& {\tt [email protected]}
\end{tabular}
\vspace{4cm}
\end{titlepage}
%
% pdflatex begining.tex
% bibtex begining.aux
% pdflatex begining.tex
% pdflatex begining.tex
%
% Abstract part
\begin{abstract}
This document provides a user guide for the Answer Set
Programming (ASP) system called \dlvhex{}.
%developed at Technische Universit\"at Wien.
% and in various other places (unical, deri galway(?), sabanci, marmara)
ASP is a declarative
problem solving paradigm, rooted in logic programming and
nonmonotonic reasoning, which has been gaining increasing
attention during the last years. The \dlvhex{} system is a
reasoner for computing the models of so-called \hex{}-programs, which are an extension of \emph{answer-set
programs} towards integration of \emph{external computation
sources}. This guide aims at explaining the syntax
of \hex{}-programs and the usage of the \dlvhex{} solver
to enable users
to interoperate with a broad set of external computation
sources. The guide refers to version 2.4 and higher.
\end{abstract}
% Generates table of contents
\setcounter{tocdepth}{2}
\tableofcontents
\newpage
\section{Introduction} % Section No.1
\label{sec:intro}
The \dlvhex{} system is a logic-programming reasoner for
computing the models of so-called \hex{}-programs~\cite{efikrs2015}, which
are an extension of \emph{answer-set programs} towards
integration of \emph{external computation sources}. To
enable access to external information, \hex{}-programs
extend programs with external atoms, which allow for a
bidirectional communication between the logic program and
external sources of computation (e.g. description logic
reasoners and Web resources)~\cite{efkr2012}.
The system is motivated by the need to interoperate with a
broad set of external computation sources and the
observation, that for meta-reasoning in the context of the
Semantic Web, no adequate support is available in ASP to
date.
To overcome this, \hex{}-programs support higher-order logic programs
(which accommodate meta-reasoning through higher-order
atoms) with external atoms for software interoperability.
This guide is intended to help beginners make use of the system and
provide a reference for the features of the tool.
The language of \hex{}-programs is an extension of disjunctive datalog,
it largely realizes the ASP-Core-2 Standard \cite{cffiklrs2013}
and extends it with external and high-order atoms.
\subsection{Download and Installation}
\dlvhex{} is written in the C++ programming language
and published under the GNU Lesser General
Public License \cite{licnc}.
In this section we provide an overview of the
download and installation process. For a quick overview,
some examples and the possibility to evaluate
\hex{}-programs directly in the browser,
an online demo is available at
\url{http://www.kr.tuwien.ac.at/research/systems/dlvhex/demo.php}.
%However the system can also be installed locally.
\subsubsection{Building from source}
There are two possibilities to install \dlvhex{}
from source: install the latest stable release of the
system or install the latest development version which may
not be stable. Both ways are described in the following
sections.
\paragraph{Latest release version (tarball)}
\label{sec:steps}
Packages (tarballs) of \dlvhex{} can be downloaded from the
project page
\href{http://www.kr.tuwien.ac.at/research/systems/dlvhex/}%
{\tt http://www.kr.tuwien.ac.at/res earch/systems/dlvhex/}.
The latest release of the
software runs on Linux-based systems, Mac OS X and
Microsoft Windows. Installation instructions are given in
the {\tt INSTALL} and {\tt README} files of \dlvhex{}
and plugin source directories. Changes between versions can
be found in the {\tt NEWS} files and details about changes in the {\tt
ChangeLog} file.
The system requires the following packages:
git, gcc and g++ (version 4.8 or later),
BZ2, Python (version 2.7 or later), bison, scons,
cmake, automake, autoconf,
%standard C++ library (version 4.8 or later),
Curl (version 4 or later),
libtool, and Boost (version 1.55 or later).
After downloading Boost from
\url{http://www.boost.org/},
the following steps should be followed in order
to install it in a way usable for \dlvhex.
Here and in the following, commands prefixed with ``\texttt{\$}'' sign are the commands executed from the system shell.
%Some commands need ``\texttt{\#}'' as prefix to ensure superuser permissions.
The following commands need to be executed:
%
\leftaligned{\texttt{\$ \thinspace ./bootstrap.sh}}
%
\leftaligned{\texttt{\$ \thinspace ./b2 install --prefix=BPREFIX}}
%
In this
command, \texttt{BPREFIX} is the directory where Boost should
be installed.
After downloading the latest release version of \dlvhex{},
the following sequence of commands \dlvhex{} will
configure \dlvhex{}:
%
\leftaligned{\texttt{\$ \thinspace ./configure}}
%
To enable the Python
features of \dlvhex{}, \texttt{--enable-python} needs to be
added as an additional parameter to \texttt{configure}.
%
If Boost was installed in a non-standard location,
the parameter \texttt{--with-boost=BPREFIX} also needs to be added.
After configuration, the following command builds the system:
%
\leftaligned{\texttt{\$ \thinspace make}}
%
To allow using of multiple
cores one should specify the \texttt{-jN} option to make
use of N cores. Finally,
%
\leftaligned{\texttt{\$ \thinspace make install}}
%
installs the package.
The installation location can be set to \texttt{HPREFIX}
by adding parameter \texttt{--prefix=HPREFIX} to \texttt{configure}.
\paragraph{Development version (git clone)}
The source code of \dlvhex{} is hosted on github at
\url{https://github.com/hexhex/}. To get the latest
development version it is necessary to clone the repository as
follows:
%
\leftaligned{\texttt{\$ \thinspace
%
git clone
https://github.com/hexhex/core --recursive}}
After cloning it is necessary to
execute the script \texttt{bootstrap.sh}.
%
\leftaligned{\texttt{\$ \thinspace ./bootstrap.sh}}
%
After that, the steps from
Section~\ref{sec:steps} (\texttt{configure}, \texttt{make}, and
\texttt{make install}) should be followed to
complete the installation.
We provide a script for installing \dlvhex{}
automatically on Ubuntu systems:
\url{https://github.com/hexhex/core/blob/master/scripts/setupdlvhex.sh}.
Once installation is completed, \dlvhex{} can be used as follows:
%
\leftaligned{\texttt{\$ \thinspace dlvhex2 program.hex}}
%
where \texttt{program.hex} refers to the input program.
Various additional command line options are available
and explained in Section~\ref{sec:commandline}.
\subsubsection{Pre-built binaries}
Pre-built binaries of \dlvhex{} are available for some
systems. For details see our website
\url{http://www.kr.tuwien.ac.at/research/systems/dlvhex/}.
\subsection{Outline}
This guide is organized as follows. Section~\ref{sec:quick}
provides an introductory example including
problem instance, encoding and its solution.
Section~\ref{sec:inputLang} explains the input language of \dlvhex{}.
In Section~\ref{sec:examples} we
introduce three real life problems which can be solved
using \hex{}. Section~\ref{sec:externalInterfaces} is
focused on the description of external interfaces which are
written in C++ or Python. Input-related warnings and errors
are described into more details in
Section~\ref{sec:inputRelatedWarnings}.
\section{Quickstart} % Section No.2
\label{sec:quick}
As an introductory example, we consider a \emph{social
graph} as used in social networks. Beginning from a
simplified scenario, we stepwisely extend it to present
various features of \dlvhex{}.
\subsection{Problem Setting}
A \emph{social graph} is a graph that represents
interconnections among people, groups
and organizations in a social network. Services such as
Facebook facilitate the exchange
of information, news, photographs, literary works, music,
art, software, opinions or even
money among users.
Individuals and organizations, called actors, are nodes of
the graph.
In this environment, the social graph
for a particular actor consists
of the set of nodes and edges which model other actors that
are directly connected to that actor.
Interdependencies,
called ties, can be multiple and diverse, including
characteristics or concepts such as age,
gender, ethnical group, genealogy, chain of command, ideas, financial
transactions, trade relationships,
political affiliations, club memberships, occupation,
education and economic status.
Social graphs contain edges between one person and related
people, places, and things they interact
with online. For this particular example, we consider a
simulation of social graphs as used, e.g., by Facebook.
Consider the situation where a birthday party should be
organized and a specific number of friends will be invited.
The \emph{person $X$} who organizes the event wants to
call his or her friends and friends of these friends up to
some distance from the root node $X$. A \emph{depth
constraint} specifies how many edges we can go away from
the root node $X$.
We make use of an external source which returns for a given
person all direct friends, while a direct access to the
full graph is not available due to privacy issues imposed
by social networks. Also, due to the large amount of data,
importing the whole graph would be infeasible (billions of
users), while only a small fraction is relevant for the
application. Given a person $X$,
the external source retrieves all
neighbour nodes (successor nodes). More details about the
external source implementation are given in
Section~\ref{sec:externalInterfaces}.
\subsection{Encoding}
The problem can be modeled as a \hex{}-program as follows:
\begin{exmp}[\exampledownloadlink%
{example_2_1/example_2_1.hex}{download example\_2\_1.hex}]
\label{faceQuery}
\begin{align*}
\rowprefix{r_1} & \mathit{personOfInterest}(\mathit{john}). \\
\rowprefix{r_2} & \mathit{friendOfDegree}(\mathit{P, 0, P})
\leftimpl \mathit{personOfInterest}(P). \\
\rowprefix{r_3} &
\begin{array}[t]{@{}r@{\,}l}
\mathit{friendOfDegree}(\mathit{P, DegPlus, F2}) \leftimpl
& \mathit{friendsOfDegree}(\mathit{P,Deg,F1)},\\
& \ext{friendsOf}{F1}{F2},\\
& \mathit{DegPlus = Deg + 1}, \\
& \mathit{DegPlus < 2},\\
& \mathit{\#int(DegPlus)}, \mathit{\#int(Deg)}.
\end{array} \\
\rowprefix{r_4} & \mathit{invite(P)} \vee \mathit{ninvite(P)
\leftimpl friendOfDegree(J,X,P), \#int(X).} \\
\rowprefix{r_5} & \leftimpl \nott \thinspace 4 = \mathit{\#count}
\{ P : \mathit{invite(P)} \}.
\end{align*}
\end{exmp}
% PS: TODO we could remove all #int here, they should not be required
% PS: TODO should we say something about the ":- not" pattern?
% PS: removed "john" in r_4 because we should use personOfInterest instead, but in this example we can also just use all discovered friends
The complete source code for this example is available at
\examplelink{example_2_1}.
Rule $\row{r_1}$ specifies the person who organizes the event
and initializes the search.
Rule $\row{r_2}$ defines that the initiating person has
distance 0 from him- or herself.
The most interesting part of the program is rule $\row{r_3}$.
It cyclically defines friends of
already known persons using an external atom
and increments the distance with each
definition. Variables used in these predicates are:
\begin{itemize}
\item $\mathit{F1}$ to represent the person for which we are
looking for the successors
\item $\mathit{F2}$ is the variable for successor
nodes of $F1$
\item $P$ represents the person of interest
\item $\mathit{Deg}$ and $\mi{DegPlus}$ are variables used to
compute the distance from the root node
\end{itemize}
The external atom \ext{friendsOf}{F1}{F2} has one input and
one output parameter. For input $\mathit{F1}$,
it finds all successor nodes of $\mi{F1}$ and returns them in
$\mathit{F2}$.
%
In other words, \ext{friendsOf}{F1}{F2} is true for all pairs
$(\mi{F1},\mi{F2})$ such that
$\mi{F2}$ is a direct friend of $\mi{F1}$ in the graph.
%
The implementation of the external atom is
discussed in Section~\ref{sec:externalInterfaces}.
%
The atom
\begin{align*}
& \mathit{friendOfDegree}(P, Deg, F1)
\end{align*}
is true for all friends $\mi{F1}$ of $P$ that we already know;
it binds the variable $\mathit{F1}$ to a person for which we
want to discover more successor nodes.
%
This value is used as input to
the external source \ext{friendsOf}{F1}{F2}
which returns all friends $F2$ of $F1$.
For each found friend $F2$ we define:
\begin{align*}
& \mathit{friendOfDegree}(P, \mi{DegPlus}, F2)
\end{align*}
where $\mathit{DegPlus}$ is $\mathit{Deg}$ incremented by
$1$ to represent that the distance to $F2$ is by $1$
greater than to $F1$. The condition
\begin{align*}
& \mathit{DegPlus} < 2
\end{align*}
ensures the distance is limited to 2.
We now move to the part where we handle invitations. Rule $\row{r_4}$ guesses all possible
persons to be invited or not. Since atom
$\mathit{friendOfDegree(J, X, P)}$ is true for person $P$, that person will be either invited or not.
We limit the number of invited persons by using an
\emph{integrity constraint} from the $\row{r_5}$
It ensures that exactly 4 persons are invited to the party.
The combination of $\row{r_4}$ and $\row{r_5}$
can be replaced by a construction called `choice`
as shown in $\row{r_5}\prime$.
This rule also allows to specify lower and upper bound
on the number of persons independently.
\begin{align*}
\rowprefixprime{r_5} 3 \lesseq \{ invite(P) : \mi{friendOfDegree}(J,X,P) \} \lesseq 3.
\end{align*}
% PS: TODO should we say something about expansion with :
\subsection{Problem Solution}
Now we are ready to solve our \emph{social graph} problem.
Consider that we have the data as specified in the Figure~\ref{fig:socialnetwork}.
\begin{figure}
\begin{center}
\begin{tikzpicture}[yscale=0.5,xscale=0.8]
\tikzset{vertex/.style = {shape=rectangle,draw,minimum width=5.5em,minimum height=2em}}
\tikzset{edge/.style = {->,> = latex'}}
% vertices
\node[vertex] (a) at (0,0) {$\mathit{John}$};
\node[vertex] (b) at (4,5) {$\mathit{Mike}$};
\node[vertex] (c) at (4,0) {$\mathit{Charly}$};
\node[vertex] (d) at (4,-5) {$\mathit{David}$};
\node[vertex] (e) at (7,7) {$\mathit{Jenifer}$};
\node[vertex] (f) at (7,5) {$\mathit{Alex}$};
\node[vertex] (g) at (7,3) {$\mathit{Serena}$};
\node[vertex] (h) at (7,0) {$\mathit{Roger}$};
\node[vertex] (i) at (7,-3) {$\mathit{Chris}$};
\node[vertex] (j) at (7,-7) {$\mathit{Joe}$};
\node[vertex] (k) at (10,7) {$\mathit{Angel}$};
\node[vertex] (l) at (10,5) {$\mathit{Thomas}$};
\node[vertex] (m) at (10,3) {$\mathit{Carolina}$};
\node[vertex] (n) at (10,0) {$\mathit{Steve}$};
\node[vertex] (o) at (10,-3) {$\mathit{Mark}$};
\node[vertex] (p) at (10,-7) {$\mathit{Christopher}$};
%edges
%\draw[edge] (a) to node [auto] {2} (b);
\draw[edge] (a) to (b);
\draw[edge] (a) to (c);
\draw[edge] (a) to (d);
\draw[edge] (b) to (e);
\draw[edge] (b) to (f);
\draw[edge] (b) to (g);
\draw[edge] (c) to (h);
\draw[edge] (d) to (i);
\draw[edge] (d) to (j);
\draw[edge] (e) to (k);
\draw[edge] (f) to (l);
\draw[edge] (g) to (m);
\draw[edge] (h) to (n);
\draw[edge] (i) to (o);
\draw[edge] (j) to (p);
\end{tikzpicture}
\end{center}
\caption{Social Network Graph}
\label{fig:socialnetwork}
\end{figure}
To compute the answer sets representing
the solution, \dlvhex{} should be invoked as follows:
%
\leftaligned{\texttt{\$ dlvhex2 --pythonplugin=example\_2\_1.py example\_2\_1.hex}}
%
where \texttt{example\_2\_1.hex} is \hex-program and
\texttt{example\_2\_1.py} is the Python plugin which realizes
the external source implementation.
Details of the Python plugin interface
are given in Section~\ref{sec:externalInterfaces},
the files can be downloaded from
\exampledownloadlink{example_2_1/example_2_1.py}{example\_2\_1.py}
(the plugin)
and
\exampledownloadlink{example_2_1/example_2_1.edgelist}{example\_2\_1.edgelist}
(the graph in Figure~\ref{fig:socialnetwork}).
The output of \dlvhex{} is as follows:
\begin{align*}
\{ &\mathit{personOfInterest(john), \ friendOfDegree(john,0,john), }\\
&\mathit{invite(john), \ friendOfDegree(john,1,mike),}\\
& \mathit{friendOfDegree(john,1,david), \ friendOfDegree(john,1,charly),} \\
& \mathit{invite(mike), \ invite(david), \ invite(charly)}
\}
\end{align*}
Note that the order of the atoms and the order of answer sets
does not bear any meaning. As we specified in the previous
section, we can travel at most 1 edge far from the root
node. Considering the graph given above only, $\mathit{John,
Mike, Charly}$ and $\mathit{David}$ are found since they
are at most one edge away from the root node. The next three atoms
express who are the new friends discovered and at which
depth
level. For the invitations, it is specified by using
aggregates that answer sets must have four distinct
$\mathit{invites}$ atoms.
In the single answer set we have four $\mathit{invites}$ atoms
which are $\mathit{invite(john),
invite(mike), invite(david), invite(charly)}$. Note that
this is the only answer set possible
from this program because
there are only 4 distinct persons with depth level 1.
If we allow the depth level to be larger there may be more
answer sets found due to the fact that more nodes will be
discovered. If we decrease the minimum number of friends to be
invited to the party there may also be more than one answer set.
Consider the different example where instead of 4
persons we want to invite only 3 persons to the party.
The integrity constraint at $\row{r_5}$ will be modified to:
\begin{align*}
& \rowprefixprimeprime{r_5} \leftimpl \nott \thinspace \mathit{\#count} \{ P : \mathit{invite(P)} \} = 3.
\end{align*}
This time we have more than one answer set. Since the depth
level is still 2 there will be 4 persons discovered again,
however, out of these 4 persons we have to invite only
three of them and one of them will not be invited.
According to this we have 4 answer sets. Two of them are
shown below:
\begin{align*}
\{ &\mathit{personOfInterest(john),
friendOfDegree(john,0,john),}\\
&\mathit{invite(john), friendOfDegree(john,1,mike),
\mathit{ninvite(charly)},}\\
&\mathit{friendOfDegree(john,1,david),
friendOfDegree(john,1,charly),}\\
&\mathit{invite(mike),invite(david)} \} \\
\{&\mathit{personOfInterest(john),
friendOfDegree(john,0,john),} \\
&\mathit{invite(john), friendOfDegree(john,1,mike),
\mathit{ninvite(mike)},}\\
&\mathit{friendOfDegree(john,1,david),
friendOfDegree(john,1,charly),} \\
&\mathit{invite(david),invite(charly)}\}
\end{align*}
(Again, note that the order of answer sets is arbitrary
and does not bear any meaning.)
%
This time in the answer set we have
$\mathit{ninvite(charly)}$ and $\mathit{ninvite(mike)}$
since one friend must be discarded and only three will be
invited. One can experiment with the \emph{depth constraint} and
\emph{aggregate atom} to see how the output and
answer sets will be affected.
\section{Input Language}% Section No. 3
\label{sec:inputLang}
This section provides an overview of the input language of
\dlvhex{} and some examples to illustrate the concepts.
\subsection{Terms and Atoms}
The vocabulary consists of terms, constants, variables and
external predicates. Terms may be integers, constants, function terms,
strings and variables as well as the \enquote{\_} token.
Constant names begin with lowercase letters or are strings
enclosed in quotation marks.
Variable names begin with uppercase letters.
Function terms (uninterpreted functions)
are \emph{complex terms} composed of a name
(like a constant) and one or more terms as arguments.
For instance,
$\mathit{at(john,t(10),X)}$ is a function term with three arguments:
constant $\mathit{john}$,
another function term $\mathit{t(12})$ with an integer argument,
and variable $X$ \cite{gkklorst2015}.
While a constant, function term, or string,
always represents itself; a variable is a
placeholder for all variable-free terms in the language of
a logic program.
An anonymous variable is a special variable
denoted by \enquote{\_} (the underscore):
each occurrence of \enquote{\_} represents
a new and unique variable which does not occur anywhere
else in the same rule. This might be used to specify that
an argument can be ignored or does not matter.
%(for example the atom $p(1,2)$ contains a different predicate than $p(1)$,
%hence if we want to ignore the second argument we must write $p(1,\_)$).
An \emph{atom} has the form $\mathit{p(t_1,\dots,t_n)}$ where
$p$ is a predicate name, $t_1,\dots,t_n$ are terms and $n \,{<}\, 0$
is the arity of the predicate $p$;
an atom $p()$ of arity 0 is usually written as $p$ without parentheses.
Atoms can be classically negated using \enquote{$\clasneg$},
yielding $q$ and $\clasneg q$.
\begin{exmp}
The following example illustrates the above concepts.
\begin{center}
\begin{tabular}{ll}
Constants: & $a$, $1$, $\mathit{a1}$,
$\mathit{9862}$, $\mathit{c1}$, ''hello'' \\
Variables: & $X$, $Y$, $Z$ \\
Atoms: & $\mathit{parent}(X,Y)$, $\mathit{employee}
(name, salary, ID, location)$ \\
Predicates: & $\mathit{parent}$, $\mathit{employee}$
\end{tabular}
\end{center}
\end{exmp}
\subsection{Normal Programs and Integrity Constraints}
A \hex{}-program is constructed using \emph{facts, rules
and integrity constraints}.
\begin{center}
\begin{tabular}{ r l@{}r }
Fact: & \texttt{$A_0$}. & \\
Rule: & \texttt{$A_0$}& $\leftimpl$ \texttt{$L_1$},\dots, \texttt{$L_n$}. \\
Constraint:&& $\leftimpl$ \texttt{$L_1$},\dots, \texttt{$L_n$}.
\end{tabular}
\end{center}
The sign \enquote{$\leftimpl$} is meant to be an
implication to the left ($\leftarrow$).
The left side of a rule is called
its head, and its right side is called body. The head
\texttt{$A_0$} of a rule or a fact is an atom. In the body of a
rule or an integrity constraint,
every \texttt{$L_j$}, $1 \les j \les n$,
is a literal of the form $\mathit{A}$ or
$\nott A$, where $A$ is an atom and the
connective $\nott$ denotes default negation. We say
that literal $L$ is positive if it is an atom and negative
otherwise. While the head atom $A_0$ of a fact
must unconditionally be true, the intuitive reading of a
rule corresponds to an implication: if all positive atoms
in the rule body are true and negated atoms are false, then
the head $A_0$ must be true. On the other hand, an integrity
constraint is a rule that filters solution candidates:
the literals in its body must not jointly be
satisfied.
A result of a \dlvhex{} computation is called an
\emph{answer set} which is a consistent explanation (model)
of the world
given the knowledge about the world represented by rules
as intuitively explained above.
We here give only informal semantics;
for a formal description of semantics of normal ASP programs and \hex{}-programs
we refer to \cite{gekakasc12a} and \cite{efikrs2015}, respectively.
\begin{exmp}
Consider the following logic program:
\begin{align*}
\rowprefix{r_1}&\mathit{ joke }. \\
\rowprefix{r_2}&\mathit{ laugh } \leftimpl \mathit{ joke }.
\end{align*}
The first line here represents an \emph{atom} which is
always true. The second line is a \emph{rule} and reads as
\enquote{If $\mathit{joke}$ is true, $\mathit{laugh}$ must
also be true}. Also we can read this as \enquote{from
$\mathit{joke}$ follows $\mathit{laugh}$}. The single
\emph{answer set} of the program above is $\{\mathit{joke},
\mathit{laugh}\}$ since they are the atoms which are true
in the program.
\end{exmp}
Another important feature of \dlvhex{} is \emph{default
negation} which is also called ``negation as failure".
Intuitively, negation as failure means:
if we cannot show truth of an atom,
then we may safely assume that it is false.
Variables in default negated atoms must be safe
which means that they must also occur in a positive atom
in the body of the same rule.
\begin{exmp}
With default negation we can represent the
complementary graph $\mi{comp\_edge}(X,Y)$
of a graph given as atoms $\mi{edge}(X,Y)$.
The complementary graph has the same nodes as the original graph,
but of all possible edges it has exactly those edges,
which do not exist in the original graph.
\begin{align*}
\rowprefix{r_1}& \mathit{node}(X) \leftimpl \mathit{edge}(X, \_). \\
\rowprefix{r_2}& \mathit{node}(Y) \leftimpl \mathit{edge}(\_, Y). \\
\rowprefix{r_3}& \mathit{comp\_edge}(X, Y) \leftimpl
\mathit{node}(X), \mathit{node}(Y),
\nott \thinspace \mathit{ edge }(X, Y).
\end{align*}
Note that $\mathit{node}(X)$ and $\mathit{node}(Y)$
are included in the body to satisfy the
safety requirement for variables $X$ and $Y$.
Also note the anonymous variables \enquote{$\_$}.
\end{exmp}
Default negation allows for generating multiple answer sets
that are equally correct as models of a single program.
%
Integrity constraints eliminate those answer set candidates
that make the body of the constraint true.
\begin{exmp}
Consider the following example for coloring nodes of a graph
either red, green or black.
\label{nodecoloring}
\begin{align*}
\rowprefix{r_1} & \mathit{node}(X) \leftimpl \mathit{edge}(X, Y). &\\
\rowprefix{r_2} & \mathit{node}(Y) \leftimpl \mathit{edge}(X, Y). & \\
\rowprefix{r_3} & \mathit{colored}(X, r) \leftimpl \mathit{node}(X), \nott \ colored(X,g), \nott \ colored(X,b). & \\
\rowprefix{r_4} & \mathit{colored}(X, g) \leftimpl \mathit{node}(X), \nott \ colored(X,r), \nott \ colored(X,b). & \\
\rowprefix{r_5} & \mathit{colored}(X, b) \leftimpl \mathit{node}(X), \nott \ colored(X,r), \nott \ colored(X,g). & \\[1ex]
\rowprefix{r_6} & \leftimpl \mathit{edge}(X, Y), \mathit{colored}
(X, C), \mathit{colored}(Y, C). & \\[1ex]
\rowprefix{r_7} & \mathit{edge}(2, 4). \ \mathit{edge}(2, 3). \
\mathit{edge}(5, 5). & \\
\rowprefix{r_8} & \mathit{edge}(4, 6). \ \mathit{edge}(4, 5). \
\mathit{edge}(5, 7). & \\
\rowprefix{r_9} & \mathit{edge}(6, 7). &
\end{align*}
In the first two rules we extract the nodes
implicitly given by the edges of the graph.
Rules \row{r_3}-\row{r_5} describe a \emph{guess}
such that for each node $X$,
either $\mi{colored}(X,r)$, $\mi{colored}(X,g)$, or $\mi{colored}(X,b)$
will be true in answer sets.
These three rules generate all possible node color combinations.
Rule \row{r_6} is a constraint, sometimes called \emph{check},
that
deletes all color combinations which do not satisfy the
requirement that there may be no edge between two nodes of
equal color.
\end{exmp}
\subsection{Classical Negation}
\dlvhex{} supports two kinds of negation. Here we will
emphasize the difference between explicitly expressing
falseness of an atom and having it done by
negation as failure.
The connective $\nott$ expresses
default negation, i.e. a literal $\nott \thinspace A$ is assumed
to hold unless atom $A$ is derived to be true.
This is also called \emph{Closed World Assumption}.
In contrast,
the classical (or strong) negation of an atom holds only if
it can be derived. In other words if there is no evidence
that an atom is true, it is considered to be false.
Classical negation \enquote{$\clasneg$} is
permitted in from of an atom. The semantic relationship
between $A$ and $\clasneg\mathit{A}$ is simply that they must not jointly
hold.
\begin{exmp}
Imagine a situation where an agent has to cross a railroad.
The agent should cross it if there is no train approaching.
With this description, one might specify the following
program:
\begin{align*}
\rowprefix{r_1}& \mathit{cross\_railroad} \leftimpl \nott \mathit{ train\_approaches}.
\end{align*}
This program has the answer set
\{$\mathit{cross\_railroad}$\} because
$\mathit{train\_approaches}$ is assumed to be false (as it
being true is not stated anywhere). This kind of negation
is called \emph{negation as failure}.
\end{exmp}
\begin{exmp}
Imagine, instead, that we use the following program
which uses classical negation (sometimes called strong or true negation).
\begin{align*}
\rowprefix{r_1}\mathit{cross\_railroad} \leftimpl \ \clasneg \mathit{train\_approaches}.
\end{align*}
Since $\clasneg\mathit{train\_approaches}$ is not known to be
true, the program has only an empty answer set.
\end{exmp}
Note the difference between the two kinds of negation:
in the first example, the railroad track is
crossed if there is no information on any trains
approaching;
while in the second example, it is only crossed if
we can prove that no train comes.
In that sense classical negation is stronger
than negation as failure.
\subsection{Disjunctive Programs}
\label{disjunction}
Disjunctive logic programs permit the connective ``$\vee$"
between atoms in rule heads.
\begin{center}
\begin{tabular}{ r l l}
\text{Fact:} & $A_0$ $\vee$ \dots $\vee$ $A_m$. \\
\text{Rule:} & $A_0$ $\vee$ \dots $\vee$ $A_m$
$\leftimpl$ $L_1,\dots,L_n. $ \\
\end{tabular}
\end{center}
A \emph{disjunctive head} holds if at least one of its
atoms is true. If all body literals $L_1,\dots,L_n$ of the
rule specified above are known to be true then the head also needs to hold, i.e. one of the atoms in $A_0$ $\vee$ \dots
$\vee$ $A_m$ needs to be true.
If our program just contains the fact
\enquote{$\mathit{a} \vee \mathit{b}$}
we obtain two answer sets \{$a$\} and \{$b$\}.
Disjunctive logic programs intuitively make
the minimum of disjunctive heads true
that are necessary to satisfy all constraints to obtain an answer set.
%
Formally speaking semantics are not that simple:
disjunctive programs have a higher expressive power
than normal logic programs
(it is possible to encode subset minimality
using advanced techniques such as \emph{saturation}
that we will not discuss further in this manual).
\begin{exmp}
In the Example~\ref{nodecoloring} we could replace
the guess in \row{r_3}-\row{r_5}
by the following single disjunctive rule:
\begin{align*}
\mathit{colored(X,r)} \vee \mathit{colored(X,g)} \vee \mathit{colored(X,b)} \leftimpl \mathit{node(X).}
\end{align*}
This generates all possible node color combinations.
\end{exmp}
\begin{exmp}
Suppose we met a friend recently and know that he had one
of his arms broken, but do not know which one. Now suppose
we did not receive a greeting card for your birthday and
wonder if you should be angry on him or he just could not
write because his right hand is broken.
Suppose that we encode this reasoning problem in \hex.
\begin{align*}
\rowprefix{r_1}& \mathit{left\_arm\_broken} \vee
\mathit{right\_arm\_broken}.\\
\rowprefix{r_2}& \mathit{can\_write} \leftimpl
\mathit{left\_arm\_broken}.\\
\rowprefix{r_3}& \mathit{be\_angry} \leftimpl
\mathit{can\_write}.
\end{align*}
For this program,
\dlvhex{} will generate two possible explanations. The
first rule is called a disjunctive rule which is read as
\enquote{For sure, either the left or the right arm is broken.} Without being sure which arm is broken \dlvhex{}
will evaluate the program and produce the two models
$\mathit{\{left\_arm\_broken, can\_write, be\_angry\}}$ and
$\mathit{\{right\_arm\_broken\}}$.
\end{exmp}
\subsection{Built-in Arithmetic Functions}
%Besides integers
%(constant arithmetic functions), written
%as sequence of digits $0$,\dots,$9$,
\dlvhex{} supports
integers and the following arithmetic operators:
%following operators for those functions:
$+$ (addition), $-$ (subtraction),
$*$ (multiplication), and $/$ (integer division).
Atom $+(X,Y,Z)$ is true,
iff $Z$ is the sum of $X$ and $Y$
and likewise for other operators.
Alternatively to \emph{prefix notation} one can also use
\emph{infix notation} to use built-in arithmetic functions
in \dlvhex{}. For instance $\mathit{+(X, Y, Z)}$
alternatively can be written as $\mathit{Z=X+Y}$.
\begin{exmp}
Suppose the following program.
\begin{align*}
&\rowprefix{r_1} \ \mathit{a}(6). \\
&\rowprefix{r_2} \ \mathit{b}(2). \\
&\rowprefix{r_3} \ c(X,Y,Z) \leftimpl a(X), b(Y),+(X, Y, Z). \\
&\rowprefix{r_4} \ d(X,Y,Z) \leftimpl a(X), b(Y),-(X, Y, Z). \\
&\rowprefix{r_5} \ e(X,Y,Z) \leftimpl a(X), b(Y),*(X, Y, Z). \\
&\rowprefix{r_6} \ f(X,Y,Z) \leftimpl a(X), b(Y),/(X, Y, Z).
\end{align*}
The single answer set for the example above is:
\begin{align*}
\{a(6),b(2),e(6,2,12),f(6,2,3),c(6,2,8),d(6,2,4)\}.
\end{align*}
The infix alternatives of rules \row{r_3}-\row{r_6} above are as follows.
\begin{align*}
&\rowprefixprime{r_3} c(X,Y,Z) \leftimpl a(X), b(Y),Z=X+Y. \\
&\rowprefixprime{r_4} d(X,Y,Z) \leftimpl a(X), b(Y),Z=X-Y. \\
&\rowprefixprime{r_5} e(X,Y,Z) \leftimpl a(X), b(Y),Z=X*Y. \\
&\rowprefixprime{r_6} f(X,Y,Z) \leftimpl a(X), b(Y),Z=X/Y.
\end{align*}
\end{exmp}
\subsection{Built-in Comparison Predicates}
\dlvhex{} features a total order among variable-free terms
using built-in predicates $==$ (equal), $\noteq$ or $\noteqq$ (not equal),
$<$ (less than), $\lesseq$ (less than or equal), $>$ (greater
than) and $\geeq$ (greater than or equal).
All ground terms and integers can be compared this way.
Integer comparison is according to numeric values. All other
comparisons just guarantee a fixed ordering
over all terms.
\begin{exmp}
Suppose we have the following program.
\begin{align*}
\rowprefix{r_1}& a(1). \\
\rowprefix{r_2}& a(2). \\
\rowprefix{r_3}& b(1). \\[1ex]
\rowprefix{r_4}& c(X,Y) \leftimpl a(X), b(Y), X \noteq Y. \\
\rowprefix{r_5}& d(X,Y) \leftimpl a(X), b(Y), X \noteqq Y. \\
\rowprefix{r_6}& e(X,Y) \leftimpl a(X), b(Y), X < Y. \\
\rowprefix{r_7}& f(X,Y) \leftimpl a(X), b(Y), X > Y. \\
\rowprefix{r_8}& g(X,Y) \leftimpl a(X), b(Y), X \lesseq Y. \\
\rowprefix{r_9}& h(X,Y) \leftimpl a(X), b(Y), X \geeq Y. \\
\rowprefix{r_{10}}& i(X,Y) \leftimpl a(X), b(Y), Y == 1.
\end{align*}
The single answer set this program is
\begin{align*}
\{ & a(1),a(2),b(1),i(1,1),i(2,1),c(2,1),\\
& d(2,1),f(2,1),g(1,1),h(1,1),h(2,1)\}.
\end{align*}
\end{exmp}
\subsection{Conditions and Conditional Literals}
\label{conditions}
A \emph{conditional literal} is of the form
\mycenterline{$L_0:L_1,\dots,L_n$}%
where every $\mathit{L_j}$
with $0 \les j \les n$ is a literal,
the literals $L_1,\dots,L_n$ are called \emph{condition},
and \enquote{:} resembles the vertical bar ($\mid$)
in mathematical set notation.
%Whenever $\mathit{n = 0}$, it is
%a regular literal and we denote it usually by $L_0$.