2016-12-06 74 views
0

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

我到目前为止有:http://www.pathwayslms.com/swipltuts/html/下面的例子,我已经能够创建和HTTP服务器,并在该使用URL接受目标状态和初始状态端口9999来启动它:

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错误:全局堆栈。现在我相信这是由于初始和目标变量是字符串而不是真正的术语,因为如果我忽略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). 

的问题是我需要多少投入的初始和目标,并将它们转换为可以将传递给STRIPS规划师方面的适当名单?

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

回答

0

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

实施例:

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). 
相关问题