【问题标题】:Error executing query in tuProlog在 tuProlog 中执行查询时出错
【发布时间】:2010-09-18 09:48:57
【问题描述】:

我下载了 tuprolog,但无法获得以下查询的结果,而我在 PROL ide 中得到答案..有人可以帮忙吗?

verb(admit-1).
verb(work-4).
nsubjpass(admit-1, patient-2).
agent(admit-1, doctor-3).
nsubj(work-4, who-5).
aux(work-4, be-6).
rcmod(doctor-3, work-4).
prep_in(work-4, clinic-7).


aggregation('part of').
aggregation('belongs to').
aggregation('subdivision of').
aggregation('have').
aggregation('contain').
aggregation('comprise').
aggregation('include').
aggregation('define').
aggregation('consist of').
aggregation('compose of').
aggregation('denote by').
aggregation('identify by').
aggregation('make up of').
aggregation('record with').

attribute('have').
attribute('contain').
attribute('comprise').
attribute('include').
attribute('define').
attribute('consist of').
attribute('compose of').
attribute('denote by').
attribute('identify by').
attribute('make up of').
attribute('record with').

generalization('be').
generalization('kind of').
generalization('type of').
generalization('classify into').
generalization('consider').

object(X) :- noun(X).
relation(X) :-  verb(X).
rel(X,Y) :- nsubjpass(X,Y).
rel(X,Y) :- agent(X,Y).
rel(X,Y) :- nsubj(X,Y).
rel(X,Y) :- aux(X,Y).
rel(X,Y) :- rcmod(X,Y).
rel(X,Y) :- prep_in(X,Y).

associatedWith(X,Y) :- rel(X,Y).
associatedWith(X,Y,Z) :- verb(Y),associatedWith(X,Y), associatedWith(Y,Z).
associatedWith(X,Y,Z) :- verb(X),associatedWith(X,Y), associatedWith(X,Z).
rel_aggregation(X,Y,R):-rel(X,Y).aggregation(R).

?- associatedWith(X,Y,Z).

我需要处理这些关系并让输出再次在 java 中处理

谁能告诉我如何集成prolog和java?

编辑: 我通过实现代码在 tuprolog 中得到了这个错误

此代码适用于 associatedWith(X,Y)。即有 2 个参数,但当我给出 3 个参数时不起作用(X,Y,Z)。怎么办..请帮助我得到这个例外

java.lang.ClassCastException: alice.tuprolog.Var cannot be cast to alice.tuprolog.Struct
        at alice.tuprolog.lib.BasicLibrary.agent_2(Unknown Source)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at alice.tuprolog.PrimitiveInfo.evalAsPredicate(Unknown Source)
        at alice.tuprolog.StateGoalEvaluation.doJob(Unknown Source)
        at alice.tuprolog.Engine.run(Unknown Source)
        at alice.tuprolog.EngineManager.solve(Unknown Source)
        at alice.tuprolog.Prolog.solve(Unknown Source)
        at javaapplication9.Main.main(Main.java:26)
Exception in thread "main" alice.tuprolog.NoSolutionException
        at alice.tuprolog.SolveInfo.getSolution(Unknown Source)
        at javaapplication9.Main.main(Main.java:28)
Java Result: 1

【问题讨论】:

    标签: java prolog tuprolog


    【解决方案1】:

    您需要在 Java 中处理的主要 tuProlog 对象是:Theory,您可以使用它来表示 Prolog 程序; Prolog,即您要询问的口译员;和SolveInfo,包含解释器找到的每个解决方案的数据。

    假设您的 Prolog 程序(即您发布的除最后一行之外的所有代码)包含在名为 theory.pl 的文件中。粗略地说(即模导入和异常),您需要遵循的过程类似于:

    Prolog engine = new Prolog();
    Theory theory = new Theory(new FileInputStream("theory.pl"));
    engine.setTheory(theory);
    SolveInfo solution = engine.solve("associatedWith(X, Y, Z).");
    if (solution.isSuccess()) {
        System.out.println(solution.getTerm("X"));
        System.out.println(solution.getTerm("Y"));
        System.out.println(solution.getTerm("Z"));
    }
    

    如果您的查询可能产生多个解决方案,请使用while 循环以Prolog.hasOpenAlternatives 测试开放选择点的存在,并使用Prolog.solveNext 检索另一个解决方案。

    手册中的第 8 章(一个 PDF 文件,您应该在任何 tuProlog 发行版的 doc 子目录中找到)包含许多关于从 Java 代码与 Prolog 交互的复杂性增加的小示例(具体请参见第 8.4 节)。

    【讨论】:

    • 此代码适用于 associatedWith(X,Y)。即有 2 个参数,但当我给出 3 个参数时不起作用(X,Y,Z)。怎么办..请帮忙
    • 在我的主要问题中添加了例外.. 请说要做什么
    【解决方案2】:

    agent/2 是 tuProlog 中的内置谓词 - 您可以尝试重命名您的版本。

    【讨论】:

    • 我很伤心。这个答案显然解决了 OP 的问题,但它没有被标记为正确。另一个答案帮助我理解了 tuProlog.. 为 allll 点赞
    • @ataulm 最初的问题是关于 Prolog 和 Java 的集成,我回答了这个问题。然后,对问题进行了编辑,添加了一个请求帮助的例外,我忽略了这个例外,但这个答案给出了。
    猜你喜欢
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-18
    • 2019-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多