gpt4 book ai didi

javascript - 在 Javascript 中使用 Prolog - Pengines

转载 作者:搜寻专家 更新时间:2023-11-01 00:30:38 27 4
gpt4 key购买 nike


我正在制作一个使用 prolog 的 AI 项目,但我希望它可以在线发布。我找到了 pengines(http://pengines.swi-prolog.org/docs/documentation.htmlhttp://www.swi-prolog.org/pldoc/doc_for?object=section(%27packages/pengines.html%27)),它应该是 Prolog 的 javascript 实现,但我似乎无法理解如何使用它。

我已经尝试使用 pengines npm 包(https://www.npmjs.com/package/pengines)并使用默认的 express-generator 应用程序运行 pengines 文档中的代码:

<html lang="en">
<head>
<script src="/vendor/jquery/jquery-2.0.3.min.js"></script>
<script src="/pengine/pengines.js"></script>
<script type="text/x-prolog">

main :-
repeat,
pengine_input(X),
pengine_output(X),
X == stop.

</script>
<script>
var pengine = new Pengine({
oncreate: handleCreate,
onprompt: handlePrompt,
onoutput: handleOutput
});
function handleCreate() {
pengine.ask('main');
}
function handlePrompt() {
pengine.input(prompt(this.data));
}
function handleOutput() {
$('#out').html(this.data);
}
</script>
</head>
<body>
<div id="out"></div>
</body>

但它只返回一个错误:

http://localhost:3000/pengine/create Failed to load resource: the server responded with a status of 404 (Not Found)

如果有人能解释如何在 javascript 中使用 pengines 或其他 prolog 实现,我将不胜感激。

谢谢!

最佳答案

我知道这篇文章已经很老了,但对于其他路过的人来说:

I'm making an AI project which uses prolog, but I want it to be publish online. I've found pengines (http://pengines.swi-prolog.org/docs/documentation.html, http://www.swi-prolog.org/pldoc/doc_for?object=section(%27packages/pengines.html%27)) which is supposed to be a javascript implementation of Prolog

它并不是真正的 Prolog 实现,它只是一个用 JavaScript 编写的客户端,可以与托管 pengine 进程的 Prolog 服务器对话。 JavaScript 客户端通过 HTTP 和称为 Prolog 传输协议(protocol) (PLTP) 的特定协议(protocol)与 pengine-server 对话。

使用 JavaScript 客户端,您可以发送简单的查询,例如 pengine.ask("member(X, [1,2,3])")等等,前提是 pengine 服务器暴露了 member/2谓词可以安全地从远程使用。但您也可以,正如您在代码片段中演示的那样,在 <script type="text/x-prolog"> 中编写序言代码并让 JavaScript 客户端将该 prolog 源代码发送到服务器,服务器将把它添加到它的知识库中。

http://localhost:3000/pengine/create Failed to load resource: the server responded with a status of 404 (Not Found)

在您的 JavaScript 客户端可以创建 Pengine 之前,必须有一个 pengine 服务器在某处运行。在您的代码片段中没有提供 URL,因此客户端将默认为主机 url。您还可以在创建 pengine 时显式指定 URL。

这里是一个简单的 pengines-server 的例子,取自 source-repo 的例子:

:- module(pengine_server,
[ server/1 % +Port
]).
:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_server_files)).
:- use_module(library(http/http_files)).
:- use_module(library(pengines)).
:- use_module(pengine_sandbox:library(pengines)).

:- http_handler(/, http_reply_from_files(web, []), [prefix]).

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

它将 pengines API 沙盒化,如果你想让其他谓词可用,你可以添加:

:- use_module(pengine_sandbox:library(semweb/rdf_db)).
sandbox:safe_primitive(rdf_db:rdf(_,_,_)).

关于javascript - 在 Javascript 中使用 Prolog - Pengines,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35973579/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com