gpt4 book ai didi

java - 如何让TuProlog识别无效事实?

转载 作者:行者123 更新时间:2023-12-02 03:58:21 25 4
gpt4 key购买 nike

我有以下两个 Prolog 文件:

ontology.pl:

isSite(Url) :- string(Url).
guestPostPublished(GuestPostId, Date, Site, Url) :-
string(GuestPostId),
date(Date),
isSite(Site),
string(Url),
\+(guestPostPublished(GuestPostId, _, _, _)).

invalidFile.pl:

isSite('somesite.com').
guestPostPublished(
'gp1',
date(2016,2,2),
'somesite.com',
'someUrl').

guestPostPublished(
'gp1',
date(2016,2,2),
'somesite.com',
'anotherUrl').

invalidFile.pl 无效,因为它违反了 ontology.pl 中指定的所有 GuestPostId 必须唯一的规则。

当我将该数据加载到引擎中时,我会抛出一些异常,表明数据无效。但事实并非如此。

我做错了什么?当我向 TuProlog 提供无效数据时,如何确保引擎,我收到某种通知(例如异常或某些标志)?

这是我的代码的相关片段(您可以找到整个代码 here ):

@Test
public void test2() throws InvalidObjectIdException, IOException,
MalformedGoalException, InvalidTheoryException, UnknownVarException, NoSolutionException,
NoMoreSolutionException, InvalidLibraryException {
final Prolog engine = createEngine();

try
{
loadPrologFiles(engine, new String[]{
"src/main/resources/ontology.pl",
"src/main/resources/invalidFile.pl"
});
Assert.fail("Engine swallows invalid Prolog file.");
}
catch (final Exception exception) {
// TODO: Check that the right exception is thrown
}
final List<String> result = getResults(engine, "guestPostPublished(_,X,_,_).", "X");
System.out.println("result: " + result);
}

private Prolog createEngine() throws InvalidObjectIdException {
final Prolog engine = new Prolog();
engine.addOutputListener(new OutputListener() {
public void onOutput(OutputEvent outputEvent) {
System.out.println(String.format("PROLOG: %s", outputEvent.getMsg()));
}
});
Library lib = engine.getLibrary("alice.tuprolog.lib.OOLibrary");
((OOLibrary)lib).register(new Struct("stdout"), System.out);
return engine;
}

private void loadPrologFiles(final Prolog engine, final String[] files) throws IOException, InvalidTheoryException {
final List<String> paths = Arrays.asList(files);
final StringBuilder theoryBuilder = new StringBuilder();

for (final String path : paths) {
theoryBuilder.append(System.lineSeparator());
theoryBuilder.append("% ");
theoryBuilder.append(path);
theoryBuilder.append(" (START)");
theoryBuilder.append(System.lineSeparator());
theoryBuilder.append(FileUtils.readFileToString(new File(path)));
theoryBuilder.append(System.lineSeparator());
theoryBuilder.append("% ");
theoryBuilder.append(path);
theoryBuilder.append(" (END)");
theoryBuilder.append(System.lineSeparator());
}

final Theory test1 = new Theory(theoryBuilder.toString());
engine.setTheory(test1);
}

private List<String> getResults(final Prolog engine, final String query, final String varName) throws
MalformedGoalException, NoSolutionException, UnknownVarException, NoMoreSolutionException {
SolveInfo res2 = engine.solve(query);

final List<String> result = new LinkedList<String>();
if (res2.isSuccess()) {
result.add(res2.getTerm(varName).toString());
while (engine.hasOpenAlternatives()) {
res2 = engine.solveNext();
final Term x2 = res2.getTerm("X");
result.add(x2.toString());
}
}
return result;
}

最佳答案

要在 Prolog 事实表上设置数据完整性约束,您需要采用不同的方法。我建议您首先尝试在纯 Prolog 中执行此操作,而不使用 Java 位,只是为了了解正在发生的情况。

如果数据库是静态的并且不会更改,则很简单:只需加载它,然后对其运行查询以执行数据完整性检查。例如,您有一个包含单列的表 site/1,并且您希望确保所有值都是字符串:

There is no site(S) so that S is not a string

\+ ( site(S), \+ string(S) )

如果您想将其包装到谓词中,则必须使用与表不同的名称来命名谓词!

site_must_be_string :-
\+ ( site(S), \+ string(S) ).

或者,对于另一个,一个唯一的列(主键):

There are no duplicates among the first arguments to guest_post_published/4

findall(ID, guest_post_published(ID, _, _, _), IDs),
length(IDs, Len),
sort(IDs, Sorted), % sort/2 removes duplicates!
length(Sorted, Len). % length does not change after sorting

您可能还需要将其包装在它自己的谓词中。

关于java - 如何让TuProlog识别无效事实?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35194122/

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