如何从Jena RDF Inf模型的Node值中获取个人名称 [英] How to get Individual name from Node value in Jena RDF Inf Model

查看:201
本文介绍了如何从Jena RDF Inf模型的Node值中获取个人名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从耶拿图中提取个人姓名时遇到了问题(带有通用规则推理程序且OntSpec的RDF推理模型为RDFS_MEM_RDFS_INF).这可能是一个简单的案例,但是我无法在网上找到合适的教程来完成此操作(耶拿规则的新手).在这种情况下需要调用什么适当的API?

I am facing a problem extracting the Individual's name from the Jena Graph (RDF Inference Model with Generic Rule Reasoner and OntSpec is RDFS_MEM_RDFS_INF ). It may be a simple case but I am not able to find proper tutorial online to get this done (new to jena rules). What is the proper api to call for in this case?

infStmts = pModel.listStatements().filterKeep( new Filter<Statement>() {
        @Override
        public boolean accept(Statement o) {                
            boolean ex = false;
            Property prop1 = pModel.getProperty(prefix + "hasPropertyP1");
            String predicateName  = o.asTriple().getPredicate().getLocalName();             
            if(predicateName.equalsIgnoreCase(prop1.getLocalName()) )                   
                ex = true;                
            return ex;

        }
    });

    Statement s = infStmts.next();
    Statement st = ResourceFactory.createStatement(s.getSubject(), s.getPredicate(), s.getObject());
    System.out.println(st.getSubject().toString() + "****" + pModel.getRDFNode(st.getSubject().asNode()).as(Individual.class));

Exception in thread "Thread-37" com.hp.hpl.jena.ontology.ConversionException: Cannot convert node 4e62503a:14b01762f42:-7eea to Individual
at com.hp.hpl.jena.ontology.impl.IndividualImpl$1.wrap(IndividualImpl.java:61)
at com.hp.hpl.jena.enhanced.EnhNode.convertTo(EnhNode.java:152)
at com.hp.hpl.jena.enhanced.EnhNode.convertTo(EnhNode.java:31)
at com.hp.hpl.jena.enhanced.Polymorphic.asInternal(Polymorphic.java:62)
at com.hp.hpl.jena.enhanced.EnhNode.as(EnhNode.java:107)
...

Jena规则文件具有以下规则

The Jena rules file has the following rule

[rule: ( :Subject1 :hasPropertyP2 :Object1) ->
       ( ?x rdf:type :Class1)
       ( ?x :hasPropertyP1 :Object2)]

我需要根据个人姓名的?x值

I need the value of ?x in terms of individuals' names

推荐答案

在下面的行中,您尝试获取主题,然后获取其Node版本,然后获取其单个版本.

In the following line you're trying to get the the subject, then get a Node version of it, and then get an Individual version of it.

System.out.println(st.getSubject().toString() + "****" + pModel.getRDFNode(st.getSubject().asNode()).as(Individual.class));

您正在对图中每个三元组的主题进行此操作. 个人的文档说:

You're doing this for the subject of every triple in the graph. The documentation for Individual says:

为了被识别为个人,而不是一般人 资源,至少一个rdf:type语句,引用一个已知的类, 必须存在于模型中.

In order to be recognised as an individual, rather than a generic resource, at least one rdf:type statement, referring to a known class, must be present in the model.

您收到的错误消息,

无法将节点4e62503a:14b01762f42:-7eea转换为个人

Cannot convert node 4e62503a:14b01762f42:-7eea to Individual

指示图中的某个地方有一个三元组,其主题是空白节点.显然,这些空白节点之一不符合成为个人"的条件.在执行 as(Individual.class)之前,您需要检查该节点是否可以是个人.您可以先使用 canAs(Individual.class)进行检查.

indicates that somewhere in the graph, there's a triple whose subject is a blank node. Apparently one of those blank nodes doesn't meet the criteria for being an Individual. You need to check whether the node can be an Individual before doing as(Individual.class). You can check first with canAs(Individual.class).

但是,您需要意识到,仅仅因为规则文件只有一个规则,并不意味着推理图中的所有三元组都将由它生成.推理图中可以有更多的三元组.

However, you need to be aware that just because the rules file only has one rule does not mean that all the triples in the inference graph will have been generated by it. There can be lots more triples in the inference graph.

一种更好的方法是使用 Model.listStatements 列出具有您关心的属性的语句,然后检查主题是否为URI资源. ,如果是,则将其作为URI资源并提取其URI:

A better way to do what you're trying to do would be to list the statements with the property that you care about using Model.listStatements, and then check whether the subject is a URI resource, and if is, then get it as a URI resource and extract its URI:

Property p1 = model.createProperty(...)
StmtIterator stmts = model.listStatements(null,p1,null); 
while ( stmts.hasNext() ) {
  Statement stmt = stmts.next();
  RDFNode subject = stmt.getSubject();
  if ( subject.isURIResource() ) {
    System.out.println( "Subject URI is: "+ subject.asResource().getURI() );
  }
}

这篇关于如何从Jena RDF Inf模型的Node值中获取个人名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆