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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| 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 A DUMMY INDIVIDUAL 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());
}
}
} |
Partager