Hi Ian and all!
Thank you for the link! It has helped me fill in some of the "gaps" in my
code. However, I'm still having trouble with addStatements() method as well
as checkValidity() method (commented) below. I have a
NullPointerException error when calling either or both methods and I don't
know where that comes from. Can you or anyone help me understand what I'm
doing wrong in both methods and fix it?
Error from addStatements()....
Exception in thread "main" java.lang.NullPointerException
[java] at
com.hp.hpl.jena.rdf.model.impl.StatementImpl.<init>(StatementImpl.java:40)
[java] at
com.hp.hpl.jena.rdf.model.impl.ModelCom.createStatement(ModelCom.java:1360)
[java] at
demo.TestWineOntologyOWL.addStatements(TestWineOntologyOWL.java:184)
[java] at demo.TestWineOntologyOWL.main(TestWineOntologyOWL.java:290)
Error from checkValidity()....
==============================================.....Validating the model for
inconsistencies...==============================================
[java]
[java] Exception in thread "main" java.lang.NullPointerException
[java] at
demo.TestWineOntologyOWL.checkValidity(TestWineOntologyOWL.java:100)
[java] at demo.TestWineOntologyOWL.main(TestWineOntologyOWL.java:281)
The complete code....
package demo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.List;
import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.ontology.DatatypeProperty;
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.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.InfModel;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.ResIterator;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.reasoner.Reasoner;
import com.hp.hpl.jena.reasoner.ReasonerRegistry;
import com.hp.hpl.jena.reasoner.ValidityReport;
import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner;
import com.hp.hpl.jena.reasoner.rulesys.Rule;
import com.hp.hpl.jena.shared.DoesNotExistException;
import com.hp.hpl.jena.util.FileManager;
import com.hp.hpl.jena.util.PrintUtil;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.VCARD;
import com.hp.hpl.jena.vocabulary.XSD;
// Create an instance of the OWL reasoner, specialized to the demo schema
// and then apply that to the demo data to obtain an inference model.
public class TestWineOntologyOWL {
//Recall Base URI and namespace declarations for additional entities to be
added to the ontology
static final String baseURI1 = "
http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine";
static final String ns1 = "
http://www.w3.org/TR/2003/PR-owl-guide-20031209/wine#";
static final String baseURI2 = "
http://www.w3.org/TR/2003/PR-owl-guide-20031209/food";
static final String ns2 = "
http://www.w3.org/TR/2003/PR-owl-guide-20031209/food#";
// additional entities to be added to the loaded ontology
private OntClass DrinkableThing;
private DatatypeProperty hasDateTimeOfMake, hasDuration; // new one to
be added
private Individual wineA, wineB;
//Entities to be extracted from the loaded ontology
private ObjectProperty hasDrink;
private DatatypeProperty hasYearValue ;
// Define the new model
//private Model model;
//String ontURL = "src/demo/ont/wine.owl";
private OntModel modelWine;
String ontURL = "src/demo/ont/wine.owl";
// constructor
public TestWineOntologyOWL() {
modelWine = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);
modelWine.read(ontURL);
}
/*
// ============================================================
// Check The validity of the loaded/infered model ...
// ============================================================
public void checkValidity() {
System.out.println("");
System.out.printf("==============================================");
System.out.printf(".....Validating the model for inconsistencies...");
System.out.printf("==============================================");
System.out.println("");
System.out.println("");
ValidityReport validity = modelWine.validate();
if (validity.isValid()) {
System.out.println("Infered model: OK");
} else {
System.out.println("Infered model: Conflicts!!");
for (Iterator i = validity.getReports(); i.hasNext(); ) {
ValidityReport.Report report = (ValidityReport.Report)i.next();
System.out.println(" - " + report);
}
}
}
*/
// ============================================================
// Create and add Classes ...
// ============================================================
public void addClasses() {
System.out.println("");
System.out.printf("=======================================");
System.out.printf(".....Adding new CLASSES to the model...");
System.out.printf("=======================================");
System.out.println("");
// create new class : DrinkableThing
DrinkableThing= modelWine.createClass( ns1 + "DrinkableThing");
// (Re)define RELATIONSHIP among classes ...
modelWine.getOntClass(ns2 + "ConsumableThing").addSubClass (
DrinkableThing );
}
// ============================================================
// Create and add properties ...
// ============================================================
public void addProperties() {
System.out.println("");
System.out.printf("==========================================");
System.out.printf(".....Extracting/Adding new PROPERTIES to the model..."
);
System.out.printf("==========================================");
System.out.println("");
// Extract existing properties in the loaded ontology as variables....
hasYearValue = modelWine.getDatatypeProperty(ns2 + "yearValue");
hasDrink = modelWine.getObjectProperty(ns2 + "hasDrink");
// add DatatypeProperty "hasDateTimeOfMake"
hasDateTimeOfMake = modelWine.createDatatypeProperty(ns2 +
"hasDateTimeOfMake");
hasDateTimeOfMake.addDomain(modelWine.getOntClass(ns2 + "ConsumableThing"
));
hasDateTimeOfMake.addRange(XSD.dateTime);
// add DatatypeProperty "hasDuration"
hasDuration = modelWine.createDatatypeProperty(ns2 + "hasDuration");
hasDuration.addDomain(modelWine.getOntClass(ns2 + "ConsumableThing"));
hasDuration.addRange(XSD.duration);
}
// ============================================================
// Create and add individuals ...
// ============================================================
public void addIndividuals() {
System.out.println("");
System.out.printf("===========================================");
System.out.printf(".....Extracting/Adding new INDIVIDUALS to the
model...");
System.out.printf("===========================================");
System.out.println("");
// Create and Add an individual as instance of an existing class
(ConsumableThing) in the loaded ontology : wineA ...
wineA = modelWine.getOntClass(ns2 + "ConsumableThing"
).createIndividual(ns2 + "wineA");
// Create and Add an individual as instance of a newly created class
: wineB ...
wineB = DrinkableThing.createIndividual(ns2 + "wineB");
}
// ============================================================
// Create and add Statements ...
// ============================================================
public void addStatements() {
// Statement with property from the loaded ontology (yearValue)
Literal yearA = modelWine.createTypedLiteral("2003", XSDDatatype.
XSDpositiveInteger);
Statement yearMakeWA = modelWine.createStatement(wineA, modelWine
.getDatatypeProperty(ns2 + "yearValue"), yearA );
modelWine.add ( yearMakeWA );
// Statement with property (yearValue) and Individual from the loaded
ontology ()
Statement yearMake = modelWine.createStatement(modelWine.getIndividual(
ns1 + "ChateauMargaux"), modelWine.getDatatypeProperty(ns2 + "yearValue"),
yearA );
modelWine.add ( yearMake );
// Statement with an added property (hasDateTimeOfMake) and literal
(timeWB)
Literal timeWB = modelWine.createTypedLiteral("2003-02-28T09:00:00Z",
XSDDatatype.XSDdateTime );
Statement timeMakeWB = modelWine.createStatement(wineB,
hasDateTimeOfMake, timeWB );
modelWine.add ( timeMakeWB );
// Statement with an added property (hasDuration) and literal
(duration10y)
Literal duration10y = modelWine.createTypedLiteral("P10YT5H10S",
XSDDatatype.XSDduration );
Statement durationWB = modelWine.createStatement(wineB, hasDuration,
duration10y);
modelWine.add ( durationWB );
}
// ============================================================
// Retrieve and print VCARD ....
// ============================================================
public void printVCARDS( String sTitle ) {
System.out.printf("List of statements: %s\n", sTitle );
System.out.printf(
"=============================================================\n");
// select all the resources with a VCARD.FN property
ResIterator iter = modelWine.listResourcesWithProperty(VCARD.FN);
if (iter.hasNext()) {
System.out.println("The database contains vcards for:");
while (iter.hasNext()) {
System.out.println(" " + iter.nextResource()
.getRequiredProperty(VCARD.FN)
.getString() );
}
} else {
System.out.println("No vcards were found in the database");
}
}
// ============================================================
// Retrieve and print the RDF statements ....
// ============================================================
public void printStatements( String sTitle ) {
int statementNo = 1;
System.out.printf("List of statements: %s\n", sTitle );
System.out.printf(
"=============================================================\n");
StmtIterator iter = modelWine.listStatements();
while (iter.hasNext()) {
Statement stmt = iter.next(); // get next statement
Resource subject = stmt.getSubject(); // get the subject
Property predicate = stmt.getPredicate(); // get the predicate
RDFNode object = stmt.getObject(); // get the object
// Objects can be either Resources or Literals ....
System.out.printf(" Statement[%4d]\n", statementNo );
System.out.printf(" Subject : %s \n", subject.toString());
System.out.printf(" Predicate: %s \n", predicate.toString());
if (object instanceof Resource) {
System.out.printf(" Object : %s \n", object.toString());
}
if (object instanceof Literal) {
System.out.printf(" Object : \"%s\" \n", object.toString());
}
statementNo = statementNo + 1;
}
System.out.println(
"=============================================================");
}
public static void main(String[] args) {
System.out.println("Step 1: Load the Wine Ontology ....
");
System.out.println(
"===================================================");
TestWineOntologyOWL wine = new TestWineOntologyOWL();
System.out.println("");
System.out.printf("Step 2: Check the validity and Print size of the
Wine Ontology ...\n");
System.out.println(
"==================================================================");
System.out.println("");
// wine.checkValidity();
System.out.println("");
System.out.println("Initial Wine ontology model size = " + wine.
modelWine.size());
System.out.println("");
wine.addClasses();
wine.addProperties();
wine.addIndividuals();
wine.addStatements();
System.out.println("");
System.out.println("Wine ontology model size after extension = " +
wine.modelWine.size());
System.out.println("");
System.out.println("");
System.out.printf("Step 3: Print details of the Wine Ontology ...\n");
System.out.println("===============================================");
System.out.println("");
wine.modelWine.write(System.out, "RDF/XML" );
System.out.println("");
System.out.printf("Step 4: Print statements in the Wine Ontology
...\n");
System.out.println("===============================================");
System.out.println("");
wine.printStatements( "Wine Ontology" );
System.out.println("");
System.out.println("===============================================");
System.out.printf("Step 5: Print VCARD in the Wine Ontology ...\n");
System.out.println("===============================================");
System.out.println("");
wine.printVCARDS( "VCARDs in Wine Ontology" );
System.out.println("");
System.out.println("===============================================");
}
}
2013/4/25 Ian Dickinson <[email protected]>
> Hi Léonard,
>
> On 25/04/13 02:17, Léonard PETNGA wrote:
>
>> Hi Ian,
>> From your answer above it looks like Jena does allow to create
>> individuals, delete/add classes, statements and properties to the loaded
>> owl ontology model
>>
> Yes. Have you read:
>
> http://jena.apache.org/**documentation/ontology/index.**html<http://jena.apache.org/documentation/ontology/index.html>
>
>
> directly from the java file. I guess the question is how? I would like to
>> focus our exchange back to a real case to illustrate part of my problem
>> using the excerpt below. I've loaded the wine ontology (.owl) in an empty
>> model and would like to write code to enrich/modify that ontology (see
>> the
>> various methods listed). Do you or anyone have example code to help fill
>> in
>> the gaps?
>>
>
> I'm sorry, but how can we fill in a specification like this:
>
>
> // ==============================**==============================
>> // Create and add individuals ...
>> // ==============================**==============================
>> public void addIndividuals() {
>> //????
>> }
>>
>
> That provides no information at all about what the code should do. That
> spec says "write a method that takes no arguments, returns no value, and
> does something". I agree that focusing on a real case would be helpful, but
> this isn't that.
>
> Ian
>
>