Download presentation
Presentation is loading. Please wait.
Published byRoland Greene Modified over 8 years ago
1
1 Knowledge Representation XII – IKT437 Knowledge Representation XII – IKT437 Part III Div Jan Pettersen Nytun, UiA Apache Jena
2
S O P Example 1 Jan Pettersen Nytun, UIA, page 2
3
S O P Ancestor An ancestor is a parent or recursively the parent of an ancestor (i.e., any person from whom one is descending). In mathematical terms: The ancestral relation is transitive. Jan Pettersen Nytun, UIA, page 3 Human1 Human2Human3 Human4Human5 hasAncestor E.g.: Human3 has ancestors: Human2 and Human1
4
S O P package ancestor; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.List; import org.apache.jena.ontology.Individual; import org.apache.jena.ontology.ObjectProperty; import org.apache.jena.ontology.OntClass; 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; public class AncesterApp { private static String defaultNamespace = "http://uia.no/ancestor#"; public static void main(String[] args){…} private static void printTriplesInDefaultNamespace(OntModel ontModel) {…} public static List getSortedTriples(Model model) {…} } Jan Pettersen Nytun, UIA, page 4 Overview
5
public static void main(String[] args){ … OntModel ancesterModel = ModelFactory.createOntologyModel( OntModelSpec.OWL_DL_MEM_RULE_INF); ancesterModel.setNsPrefix("", defaultNamespace); System.out.println("OntModel created."); System.out.println("Language profile: OWL DL"); System.out.println("Storage model: in-memory"); System.out.println("Reasoner: rule-based reasoner with OWL rules"); System.out.println(“..................................................................\n"); … Jan Pettersen Nytun, UIA, page 5 OntModel created. Language profile: OWL DL Storage model: in-memory Reasoner: rule-based reasoner with OWL rules............................................................................. Output:
6
S O P public static void main(String[] args){ … OntClass humanClass = ancesterModel. createClass (defaultNamespace+"Human"); System.out.println("Class Human created:"); printTriplesInDefaultNamespace(ancesterModel); … Jan Pettersen Nytun, UIA, page 6 Class Human created:............................................................................. Human equivalentClass Human. Human subClassOf Human. Human subClassOf Resource. Human subClassOf Thing. Human type Class. Human type Resource.
7
Individual human1 = ancesterModel.createIndividual( defaultNamespace + "Human1", humanClass ); Individual human2 = ancesterModel.createIndividual( defaultNamespace + "Human2", humanClass ); Individual human3 = ancesterModel.createIndividual( defaultNamespace + "Human3", humanClass ); Individual human4 = ancesterModel.createIndividual( defaultNamespace + "Human4", humanClass ); Individual human5 = ancesterModel.createIndividual( defaultNamespace + "Human5", humanClass ); System.out.println("5 human created:"); printTriplesInDefaultNamespace(ancesterModel); Jan Pettersen Nytun, UIA, page 7 5 human created:............................................................................. Human equivalentClass Human. … Human1 sameAs Human1. Human1 type Human. Human1 type Resource. Human1 type Thing. Human2 sameAs Human2. …
8
ObjectProperty hasAncesterObjectProperty = ancesterModel.createTransitiveProperty( defaultNamespace + "hasAncestor", false); System.out.println("Transitive property hasAncestor created:"); printTriplesInDefaultNamespace(ancesterModel); Jan Pettersen Nytun, UIA, page 8 Transitive property hasAncestor created: … hasAncestor subPropertyOf hasAncestor. hasAncestor type ObjectProperty. hasAncestor type Property. hasAncestor type Resource. hasAncestor type TransitiveProperty.............................................................................. boolean functional
9
human3.setPropertyValue(hasAncesterObjectProperty, human2); human2.setPropertyValue(hasAncesterObjectProperty, human1); human5.setPropertyValue(hasAncesterObjectProperty, human4); human4.setPropertyValue(hasAncesterObjectProperty, human1); System.out.println("Two ancester cains has been created:"); System.out.println(":Human3 :hasAncestor :Human2. :Human2 :hasAncestor :Human1"); System.out.println(":Human5 :hasAncestor :Human4. :Human4 :hasAncestor :Human1"); System.out.println("After adding the transitive properties:"); printTriplesInDefaultNamespace(ancesterModel); Jan Pettersen Nytun, UIA, page 9 Two ancestor cains have been created: :Human3 :hasAncestor :Human2. :Human2 :hasAncestor :Human1 :Human5 :hasAncestor :Human4. :Human4 :hasAncestor :Human1 After adding the transitive properties: … Human2 hasAncestor Human1. … Human3 hasAncestor Human1. Human3 hasAncestor Human2. …
10
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String cmd; while (true) { System.out.println("\nList of all humans:"); System.out.println(" 1: Human1"); System.out.println(" 2: Human2"); System.out.println(" 3: Human3"); System.out.println(" 4: Human4"); System.out.println(" 5: Human5"); System.out.println("\nGet ancestors of which human (give a number; 0 gives exit):"); try { cmd = in.readLine(); int cmdAsNumber = Integer.parseInt(cmd); if (cmdAsNumber 5) break; Jan Pettersen Nytun, UIA, page 10
11
// decide URI for selected human String humanLocalName = "Human" + cmdAsNumber; String humanURI = defaultNamespace + humanLocalName; Resource oneHuman = ancesterModel.getResource(humanURI); StmtIterator iterAncestors = oneHuman.listProperties(hasAncesterObjectProperty); if (!iterAncestors.hasNext()){ System.out.println("\nHuman: " + humanLocalName + " has not registred any ancestors."); } else { System.out.println("\nHuman: " + humanLocalName + " has the following ancestors:"); while (iterAncestors.hasNext()) { System.out.println(" " + iterAncestors.nextStatement().getObject().asResource().getLocalName()); } } catch (IOException e) { e.printStackTrace(); break; } Jan Pettersen Nytun, UIA, page 11
12
Jan Pettersen Nytun, UIA, page 12 List of all humans: 1: Human1 2: Human2 3: Human3 4: Human4 5: Human5 Get ancestors of which human (give a number; 0 gives exit): 1 Human: Human1 has not registered any ancestors. List of all humans: 1: Human1 2: Human2 3: Human3 4: Human4 5: Human5 Get ancestors of which human (give a number; 0 gives exit): 2 Human: Human2 has the following ancestors: Human1
13
Jan Pettersen Nytun, UIA, page 13 List of all humans: 1: Human1 2: Human2 3: Human3 4: Human4 5: Human5 Get ancestors of which human (give a number; 0 gives exit): 3 Human: Human3 has the following ancestors: Human2 Human1
14
S O P private static void printTriplesInDefaultNamespace(OntModel ontModel) { List triples = getSortedTriples(ontModel); System.out.println("............................................................................."); for (String oneTriple : triples) System.out.println(oneTriple); System.out.println(".............................................................................\n"); } Jan Pettersen Nytun, UIA, page 14
15
S O P public static List getSortedTriples(Model model){ List triples = new ArrayList (); StmtIterator iter = model.listStatements(); String format = "%-40s"; String defaultNamespace = model.getNsPrefixMap().get(""); if (defaultNamespace == null) return triples; while (iter.hasNext()) { String tripleAsString = ""; Statement stmt = iter.nextStatement(); // get next statement Resource subject = stmt.getSubject(); // get the subject Property predicate = stmt.getPredicate(); // get the predicate RDFNode object = stmt.getObject(); // get the object if (subject.asNode().isBlank()) { tripleAsString = String.format(format, "_:" + subject.asNode().getBlankNodeId()); } else { String namespace = subject.getNameSpace(); if (!namespace.equals(defaultNamespace)) continue; tripleAsString = String.format(format, subject.getLocalName().toString()); } tripleAsString = tripleAsString + String.format(format, predicate.getLocalName().toString()); if (object instanceof Resource) { if (object.asNode().isBlank()) { tripleAsString = tripleAsString + "_:" + object.asNode().getBlankNodeId() + "."; } else { 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 15 Rest of code
16
S O P Example 2 Jan Pettersen Nytun, UIA, page 16
17
S O P Extending The Ancestor Application … OntModel ancesterModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM_RULE_INF); … OntClass humanClass = ancesterModel.createClass(defaultNamespace+ "Human"); … DatatypeProperty hasName = ancesterModel.createDatatypeProperty(defaultNamespace + "hasName"); DatatypeProperty hasAddress = ancesterModel.createDatatypeProperty(defaultNamespace + "hasAddress"); … Jan Pettersen Nytun, UIA, page 17
18
… createHuman( ancesterModel, defaultNamespace, "Bob Dylan", "California", defaultNamespace + "HumanBob“ ); createHuman( ancesterModel, defaultNamespace, "Tom", "Norway", defaultNamespace + "HumanTom“ ); String address = getAddress( ancesterModel, defaultNamespace, "Bob Dylan" ); updateAddress( ancesterModel, defaultNamespace, "Bob Dylan", "Grimstad" ); … Jan Pettersen Nytun, UIA, page 18
19
public static void createHuman(OntModel ontModel, String namespace, // same namespace is used for person and class Human String personName, String address, String personUri ) { String queryRequest = "PREFIX rdfs: " + " PREFIX ns: " + " " + " INSERT DATA " + "{" + " " + " rdfs:type " + "ns:Human ;" + "ns:hasName " + "\"" + personName +"\"" + " ;"+ "ns:hasAddress " + "\"" + address +"\"."+ "}"; System.out.println("queryRequest:" + queryRequest); UpdateAction.parseExecute(queryRequest, ontModel.getGraph()); } Jan Pettersen Nytun, UIA, page 19 Output Example, queryRequest: PREFIX rdfs: PREFIX ns: INSERT DATA { rdfs:type ns:Human ; ns:hasName "Bob Dylan" ; ns:hasAddress "California".}
20
public static String getAddress( OntModel ontModel, String namespace, String personName ) { String address = ""; String personUri = ""; String queryRequest = "PREFIX rdfs: " + " PREFIX ns: " + " " + " SELECT ?address ?personUri " + "WHERE {" + "?personUri " + " rdfs:type " + "ns:Human ;" + "ns:hasName " + "\"" + personName +"\"" + " ; " + "ns:hasAddress " + "?address."+ "}"; … Jan Pettersen Nytun, UIA, page 20 PREFIX rdfs: PREFIX ns: INSERT DATA { rdfs:type ns:Human ; ns:hasName "Tom" ; ns:hasAddress "Norway".}
21
Query query; QueryExecution qexec; try { query = QueryFactory.create(queryRequest); qexec = QueryExecutionFactory.create(query, ontModel); } catch(Exception e){ System.out.println(e); return null; } //Run Select ResultSet response = null; try { response = qexec.execSelect(); Jan Pettersen Nytun, UIA, page 21
22
while( response.hasNext()){ // OBS. Only interested in the first result… QuerySolution QuerySolution = response.nextSolution(); RDFNode addressNode = QuerySolution.get("?address"); if( addressNode != null ){ address = addressNode.toString(); } RDFNode personUriNode = QuerySolution.get("?personUri"); // This only to show how //to handle several variables if( personUriNode != null ){ personUri = personUriNode.toString(); break; } } finally {qexec.close();} return address; } Jan Pettersen Nytun, UIA, page 22
23
public static void updateAddress( OntModel ontModel, String namespace, String personName, String newAddress ) { String queryRequest = "PREFIX rdfs: " + "PREFIX ns: " + " " + "DELETE { ?personUri " + "ns:hasAddress ?address } " + "INSERT { ?personUri ns:hasAddress " + "\"" + newAddress + "\"} " + "WHERE { ?personUri ns:hasName " + "\"" + personName + "\"} "; UpdateAction.parseExecute(queryRequest, ontModel.getGraph()); } Jan Pettersen Nytun, UIA, page 23
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.