Hi Panagiotis,
On 23/01/13 18:46, Panagiotis Papadakos wrote:
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).
I think you have several confusions here. You are right that, in your
ontology, Manufacturer is the root class. However I think your modelling
is confused, because Germany is not a sub-class of Manufacturer. If it
was, then every instance of a Germany would also be an instance of a
Manufacturer. That may possibly be true in some domain, but I would
think that it is unlikely, and it certainly doesn't really match reality.
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).
The problem here is that you are mixing ontology languages. You have
declared instances of rdfs:Class, but then are using an ontology model
with the OWL profile. So without the inference engine's help, you don't
get any results from listHierarchyRootClasses(), which in turn gives you
no instances in the output.
The answer to this is to split up your code so that each method only has
a single responsibility, and write JUnit tests for each one. Then you
would see when your expectations (this hierarchy has some root classes)
are violated (but the code isn't finding them).
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?
My hint would be: get it working, before you worry about getting it
working quickly.
http://c2.com/cgi/wiki?PrematureOptimization
When you have the core of your application running, use a profiler to
find out where it runs slowly, and then optimize those bits. It almost
certainly won't be where you expect. If necessary, add some performance
tests to your unit and functional test suite ... which will of course be
quite extensive by then :)
There were some other problems with your code, so I've attached a
fixed-up version that addresses some of the problems and shows you that
you can use either inference or an RDFS model.
Regards,
Ian
--
____________________________________________________________
Ian Dickinson Epimorphics Ltd, Bristol, UK
mailto:[email protected] http://www.epimorphics.com
cell: +44-7786-850536 landline: +44-1275-399069
------------------------------------------------------------
Epimorphics Ltd. is a limited company registered in England
(no. 7016688). Registered address: Court Lodge, 105 High St,
Portishead, Bristol BS20 6PT, UK
package test;
import java.util.*;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
public class PanagiotisTest {
private OntModel model;
// public static String SOURCE = "/home/mythos/Virtuoso/WISE_Demo_Data/data/TestInference.rdf";
public static String SOURCE = "./src/main/resources/TestInference.rdf";
public PanagiotisTest( boolean withInference ) {
model = initializeModel( withInference );
}
protected OntModel initializeModel( boolean withInference ) {
OntModel m = null;
if (withInference) {
m = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF);
}
else {
m = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM );
}
FileManager.get().readModel( m, SOURCE );
return m;
}
public Map<String,List<String>> getAllTopClassesWithInstances() {
Map<String,List<String>> instances = new HashMap<String, List<String>>();
for (ExtendedIterator<OntClass> it = roots( model ); it.hasNext(); ) {
OntClass c = it.next();
instances.put( c.getLocalName(), collectInstances( c ) );
}
return instances;
}
protected ExtendedIterator<OntClass> roots( OntModel m ) {
return model.listHierarchyRootClasses();
}
protected List<String> collectInstances( OntClass c ) {
List<String> instanceNames = new ArrayList<String>();
for (ExtendedIterator<? extends OntResource> i = c.listInstances(); i.hasNext(); ) {
instanceNames.add( i.next().getLocalName() );
}
return instanceNames;
}
public static void main(String[] args) {
PanagiotisTest test = new PanagiotisTest( true );
displayResults( "With inference and OWL model ... ", test.getAllTopClassesWithInstances() );
test = new PanagiotisTest( false );
displayResults( "With no inference and RDFS model ... ", test.getAllTopClassesWithInstances() );
}
private static void displayResults( String msg, Map<String,List<String>> result ) {
System.out.println( msg );
for (Iterator<String> i = result.keySet().iterator(); i.hasNext(); ) {
String k = i.next();
System.out.println( "Root: " + k + " => instances: " + result.get(k) );
}
}
}