Ian and Dave, thank you both for your help.
I didn't post the correct code and I am sorry for this.
Regarding the ontology, I know it is not correct.
Maybe changing Europe to European, Germany to German, etc. would be better.
Now regarding the listInstances method, I still believe something is wrong
either in the code, in the API or in my way of thinking.
listInstances is supposed to return the instances, either direct or
instances of its subclasses. Unfortunately if I use a simple RDF_MEM model
with no inference, listInstances(false) for the Manufacturer class returns
no result. Somehow I feel this is wrong.
I was thinking that internally, since there is no inference, jena should
visit each subclass, and the subclasses of them, etc. getting the direct
instances of each one and returning all the instances of the class and its
subclasses. Is this correct?
Now regarding listInstances(true), I am supposing that it should return all
direct instances of the class, even if these instances are also instances
of a subclass (which for example can happen if I load the TestInference.rdf
file). But it seems this is not the case. For example, for the Manufacturer
no instance is returned, although one exists (ID21_Car_Audi_A3...). The
problem seems to be that this instance is also instance of the subclasses.
If I limit ID21_Car_Audi_A3 to be instance of the Manufacturer only (like
in the attached Test_Super.rdf), then it is returned correctly by
listInstances(true).
Finally, regarding performance,I would like to know if jena internally
uses indices that can provide me fast access from classes to instances and
from instances to classes. Else I probably will have to implement them.
Please, keep in mind that I am a real newbie to RDF....
Thank you very much for your time
Regards
Papadakos Panagiotis
On Wed, Jan 23, 2013 at 8:46 PM, Panagiotis Papadakos <[email protected]>wrote:
> Hi.
>
>
> Assume the attached RDF file. This is a simple
>
> Manufacturer -> Europe (subclass) -> Germany (subclass) -> Audi (subclass)
> -> Instance,
>
> which has been created using inference for subclass and type from jena.
>
>
> The problem that I have is that if I get the root class (in this example
> Manufacturer)
>
> and call the listInstances(true) method, I get an empty set (I assume that
> I should
>
> get Manufacturer, since it has a direct instance).
>
>
> Furthermore, if I use the transitive reduction of the above code and use
> no inference
>
> in the model, the listInstances(false) method this time, again I get an
> empty result
>
> (I assume that again I should get the Manufacturer class).
>
>
> Finally, I am really interested in very fast access from classes or
> subclasses to instances and from instances to classes and
> subclasses. Can you provide me any hints?
>
> Thank you!
>
> public class JenaTest {
> private OntModel model;
> private String inputFileName;
>
> public JenaTest() {
>
> // Create a simple RDFS++ Reasoner.
> //StringBuilder sb = new StringBuilder();
>
> //sb.append("[rdfs9: (?x rdfs:subClassOf ?y), (?a rdf:type ?x)
> -> (?a rdf:type ?y)] ");
> //sb.append("[rdfs11: (?x rdfs:subClassOf ?y), (?y
> rdfs:subClassOf ?z) -> (?x rdfs:subClassOf ?z)] ");
>
> //Reasoner reasoner = new
> GenericRuleReasoner(Rule.parseRules(sb.toString()));
>
> // an OWL/RDF model that performs no reasoning at all:
> model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
>
> //Model base = ModelFactory.createDefaultModel();
> //model = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM,
> ModelFactory.createInfModel(reasoner, base));
>
>
> // using the FileManager to find the input file:
> InputStream in =
> FileManager.get().open("/home/mythos/Virtuoso/WISE_Demo_Data/data/TestInference.rdf");
>
> if (in == null) {
> System.out.println("File: \"" + inputFileName + "\" not
> found");
> }
>
> // read the RDF/XML file
> try {
> model.read(in, "");
> } catch (Exception e) {
> System.err.println("Could not read file \"" + inputFileName +
> "\". It's either malformed or corrupted\n");
> }
>
> }
>
> public Set<String> getAllTopClassesWithInstances() {
> Set <String> top = new HashSet<String>();
>
> ExtendedIterator<OntClass> it = model.listHierarchyRootClasses();
> for (; it.hasNext();) {
> OntClass cl = it.next();
> if (cl.listInstances(false).hasNext()) {
> String c = cl.getLocalName();
> top.add(c);
> }
> }
>
> return top;
> }
> /**
> * @param args the command line arguments
> */
> public static void main(String[] args) {
> JenaTest test = new JenaTest();
> Set <String> top = test.getAllTopClassesWithInstances();
> System.out.println(top);
>
> }
> }
>
> --
>
> Panagiotis Papadakos,
>
>
> PhD Student
>
> Computer Science Department
>
> University of Crete
>
> http://www.csd.uoc.gr/~papadako
>
--
http://www.flickr.com/photos/papadako
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns="http://www.semanticweb.org/ontologies/2012/Preferences#"
xmlns:ontologies="http://www.semanticweb.org/ontologies/2012/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#" >
<rdf:Description rdf:about="http://www.semanticweb.org/ontologies/2012/Preferences#Europe">
<rdfs:subClassOf rdf:resource="http://www.semanticweb.org/ontologies/2012/Preferences#Manufacturer"/>
<rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Class"/>
</rdf:Description>
<rdf:Description rdf:about="http://www.semanticweb.org/ontologies/2012/Preferences#Manufacturer">
<rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Class"/>
</rdf:Description>
<rdf:Description rdf:about="http://www.semanticweb.org/ontologies/2012/Preferences#Germany">
<rdfs:subClassOf rdf:resource="http://www.semanticweb.org/ontologies/2012/Preferences#Manufacturer"/>
<rdfs:subClassOf rdf:resource="http://www.semanticweb.org/ontologies/2012/Preferences#Europe"/>
<rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Class"/>
</rdf:Description>
<rdf:Description rdf:about="http://www.semanticweb.org/ontologies/2012/Preferences#Audi">
<rdfs:subClassOf rdf:resource="http://www.semanticweb.org/ontologies/2012/Preferences#Manufacturer"/>
<rdfs:subClassOf rdf:resource="http://www.semanticweb.org/ontologies/2012/Preferences#Europe"/>
<rdfs:subClassOf rdf:resource="http://www.semanticweb.org/ontologies/2012/Preferences#Germany"/>
<rdf:type rdf:resource="http://www.w3.org/2000/01/rdf-schema#Class"/>
</rdf:Description>
<rdf:Description rdf:about="http://www.semanticweb.org/ontologies/2012/Preferences#ID21_Car_Audi_A3_2012_Automatic_Front_1.984lt_200hp_0Cyl_0.0022THC_0.71CO_428CO2_0.011NOx_0PM_0CH4_0N20">
<rdf:type rdf:resource="http://www.semanticweb.org/ontologies/2012/Preferences#Manufacturer"/>
<rdf:type rdf:resource="http://www.semanticweb.org/ontologies/2012/Preferences#Europe"/>
<rdf:type rdf:resource="http://www.semanticweb.org/ontologies/2012/Preferences#Germany"/>
<rdf:type rdf:resource="http://www.semanticweb.org/ontologies/2012/Preferences#Audi"/>
</rdf:Description>
</rdf:RDF>
<?xml version="1.0"?>
<!DOCTYPE rdf:RDF [
<!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 ontologies "http://www.semanticweb.org/ontologies/2012/" >
]>
<rdf:RDF xmlns="&ontologies;Preferences#"
xml:base="&ontologies;Preferences#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:ontologies="http://www.semanticweb.org/ontologies/2012/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<!--CLASSES-->
<!--MANUFACTURER-->
<rdfs:Class rdf:about="&ontologies;Preferences#Manufacturer"/>
<rdfs:Class rdf:about="&ontologies;Preferences#Europe">
<rdfs:subClassOf rdf:resource="&ontologies;Preferences#Manufacturer"/>
</rdfs:Class>
<!-- German Cars-->
<rdfs:Class rdf:about="&ontologies;Preferences#Germany">
<rdfs:subClassOf rdf:resource="&ontologies;Preferences#Europe"/>
</rdfs:Class>
<rdfs:Class rdf:about="&ontologies;Preferences#Audi">
<rdfs:subClassOf rdf:resource="&ontologies;Preferences#Germany"/>
</rdfs:Class>
<rdf:Description rdf:about="&ontologies;Preferences#ID21_Car_Audi_A3_2012_Automatic_Front_1.984lt_200hp_0Cyl_0.0022THC_0.71CO_428CO2_0.011NOx_0PM_0CH4_0N20">
<rdf:type rdf:resource="&ontologies;Preferences#Manufacturer"/>
</rdf:Description>
</rdf:RDF>