- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下两个 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 thatS
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/
我正在尝试使用以下命令通过 ansible 收集服务器 list : ansible all -m setup -a --tree facts/ 但这会在facts文件夹下为每个主机生成很多操作系统单
我决定学习 prolog 只是为了好玩,我正在看一些视频教程。我也在互联网上搜索了一些问题试图解决,但找不到解决方法。 我能够在纸上解决这个难题,但无法将其传递到代码中。 问题: I have 8 c
我正在通过DRT(规则模板)读取Excel表,然后通过Java类中的静态方法将其转换为类的对象。并将其插入到工作内存中。这样,工作内存就会被 Excel 表格中的所有事实填满。 现在,我通过 sess
我确信这非常简单,但我显然没有用谷歌搜索正确的东西。在几个单元测试相关的博客中,我看到了几个方法被赋予的属性 [fact] ,但我似乎无法弄清楚这意味着什么。示例: public class Some
我想知道这些术语之间的区别: 事实 仿函数 谓词。 规则 在Prolog中。 如果我写:brother(john, jack).这是事实吗?或谓词? 最佳答案 从 ISO/IEC 13211-1 Fi
问题 为什么选择 Ansible 事实,例如 ansible_distribution在角色部分不可用? --- - name: Test hosts: all tasks: - na
puppet 事实看起来像这样: processors => {"models"=>["AMD Opteron(tm) Processor 6172", "AMD Opteron(tm) Proces
在做了一些研究之后,我对 Prolog 以非常简单的方式表达查询的能力感到惊讶,几乎就像口头告诉机器该做什么一样。发生这种情况是因为我对工作中的 Propel 和 PHP 感到非常厌烦。 所以,我一直
我想生成一个密码和其他一些不存在的值。 像这样的东西: - name: Retrieve or generate my_password generated_fact: shell: so
我需要从 SEC 10-K 文件中获取一些事实,例如毛收入、毛利润、毛利率、营业费用等以及相应的上下文。 对于像 https://www.sec.gov/Archives/edgar/data/131
我是 puppet 新手,并计划在我们的环境中实现它。 我有在不同版本的 Redhat 上运行的 puppet 代理。 现在,我计划从 puppet master 推送存储库文件,我需要您的指导来实现
我是 puppet 新手,并计划在我们的环境中实现它。 我有在不同版本的 Redhat 上运行的 puppet 代理。 现在,我计划从 puppet master 推送存储库文件,我需要您的指导来实现
是否可以使用通常包含在 ansible_facts 中的内容?在 Ansible 临时命令中? 例如,我有一个位于 /tmp/myFile 的文件。在我所有的服务器上,我想做: ansible all
获得正确的术语是成功传达概念的一部分,当在 SO 中使用错误的术语时,带有 Prolog 标签的受访者会很好地指出错误。 在阅读 William F. Clocksin 于 1997 年 ( Worl
上下文:我的系统包含来自不同存储 Controller 的磁盘组合,因此每种类型的磁盘都有不同的用途。我是 ansible 的新手,我一边学习一边学习。编写一个剧本,从每种类型的 Controller
我是数据仓库的新手,我希望有一个关于构建星型模式的简单问题: 如果我有一个事实表,其中事实记录自然与单个维度具有一对多关系,那么如何建模星型模式来支持这一点?例如: 事实表:销售点条目(衡量标准是美元
我正在用 Idris 编写一个基本的 monadic 解析器,以适应与 Haskell 的语法和差异。我有基本的工作,但我坚持尝试为解析器创建 VerifiedSemigroup 和 Verified
我的应用程序需要足够的准确性,但 Google Places 按类别过滤的准确性似乎很差。所以我正在考虑迁移到 Factual API。大家用过吗?您如何看待它的准确性? 另一方面,我需要知道到某个地
我是一名优秀的程序员,十分优秀!