java - Jena - 访问给定数据类型属性的个人

标签 java jena semantic-web owl

我创建了一个简单的本体,在这个问题的末尾给出。有一个客户类和两个实例,customer1 和 customer2,具有数据类型属性 ssn、loan 和 account。贷款与客户 1 而不是客户 2 相关联。我想打印具有贷款值(value)的个人。这是我的耶拿代码:

import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.DatatypeProperty;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.NodeIterator;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import java.io.InputStream;
import java.util.Iterator;

public class PropertyAccess {

    static final String owlFile = "C:\\Users\\Subham\\Desktop\\customer.owl";

    public static void main(String[] args) throws Exception {

        OntModel inf = ModelFactory.createOntologyModel();
        InputStream in = FileManager.get().open(owlFile);
        inf.read(in, "");       
        ExtendedIterator classes = inf.listClasses();
        while(classes.hasNext())
        {
            OntClass obj = (OntClass) classes.next();
            String className = obj.getLocalName().toString();
            System.out.println("Class Name : "+className);                         
        }   
        ExtendedIterator instances = inf.listIndividuals();
        while(instances.hasNext())
        {
            Individual ind = (Individual) instances.next();
            String indName = ind.getLocalName().toString();
            System.out.println("Individual Name : "+indName);                          
        }       

        ExtendedIterator property = inf.listDatatypeProperties();
        while(property.hasNext())
        {
            DatatypeProperty prop = (DatatypeProperty) property.next();
            String propName = prop.getLocalName().toString();
            System.out.println("Propties Name : "+propName);                           
        }

        DatatypeProperty loan = inf.getDatatypeProperty("loan"); 

        System.out.println("Persons having loan : ");

        ExtendedIterator individuals = inf.listIndividuals();
        while(individuals.hasNext())
        {
            Individual ind = (Individual) individuals.next();
            if(ind.hasProperty(loan))
            {
                String indName = ind.getLocalName().toString();
                System.out.println(indName);
            }          
        }       

    }
}

我得到以下输出:

Class Name : Customer
Individual Name : customer1
Individual Name : customer2
Propties Name : ssn
Propties Name : loan
Propties Name : account
Persons having loan : 
customer1
customer2

输出必须只有 customer1。但是我同时获得了 customer1 和 customer2。这里有什么问题?

猫头鹰文件

<?xml version="1.0"?>


<!DOCTYPE rdf:RDF [
    <!ENTITY owl "http://www.w3.org/2002/07/owl#" >
    <!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
    <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
    <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
    <!ENTITY customer "http://www.semanticweb.org/subham/ontologies/2014/9/customer.owl#" >
]>


<rdf:RDF xmlns="http://www.semanticweb.org/subham/ontologies/2014/9/customer.owl#"
     xml:base="http://www.semanticweb.org/subham/ontologies/2014/9/customer.owl"
     xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
     xmlns:owl="http://www.w3.org/2002/07/owl#"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:customer="http://www.semanticweb.org/subham/ontologies/2014/9/customer.owl#">
    <owl:Ontology rdf:about="http://www.semanticweb.org/subham/ontologies/2014/9/customer.owl"/>



    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Data properties
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->




    <!-- http://www.semanticweb.org/subham/ontologies/2014/9/customer.owl#account -->

    <owl:DatatypeProperty rdf:about="&customer;account">
        <rdfs:domain rdf:resource="&customer;Customer"/>
        <rdfs:range rdf:resource="&xsd;integer"/>
    </owl:DatatypeProperty>



    <!-- http://www.semanticweb.org/subham/ontologies/2014/9/customer.owl#loan -->

    <owl:DatatypeProperty rdf:about="&customer;loan">
        <rdfs:domain rdf:resource="&customer;Customer"/>
        <rdfs:range rdf:resource="&xsd;integer"/>
    </owl:DatatypeProperty>



    <!-- http://www.semanticweb.org/subham/ontologies/2014/9/customer.owl#ssn -->

    <owl:DatatypeProperty rdf:about="&customer;ssn">
        <rdfs:domain rdf:resource="&customer;Customer"/>
        <rdfs:range rdf:resource="&xsd;integer"/>
    </owl:DatatypeProperty>



    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Classes
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->




    <!-- http://www.semanticweb.org/subham/ontologies/2014/9/customer.owl#Customer -->

    <owl:Class rdf:about="&customer;Customer"/>



    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Individuals
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->




    <!-- http://www.semanticweb.org/subham/ontologies/2014/9/customer.owl#customer1 -->

    <owl:NamedIndividual rdf:about="&customer;customer1">
        <rdf:type rdf:resource="&customer;Customer"/>
        <account rdf:datatype="&xsd;integer">1</account>
        <loan rdf:datatype="&xsd;integer">1200</loan>
        <ssn rdf:datatype="&xsd;integer">1441</ssn>
    </owl:NamedIndividual>



    <!-- http://www.semanticweb.org/subham/ontologies/2014/9/customer.owl#customer2 -->

    <owl:NamedIndividual rdf:about="&customer;customer2">
        <rdf:type rdf:resource="&customer;Customer"/>
        <ssn rdf:datatype="&xsd;integer">1234</ssn>
        <account rdf:datatype="&xsd;integer">2</account>
    </owl:NamedIndividual>
</rdf:RDF>



<!-- Generated by the OWL API (version 3.4.2) http://owlapi.sourceforge.net -->

最佳答案

改变:

DatatypeProperty loan = inf.getDatatypeProperty("loan"); 

DatatypeProperty loan = inf.getDatatypeProperty("http://www.semanticweb.org/subham/ontologies/2014/9/customer.owl#loan");

您必须完全量化属性名称。如果您这样做 DatatypeProperty loan = inf.getDatatypeProperty("loan"); 它将返回 null。

关于java - Jena - 访问给定数据类型属性的个人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26505377/

相关文章:

java - 单击按钮即可执行多个步骤操作

java - ArrayList元素操作后未更新

java - 使用 D2RQ 映射数据库

rdf - 如何构建正确的 SPARQL 查询

semantic-web - [免费基地] : Finding relationship between nodes

java - 消除类型转换中的中间步骤

java - 如何检查java类中是否有特定的方法?

java - 当我们有主题和属性时,如何在不使用 sparql 的情况下使用 jena 检索 RDF 中对象的值?

java - Sparql 与 Java Jena

java - 安装 Jena Fuseki 需要哪些额外条件(如果有)?