Prolog HTTP动态添加到html的链接

Xas*_*ser 2 prolog swi-prolog

我是prolog和声明性编程的新手,我很难实现以下目标.

我正在学习本教程,现在想在页面上显示一些链接.要显示哪些链接取决于某些事实/变量.

这是我目前的代码:

link_collection(Request) :-
http_parameters(Request,
[
    foo(Foo, [optional(true)])
]),
reply_html_page(
    [title('Dynamic Link Collection')],
    [
        a([href='/questionalice'], 'Question Alice'),       /* Should only show if has(investigate, body) is true */
        a([href='/questionbob'], 'Question Bob'),          /* Should only show if Foo = bar */
        a([href='/investigatebody'], 'Investigate Body')   /* Show always */
    ]
).
Run Code Online (Sandbox Code Playgroud)

请注意,"排列"的数量不允许我只是"或" link_collection语句.我也希望条件是任意复杂的.

mat*_*mat 5

您可以在非常一般的上下文中回答您的问题,即即使没有考虑HTTP的具体用例.

一般问题似乎是:我如何动态选择一些可用选项的子集.

对于这一点,假设你代表每一个环节不能简单地作为"本身",而是一种形式的Link-Condition,与该解释Link只应包括是否 Condition.

让我们首先考虑我们想表达的条件,并确定它们何时成立.重要的是,您的条件还取决于其值Foo,因此必须考虑到这一点:

is_true_with_foo(_, has(investigate, body)) :- has(investigate, body).
is_true_with_foo(Foo, Foo = bar)            :- Foo = bar.
is_true_with_foo(_, true).

因此,这描述了特定条件何时为真,也取决于值Foo.

现在,您的样本条件可以表示如下:

links_conditions(Foo,
    [
        a([href='/questionalice'], 'Question Alice')-has(investigate, body),
        a([href='/questionbob'], 'Question Bob')-(Foo = bar),
        a([href='/investigatebody'], 'Investigate Body')-true
    ]).

为了描述一个列表,请考虑使用DCG().

例如:

links_subset([], _) --> [].
links_subset([L-Cond|Ls], Foo) -->
        (   { is_true_with_foo(Foo, Cond) } ->
            [L]
        ;   []
        ),
        links_subset(Ls, Foo).

你现在可以打电话:

?- links_conditions(Foo, LCs0),
   phrase(links_subset(LCs0, no), LCs).

并在LCs其余链接中获取.在这种情况下:

LCs = [a([href='/questionalice'], 'Question Alice'),
a([href='/investigatebody'], 'Investigate Body')].

所以,我们可以在回复中使用结果链接:

link_collection(Request) :-
        http_parameters(Request, [foo(Foo, [optional(true)])]),
        links_conditions(Foo, LCs0),
        phrase(links_subset(LCs0, Foo), LCs),
        reply_html_page([title('Dynamic Link Collection')], LCs).

请注意Foo这些谓词中是如何传递的.

PS:您的示例代码段存在基本语法错误,因此我怀疑您的代码是否以任何方式运行.