Reputation: 41
I have created a simple ontology, given at the end of this question. There is a customer class and there are two instances, customer1 and customer2, with the datatype properties ssn, loan, and account. loan is associated with customer1 and not customer2. I want to print the individuals having values for loan. This is my Jena code:
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);
}
}
}
}
I get the following output:
Class Name : Customer
Individual Name : customer1
Individual Name : customer2
Propties Name : ssn
Propties Name : loan
Propties Name : account
Persons having loan :
customer1
customer2
The output must be only customer1. But I get both customer1 and customer2. What is wrong here?
<?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 -->
Upvotes: 2
Views: 1298
Reputation: 745
Change:
DatatypeProperty loan = inf.getDatatypeProperty("loan");
to
DatatypeProperty loan = inf.getDatatypeProperty("http://www.semanticweb.org/subham/ontologies/2014/9/customer.owl#loan");
you have to fully quantify the propertyname. If you do it like this DatatypeProperty loan = inf.getDatatypeProperty("loan");
it will return null.
Upvotes: 2