我创建了一个简单的本体,在本问题的末尾给出。有一个客户类,有两个实例customer1和customer2,它们的数据类型属性为ssn,bone和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 -->
最佳答案
更改:
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 - 耶拿-访问具有数据类型属性的个人,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26505377/