Skip to content

Commit

Permalink
Merge branch 'master' of github.com:friguzzi/swish into tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
giuseta committed Jan 25, 2016
2 parents 2ae3d77 + a9d90fa commit c6a144c
Show file tree
Hide file tree
Showing 16 changed files with 344 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
node_modules
bower_components
passwd
storage
log
*.log
*~
*.swp
4 changes: 4 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
- User provided rendering?
- Allow for rewriting answer terms?
- Allow generating HTML? How to deal with security?
- Allow copy of Prolog representation. See
http://www.javascriptkit.com/javatutors/copytoclipboard.shtml
- Allow download of rendered (HTML/SVG) representation.

## Sharing

Expand Down Expand Up @@ -71,6 +74,7 @@ _not_ see exactly the same UI for cooperation. What about
- etc ...
- Make it easy to add HTTPS certificate based login.
- not SWISH specific
- Change to digest login as default.

## Search

Expand Down
5 changes: 4 additions & 1 deletion examples/exauc.pl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

main(AUCROC, ROC, AUCPR, PR):-
compute_areas_diagrams(
[0.7 - a, 0.7 - a, 0.7 - \+(a), 0.6 - a, 0.6 - \+(a), 0.5 - a, 0.4 - \+(a)],
% list of couples prob-example where example is an atom for positive
% examples and \+(atom) for negative examples
[0.7 - a, 0.7 - a, 0.7 - \+(a), 0.6 - a,
0.6 - \+(a), 0.5 - a, 0.4 - \+(a)],
AUCROC, ROC, AUCPR, PR).


Expand Down
1 change: 1 addition & 0 deletions examples/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[
{ "file":"inference_examples.swinb", "title":"cplint inference example programs"},
{ "file":"learning_examples.swinb", "title":"cplint learning example programs"},
{ "file":"exauc.pl", "title":"AUC calculator"},
{ "file":"examples.swinb", "title":"Prolog Example programs" },
"--",
{ "file":"dict.swinb", "title":"Dict tutorial" },
Expand Down
1 change: 1 addition & 0 deletions examples/inference_examples.swinb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ This notebook gives an overview of example programs for inference.
- Three sided dice ([threesideddice.cpl](example/threesideddice.cpl),
[threesideddice.pl](example/threesideddice.pl) A three-sided die is repeatedly thrown until the outcome is three. From F. Riguzzi and T. Swift. _The PITA system: Tabling and answer subsumption for reasoning under uncertainty_. Theory and Practice of Logic Programming, 27th International Conference on Logic Programming (ICLP'11) Special Issue, 11(4-5), pages 433-449, 2011.
- Mendelian inheritance of the color of pea plant - larger family ([mendelc.cpl](example/mendelc.cpl), [mendelc.pl](example/mendelc.pl)) From H. Blockeel. _Probabilistic logical models for mendel's experiments: An exercise_. In Inductive Logic Programming, Work in Progress Track, 2004.
- Meta probabilities ([meta.cpl](example/meta.cpl), [meta.pl](example/meta.pl)) Probabilities computation in the body of probabilistic clauses.
</div>

</div>
20 changes: 20 additions & 0 deletions examples/meta.cpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Probabilities computation in the body of probabilistic clauses.
*/

a:0.2:-
prob(b,P),
P>0.2.

b:0.5:-
c.

c.

:- end_cplint.

/** <examples>

?- a.

*/
28 changes: 28 additions & 0 deletions examples/meta.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
Probabilities computation in the body of probabilistic clauses.
*/
:- use_module(library(pita)).

:- if(current_predicate(use_rendering/1)).
:- use_rendering(c3).
:- endif.

:- cplint.

a:0.2:-
prob(b,P),
P>0.2.

b:0.5:-
c.

c.

:- end_cplint.

/** <examples>
?- prob(a,Prob).
?- prob_bar(a,Prob).
*/
78 changes: 74 additions & 4 deletions lib/authenticate.pl
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,23 @@
:- use_module(library(debug)).
:- use_module(library(crypt)).
:- use_module(library(http/http_authenticate)).
:- use_module(library(settings)).

:- use_module(config).
:- use_module(page).

:- if(exists_source(library(http/http_digest))).
:- use_module(library(http/http_digest)).
:- setting(method, oneof([basic,digest]), digest,
"HTTP authentication method used").
:- else.
:- setting(method, oneof([basic]), basic,
"HTTP authentication method used").
:- endif.

:- setting(realm, atom, 'SWISH user',
"HTTP authentication realm").

:- multifile
swish_config:config/2,
swish_config:authenticate/2,
Expand Down Expand Up @@ -68,8 +81,14 @@
password_file_cache(File), !.
password_file(File) :-
absolute_file_name(swish(passwd), File, [access(read)]),
update_auth_type(File),
asserta(password_file_cache(File)).

logged_in(Request, User) :-
setting(method, digest), !,
setting(realm, Realm),
password_file(File),
http:authenticate(digest(File, Realm), Request, [user(User)|_Details]).
logged_in(Request, User) :-
password_file(File),
http_authenticate(basic(File), Request, [User|_Fields]), !,
Expand Down Expand Up @@ -103,19 +122,59 @@
logged_in(Request, User).


%% update_auth_type(+File)
%
% Update the authentication type to match the password hashes

update_auth_type(File) :-
digest_password_file(File), !,
setting(method, Method),
( Method == digest
-> true
; print_message(warning, http_auth_type(Method, digest)),
set_setting(method, digest)
).
update_auth_type(_) :-
setting(method, Method),
( Method == basic
-> true
; print_message(warning, http_auth_type(Method, basic)),
set_setting(method, basic)
).

digest_password_file(File) :-
http_read_passwd_file(File, [passwd(_User, Hash, _Fields)|_]),
is_sha1(Hash).

is_sha1(Hash) :-
atom_length(Hash, 32),
forall(sub_atom(Hash, _, 1, _, Char),
char_type(Char, xdigit(_))).

%% swish_add_user(+User, +Passwd, +Fields) is det.
%
% Add a new user to the SWISH password file.

:- if(current_predicate(http_digest_password_hash/4)).
swish_add_user(User, Passwd, Fields) :-
setting(method, digest), !,
setting(realm, Realm), !,
http_digest_password_hash(User, Realm, Passwd, Hash),
update_password(passwd(User, Hash, Fields)).
:- endif.
swish_add_user(User, Passwd, Fields) :-
phrase("$1$", E, _), % use Unix MD5 hashes
crypt(Passwd, E),
string_codes(Hash, E),
update_password(passwd(User, Hash, Fields)).

Entry = passwd(User, Hash, Fields),

absolute_file_name(swish(passwd), File,
[access(write)]),
update_password(Entry) :-
arg(1, Entry, User),
( catch(password_file(File), _, fail)
-> true
; absolute_file_name(swish(passwd), File,
[access(write)])
),
( exists_file(File)
-> http_read_passwd_file(File, Data)
; Data = []
Expand All @@ -126,3 +185,14 @@
),
http_write_passwd_file(File, NewData).


/*******************************
* MESSAGES *
*******************************/

:- multifile prolog:message//1.

prolog:message(http_auth_type(ReqMethod, Method)) -->
[ 'Using HTTP authentication ~q instead of ~q due to password file format'
-[Method, ReqMethod]
].
17 changes: 16 additions & 1 deletion lib/render/c3.pl
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
% Renders Term as a C3.js chart. This renderer recognises C3, as a
% dict with tag `c3`.

% This renderer hooks into SWISH using two event-handlers and associated
% classes. The `reactive-size` is called after the window or pane is
% resized and the `export-dom` is called from the _download_ button.

term_rendering(C30, _Vars, _Options) -->
{ is_dict(C30, Tag),
Tag == c3,
Expand All @@ -62,7 +66,7 @@
atom_concat(#, Id, RefId),
put_dict(bindto, C3, RefId, C3b)
},
html(div([ class(['render-C3', 'reactive-size']),
html(div([ class(['render-C3', 'reactive-size', 'export-dom']),
'data-render'('As C3 chart')
],
[ div(id(Id), []),
Expand All @@ -75,6 +79,17 @@
var sizing = {};
var tmo;

div.on("export-dom", function(ev, r) {
var svg = div.find("svg");
svg.attr("xmlns", "http://www.w3.org/2000/svg");
svg.css("font", "10px sans-serif");
svg.find(".c3-path,.c3-line,.tick,.domain").css("fill", "none");
svg.find(".tick,.domain").css("stroke", "#000");
r.element = svg[0];
r.extension = "svg";
r.contentType = "image/svg+xml";
});

div.on("reactive-resize", function() {
if ( chart ) {
if ( tmo ) clearTimeout(tmo);
Expand Down
54 changes: 54 additions & 0 deletions lib/render/url.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (C): 2014, VU University Amsterdam
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
As a special exception, if you link this library with other files,
compiled with a Free Software compiler, to produce an executable, this
library does not by itself cause the resulting executable to be covered
by the GNU General Public License. This exception does not however
invalidate any other reasons why the executable file might be covered by
the GNU General Public License.
*/

:- module(swish_render_url,
[ term_rendering//3 % +Term, +Vars, +Options
]).
:- use_module(library(http/html_write)).
:- use_module('../render').

:- register_renderer(url, "Make URLs clickable").

/** <module> SWISH url renderer
Renders a URL as a clickable link
*/

%% term_rendering(+URL, +Vars, +Options)//
%
% Renders a URL as a clickable link.

term_rendering(URL, _Vars, _Options) -->
{ ( atom(URL)
-> true
; string(URL)
),
uri_is_global(URL)
},
html(a(href(URL), URL)).
2 changes: 2 additions & 0 deletions swish.pl
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,12 @@
:- use_module(swish(lib/render/svgtree), []).
:- use_module(swish(lib/render/graphviz), []).
:- use_module(swish(lib/render/c3), []).
:- use_module(swish(lib/render/url), []).
:- use_module(swish(lib/render/lpad), []).

%:- use_module(lib/pita).
:- use_module(library(pita)).
:- use_module(library(slipcover)).
:- use_module(library(auc)).


8 changes: 7 additions & 1 deletion web/css/answer.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ div.render-multi {
#render-select {
position: absolute;
background-color: #fff;
padding: 5px 20px;
padding: 5px 0px 5px 20px;
border: 1px solid #000;
border-radius: 5px;
box-shadow: 5px 5px 5px #888;
Expand All @@ -34,6 +34,12 @@ div.render-multi {
width: 20px; height: 20px;
}

div.render-item a {
float: right;
padding: 0px 10px 0px 10px;
}


/*******************************
* ERRORS *
*******************************/
Expand Down
5 changes: 5 additions & 0 deletions web/help/help-cplint.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ <h1 id="syn">Syntax</h1>
<p>plus the predicate</p>
<pre><code>average/2</code></pre>
<p>that, given a list of numbers, computes its arithmetic mean.</p>
<p>The body of rules may also contain the predicate <code>prob/2</code> that computes the probability of an atom, thus allowing nested probability computations. For example</p>
<pre><code>a:0.2:-
prob(b,P),
P&gt;0.2.</code></pre>
<p>is a valid rule.</p>
<h1 id="inf">Inference</h1>
<p><code>cplint</code> answers queries using the module <code>pita</code>. It performs the program transformation technique of <span class="citation">[7]</span>. Differently from that work, techniques alternative to tabling and answer subsumption are used.</p>
<p>For answering queries, you have to prepare a Prolog file where you first load <code>pita</code> and then enclose the probabilistic clauses in <code>:-cplint.</code> and <code>:-end_cplint.</code> For example, the coin program above can be stored in <code>coin.pl</code> as follows</p>
Expand Down
Loading

0 comments on commit c6a144c

Please sign in to comment.