I used a lot of documentation, and I think I finally found the solution.
It's not exactly what I expected, but for now it will be enough.
The main idea : create individuals (which are instances of concepts/classes), link them together, and ask a reasoner to discover things (inferences).
Useful docs (thank you StakOverflow for the links) :
https://jena.apache.org/documentation/inference/#owl
http://jena.apache.org/documentation/ontology/#instances-or-individuals
http://jena.apache.org/documentation/ontology/index.html
First, what is my solution :
- instantiate the base model (and load the pizza ontology)
- instantiate an inference model (and choose a reasoner)
- create in the base model : multiple individuals and properties/relations between them
- check validity of model, ask for assertion (in base model) and
It's working. I can create an individual "MozzarellaTopping", an individual "DeepPanBase", and an individual "Food". I added two properties to "Food" : hasBase to individual DeepPanBase, and hasTopping to individual MozzarellaTopping.
Here is the code explained step by step (full code at the end) :
Instantiate and Load base model from pizza.owl.rdf
public static final String SOURCE = "./resources/";
public static final String PIZZA_NS = "http://www.co-ode.org/ontologies/pizza/pizza.owl#";
public void run()
{
// Prefix/Header for SPARQL requests
final String prefix = "prefix pizza: <" + PIZZA_NS + ">\n"
+ "prefix rdfs: <" + RDFS.getURI() + ">\n" + "prefix owl: <"
+ OWL.getURI() + ">\n";
// Prefix for classes, individuals, ... for every object
final String NS = PIZZA_NS;
System.out.println("CREATE THE BASE MODEL\n");
// CREATE THE BASE MODEL
OntModel base = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
base.read(SOURCE + "pizza.owl.rdf", "RDF/XML");
Create the inferred model :
System.out.println("CREATE THE REASONING MODEL USING THE BASE\n");
// CREATE THE REASONING MODEL USING THE BASE
OntModel inf = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF, base);
// OWL_MEM_MICRO_RULE_INF // It works + Very quick
// OWL_MEM_MINI_RULE_INF // It works + Slow (40Mins) + 1,1Go RAM (Validity is slow)
// OWL_MEM_RULE_INF // It works (mights loop if error) + VERY SLOW + 2,1 GO RAM (unfinished)
// OWL_MEM_TRANS_INF // It works (SPARQL mights not work) + Ultra Speed / No inference
Get useful Classes and Properties in java for future individuals instantiation :
System.out.println("CREATE INDIVIDUALS FOR TESTING PURPOSE\n");
// CREATE INDIVIDUALS FOR TESTING PURPOSE
// Instantiate each useful Class
OntClass ThingClass = base.getOntClass(NS + "owl:Thing");
OntClass FoodClass = base.getOntClass(NS + "Food");
OntClass IceCreamClass = base.getOntClass(NS + "IceCream");
OntClass PizzaClass = base.getOntClass(NS + "Pizza");
OntClass MozzaToppingClass = base.getOntClass(NS + "MozzarellaTopping");
OntClass DeepPanBaseClass = base.getOntClass(NS + "DeepPanBase");
// Instantiate each useful Property (relation)
OntProperty hasIngredientProperty = base.createObjectProperty(NS + "hasIngredient");
OntProperty hasBaseProperty = base.createObjectProperty(NS + "hasBase");
OntProperty hasToppingProperty = base.createObjectProperty(NS + "hasTopping");
// Instantiate each useful individual
Individual MozzaTopping = base.createIndividual(NS + "MyMozzaTopping", MozzaToppingClass);
Individual DeepPanBase = base.createIndividual(NS + "MyDeepPanBase", DeepPanBaseClass);
Then, I first check an individual with two simultaneous classes (MozzarellaTopping and DeepPanBase)... the reasoner see a CheeseyPizza, but the validity report doesn't work :
/*
* BEGINNING OF THE TESTS HERE
*/
System.out.println("\nTEST VALIDITY BEFORE ADDING INDIVIDUALS\n");
checkValidity(inf);
// Instantiate testing individuals
// MyPizza1 : individual with 2 classes simultaneously (Mozza & DeepPan)
Individual MyPizza1 = base.createIndividual(NS + "MyPizza1", ThingClass);
MyPizza1.setOntClass(MozzaToppingClass);
MyPizza1.addOntClass(DeepPanBaseClass);
System.out.println("\nTest MyPizza1\n");
showAsserted(base, NS + "MyPizza1");
showInferred(inf, NS + "MyPizza1");
System.out.println("\nTest Validity of MyPizza1 : ");
checkValidity(inf); // ERROR
MyPizza1.remove();
System.out.println("\nRemove MyPizza1, Validity should be OK now : ");
checkValidity(inf); // OK
Then, I tried a "Food" (or "Pizza") individual to which I had a relation hasBase DeepPanBase, and another relation hasTopping MozzarellaTopping. It's working, ni problem in validity check :
// MyPizza2 : individual of class "Food", linked with Mozza & DeepPan
Individual MyPizza2 = base.createIndividual(NS + "MyPizza2", FoodClass);
MyPizza2.addProperty(hasBaseProperty, DeepPanBase);
MyPizza2.addProperty(hasToppingProperty, MozzaTopping);
System.out.println("\nTest MyPizza2\n");
showAsserted(base, NS + "MyPizza2");
showInferred(inf, NS + "MyPizza2");
System.out.println("\nTest Validity of MyPizza2 : ");
checkValidity(inf); // OK
MyPizza2.remove();
System.out.println("\nRemove MyPizza2, Validity should be OK now : ");
checkValidity(inf); // OK
Then, I try a DeepPanBase individual, to which I give a property/relation hasTopping MozzarellaTopping. The reasoner also acts as you might think : he says it's a CheeseyPizza, but, the validity check says it's wrong.
// MyPizza3 : individual of class "DeepPanBase", linked with Mozza
Individual MyPizza3 = base.createIndividual(NS + "MyPizza3", DeepPanBaseClass);
MyPizza3.addProperty(hasToppingProperty, MozzaTopping);
System.out.println("\nTest MyPizza3\n");
showAsserted(base, NS + "MyPizza3");
showInferred(inf, NS + "MyPizza3");
System.out.println("\nTest Validity of MyPizza3 : ");
checkValidity(inf); // ERROR
MyPizza3.remove();
System.out.println("\nRemove MyPizza3, Validity should be OK now : ");
checkValidity(inf); // OK
Finally, a test with an IceCream individual (Food) is made. I give it a relation hasBase DeepPanBase and another relation hasTopping MozzarellaTopping. The reasoner says it's a CheeseyPizza, and the validity check shout that it's wrong.
// IceCream : individual of class "IceCream", linked with Moza & DeePan
Individual MyIceCream = base.createIndividual(NS + "MyIceCream", IceCreamClass);
MyIceCream.addProperty(hasBaseProperty, DeepPanBase);
MyIceCream.addProperty(hasToppingProperty, MozzaTopping);
System.out.println("\nTest IceCream\n");
showAsserted(base, NS + "MyIceCream");
showInferred(inf, NS + "MyIceCream");
System.out.println("\nTest Validity of IceCream : ");
checkValidity(inf);
The validity checker is right. If you check what is a Pizza, you'll see it's an individual "Food" which contains "ingredients"/toppings, and at least one PizzaBase... but it's also something that is NOT a PizzaTopping, NOT a PizzaBase, and NOT an IceCream. (this is why the validity check is crying... if I try to put PizzaTopping on an IceCream, it's impossible...)
Anyway, As promised I give the full code here :
/*
* Example of usage of reasoner with Java. Everything is coming from Apache JENA
* examples. I modified a lot of things for making my personal requests.
* Fabrice Boissier
*/
package Jena_Reasoner_Simple;
import java.util.Date;
import java.util.Iterator;
import org.apache.jena.ontology.Individual;
import org.apache.jena.ontology.OntClass;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.ontology.OntModelSpec;
import org.apache.jena.ontology.OntProperty;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.reasoner.ValidityReport;
import org.apache.jena.vocabulary.OWL;
import org.apache.jena.vocabulary.RDFS;
public class Simple_Reasoner_StepByStep
{
public static void main(String[] args)
{
System.out.println("BEGIN : " + new Date());
new Simple_Reasoner_StepByStep().run();
System.out.println("END : " + new Date());
}
public static final String SOURCE = "./resources/";
public static final String PIZZA_NS = "http://www.co-ode.org/ontologies/pizza/pizza.owl#";
public void run()
{
// Prefix/Header for SPARQL requests
final String prefix = "prefix pizza: <" + PIZZA_NS + ">\n"
+ "prefix rdfs: <" + RDFS.getURI() + ">\n" + "prefix owl: <"
+ OWL.getURI() + ">\n";
// Prefix for classes, individuals, ... for every object
final String NS = PIZZA_NS;
System.out.println("CREATE THE BASE MODEL\n");
// CREATE THE BASE MODEL
OntModel base = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
base.read(SOURCE + "pizza.owl.rdf", "RDF/XML");
System.out.println("CREATE THE REASONING MODEL USING THE BASE\n");
// CREATE THE REASONING MODEL USING THE BASE
OntModel inf = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM_MICRO_RULE_INF, base);
// OWL_MEM_MICRO_RULE_INF // It works + Very quick
// OWL_MEM_MINI_RULE_INF // It works + Slow (40Mins) + 1,1Go RAM (Validity is slow)
// OWL_MEM_RULE_INF // It works (mights loop if error) + VERY SLOW + 2,1 GO RAM (unfinished)
// OWL_MEM_TRANS_INF // It works (SPARQL mights not work) + Ultra Speed / No inference
System.out.println("CREATE INDIVIDUALS FOR TESTING PURPOSE\n");
// CREATE INDIVIDUALS FOR TESTING PURPOSE
// Instantiate each useful Class
OntClass ThingClass = base.getOntClass(NS + "owl:Thing");
OntClass FoodClass = base.getOntClass(NS + "Food");
OntClass IceCreamClass = base.getOntClass(NS + "IceCream");
OntClass PizzaClass = base.getOntClass(NS + "Pizza");
OntClass MozzaToppingClass = base.getOntClass(NS + "MozzarellaTopping");
OntClass DeepPanBaseClass = base.getOntClass(NS + "DeepPanBase");
// Instantiate each useful Property (relation)
OntProperty hasIngredientProperty = base.createObjectProperty(NS + "hasIngredient");
OntProperty hasBaseProperty = base.createObjectProperty(NS + "hasBase");
OntProperty hasToppingProperty = base.createObjectProperty(NS + "hasTopping");
// Instantiate each useful individual
Individual MozzaTopping = base.createIndividual(NS + "MyMozzaTopping", MozzaToppingClass);
Individual DeepPanBase = base.createIndividual(NS + "MyDeepPanBase", DeepPanBaseClass);
/*
* BEGINNING OF THE TESTS HERE
*/
System.out.println("\nTEST VALIDITY BEFORE ADDING INDIVIDUALS\n");
checkValidity(inf);
// Instantiate testing individuals
// MyPizza1 : individual with 2 classes simultaneously (Mozza & DeepPan)
Individual MyPizza1 = base.createIndividual(NS + "MyPizza1", ThingClass);
MyPizza1.setOntClass(MozzaToppingClass);
MyPizza1.addOntClass(DeepPanBaseClass);
System.out.println("\nTest MyPizza1\n");
showAsserted(base, NS + "MyPizza1");
showInferred(inf, NS + "MyPizza1");
System.out.println("\nTest Validity of MyPizza1 : ");
checkValidity(inf); // ERROR
MyPizza1.remove();
System.out.println("\nRemove MyPizza1, Validity should be OK now : ");
checkValidity(inf); // OK
// MyPizza2 : individual of class "Food", linked with Mozza & DeepPan
Individual MyPizza2 = base.createIndividual(NS + "MyPizza2", FoodClass);
MyPizza2.addProperty(hasBaseProperty, DeepPanBase);
MyPizza2.addProperty(hasToppingProperty, MozzaTopping);
System.out.println("\nTest MyPizza2\n");
showAsserted(base, NS + "MyPizza2");
showInferred(inf, NS + "MyPizza2");
System.out.println("\nTest Validity of MyPizza2 : ");
checkValidity(inf); // OK
MyPizza2.remove();
System.out.println("\nRemove MyPizza2, Validity should be OK now : ");
checkValidity(inf); // OK
// MyPizza3 : individual of class "DeepPanBase", linked with Mozza
Individual MyPizza3 = base.createIndividual(NS + "MyPizza3", DeepPanBaseClass);
MyPizza3.addProperty(hasToppingProperty, MozzaTopping);
System.out.println("\nTest MyPizza3\n");
showAsserted(base, NS + "MyPizza3");
showInferred(inf, NS + "MyPizza3");
System.out.println("\nTest Validity of MyPizza3 : ");
checkValidity(inf); // ERROR
MyPizza3.remove();
System.out.println("\nRemove MyPizza3, Validity should be OK now : ");
checkValidity(inf); // OK
// IceCream : individual of class "IceCream", linked with Moza & DeePan
Individual MyIceCream = base.createIndividual(NS + "MyIceCream", IceCreamClass);
MyIceCream.addProperty(hasBaseProperty, DeepPanBase);
MyIceCream.addProperty(hasToppingProperty, MozzaTopping);
System.out.println("\nTest IceCream\n");
showAsserted(base, NS + "MyIceCream");
showInferred(inf, NS + "MyIceCream");
System.out.println("\nTest Validity of IceCream : ");
checkValidity(inf);
/*
* END OF THE TESTS HERE
*/
System.out.println("End Tests\n");
}
protected void showAsserted(OntModel m, String individualURI)
{
// list the asserted types
Individual instance = m.getIndividual(individualURI); // BASE
for (Iterator<Resource> i = instance.listRDFTypes(false); i.hasNext();)
{
System.out
.println(instance.getURI() + " is asserted in class " + i.next());
}
}
protected void showInferred(OntModel m, String individualURI)
{
// list the inferred types
Individual instance = m.getIndividual(individualURI); // INFERED
for (Iterator<Resource> i = instance.listRDFTypes(false); i.hasNext();)
{
System.out.println(
instance.getURI() + " is inferred to be in class " + i.next());
}
}
protected void checkValidity(OntModel inf)
{
ValidityReport validity = inf.validate();
if (validity.isValid())
{
System.out.println("OK");
}
else
{
System.out.println("Conflicts");
for (Iterator i = validity.getReports(); i.hasNext();)
{
System.out.println(" - " + i.next());
}
}
}
}
To make the code runs on Eclipse or other, you'll need first to put the pizza ontologie file (pizza.owl.rdf) in a folder named "resources", and add these JAR (in Eclipse : Build Path -> Configure Build Path -> Add JARs) :
- commons-cli-1.3.jar
- commons-lang3-3.4.jar
- httpclient-4.5.2.jar
- httpclient-cache-4.5.2.jar
- httpcore-4.4.4.jar
- jackson-annotations-2.7.0.jar
- jackson-core-2.7.4.jar
- jackson-databind-2.7.4.jar
- jena-arq-3.3.0.jar
- jena-base-3.3.0.jar
- jena-core-3.3.0.jar
- jena-iri-3.3.0.jar
- jena-rdfconnection-3.3.0.jar
- jena-shaded-guava-3.3.0.jar
- jsonld-java-0.9.0.jar
- libthrift-0.9.3.jar
- log4j-1.2.17.jar
- slf4j-api-1.7.21.jar
- slf4j-log4j12-1.7.21.jar
- xercesImpl-2.11.0.jar
- xml-apis-1.4.01.jar
OntModel
, documentation on how to use inference is here: jena.apache.org/documentation/inference – UninformedUser