Skip to content

Commit

Permalink
Merge github.com:SWI-Prolog/swish
Browse files Browse the repository at this point in the history
  • Loading branch information
friguzzi committed Jan 6, 2023
2 parents 4bcdac1 + b5e5923 commit 7be3ee4
Show file tree
Hide file tree
Showing 26 changed files with 914 additions and 133 deletions.
5 changes: 2 additions & 3 deletions .fileheader
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/* Part of SWISH

Author: Jan Wielemaker
E-mail: [email protected]
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (C): %Y, VU University Amsterdam
CWI Amsterdam
Copyright (C): %Y, SWI-Prolog Solutions b.v.
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ SWIPL=swipl

# Packs to download and configure. Run `git submodule` to see the
# available packs.
PACKS=profile rserve_client smtp pcache
PACKS=profile rserve_client smtp pcache sCASP

all:
@echo "Targets"
Expand Down
4 changes: 4 additions & 0 deletions client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ Check out [RubyPengines](https://github.com/simularity/RubyPengine)

Check out [erl_pengine](https://github.com/Limmen/erl_pengine)

## Extracting results using Go

Check out [guregu/pengine](https://github.com/guregu/pengine)

---
If you write or find another client, please make a pull request for this
page!
48 changes: 48 additions & 0 deletions config-available/plugin_menu.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* Part of SWISH
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (C): 2022, VU University Amsterdam
CWI Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/

:- module(plugin_menu, []).

/** <module> Adjust the SWISH menu bar and define new actions
*/

:- multifile
swish_config:web_plugin/1.

swish_config:web_plugin(
json{name: menu,
js: plugin('menu.js')
}).
117 changes: 117 additions & 0 deletions config-available/web/plugin/menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/* Part of SWISH
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (C): 2022, SWI-Prolog Solutions b.v.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/

/**
* @fileOverview
*
* Demo file to illustrate adjusting the SWISH interface, notably
* modifying the navbar menu. This code is loaded using
* swish_config:web_plugin/1 as defined in
* config_available/plugin_menu.pl. It defines a JavaScript module that
* is executed by require.js after the dependencies are loaded. We want
* to post-configure the SWISH menu and thus need to run after SWISH is
* initialized. This is done using the trigger `post-config`.
*
* The navbar is edited using the jQuery plugin `navbar`, notably using
* `clearDropdown` which removes a complete menu or a specific entry
* from a menu and `populateDropdown` which adds items to a pulldown of
* the navigation bar. The added object is a function that may have some
* properties that defines how exactly it is added. Notably:
*
* - `glyph` is the name of a Bootstrap-3 glyph icon
* - `after` or `before` indicates the label of another item which
* is used to define the new location in the pulldown.
*
* Finally, we extend the `storage` plugin. This is normally not
* possible. The plugin is represented using a function `$.fn.<plugin>`,
* but the methods are not exposed. We have added a methods property to
* the function of some of the plugins to allow modifying the methods.
*
* @version 0.2.0
* @author Jan Wielemaker, [email protected]
*/

define([ "jquery", "jswish", "navbar", "storage", "config", "modal" ],
function($, swish, navbar, storage, config, modal) {

swish.swish.on("post-config", function() {
// define our new menu item
const saveToApp = function() {
swish.trigger("storage", "saveToApp");
};
saveToApp.glyph = "cloud-upload"; // Bootstrap 3 glyph name
saveToApp.after = null; // insert as first

// Remove an item and add our own
$("#navbar").
navbar('clearDropdown', "File", "Save").
navbar('populateDropdown',
"File",
{ "Save to my app": saveToApp
});

// Add our new method to the jQuery storage plugin

$.fn.storage.methods.saveToApp = function() {
var elem = this;
var data = this.data("storage");
var post = { data: data.getValue(),
url: data.url
};

$.ajax({ url: config.http.locations.save_to_app,
type: "POST",
data: JSON.stringify(post),
contentType: "application/json",
dataType: "json",
success: function(data) {
if ( data.status == true ) {
if ( data.url ) {
elem.storage('kill', true);
window.location = data.url;
}
} else {
elem.prologEditor('highlightError', {
data: $(`<span class="error">${data.message}</span>`),
location: data.location || {line: 1, ch:0}
}, true);
}
},
error: function(jqXHDR) {
modal.ajaxError(jqXHDR);
}
});
}
});
});
21 changes: 5 additions & 16 deletions examples/scasp.swinb
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,13 @@
s(CASP) is related to ASP. Unlike ASP, which is traditionally solved using _grounding_ and a SAT solver, s(CASP) is solved using top-down goal directed search without grounding. This allows s(CASP) to solve problems that cannot be grounded, while the generated proof tree is a good basis for giving a justification for the answer. s(CASP) supports both negation as failure (NAF) and classical negation. These features make s(CASP) particularly suitable for commonsense reasoning tasks that require a
justification of the answer.

For more information we refer to [Gopal Gupta's home page](https://personal.utdallas.edu/~gupta/) which provides many pointers to related resources. The original source by Joaquin Arias is at [here](https://gitlab.software.imdea.org/ciao-lang/sCASP)
For more information we refer to [Gopal Gupta's home page](https://personal.utdallas.edu/~gupta/) which provides many pointers to related resources. The original source by Joaquin Arias is [here](https://gitlab.software.imdea.org/ciao-lang/sCASP)

## S(CASP) and SWI-Prolog

s(CASP) traditionally comes as an executable `scasp` which takes an s(CASP) program, usually with an embedded query, and prints the result at different levels of detail. The SWI-Prolo port primarily aims at __embedding s(CASP) into Prolog__. It provides two ways for embedding. One embeds the s(CASP) program as a _block_ and makes some of its predicates available as normal Prolog predicates, e.g.,

```
:- begin_scasp(pq, [p/0]).
p(X) :- not q(X).
q(X) :- not p(X).
:- end_scasp.
```

After which we can execute e.g.,

?- p(X).

Alternatively, we can evaluate normal Prolog code using s(CASP) semantics using the predicate scasp/1 or its shorthand ?/1. This collects the reachable call tree, all _global constraints_ whose call tree _overlaps_ with the reachable call tree, prepares this program for execution usig the s(CASP) solver and solves the query. Note that unlike default s(CASP) this implies that global constraints that are completely unrelated from the query are not considered. This is the implementation currently embedded into SWISH.
s(CASP) traditionally comes as an executable `scasp` which takes an s(CASP) program, usually with an embedded query, and prints the result at different levels of detail. The SWI-Prolog port primarily aims at __embedding s(CASP) into Prolog__. Because s(CASP) programs are close to Prolog predicates, embedded s(CASP) does a few program transformations while loading Prolog code (deal with classical notation and
global constraints) and provides a _meta_ goal scasp/2 to evaluate the program under s(CASP) semantics.
The scasp/2 predicate collects the reachable call tree, all _global constraints_ whose call tree _overlaps_ with the reachable call tree, prepares this program for execution usig the s(CASP) solver and solves the query. Note that unlike default s(CASP) this implies that global constraints that are completely unrelated from the query are not considered. This is the implementation currently embedded into SWISH.

### Differences from Prolog

Expand Down Expand Up @@ -69,7 +58,7 @@ baby(tuesday).
</div>

<div class="nb-cell query" name="q4">
scasp(opera(D)).
scasp(opera(D), []).
</div>

<div class="nb-cell markdown" name="md4">
Expand Down
7 changes: 5 additions & 2 deletions ide.pl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
swish_config:verify_write_access/3, % Request, File, Options
pengines:authentication_hook/3, % Request, Application, User
pengines:not_sandboxed/2, % User, Application
user:file_search_path/2. % Alias, Path
user:file_search_path/2, % Alias, Path
http:location/3. % Alias, Path, Options

user:file_search_path(project, '.').

Expand All @@ -84,6 +85,8 @@
:- endif.
current_user(default).

http:location(swish, root(swish), [priority(100)]).
:- create_prolog_flag(swish_ide, true, []).

:- use_module(swish).

Expand Down Expand Up @@ -129,7 +132,7 @@
open_browser(Address) :-
host_port(Address, Host, Port),
http_server_property(Port, scheme(Scheme)),
http_absolute_location(root(.), Path, []),
http_absolute_location(swish(.), Path, []),
format(atom(URL), '~w://~w:~w~w', [Scheme, Host, Port, Path]),
www_open_url(URL).

Expand Down
49 changes: 36 additions & 13 deletions lib/page.pl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@
%
% - code(Code)
% Use Code as initial code. Code is either an HTTP url or
% concrete source code.
% - url(URL)
% Download code from URL. As code(URL), but makes the browser
% download the source rather than the server.
% - background(Code)
% Similar to Code, but not displayed in the editor.
% - examples(Code)
Expand Down Expand Up @@ -133,6 +135,8 @@
swish_reply_config(Request, Options), !.
swish_reply2(SwishOptions, Request) :-
Params = [ code(_, [optional(true)]),
url(_, [optional(true)]),
label(_, [optional(true)]),
show_beware(_, [optional(true)]),
background(_, [optional(true)]),
examples(_, [optional(true)]),
Expand Down Expand Up @@ -224,6 +228,10 @@
-> Options = [url(Code),st_type(external)|Options0]
; Options = Options0
).
source_option(_Request, Options0, Options) :-
option(url(_), Options0),
option(format(swish), Options0), !,
Options = [st_type(external),download(browser)|Options0].
source_option(Request, Options0, Options) :-
source_file(Request, File, Options0), !,
option(path(Path), Request),
Expand Down Expand Up @@ -582,12 +590,16 @@
% Defines the title used for the tab.

source(pl, Options) -->
{ option(code(Spec), Options), !,
download_source(Spec, Source, Options),
phrase(source_data_attrs(Options), Extra)
{ ( option(code(Spec), Options)
; option(download(browser), Options)
),
!,
download_source(Spec, Source, Options),
phrase(source_data_attrs(Options), Extra),
option(label(Label), Options, 'Program')
},
html(div([ class(['prolog-editor']),
'data-label'('Program')
'data-label'(Label)
],
[ textarea([ class([source,prolog]),
style('display:none')
Expand All @@ -600,6 +612,7 @@
source_data_attrs(Options) -->
(source_file_data(Options) -> [] ; []),
(source_url_data(Options) -> [] ; []),
(source_download_data(Options) -> [] ; []),
(source_title_data(Options) -> [] ; []),
(source_meta_data(Options) -> [] ; []),
(source_st_type_data(Options) -> [] ; []),
Expand All @@ -611,6 +624,9 @@
source_url_data(Options) -->
{ option(url(URL), Options) },
['data-url'(URL)].
source_download_data(Options) -->
{ option(download(Who), Options) },
['data-download'(Who)].
source_title_data(Options) -->
{ option(title(File), Options) },
['data-title'(File)].
Expand Down Expand Up @@ -700,8 +716,23 @@
% @bug: Should try to interpret the encoding from the HTTP
% header.

download_source(_HREF, Source, Options) :-
option(download(browser), Options),
!,
Source = "".
download_source(HREF, Source, Options) :-
uri_is_global(HREF), !,
download_href(HREF, Source, Options).
download_source(Source0, Source, Options) :-
option(max_length(MaxLen), Options, 1_000_000),
string_length(Source0, Len),
( Len =< MaxLen
-> Source = Source0
; format(string(Source),
'% ERROR: Content too long (max ~D)~n', [MaxLen])
).

download_href(HREF, Source, Options) :-
option(timeout(TMO), Options, 10),
option(max_length(MaxLen), Options, 1_000_000),
catch(call_with_time_limit(
Expand All @@ -713,14 +744,6 @@
read_source(In, MaxLen, Source, Options),
close(In))),
E, load_error(E, Source)).
download_source(Source0, Source, Options) :-
option(max_length(MaxLen), Options, 1_000_000),
string_length(Source0, Len),
( Len =< MaxLen
-> Source = Source0
; format(string(Source),
'% ERROR: Content too long (max ~D)~n', [MaxLen])
).

read_source(In, MaxLen, Source, Options) :-
option(encoding(Enc), Options, utf8),
Expand Down
6 changes: 5 additions & 1 deletion lib/paths.pl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@
% Attach pack submodules from swish(pack)

attach_local_packs :-
attach_packs(swish_pack(.), [duplicate(replace), search(first)]).
( current_prolog_flag(swish_ide, true)
-> Duplicate = keep
; Duplicate = replace
),
attach_packs(swish_pack(.), [duplicate(Duplicate), search(first)]).

%! set_data_path
%
Expand Down
Loading

0 comments on commit 7be3ee4

Please sign in to comment.