如何在 Java 中使用 Prolog?

发布于 2024-10-04 23:09:41 字数 232 浏览 0 评论 0原文

在 Java/Eclipse 应用程序的上下文中,我想使用 Prolog 来完成特定任务。有哪些可用的解决方案和工具可以实现这一点,以及相关的优缺点?

我可以启动外部 Prolog 解释器,在文件中生成结果吗? 我可以使用完整的 Prolog Java 库(用 java 实现的 Prolog 解释器)吗? 我可以使用专用于与外部 Prolog 解释器通信的 java 库吗?

预先感谢您的帮助,

马努

In the context of a Java/Eclipse application I would like to use Prolog for a particular task. What are the available solutions and tools to do that, and associated pro and cons ?

I can launch an external Prolog interpreter generating the result in a file ?
I can use a full Prolog Java Library (Prolog interpreter implemented in java) ?
I can use a java library dedicated to the communication with an external Prolog interpreter ?

Thanks in advance for your help,

Manu

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

千仐 2024-10-11 23:09:41

我会尝试GNU Prolog for Java。来自网站:

GNU Prolog for Java 是 ISO Prolog 作为 Java 库的实现

I would give GNU Prolog for Java a try. From the website:

GNU Prolog for Java is an implementation of ISO Prolog as a Java library

倾听心声的旋律 2024-10-11 23:09:41

我正在使用tuProlog官方网站代码存储库。他们在 1 个 Jar 文件中嵌入了一个很棒的 Prolog。他们也反应敏捷!

I'm using tuProlog Official Site, Code Repository on Android. They have a great embedded Prolog in the form on 1 Jar File. They are responsive too!

任性一次 2024-10-11 23:09:41

快速谷歌返回一个选项列表(可惜已经5年没有更新了)。

至于优点/缺点 - 据我所知
带有绑定的本机引擎在分发和集成方面是更糟糕的选择,但可能比本机 Java 引擎更快(但我首先想要证明)。
带有 API 的 Java 引擎会在集成点导致一些尴尬,但应该很容易分发。
Prolog 到字节码编译器应该很好地处理两种语言之间的集成,并且意味着您不会随二进制文件一起传送 prolog 脚本(可能是一个缺点)。这有很大的缺点 - 似乎没有一个是活着的。
类似于 Prolog 的语言到字节码编译器,与上面相同,但有些似乎仍然浮动,也需要了解新语言。
Java 规则引擎 - 我认为这是最简洁的解决方案。不是使用两种语言以 Java 方式编码规则,而是取决于以这种方式表达规则的复杂程度。

要考虑的第二件事是 - 该项目是否仍在更新。从我的快速谷歌来看,很少有。 ProvaMandarax 是后一个选项的两个例外。

A quick google returns a list of options (alas not updated in 5 years).

As for pros/cons - as I see it
Native engine with bindings would be the worse option when it comes to distribution and integration, but may be faster than the native Java engines (but I would want proof first).
Java engine with API would lead to some awkwardness at integration points, but should be easy to distribute.
Prolog to bytecode compiler should handle integration between the two languages nicely, and would mean that you are not shipping prolog scripts with the binary (may be a disadvantage). Big disadvantage with this - none still seem to be a live.
Prolog-alike language to bytecode compiler, same as above, but some still seem to be floating about, also would require knowledge of a new language.
Java rules engine - I'd see this as the neatest solution. Rather than use two languages code the rules in a Java-y way, but it depends on how complicated the rules are to express in that way.

A second thing to consider is - is the project still being updated. From my quick google, few are. Prova and Mandarax are two exceptions from the latter options.

骄傲 2024-10-11 23:09:41

当查看多线程 Prolog 系统时,有不同的
额外的考虑因素。一个愿望就是分开
线程和逻辑引擎之间。这里表达了这一点:
http:// www.cs.nmsu.edu/ALP/2011/03/concurrent-programming-constructs-and-first-class-logic-engines/

有一些 Prolog 系统实现了这种分离。杰克杰克
Prolog 也是这些系统之一。有一份报告 (*) 显示
如何利用这种分离。在各种场景中
我们看到“线程”是由外部系统提供的
逻辑引擎:

  • 终端部署:Thread是Java主线程。
  • 独立部署:线程是AWT/Swing 线程。
  • Applet 部署:线程是某个浏览器线程。
  • Servlet部署:线程来自Web服务器池。
  • 客户端部署:线程是AWT/Swing 线程。

最好的问候

http://www.jekejeke .ch/idatab/doclet/prod/en/docs/05_run/15_stdy/08_deploy/package.html

When looking at multi-threaded Prolog systems, there are different
additional considerations. One desiderata is to have a separation
between threads and logic engines. This was expressed here:
http://www.cs.nmsu.edu/ALP/2011/03/concurrent-programming-constructs-and-first-class-logic-engines/

There are some Prolog systems that realize this separation. Jekejeke
Prolog is also among these systems. There is a report (*) that shows
how this separation can be put to use. In the various scenarios
we see that the "thread" is provided by a system external to the
logic engine:

  • Terminal Deployment: Thread is the Java main thread.
  • Standalone Deployment: Thread is the AWT/Swing thread.
  • Applet Deployment: Thread is some browser thread.
  • Servlet Deployment: Thread comes from web server pool.
  • Client Deployment: Thread is the AWT/Swing thread.

Best Regards

http://www.jekejeke.ch/idatab/doclet/prod/en/docs/05_run/15_stdy/08_deploy/package.html

给不了的爱 2024-10-11 23:09:41

Projog ,它是用 Java 编写的 Prolog,但也提供了 Prolog REPL:

Projog 提供 Prolog 编程语言的实现
对于Java平台。 /.../

Projog 可以用作独立的控制台应用程序或嵌入到
您的 Java 应用程序作为 Maven 依赖项。

projog.consultFile(new File("src/main/resources/test.pl"));

QueryStatement s1 = projog.createStatement("test(X,Y).");
s1.setTerm("X", new Atom("d"));
QueryResult r2 = s1.executeQuery();
while (r2.next()) {
   System.out.println("Y = " + r2.getTerm("Y"));
}

或在 Java 中实现谓词:

public class SingleResultPredicateExample extends AbstractSingleResultPredicate {
   @Override
   public boolean evaluate(Term term1, Term term2) {
      Atom upperCaseTerm1 = new Atom(getAtomName(term1).toUpperCase());
      return term2.unify(upperCaseTerm1);
   }
}

或:

private static class RetryablePredicate implements Predicate {
   private final String[] split;
   private final Term target;
   private int idx;

   RetryablePredicate(String[] split, Term target) {
      this.split = split;
      this.target = target;
   }

   @Override
   public boolean evaluate() {
      while (idx < split.length) {
         target.backtrack();
         String next = split[idx++];
         if (target.unify(new Atom(next))) {
            return true;
         }
      }

      return false;
   }

   @Override
   public boolean couldReevaluationSucceed() {
      return idx < split.length;
   }
}

There is Projog which is a Prolog written in Java, but also provides a Prolog REPL:

Projog provides an implementation of the Prolog programming language
for the Java platform. /.../

Projog can be used as a stand-alone console application or embedded in
your Java applications as a Maven dependency.

projog.consultFile(new File("src/main/resources/test.pl"));

QueryStatement s1 = projog.createStatement("test(X,Y).");
s1.setTerm("X", new Atom("d"));
QueryResult r2 = s1.executeQuery();
while (r2.next()) {
   System.out.println("Y = " + r2.getTerm("Y"));
}

or implementing predicates in Java:

public class SingleResultPredicateExample extends AbstractSingleResultPredicate {
   @Override
   public boolean evaluate(Term term1, Term term2) {
      Atom upperCaseTerm1 = new Atom(getAtomName(term1).toUpperCase());
      return term2.unify(upperCaseTerm1);
   }
}

or:

private static class RetryablePredicate implements Predicate {
   private final String[] split;
   private final Term target;
   private int idx;

   RetryablePredicate(String[] split, Term target) {
      this.split = split;
      this.target = target;
   }

   @Override
   public boolean evaluate() {
      while (idx < split.length) {
         target.backtrack();
         String next = split[idx++];
         if (target.unify(new Atom(next))) {
            return true;
         }
      }

      return false;
   }

   @Override
   public boolean couldReevaluationSucceed() {
      return idx < split.length;
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文