如何从序言网页查询序言数据库?

How to query prolog database from prolog webpage?

我正在使用 swipl。我正在尝试从网页查询下面的 parts.pl 数据库文件,但我不确定如何 post 查询并将其 "query" 数据库和 return 那些结果。

即,我希望能够像在 swipl 命令行(例如 'part(jeep, 100, A, B).' 上输入查询一样输入查询,并在 return 上向我提供相同的结果页面。

任何人都可以就如何执行此操作提供一些指导。这是我到目前为止所得到的。

:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_error)).
:- use_module(library(http/html_write)).
:- use_module(library(http/http_client)).

:- use_module(parts).

:- http_handler('/', my_query_form, []).

server(Port) :-
    http_server(http_dispatch, [port(Port)]).

my_query_form(Request) :-
    reply_html_page(
        my_style,
        [title('My Test')],
        [\page_content(Request)]).

page_content(_Request) -->
    html(
        [
        form([action='/landing', method='POST'], [
            p([], [
                label([for=model], 'Model '),
                input([name=model, type=textarea])
                    ]),
            p([], [
                label([for=major], 'Major '),
                input([name=major, type=textarea])
                    ]),
            p([], [
                label([for=minor], 'Minor '),
                input([name=part, type=textarea])
                    ]),
            p([], input([name=submit, type=submit, value='Search'], []))
        ])]).

:- http_handler('/landing', landing_pad, []).

landing_pad(Request) :-
    member(method(post), Request), !,
    http_read_data(Request, Data, []),
    format('Content-type: text/html~n~n', []),
    format('<p>', []),
    portray_clause(Data),
    format('</p<p>=======~n', []),
    portray_clause(Request),
    format('</p>').

:- multifile
    user:body//2.

user:body(my_style, Body) -->
    html(body([
        div(id(top), h3('Parts Query')),
        div(id(beta), p(i('(beta)'))),
        div(id(content), Body)
        ])).

这是我的 parts.pl 文件。

:- module(parts,[part/4]).
% part(model, major, minor, description).
part(jeep, 100, 1000, 'description of 100-1000').
part(jeep, 100, 1001, 'description of 100-1001').
part(jeep, 100, 1002, 'description of 100-1002').
part(jeep, 101, 1000, 'description of 101-1000').
part(jeep, 101, 1001, 'description of 101-1001').
part(jeep, 101, 1002, 'description of 101-1002').
part(ford, 101, 1000, 'description of 101-1000').

更新:(我最新的实际上确实查询了,但是 return 是整个数据库)

landing_pad(Request) :-
    member(method(post), Request), !,
    http_read_data(Request, Data, []),
    format('Content-type: text/html~n~n', []),    
    format('<p>', []),
    memberchk(model=Model, Data),
    findall(p(Model, Major, Minor, Description),
        part(Model, Major, Minor, Description), Descriptions),
    maplist(description, Descriptions),
    format('</p>').   

description(p(M,A,I,D)) :- format("~q ~q:~q - ~q</br>", [M,A,I,D]).

非常接近。

剩下的就是使用指定的组件实际查询您的数据库。我给你这个查询的一部分,剩下的作为练习填写:

landing_pad(Request) :-
    member(method(post), Request), !,
    http_read_data(Request, Data, []),
    format('Content-type: text/html~n~n', []),
    format('<p>', []),
    memberchk(model=Model, Data),
    findall(p(Major,Description), part(Model, Major, _, Description), Descriptions),
    maplist(description, Descriptions),
    format('</p>').

我在 粗体 中突出显示了新部分。

findall/3收集所有匹配的部分。在这种情况下,仅使用表单数据中的 Model 组件。您可以填写其余部分。

要显示描述,您可以使用例如:

description(p(M,D)) :- format("<p>~q: ~q</p>\n", [M,D]). 

作为 description/1.

的定义

提示: 注意 ~q 格式说明符的使用。它使调试变得容易得多。如果您将附加功能添加为查询的一部分,您就会明白我的意思,因为您需要区分整数和原子。