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

时间:2014-10-22 10:26:22

标签: java jena semantic-web owl

我在这个问题的最后给出了一个简单的本体论。有一个客户类,有两个实例,customer1和customer2,具有数据类型属性ssn,loan和account。贷款与customer1而非customer2相关联。我想打印有贷款价值的个人。这是我的耶拿代码:

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。这有什么不对?

OWL文件

<?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 -->

1 个答案:

答案 0 :(得分:2)

变化:

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。