-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathversion_4x.html
1761 lines (1601 loc) · 102 KB
/
version_4x.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
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
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>EmbASP - version 4</title>
<!-- Bootstrap -->
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp"
crossorigin="anonymous">
<!-- highlight.js -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.5/styles/default.min.css">
<style>
h2 {
padding-top: 55px;
}
.layer {
text-transform: uppercase;
}
.footer {
padding-top: 40px;
padding-bottom: 40px;
margin-top: 40px;
border-top: 1px solid #eee;
}
#toTop {
position: fixed;
bottom: 10px;
right: 10px;
cursor: pointer;
display: none;
}
</style>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-main-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="">
<img src="img/EmbASP_Logo.svg" alt="EmbASP logo" height="25" onerror="this.src='img/EmbASP_Logo.png'" />
</a>
<p class="navbar-text">
<strong>
<em>EmbASP</em>
</strong>
</p>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-right" id="bs-main-navbar-collapse-1">
<ul class="nav navbar-nav">
<li>
<a href="#Download">Download</a>
</li>
<li>
<a href="#Documentation">Documentation</a>
</li>
<li>
<a href="#CoreTeam">Core Team</a>
</li>
<li>
<a href="#Contacts">Contacts</a>
</li>
<li>
<a href="#License">License</a>
</li>
<li>
<a href="#Publications">Publications</a>
</li>
<li>
<a href="#Applications">Applications</a>
</li>
<!--<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">Separated link</a></li>
<li role="separator" class="divider"></li>
<li><a href="#">One more separated link</a></li>
</ul>
</li>-->
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container-fluid -->
</nav>
<div class="container" style="padding-top: 40px">
<div class="page-header">
<!--<h2 style="text-align: center"><img src="img/EmbASP_Logo.png" alt="embASP" height="150" style=" padding-bottom: 0.2cm;" /></h2>-->
<div class="jumbotron">
<div class="row">
<div class="col-xs-12 col-sm-4">
<img class="center-block" src="img/EmbASP_Logo.png" alt="EmbASP logo" height="150" onerror="this.src='img/EmbASP_Logo.png'"
/>
</div>
<p class="col-xs-12 col-sm-8" style="padding-top: 15px">
<strong>
<em>EmbASP</em>
</strong> is a framework for the integration (embedding) of Logic Programming in external systems for generic
applications.
<br /> It helps developers at designing and implementing complex reasoning tasks by means of solvers on
different platforms.
</p>
</div>
</div>
<div class="alert alert-info" role="alert">
NOTE: this is the official site of versions 3x of the EmbASP Framework. If you are looking for an older version, please
<a href="version_2x.html">click here.</a>
</div>
<div>
<p>
The framework can be implemented in a object-oriented programming language of choice, easing and guiding the generation of
suitable libraries for the use of specific solvers on selected platforms. We currently provide 2 implementations
(one in
<a href="https://www.java.com">
<strong>Java</strong>
</a> and one in
<a href="https://www.python.org">
<strong>Python</strong>
</a>) and ready-made libraries for the embedding of the
<em>ASP (Answer Set Programming)</em> solvers
<a href="http://www.dlvsystem.com/dlv">DLV</a>,
<a href="https://potassco.org/clingo">clingo</a> and
<a href="https://www.mat.unical.it/DLV2">DLV2</a> and the
<em>PDDL (Planning Domain Definition Language)</em> cloud solver
<a href="http://solver.planning.domains">Solver.Planning.Domains</a> (SPD) on the Desktop platform and of DLV and SPD on the Mobile (Android™)
one (available only for the Java version).
</p>
<p>
However, the framework has been designed to be easily extensible and adaptable to different solvers and platforms. It is
worth to notice that solvers are invoked in different modes; for instance, SPD is invoked via a remote
connection, while for the other, binaries are effectively embedded and natively executed.
</p>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<h2 id="Download">Download
<a href="https://github.com/DeMaCS-UNICAL/EmbASP">
<img src="img/GitHub-Mark-120px-plus.png" alt="GitHub" height="40" />
</a>
</h2>
<div>
<h3 id="release">Latest release version</h3>
<div>To use the specialized library on Desktop, a JAR file
<!-- and an EGG file have -->
has been released.</div>
<div>To use the specialized library on Mobile (Android), an AAR file has been released.</div>
<div>You can download their last stable versions
<a href="https://github.com/DeMaCS-UNICAL/EmbASP/releases">here</a>.</div>
</div>
<div>
<h3>Development Version</h3> You can find the current development version
<a href="https://github.com/DeMaCS-UNICAL/EmbASP">on GitHub</a>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<h2 id="Documentation">Documentation</h2>
<!--<p>
<div class="list-group">
<a class="list-group-item list-group-item-info" href="#javadoc">Javadoc </a>
<a class="list-group-item list-group-item-info" href="#class-diagram">Class Diagram </a>
<a class="list-group-item list-group-item-info" href="#getting-started">Getting Started </a> <a class="list-group-item list-group-item-info"
href="#setting-up">Using EmbASP</a>
<a
class="list-group-item list-group-item-info" href="#arch-desc">Architecture
Description </a> <a class="list-group-item list-group-item-info"
href="#detailed-desc">Detailed Description </a>
</div>
</p>-->
<!-- Nav tabs -->
<ul class="nav nav-tabs nav-justified" role="tablist">
<li role="presentation" class="active">
<a href="#architecture" aria-controls="architecture" role="tab" data-toggle="tab">Abstract Architecture</a>
</li>
<li role="presentation">
<a href="#implementation_java" aria-controls="implementation_java" role="tab" data-toggle="tab">Java implementation</a>
</li>
<li role="presentation">
<a href="#implementation_python" aria-controls="implementation_python" role="tab" data-toggle="tab">Python implementation</a>
</li>
<li role="presentation">
<a href="#android_example" aria-controls="android_example" role="tab" data-toggle="tab">Example DLV on Android</a>
</li>
<li role="presentation">
<a href="#desktop_example" aria-controls="desktop_example" role="tab" data-toggle="tab">Example SPD on Desktop</a>
</li>
</ul>
<!-- Tab panes -->
<div class="tab-content" style="padding-top: 15px">
<div role="tabpanel" class="tab-pane fade in active" id="architecture">
<div class="panel panel-primary" id="arch-desc">
<div class="panel-heading">
<h3 class="panel-title">Architecture Description</h3>
</div>
<div class="panel-body">
<p>
The general architecture of EmbASP is depicted in the following figure: it is intended as an abstract framework to be implemented
in some object-oriented programming language.
</p>
<p>
In addition, this architecture can be specialized for different platforms, logic languages and solvers.
</p>
<p>
The figure also reports general dependencies among the main modules.
</p>
<figure style="padding-bottom: 1em">
<img class="img-responsive center-block" src="img/architecture_v3.svg" alt="Abstract Architecture" onerror="this.src='img/architecture_v3.png'">
<figcaption class="text-center">
Abstract Architecture of the framework.
<br /> Blocks overshadowed in white depict the abstract components of the framework, while
dark-grey blocks represent the modules more closely related to the proposed specialization.
</figcaption>
</figure>
<p>
The framework architecture has been designed by means of four modules:
<code class="layer">Core</code>,
<code class="layer">Platforms</code>,
<code class="layer">Languages</code>, and
<code class="layer">Systems</code>. In the following, we denote with "solver" a logic-based system that is meant to be
used by external application with the help of EmbASP.
</p>
<h4>Core Module</h4>
<p>The
<code class="layer">Core</code> module defines the basic components of the Framework.</p>
<p>
The
<code>Handler</code> component mediates the communication between the Framework and the user that can provide
it with the input program(s) via the component
<code>InputProgram</code>, along with any desired solver's option(s) via the component
<code>OptionDescriptor</code>. A
<code>Service</code> component is meant for managing the chosen solver executions.
</p>
<p>
Two different execution modes can be made available: synchronous or asynchronous. While in the synchronous mode any call
to the execution of the solver is
<em>blocking</em> (i.e., the caller waits until the reasoning task is completed), in asynchronous
mode the call is non-blocking: a
<code>Callback</code> component notifies the caller once the reasoning task is completed. The result of the
execution (i.e., the output of the logic system) is handled by the
<code>Output</code> component, in both modes.</p>
<h4>Platforms Module</h4>
<p>
The
<code class="layer">Platforms</code> module is meant for containing what is platform-dependent; in particular, the
<code>Handler</code> and
<code>Service</code> components from the
<code class="layer">Core</code> module that should be adapted according to the platform at hand, since they take care
of practically launching solvers.
</p>
<h4>Languages Module</h4>
<p>
The
<code class="layer">Languages</code> module defines specific facilities for each supported logic language.
</p>
<p>
The generic
<code>Mapper</code> component is conceived as an utility for managing input and output via objects, if the
programming language at hand permits it.
</p>
<p>
The sub-module ASP comprises components such as
<code>ASPInputProgram</code> that adapts
<code>InputProgram</code> to the ASP case, while
<code>AnswerSet</code> and
<code>AnswerSets</code> represent the
<code>Output</code> for ASP. Moreover the
<code>ASPMapper</code> allow the management of ASP input facts and answer sets via objects.
</p>
<p>
Similarly, the sub-module PDDL includes
<code>PDDLInputProgram</code>,
<code>Action</code>,
<code>Plan</code> and
<code>PDDLMapper</code>.
</p>
<h4>Systems Module</h4>
<p>
The
<code class="layer">Systems</code> module defines what is system-dependent; in particular, the
<code>InputProgram</code>,
<code>Output</code> and
<code>OptionDescriptor</code> components from the
<code class="layer">Core</code> module should be adapted in order to effectively interact with the solver at hand.
</p>
</div>
</div>
</div>
<div role="tabpanel" class="tab-pane fade" id="implementation_java">
<div class="panel panel-primary" id="arch-desc">
<div class="panel-heading">
<h3 class="panel-title">Implementation Description</h3>
</div>
<div class="panel-body">
<p>
As mentioned above, we provide a
<a href="https://www.oracle.com/java">Java</a> implementation of the EmbASP framework.
</p>
<p>
The following figure provides some details about classes and interfaces of the implementation.
</p>
<figure style="padding-bottom: 1em">
<img class="img-responsive center-block" src="img/class_diagram_v3.svg" alt="Simplified Class Diagram" onerror="this.src='img/class_diagram_v3.png'">
<figcaption class="text-center">
Simplified class diagram of the provided Java implementation of EmbASP.
</figcaption>
</figure>
<h4>Core module implementation</h4>
<p>
Each component in the
<code class="layer">Core</code> module has been implemented by means of an homonymous class or interface.
</p>
<p>
In particular, the
<code>Handler</code> class collects
<code>InputProgram</code> and
<code>OptionDescriptor</code> objects communicated by the user.
</p>
<p>
For what the asynchronous mode is concerned, the class
<code>Service</code> depends from the interface
<code>Callback</code>, since once the reasoning service has terminated, the result of the computation is returned
back via a class
<code>Callback</code>.
</p>
<h4>Platforms module implementation</h4>
<p>
In order to support a new platform, the
<code>Handler</code> and
<code>Service</code> components must be adapted.
</p>
<p>
As for the Android platform, we developed an
<code>AndroidHandler</code> that handles the execution of an
<code>AndroidService</code>, which provides facilities to manage the execution of a solver on the Android platform.
</p>
<p>
Similarly, for the desktop platform we developed a
<code>DesktopHandler</code> and a
<code>DesktopService</code>, which generalizes the usage of a solver on the desktop platform, allowing both synchronous
and asynchronous execution modes.
</p>
<h4>Languages module implementation</h4>
<p>
This module includes specific classes for the management of input and output to ASP and PDDL solvers.
</p>
<p>
The
<code>Mapper</code> component of the
<code class="layer">Languages</code> module is implemented via a
<code>Mapper</code> class, that allows to translate input and output into Java objects. Such translations
are guided by
<a href="https://docs.oracle.com/javase/tutorial/java/annotations">Java Annotations</a>, a form of metadata that mark Java code and provide information
that is not part of the program itself: they have no direct effect on the operation of
the code they annotate.
</p>
<p>
In our setting, we make use of such feature so that it is possible to translate facts into strings and vice-versa via two
custom annotations, defined according to the following syntax:
</p>
<ul>
<li>
<code>@Id (string_name)</code>: the target must be a class, and defines the predicate name (in the ASP case) and
the action name (in the PDDL case) the class is mapped to;
</li>
<li>
<code>@Param (integer_position)</code>: the target must be a field of a class annotated via
<code>@Id</code>, and defines the term (and its position) in the atom (in the ASP case) and in the
action (in the PDDL case) the field is mapped to.
</li>
</ul>
<p>
By means of the Java Reflection mechanisms, annotations are examined at runtime, and taken into account to properly define
the translation.
</p>
<p>
If the classes intended for the translation are not annotated or not correctly annotated, an exception is raised.
</p>
<p>
In addition to the
<code>Mapper</code>, this module features two sub-modules which are more strictly related to ASP and PDDL.
</p>
<h4>Systems Module Implementation</h4>
<p>
The classes
<code>DLVAnswerSets</code>,
<code>ClingoAnswerSets</code>,
<code>DLV2AnswerSets</code> and
<code>SPDPlan</code> implement specific extensions of the
<code>AnswerSets</code> or
<code>Plan</code> classes, in charge of manipulating the output of the respective solvers.
</p>
<p>
Moreover, this module can contain classes extending
<code>OptionDescriptor</code> to implement specific options of the solver at hand.
</p>
</div>
</div>
<div class="panel panel-primary" id="javadoc">
<div class="panel-heading">
<h3 class="panel-title">Javadoc</h3>
</div>
<div class="panel-body">
<p>
The Javadoc documentation is available
<a href="javadoc/index.html">here</a>.
</p>
<p>
For an offline usage, you can download it
<a href="javadoc-embasp.zip">here</a>.
</p>
</div>
</div>
<div class="panel panel-primary" id="class-diagram">
<div class="panel-heading">
<h3 class="panel-title">Class Diagram</h3>
</div>
<div class="panel-body">
A complete UML Class Diagram is available
<a href="img/complete_class_diagram2.png">here</a>.
</div>
</div>
</div>
<div role="tabpanel" class="tab-pane fade" id="implementation_python">
<div class="panel panel-primary" id="arch-desc">
<div class="panel-heading">
<h3 class="panel-title">Implementation Description</h3>
</div>
<div class="panel-body">
<p>
As mentioned above, we provide a
<a href="https://www.python.org/">Python</a> implementation of the EmbASP framework.
</p>
<p>
The following figure provides some details about classes and interfaces of the implementation.
</p>
<figure style="padding-bottom: 1em">
<img class="img-responsive center-block" src="img/class_diagram_python.png" alt="Simplified Class Diagram" onerror="this.src='img/class_diagram_v3.png'">
<figcaption class="text-center">
Simplified class diagram of the provided Python implementation of EmbASP.
</figcaption>
</figure>
<h4>Core module implementation</h4>
<p>
Each component in the
<code class="layer">Core</code> module has been implemented by means of an abstract class or generic classes that will
specialize in the following packages.
</p>
<p>
In particular, the
<code>Handler</code> class collects
<code>InputProgram</code> and
<code>OptionDescriptor</code> objects communicated by the user.
</p>
<p>
For what the asynchronous mode is concerned, the class
<code>Service</code> depends from the interface
<code>Callback</code>, since once the reasoning service has terminated, the result of the computation is returned
back via a class
<code>Callback</code>.
</p>
<h4>Platforms module implementation</h4>
<p>
In order to support a new platform, the
<code>Handler</code> and
<code>Service</code> components must be adapted.
</p>
<p>
For the desktop platform we developed a
<code>DesktopHandler</code> and a
<code>DesktopService</code>, which generalizes the usage of a solver on the desktop platform, allowing both synchronous
and asynchronous execution modes.
</p>
<h4>Languages module implementation</h4>
<p>
This module includes specific classes for the management of input and output to ASP and PDDL solvers.
</p>
<p>
The
<code>Mapper</code> component of the
<code class="layer">Languages</code> module is implemented via a
<code>Mapper</code> class, that allows to translate input and output into Python objects. Such translations
are guided by
<code>Predicate</code> abstract class, also present in the module.
</p>
<p>
To make possible translate facts into strings and vice versa, the classes that want to represent a predicate, must extend
the abstract class
<code>Predicate</code>, and must be implemented by including the following code:
</p>
<ul>
<li>
<code>predicateName="string_name"</code>: must be entered as a class field and must contain the predicate name (in the ASP
case) or the action name (in the PDDL case) to map;
</li>
<li>
<code>[("class_field_name_1", int), ("class_field_name_2"), ...]</code>: Is a list that must be passed to super in the constructor, and must contain so
many tuples how many are the class field, containing the field name, sorted by the
position of the terms they represent, and optionally the keyword
<code>int</code> if the field represents an integer.
</li>
</ul>
<p>
Thanks to the structure of the
<code>Predicate</code> class, this information is passed to the
<code>Mapper</code> class, to correctly perform the translation mechanism.
</p>
<p>
If the classes intended for the translation are not constructed correctly in this way, an exception is raised.
</p>
<p>
In addition to the
<code>Mapper</code>, this module features two sub-modules which are more strictly related to ASP and PDDL.
</p>
<h4>Systems Module Implementation</h4>
<p>
The classes
<code>DLVAnswerSets</code>,
<code>ClingoAnswerSets</code>,
<code>DLV2AnswerSets</code> and
<code>SPDPlan</code> implement specific extensions of the
<code>AnswerSets</code> or
<code>Plan</code> classes, in charge of manipulating the output of the respective solvers.
</p>
<p>
Moreover, this module can contain classes extending
<code>OptionDescriptor</code> to implement specific options of the solver at hand.
</p>
</div>
</div>
</div>
<div role="tabpanel" class="tab-pane fade" id="android_example">
<div class="panel panel-primary" id="getting-started">
<div class="panel-heading">
<h3 class="panel-title">Getting Started</h3>
</div>
<div class="panel-body">
<mark>This example is available only in Java due to the restrictions of the Android mobile operating
system.
</mark>
<h4>How to import the framework on Android Studio</h4>
<p>In order to use the framework in your applications you have to import it as module on Android
Studio.
</p>
<ol>
<li>Import the framework module:
<ol>
<li>Download the framework last released
<a href="#release">module</a>.
</li>
<li>In the project view, right-click on your project
<b>New > Module</b>.
</li>
<li>Select
<b>Import .JAR/.AAR Package</b>.
</li>
<li>Select the directory in which the module has been downloaded.
</li>
</ol>
</li>
<li>Set the dependency:
<ol>
<li>In the Android Studio menu:
<b>File > Project Structure
</b>.
</li>
<li>Select your project module (by default called
<b>app</b>).
</li>
<li>In the
<b>Dependencies Tab</b> add as
<b>Module Dependency
</b> the previously imported framework.
</li>
</ol>
</li>
</ol>
</div>
</div>
<div class="panel panel-primary" id="setting-up">
<div class="panel-heading">
<h3 class="panel-title">Using EmbASP</h3>
</div>
<div class="panel-body">
<p>
In the following, we describe an the actual usage of the framework by means of a running example; as a use case, we will
develop a simple Android application for solving Sudoku puzzles.
</p>
<p>
The complete code of this example is freely available
<a href="files/EmbASP_Android_Sudoku.zip">here</a>. Please note that the zip file contains an Android Studio (version 2.3) project
and uses the version 3.1.0 of EmbASP.
</p>
<p>
The framework features a annotation-guided mapping, offered by the
<i>ASPMapper</i> component, for two-way translations between strings recognizable by ASP
solvers and objects in the programming language at hand, directly employable within applications.
By means of this feature, the ASP-based aspects can be separated from the Java coding:
the programmer doesn't even necessarily need to be aware of ASP.
</p>
<p>
Let us think of a user that designed (or has been given) a proper logic program
<i>P</i> to solve a sudoku puzzle and has also an initial schema. We assume that the initial
schema is well-formed i.e. the complete schema solution exists and is unique. A possible
program
<i>P</i> is embedded in the complete example, that, coupled with a set of facts
<i>F</i> representing the given initial schema, allows to obtain the only admissible solution.
</p>
<p>
By means of the annotation-guided mapping, the initial schema can be expressed in forms of Java objects. To this extent,
we define the class
<i>Cell</i>, aimed at representing the single cell of the sudoku schema, as follows:
</p>
<pre><code class="language-java">
@Id("cell")
public class Cell {
@Param(0)
private int row;
@Param(1)
private int column;
@Param(2)
private int value;
[...]
}
</code></pre>
<p>
It is worth noticing how the class has been annotated by two custom annotations, defined according to the following syntax:
</p>
<ul>
<li>
<i>@Id(string_name)</i>: the target must be a class, and defines the predicate name
the class is mapped to;
</li>
<li>
<i>@Param(integer_position)</i>: the target must be a field of a class annotated via
@Id, and defines the term (and its position) in the ASP atom the field is mapped
to.
</li>
</ul>
<p>
Thanks to these annotations the
<i>ASPMapper</i> class will be able to map
<i>Cell</i> objects into strings properly recognizable from the ASP solver as logic facts
of the form
<i>cell(Row,Column,Value)</i>. At this point, we can create an
<a href="https://developer.android.com/reference/android/app/Activity.html">Android Activity Component</a>, and start deploying our sudoku application:
</p>
<pre><code class="language-java">
public class MainActivity extends AppCompatActivity {
[...]
private Handler handler;
@Override
protected void onCreate(Bundle bundle) {
handler = new AndroidHandler(getApplicationContext(), DLVAndroidService.class);
[...]
}
public void onClick(final View view){
startReasoning();
[...]
}
public void startReasoning() {
InputProgram inputProgram = new InputProgram();
for (int i = 0; i < 9; i++){
for (int j = 0; j < 9; j++)
try {
if(sudokuMatrix[i][j]!=0) {
inputProgram.addObjectInput(new Cell(i, j, sudokuMatrix[i][j]));
}
} catch (Exception e) {
// Handle Exception
}
}
handler.addProgram(inputProgram);
String sudokuEncoding = getEncodingFromResources();
handler.addProgram(new InputProgram(sudokuEncoding));
Callback callback = new MyCallback();
handler.startAsync(callback);
}
}
</code></pre>
<p>
The class contains an
<i>Handler</i> instance as field, that is initialized when the
<i>Activity</i> is created as an
<i>AndroidHandler</i>. Required parameters include the
<i>Android Context</i> (an Android utility, needed to start an Android Service Component)
and the type of
<i>AndroidService</i> to use, in our case a
<i>DLVAndroidService</i>.
</p>
<p>
In addiction, in order to represent an initial sudoku schema, the class features a matrix of integers as another field where
position
<i> (i,j) </i> contains the value of cell
<i> (i,j) </i> in the initial schema; cells initially empty are represented by positions
containing zero.
</p>
<p>
The method
<i>startReasoning</i> is in charge of actually managing the reasoning: in our case, it is
invoked in response to a click event that is generated when the user asks for the solution.
It is firstly created an
<i>InputProgram</i> object that is filled with
<i>Cell</i> objects representing the initial schema, which is then provided to the handler;
then it is provided with the sudoku encoding. It could be loaded, for instance, by means
of an utility function that retrieves it from the
<a href="http://developer.android.com/guide/topics/resources">Android Resources folder</a>, which, within Android applications, is typically meant
for containing images, sounds, files and resources in general.
</p>
<p>
At this point, the reasoning process can start; since for Android we provide only the asynchronous execution mode, a callback
object is in charge of fetching the output when the ASP system has done.
</p>
<p>
Finally, once the computation is over, from within the callback function the output can be retrieved directly in form of
Java objects. For instance, in our case an inner class
<i>MyCallback</i> implements the interface
<i>Callback</i>:</p>
<pre><code class="language-java">
private class MyCallback implements Callback {
@Override
public void callback(Output o) {
if(!(o instanceof AnswerSets))
return;
AnswerSets answerSets=(AnswerSets)o;
if(answerSets.getAnswersets().isEmpty())
return;
AnswerSet as = answerSets.getAnswersets().get(0);
try {
for(Object obj:as.getAtoms()) {
Cell cell = (Cell) obj;
sudokuMatrix[cell.getRow()][cell.getColumn()] = cell.getValue();
}
} catch (Exception e) {
// Handle Exception
}
displaySolution();
}
}
</code></pre>
</div>
</div>
</div>
<div role="tabpanel" class="tab-pane fade" id="desktop_example">
<div class="panel panel-primary" id="getting-started">
<div class="panel-heading">
<h3 class="panel-title">Getting Started</h3>
</div>
<div class="panel-body">
<div class="row">
<div class="col-xs-12 col-md-6">
<h4>Java version</h4>
<p>
The framework is released as JAR file to be used on a Desktop platform, therefore it can be easily imported and used in any
Java project.
</p>
<p>
The SPD solver is invoked via an HTTP connection using JSON files as data interchange format. In order to manage them, the
framework needs the
<a href="https://cliftonlabs.github.io/json-simple">json-simple library</a> (you can download the JAR and include directly in your
project or you can use Gradle or Maven)
</p>
<!--<p>
It's worth noticing that in order to use the ASP solvers (DLV, clingo and DLV2) on a Desktop platform, the binaries for the
specific machine must be download in advance.
</p>-->
</div>
<div class="col-xs-12 col-md-6">
<h4>Python version</h4>
<p>
The framework is released as EGG file to be used on a Desktop platform, it can be easily installed in a Python installation.
</p>
<p>
The framework is stand alone and does not need any library for its operation, except for a Python installation.
</p>
</div>
</div>
</div>
</div>
<div class="panel panel-primary" id="setting-up">
<div class="panel-heading">
<h3 class="panel-title">Using EmbASP</h3>
</div>
<div class="panel-body">
<p>
In the following, we describe an the actual usage of the framework by means of a running example; as a use case, we will
develop a simple Desktop application to solve the blocks-world problem.
</p>
<p>
The complete code of this example is freely available
<a href="files/EmbASP_Desktop_Blocksworld.zip">here</a>. Please note that the zip file contains an Eclipse (Neon) project and uses the
version 3.1.0 of EmbASP.
</p>
<figure style="padding-bottom: 1em">
<img class="img-responsive center-block" src="img/blocks-world.svg" alt="blocks-world configuration" onerror="this.src='img/blocks-world.png'">
<figcaption class="text-center">
Sample configuration for blocks-world.
</figcaption>
</figure>
<p>
We will make use of the annotation-guided mapping, in order to retrieve the actions constituting a PDDL plan via Java objects.
To this purpose, the following classes are intended to represent possible actions that
a blocks-world solution plan can feature:
</p>
<div class="row">
<div class="col-xs-12 col-md-6">
<h4>Java version</h4>
<pre><code class="language-java">
@Id("pick-up")
public class PickUp {
@Param(0)
private String block;
[...]
}
</code></pre>
<pre><code class="language-java">
@Id("put-down")
public class PutDown {
@Param(0)
private String block;
[...]
}
</code></pre>
<pre><code class="language-java">
@Id("stack")
public class Stack {
@Param(0)
private String block1;
@Param(1)
private String block2;
[...]
}
</code></pre>
<pre><code class="language-java">
@Id("unstack")
public class Unstack {
@Param(0)
private String block1;
@Param(1)
private String block2;
[...]
}
</code></pre>
</div>
<div class="col-xs-12 col-md-6">
<h4>Python version</h4>
<pre><code class="language-python">
class PickUp(Predicate):
predicateName="pick-up"
def __init__(self, block=None):
super(PickUp, self).__init__([("block")])
self.block = block
[...]
</code></pre>
<pre><code class="language-python">
class PutDown (Predicate):
predicateName="put-down"
def __init__(self, block=None):
super(PutDown, self).__init__([("block")])
self.block = block
[...]
</code></pre>
<pre><code class="language-python">
class Stack (Predicate):
predicateName="stack"
def __init__(self, block1=None, block2=None):
super(Stack, self).__init__([("block1"), ("block2")])
self.block1 = block1
self.block2 = block2
[...]
</code></pre>
<pre><code class="language-python">
class Unstack (Predicate):
predicateName="unstack"
def __init__(self, block1=None, block2=None):
super(Unstack, self).__init__([("block1"), ("block2")])
self.block1 = block1
self.block2 = block2
[...]