【问题标题】:Converting a string to a list of terms in prolog将字符串转换为序言中的术语列表
【发布时间】:2016-12-06 16:29:39
【问题描述】:

背景: 我对 prolog 不太了解,但我一直在玩 STRIPS 规划器概念(块世界),当直接在 SWI-PL 终端中查询计划时,它会自行工作。我正在尝试为 SWI-Prolog http 服务器创建一个 Web 界面,并希望将目标状态和初始状态作为查询字符串参数传递给 Prolog 服务器,但似乎查询字符串参数被视为字符串而不是真的是一个术语列表。

我目前所拥有的: 按照http://www.pathwayslms.com/swipltuts/html/ 上的示例,我已经能够创建 HTTP 服务器并在端口 9999 启动它,该端口使用 url 接受目标状态和初始状态:

http://localhost:9999/block_plan?init=[clear(2),clear(4),clear(b),clear(c),on(a,1),on(b,3),on(c,a)]&goal=[on(a,b)]

但我收到服务器 500 错误:超出全局堆栈。现在我相信这是因为 Initial 和 Goal 变量是字符串而不是真正的术语,因为如果我忽略 plan(Init,Goal,P,F) 连词并将其替换为 plan([clear(2), clear(4), clear(b), clear(c), on(a,1), on(b,3), on(c,a)],[on(a,b)], Plan, FinState) 服务器会以正确的计划响应。

:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_parameters)).
:- use_module(library(uri)).
:- http_handler(root(block_plan), get_block_planner_solution, []).

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

get_block_planner_solution(_Request) :-
    format('content-type: text/plain~n~n'),
    http_parameters(_Request, [ init(Init,[optional(false)]),
                                goal(Goal,[optional(false)])
                    ]),
    format('Content-type: text/html~n~n', []),
    format('<html><div>~n', []),
    format('<div>Start State: ~w </div>~n', Init),
    format('<div>Goal State: ~w </div>~n', Goal),
%   plan([clear(2), clear(4), clear(b), clear(c), on(a,1), on(b,3), on(c,a)],[on(a,b)], Plan, FinState),    
    plan(Init,Goal,P,F),
    format('<div>Plan: ~w </div>~n~n', [P]),
    format('<table border=1>~n', []),
    print_request(_Request),
    format('~n</table>~n',[]),
    format('</html>~n', []).

    print_request([]).

    print_request([H|T]) :-
        H =.. [Name, Value],
        format('<tr><td>~w<td>~w~n', [Name, Value]),
        print_request(T).

问题是我如何获取输入 Initial 和 Goal 并将它们转换为可以传递给 STRIPS 规划器的适当术语列表?

如果这种方法不是最佳方法,我愿意接受任何其他将 Web UI 与 SWI prolog 规划器接口的建议。

【问题讨论】:

    标签: prolog swi-prolog


    【解决方案1】:

    好吧,我昨晚认真研究了它,我想我找到了解决我自己问题的方法。基本思想是使用 atomic_list_concat/3 将查询字符串转换为原子列表,然后创建一个处理原子列表的谓词,该谓词调用 atom_to_term/2每个原子,构造一个新列表。

    例子:

    get_block_planner_solution(_Request) :
    http_parameters(_Request, [ init(InitString,[optional(false),string]),
                                goal(GoalString,[optional(false),string])
                    ]), 
    atomic_list_concat(InitL, ;,InitString),
    atomic_list_concat(GoalL, ;,GoalString),
    process_atoms(InitL,TermListi),
    process_atoms(GoalL,TermListg),
    plan(TermListi,TermListg,P,F),
    format('Content-type: text/html~n~n', []),
    format('<html><div>~n', []),
    format('<div>Start State: ~w </div>~n', [InitL]),
    format('<div>Goal State: ~w </div>~n', [GoalL]),
    format('<div>Plan: ~w </div>~n~n', [P]),
    format('<div>TermListI: ~w</div>~n',[TermListi]),
    format('<div>TermListG: ~w</div>~n',[TermListg]),
    format('<table border=1>~n', []),
    print_request(_Request),
    format('~n</table>~n',[]),
    format('</html>~n', []).
    
    process_atoms([],[]).
    
    process_atoms([H|T], [HT|RT]) :-
            atom_to_term(H,PT,Bindings),
            HT = PT,
            process_atoms(T, RT).
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-09
      • 1970-01-01
      相关资源
      最近更新 更多