1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
import org.mindswap.pellet.jena.PelletReasonerFactory;
import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.ObjectProperty;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
/**
* An example program that tests the DL-safe rules example from Table 3 in the
* paper: B. Motik, U. Sattler, R. Studer. Query Answering for OWL-DL with
* Rules. Proc. of the 3rd International Semantic Web Conference (ISWC 2004),
* Hiroshima, Japan, November, 2004, pp. 549-563
*
* @author Evren Sirin
*/
public class RulesExample {
public static void main(String[] args) {
String ont = "http://owldl.com/ontologies/dl-safe.owl";
OntModel model = ModelFactory.createOntologyModel( PelletReasonerFactory.THE_SPEC, null );
model.read( ont );
ObjectProperty sibling = model.getObjectProperty( ont + "#sibling" );
OntClass BadChild = model.getOntClass( ont + "#BadChild" );
OntClass Child = model.getOntClass( ont + "#Child" );
Individual Abel = model.getIndividual( ont + "#Abel" );
Individual Cain = model.getIndividual( ont + "#Cain" );
Individual Remus = model.getIndividual( ont + "#Remus" );
Individual Romulus = model.getIndividual( ont + "#Romulus" );
model.prepare();
// Cain has sibling Abel due to SiblingRule
printPropertyValues( Cain, sibling );
// Abel has sibling Cain due to SiblingRule and rule works symmetric
printPropertyValues( Abel, sibling );
// Remus is not inferred to have a sibling because his father is not
// known
printPropertyValues( Remus, sibling );
// No siblings for Romulus for same reasons
printPropertyValues( Romulus, sibling );
// Cain is a BadChild due to BadChildRule
printInstances( BadChild );
// Cain is a Child due to BadChildRule and ChildRule2
// Oedipus is a Child due to ChildRule1 and ChildRule2 combined with the
// unionOf type
printInstances( Child );
}
public static void printPropertyValues(Individual ind, Property prop) {
System.out.print( ind.getLocalName() + " has " + prop.getLocalName() + "(s): " );
printIterator( ind.listPropertyValues( prop ) );
}
public static void printInstances(OntClass cls) {
System.out.print( cls.getLocalName() + " instances: " );
printIterator( cls.listInstances() );
}
public static void printIterator(ExtendedIterator i) {
if( !i.hasNext() ) {
System.out.print( "none" );
}
else {
while( i.hasNext() ) {
Resource val = (Resource) i.next();
System.out.print( val.getLocalName() );
if( i.hasNext() )
System.out.print( ", " );
}
}
System.out.println();
}
} |
Partager