Apache Jena Part II Jan Pettersen Nytun, UiA 1 S O P This - - PowerPoint PPT Presentation

apache jena
SMART_READER_LITE
LIVE PREVIEW

Apache Jena Part II Jan Pettersen Nytun, UiA 1 S O P This - - PowerPoint PPT Presentation

Knowledge Representation Apache Jena Part II Jan Pettersen Nytun, UiA 1 S O P This presentation is based on: Jena Ontology API http://jena.apache.org/documentation/ontology/ Jan Pettersen Nytun, UIA, page 2 Jena is fundamentally an


slide-1
SLIDE 1

1

Knowledge Representation Part II

Jan Pettersen Nytun, UiA

Apache Jena

slide-2
SLIDE 2

S O

P

This presentation is based on: Jena Ontology API

http://jena.apache.org/documentation/ontology/

Jan Pettersen Nytun, UIA, page 2

slide-3
SLIDE 3

… Jena is fundamentally an RDF platform, … limited to ontology formalisms built on top of RDF. … this means RDFS, the varieties of OWL.

3

slide-4
SLIDE 4

The OWL language is sub-divided into three syntax classes: OWL Lite, OWL DL and OWL Full.

4

[Wikipedia]:

  • Every legal OWL Lite ontology is a legal

OWL DL ontology.

  • Every legal OWL DL ontology is a legal

OWL Full ontology.

slide-5
SLIDE 5

5

[Wikipedia]:

OWL Lite … intended to support those

users primarily needing a classification hierarchy and simple constraints … Development of OWL Lite tools has … proven almost as difficult as development

  • f tools for OWL DL, and OWL Lite is not

widely used.

slide-6
SLIDE 6

6

[Wikipedia]:

OWL DL is designed to provide the maximum

expressiveness possible while retaining computational completeness, decidability, and the availability of practical reasoning algorithms. OWL DL includes all OWL language constructs, but they can be used only under certain restrictions …

slide-7
SLIDE 7

S O

P

[http://philosophy.stackexchange.com/questions/6992/ the-difference-between-soundness-and-completeness]

  • Soundness is the property of only being able to prove

"true" things.

  • Completeness is the property of being able to

prove all true things.

  • So a given logical system is sound if and only if the

inference rules of the system admit only valid formulas. Or another way, if we start with valid premises, the inference rules do not allow an invalid conclusion to be drawn.

  • A system is complete if and only if all valid formula can

be derived from the axioms and the inference rules. So there are no valid formula that we can't prove.

  • Together they imply that all and only validities are

provable.

Jan Pettersen Nytun, UIA, page 7

slide-8
SLIDE 8

S O

P

Decidability

The logically valid formulas of a system are sometimes called the theorems of the system, … A logical system is decidable if there is an effective method for determining whether arbitrary formulas are theorems of the logical system.

Jan Pettersen Nytun, UIA, page 8

[Wikipedia]:

slide-9
SLIDE 9

S O

P

Gödel's incompleteness theorem

Recursively axiomatizable first-order theories that are rich enough to allow general mathematical reasoning to be formulated cannot be complete, as demonstrated by Gödel's incompleteness theorem. … For any such system, there will always be statements about the natural numbers that are true, but that are unprovable within the system.

Jan Pettersen Nytun, UIA, page 9

[Wikipedia]:

slide-10
SLIDE 10

10

[Wikipedia]:

OWL Full[edit] … a class can be treated simultaneously as a collection of individuals and as an individual in its own right ... OWL Full allows an ontology to augment the meaning of the pre-defined (RDF or OWL) vocabulary. OWL Full is undecidable, so no reasoning software is able to perform complete reasoning for it.

slide-11
SLIDE 11

S O

P

Jena Graph Interface

Graph is an internal Jena interface that supports the composition of sets of RDF triples. The asserted statements, which may have been read in from an ontology document, are held in the base graph.

Jan Pettersen Nytun, UIA, page 11

slide-12
SLIDE 12

S O

P

The reasoner, or inference engine, can use the contents

  • f the base graph and the

semantic rules of the language to show a more complete set

  • f base and entailed triples.

This is also presented via a Graph interface, so the OntModel works only with the

  • utermost interface.

Jan Pettersen Nytun, UIA, page 12

slide-13
SLIDE 13

S O

P

The base graph can be an in- memory store, a database- backed persistent store, or some other storage structure altogether …

Jan Pettersen Nytun, UIA, page 13

slide-14
SLIDE 14

S O

P

Creating Ontology Models

OntModel m = ModelFactory.createOntologyModel();

This will create an ontology model with the default settings:

  • OWL-Full language
  • in-memory storage
  • RDFS inference, which principally produces

entailments from the sub-class and sub- property hierarchies.

Jan Pettersen Nytun, UIA, page 14

slide-15
SLIDE 15

S O

P

In many applications, such as driving a GUI, RDFS inference is too strong. An OWL model that performs no reasoning at all can be created with:

OntModel m = ModelFactory.createOntologyModel( OntModelSpec.OWL_MEM );

Jan Pettersen Nytun, UIA, page 15

slide-16
SLIDE 16

S O

P

Jan Pettersen Nytun, UIA, page 16

slide-17
SLIDE 17

S O

P

Testing Reasoning in Jena

Input Ontology

@prefix : <http://www.uia.no/janpettersennytun/inference#> . @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . @prefix owl: <http://www.w3.org/2002/07/owl#> . @prefix xml: <http://www.w3.org/XML/1998/namespace> . @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

:hasApprovedId a owl:DatatypeProperty ; rdfs:subPropertyOf :hasSomeId . :hasSomeId a owl:DatatypeProperty . :hasHusband a owl:ObjectProperty ; owl:inverseOf :hasWife . :hasWife a owl:ObjectProperty . :Man a owl:Class ; rdfs:subClassOf :Human . :Woman a owl:Class ; rdfs:subClassOf :Human . :Human a owl:Class . :Homer a owl:NamedIndividual , :Man ; :hasSomeId "Homer_ID1_SomeID" . :Marge a owl:NamedIndividual , :Woman ; :hasApprovedId "Marge_ID1_Approved" ; :hasHusband :Homer .

Jan Pettersen Nytun, UIA, page 17

slide-18
SLIDE 18

S O

P

Testing Reasoning in Jena

Application Heading

package testreasoners; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.apache.jena.ontology.OntModel; import org.apache.jena.ontology.OntModelSpec; import org.apache.jena.rdf.model.Model; import org.apache.jena.rdf.model.ModelFactory; import org.apache.jena.rdf.model.Property; import org.apache.jena.rdf.model.RDFNode; import org.apache.jena.rdf.model.Resource; import org.apache.jena.rdf.model.Statement; import org.apache.jena.rdf.model.StmtIterator; import org.apache.jena.util.FileManager;

Jan Pettersen Nytun, UIA, page 18

slide-19
SLIDE 19

S O

P

public class TestReasoners { private static String inputFileName = "InferenceTest__InputFile.ttl"; private static String outputFileName = "InferenceTest__OutputFile.ttl"; public static void main(String[] args){ … } public static List<String> getSortedTriples(Model model){ … } public static OntModel readFile(OntModelSpec ontModelSpec){ … } }

Jan Pettersen Nytun, UIA, page 19

Testing Reasoning in Jena

Method Overview

slide-20
SLIDE 20

S O

P

public static OntModel readFile(OntModelSpec ontModelSpec){ OntModel ontModel = ModelFactory.createOntologyModel(ontModelSpec); InputStream in = FileManager.get().open( inputFileName ); if (in == null) { System.out.println("File: " + inputFileName + " not found"); System.exit(1); } // read the Turtle file

  • ntModel.read(in, null, "Turtle");

//ontModel.write(System.out, "Turtle"); return ontModel; }

Jan Pettersen Nytun, UIA, page 20

Testing Reasoning in Jena

readFile - Method

slide-21
SLIDE 21

S O

P

public static List<String> getSortedTriples(Model model){ List<String> triples = new ArrayList<String>(); StmtIterator iter = model.listStatements(); String format = "%-20s"; String defaultNamespace = model.getNsPrefixMap().get(""); if (defaultNamespace == null) return triples;

Jan Pettersen Nytun, UIA, page 21

Testing Reasoning in Jena

getSortedTriples- Method

slide-22
SLIDE 22

while (iter.hasNext()) { Statement stmt = iter.nextStatement(); // get next statement Resource subject = stmt.getSubject(); // get the subject String namespace = subject.getNameSpace(); if (namespace == null || !namespace.equals(defaultNamespace)) continue; Property predicate = stmt.getPredicate(); // get the predicate RDFNode object = stmt.getObject(); // get the object String tripleAsString = String.format(format, subject.getLocalName().toString()); tripleAsString = tripleAsString + String.format(format, predicate.getLocalName().toString()); if (object instanceof Resource) { tripleAsString = tripleAsString + object.asResource().getLocalName().toString() + " ."; } else { // object is a literal tripleAsString = tripleAsString + " \"" + object.toString() + "\"" + " ."; } triples.add(tripleAsString); } Collections.sort(triples); return triples; }

Jan Pettersen Nytun, UIA, page 22

slide-23
SLIDE 23

S O

P

public static void main(String[] args){ OntModel model_OWL_DL_MEM = readFile(OntModelSpec.OWL_DL_MEM); List<String> triples_OWL_DL_MEM = getSortedTriples(model_OWL_DL_MEM); System.out.println("Triple found in model_OWL_DL_MEM"); System.out.println("............................................................................."); for (String oneTriple : triples_OWL_DL_MEM) System.out.println(oneTriple); System.out.println(".............................................................................\n"); model_OWL_DL_MEM.close(); …

Jan Pettersen Nytun, UIA, page 23

slide-24
SLIDE 24

Triple found in model_OWL_DL_MEM ................................................................ Homer hasSomeId "Homer_ID1_SomeID" . Homer type Man . Homer type NamedIndividual . Human type Class . Man subClassOf Human . Man type Class . Marge hasApprovedId "Marge_ID1_Approved" . Marge hasHusband Homer . Marge type NamedIndividual . Marge type Woman . Woman subClassOf Human . Woman type Class . hasApprovedId subPropertyOf hasSomeId . hasApprovedId type DatatypeProperty . hasHusband inverseOf hasWife . hasHusband type ObjectProperty . hasSomeId type DatatypeProperty . hasWife type ObjectProperty . ................................................................

slide-25
SLIDE 25

OntModel model_OWL_DL_MEM_RDFS_INF = readFile(OntModelSpec.OWL_DL_MEM_RDFS_INF); List<String> triples_OWL_DL_MEM_RDFS_INF = getSortedTriples(model_OWL_DL_MEM_RDFS_INF); System.out.println("Triple found in model_OWL_DL_MEM_RDFS_INF"); System.out.println("............................................................................."); for (String oneTriple : triples_OWL_DL_MEM_RDFS_INF) { System.out.println(oneTriple); } System.out.println(".............................................................................\n"); model_OWL_DL_MEM_RDFS_INF.close();

Jan Pettersen Nytun, UIA, page 25

slide-26
SLIDE 26

Triple found in model_OWL_DL_MEM_RDFS_INF ................................................................. Homer hasSomeId "Homer_ID1_SomeID" . Homer type Human . Homer type Man . Homer type NamedIndividual . Human subClassOf Human . Human type Class . Human type Class . Human type Resource . Man subClassOf Human . Man subClassOf Man . Man type Class . Man type Class . Man type Resource . Marge hasApprovedId "Marge_ID1_Approved" . Marge hasHusband Homer . Marge hasSomeId "Marge_ID1_Approved" . Marge type Human . Marge type NamedIndividual . Marge type Woman .

slide-27
SLIDE 27

Woman subClassOf Human . Woman subClassOf Woman . Woman type Class . Woman type Class . Woman type Resource . hasApprovedId subPropertyOf hasApprovedId . hasApprovedId subPropertyOf hasSomeId . hasApprovedId type DatatypeProperty . hasApprovedId type Property . hasApprovedId type Resource . hasHusband inverseOf hasWife . hasHusband type ObjectProperty . hasSomeId subPropertyOf hasSomeId . hasSomeId type DatatypeProperty . hasSomeId type Property . hasSomeId type Resource . hasWife type ObjectProperty . .........................................................

Jan Pettersen Nytun, UIA, page 27

slide-28
SLIDE 28

OntModel model_OWL_DL_MEM_RULE_INF = readFile(OntModelSpec.OWL_DL_MEM_RULE_INF); List<String> triples_OWL_DL_MEM_RULE_INF = getSortedTriples(model_OWL_DL_MEM_RULE_INF); System.out.println("Triple found in model_OWL_DL_MEM_RULE_INF"); System.out.println("............................................................................."); for (String oneTriple : triples_OWL_DL_MEM_RULE_INF) System.out.println(oneTriple); System.out.println(".............................................................................\n"); try { OutputStream outFile = new FileOutputStream(outputFileName); model_OWL_DL_MEM_RULE_INF.write(outFile,"Turtle"); } catch (FileNotFoundException e) { e.printStackTrace(); } model_OWL_DL_MEM_RULE_INF.close(); }

Jan Pettersen Nytun, UIA, page 28

slide-29
SLIDE 29

Triple found in model_OWL_DL_MEM_RULE_INF .................................................... Homer hasSomeId "Homer_ID1_SomeID" . Homer hasWife Marge . Homer sameAs Homer . Homer type Human . Homer type Man . Homer type NamedIndividual . Homer type Resource . Homer type Thing . Human equivalentClass Human . Human subClassOf Human . Human subClassOf Resource . Human subClassOf Thing . Human type Class . Human type Class . Human type Resource .

Jan Pettersen Nytun, UIA, page 29

slide-30
SLIDE 30

Man equivalentClass Man . Man subClassOf Human . Man subClassOf Man . Man subClassOf Resource . Man subClassOf Thing . Man type Class . Man type Class . Man type Resource . Marge hasApprovedId "Marge_ID1_Approved" . Marge hasHusband Homer . Marge hasSomeId "Marge_ID1_Approved" . Marge sameAs Marge . Marge type Human . Marge type NamedIndividual . Marge type Resource . Marge type Thing . Marge type Woman .

Jan Pettersen Nytun, UIA, page 30

slide-31
SLIDE 31

Woman equivalentClass Woman . Woman subClassOf Human . Woman subClassOf Resource . Woman subClassOf Thing . Woman subClassOf Woman . Woman type Class . Woman type Class . Woman type Resource . hasApprovedId subPropertyOf hasApprovedId . hasApprovedId subPropertyOf hasSomeId . hasApprovedId type DatatypeProperty . hasApprovedId type Property . hasApprovedId type Resource .

Jan Pettersen Nytun, UIA, page 31

slide-32
SLIDE 32

hasHusband domain Resource . hasHusband domain Thing . hasHusband inverseOf hasWife . hasHusband range Resource . hasHusband range Thing . hasHusband subPropertyOf hasHusband . hasHusband type ObjectProperty . hasHusband type Property . hasHusband type Resource . hasSomeId subPropertyOf hasSomeId . hasSomeId type DatatypeProperty . hasSomeId type Property . hasSomeId type Resource .

Jan Pettersen Nytun, UIA, page 32

slide-33
SLIDE 33

hasWife domain Resource . hasWife domain Thing . hasWife inverseOf hasHusband . hasWife range Resource . hasWife range Thing . hasWife subPropertyOf hasWife . hasWife type ObjectProperty . hasWife type Property . hasWife type Resource . ...................................................

Jan Pettersen Nytun, UIA, page 33