Jena Examples Expert Family Tree 1162020 1 Expert

  • Slides: 42
Download presentation
Jena Examples Expert Family Tree 11/6/2020 1

Jena Examples Expert Family Tree 11/6/2020 1

Expert C: JenaTutorialexpert (expert folder in Tutorial. zip) 11/6/2020 2

Expert C: JenaTutorialexpert (expert folder in Tutorial. zip) 11/6/2020 2

Expert l Using Protege to build up expert. owl: l l Three classes: Expert,

Expert l Using Protege to build up expert. owl: l l Three classes: Expert, Research, Subject Properties: has. Research: expert can have many researches; research is associated with many subjects Create some instances. Output this ontology as expert. owl (for example: C: JenaTutorialexpert. owl) 11/6/2020 3

ontology. DB l Introduce some methods to handle store, read ontology in/from persistent database

ontology. DB l Introduce some methods to handle store, read ontology in/from persistent database (here takes My. SQL as example): l l 11/6/2020 connect. DB create. DBModel. From. File get. Model. From. DB get. Model. Spec 4

ontology. DB import java. util. *; com. hpl. jena. db. *; com. hpl. jena.

ontology. DB import java. util. *; com. hpl. jena. db. *; com. hpl. jena. ontology. *; com. hpl. jena. rdf. model. *; public class ontology. DB { /* link database */ public static IDBConnection connect. DB(String DB_URL, String DB_USER, String DB_PASSWD, String DB_NAME) { return new DBConnection(DB_URL, DB_USER, DB_PASSWD, DB_NAME); } /* Read ontology from filesystem and store it into database */ public static Ont. Model create. DBModel. From. File(IDBConnection con, String name, String file. Path) { Model. Maker maker = Model. Factory. create. Model. RDBMaker(con); Model base = maker. create. Model(name); Ont. Model newmodel = Model. Factory. create. Ontology. Model( get. Model. Spec(maker), base ); newmodel. read(file. Path); return newmodel; } 11/6/2020 5

ontology. DB /* Get ontology from database */ public static Ont. Model get. Model.

ontology. DB /* Get ontology from database */ public static Ont. Model get. Model. From. DB(IDBConnection con, String name) { Model. Maker maker = Model. Factory. create. Model. RDBMaker(con); Model base = maker. get. Model(name); Ont. Model newmodel = Model. Factory. create. Ontology. Model( get. Model. Spec(maker), base); return newmodel; } public static Ont. Model. Spec get. Model. Spec(Model. Maker maker) { Ont. Model. Spec spec = new Ont. Model. Spec(Ont. Model. Spec. OWL_MEM); spec. set. Import. Model. Maker(maker); return spec; } } C: JenaTutorialexpertontology. DB. java 11/6/2020 6

Main. java l l l Store expert. owl into My. SQL database, Read expert.

Main. java l l l Store expert. owl into My. SQL database, Read expert. owl from My. SQL database, List the classes of expert. owl 11/6/2020 7

import import java. io. *; java. util. *; com. hpl. jena. ontology. *; com.

import import java. io. *; java. util. *; com. hpl. jena. ontology. *; com. hpl. jena. rdf. model. Maker; com. hpl. jena. util. File. Manager; com. hpl. jena. util. *; com. hpl. jena. db. *; public class Main public static public static { final final String String C: JenaTutorialexpertMain. java DB_URL = "jdbc: mysql: //localhost/expert"; DB_USER = "root"; DB_PASSWD = "111"; DB = "My. SQL"; DB_DRIVER = "com. mysql. jdbc. Driver"; public static void main (String args[]) { try { Class. for. Name("com. mysql. jdbc. Driver"); } catch (Class. Not. Found. Exception e) { e. print. Stack. Trace(); } String file. Path = "file: ///C: /Jena/Tutorial/expert. owl"; IDBConnection con = ontology. DB. connect. DB(DB_URL, DB_USER, DB_PASSWD, DB); System. out. println(con); ontology. DB. create. DBModel. From. File(con, "expert", file. Path); Ont. Model model = ontology. DB. get. Model. From. DB(con, "expert"); for (Iterator i = model. list. Classes(); i. has. Next(); ) { Ont. Class c = (Ont. Class) i. next(); System. out. println(c. get. Local. Name()); } } } 11/6/2020 8

Expert l Open My. SQL l l Create expert database in My. SQL l

Expert l Open My. SQL l l Create expert database in My. SQL l l l Mysql>mysql –uroot –p 111 Mysql>create database expert; Parse ontology. DB. java and Main. java Run Main. java Notes: Make sure all classes are owl classes because OWL_MEM: <owl: Class rdf: ID="Research"/> <owl: Class rdf: ID="Expert"/> <owl: Class rdf: ID="Subject"/> 11/6/2020 9

Expert - Class l Main. java l l Not using method to output class

Expert - Class l Main. java l l Not using method to output class of the ontology Main 1. java l Using defined Simple. Read. Ontology method to output class of the ontology public static void Simple. Read. Ontology(Ont. Model model) { for (Iterator i = model. list. Classes(); i. has. Next(); ) { Ont. Class c = (Ont. Class) i. next(); System. out. println(c. get. Local. Name()); } } 11/6/2020 10

Expert - individual l Main. Individual. java l l C: JenaTutorialexpertMain. Individual. java, defined

Expert - individual l Main. Individual. java l l C: JenaTutorialexpertMain. Individual. java, defined prix is default namespace from expert. owl, using get. Instance method. But you have to compile ontology. DB. java, as all the Main* files are calling the methods defined in ontology. DB. class public static void get. Instance(Ont. Model model){ String prix = "http: //www. owl-ontologies. com/Expert. owl#"; /*get Expert class from the onotlogy*/ Ont. Class expert = model. get. Ont. Class(prix + "Expert"); //print out the name of the Expert class System. out. println(expert. get. Local. Name()); //get instances of the Expert class Iterator it = expert. list. Instances(); //print out the instances of the Expert class while(it. has. Next()){ Individual oi = (Individual)it. next(); System. out. println(oi. get. Local. Name()); } } 11/6/2020 11

Expert - property l Main. Property. java l l l 11/6/2020 C: JenaTutorialexpertMain. Property.

Expert - property l Main. Property. java l l l 11/6/2020 C: JenaTutorialexpertMain. Property. java Using get. Property method Compile ontology. DB. java first 12

Expert – get. Property method public static void get. Property(Ont. Model model) { String

Expert – get. Property method public static void get. Property(Ont. Model model) { String NS = "http: //www. owl-ontologies. com/Expert. owl#"; /* get the Expert class */ Ont. Class expert = model. get. Ont. Class(NS + "Expert"); // print out the name of the Expert class System. out. println(expert. get. Local. Name()); // get the instances of the Expert class Iterator it = expert. list. Instances(); // print out the instances of the Expert class while (it. has. Next()) { Individual oi = (Individual) it. next(); System. out. println(oi. get. Local. Name()); //get the properties of the instances of the Expert class for (Iterator ipp = expert. list. Declared. Properties(); ipp. has. Next(); ) { Ont. Property p = (Ont. Property) ipp. next(); //print out property name and its values System. out. println( p. get. Local. Name()); for (Iterator ivv = oi. list. Property. Values(p); ivv. has. Next(); ) { String valuename = ivv. next(). to. String(); System. out. println(valuename); } } 11/6/2020 } } 13

Query Expert l l Using SPARQL and Jena Reasoner Add familiar_with property (domain: Expert,

Query Expert l l Using SPARQL and Jena Reasoner Add familiar_with property (domain: Expert, range: Subject) to expert. owl. l l Query: list the experts and their familiar_with subjects. Setting up rules for the reasoner: l l There is no instance for this property. We will use reasoner to find its inferred instances Expert. A has. Research. B, Research. B is associated with Subject. C Expert. A is familiar_with Subject. C Using Sparql to query Expert. X (is familir_with) Subject. Y l l 11/6/2020 Select ? expert ? subject Where { ? expert faimilar_with ? subject } 14

Main. Query. java import import com. hpl. jena. ontology. *; com. hpl. jena. rdf.

Main. Query. java import import com. hpl. jena. ontology. *; com. hpl. jena. rdf. model. *; com. hpl. jena. util. *; com. hpl. jena. query. * ; com. hpl. jena. sparql. *; com. hp. hpl. jena. reasoner. rulesys. *; com. hpl. jena. db. *; public class Main. Query { public static final String DB_URL = "jdbc: mysql: //localhost/expert"; public static final String DB_USER = "root"; public static final String DB_PASSWD = "111"; public static final String DB = "My. SQL"; public static final String DB_DRIVER = "com. mysql. jdbc. Driver"; public static void main (String args[]) { try { Class. for. Name("com. mysql. jdbc. Driver"); } catch (Class. Not. Found. Exception e) { e. print. Stack. Trace(); } String file. Path = "file: ///C: /Jena/Tutorial/expert. owl"; IDBConnection con = ontology. DB. connect. DB(DB_URL, DB_USER, DB_PASSWD, DB); System. out. println(con); ontology. DB. create. DBModel. From. File(con, "expert", file. Path); Ont. Model model = ontology. DB. get. Model. From. DB(con, "expert"); 11/6/2020 search. Onto(model); } 15

Main. Query. java public static void search. Onto(Ont. Model model){ /*Setting up rules*/ String

Main. Query. java public static void search. Onto(Ont. Model model){ /*Setting up rules*/ String rule = "[rule 1: (? x http: //www. owl-ontologies. com/Expert. owl#has. Research ? y) " + "(? y http: //www. owl-ontologies. com/Expert. owl#associate ? z) " + "->(? x http: //www. owl-ontologies. com/Expert. owl#familiar_with ? z)]"; /*query String*/ String query. String = "PREFIX Expert: <http: //www. owl-ontologies. com/Expert. owl#> " + "SELECT ? expert ? subject " + "WHERE {? expert Expert: familiar_with ? subject} "; /*set up reasoner*/ Reasoner reasoner 2 = new Generic. Rule. Reasoner(Rule. parse. Rules(rule)); Inf. Model inf = Model. Factory. create. Inf. Model(reasoner 2, model); Query query = Query. Factory. create(query. String); Query. Execution qe = Query. Execution. Factory. create(query, inf); Result. Set results = qe. exec. Select(); /*output result*/ Result. Set. Formatter. out(System. out, results, query); qe. close(); } } C: C: JenaTutorialexpertMain. Query. java 11/6/2020 16

Query Expert l Main. Query. java l l l 11/6/2020 C: JenaTutorialexpertMain. Query. java

Query Expert l Main. Query. java l l l 11/6/2020 C: JenaTutorialexpertMain. Query. java Using search. Onto method Compile ontology. DB. java first 17

Family Tree C: JenaTutorialfamilytree (familytree folder under Tutorial. zip) 11/6/2020 18

Family Tree C: JenaTutorialfamilytree (familytree folder under Tutorial. zip) 11/6/2020 18

Family Tree l The example shows: l l l How to create and populate

Family Tree l The example shows: l l l How to create and populate RDF models How to persist them to a database, How to query them programmatically using SPARQL query language How to show Jena reasoning capabilities which can be used to infer knowledge about models from an ontology URL: http: //www 128. ibm. com/developerworks/xml/library/j-jena/ 11/6/2020 19

Creating a simple RDF model l Create a model from scratch and add RDF

Creating a simple RDF model l Create a model from scratch and add RDF statements to it. Create a model to represent the relationships in a family using different relationship types, such as sibling. Of, spouse. Of, parent. Of, child. Of (more details about relationship ontology: http: //vocab. org/relationship/) Define family members using URIs from a made-up namespace: http: //family/. It is useful to declare them as Java constants. 11/6/2020 20

Family Tree … adam + dotty edward beth + chuck fan + greg harriet

Family Tree … adam + dotty edward beth + chuck fan + greg harriet 11/6/2020 21

Creating a simple RDF model import java. util. *; java. io. *; com. hpl.

Creating a simple RDF model import java. util. *; java. io. *; com. hpl. jena. rdf. model. *; com. hpl. jena. util. File. Manager; //A small family tree held in a Jena Model public class Family. Model { // Namespace declarations static final String family. Uri = "http: //family/"; static final String relationship. Uri = "http: //purl. org/vocab/relationship/"; public static void main(String args[]) { // Create an empty Model model = Model. Factory. create. Default. Model(); . . . //set namespace Resource NAMESPACE = model. create. Resource( relationship. Uri ); model. set. Ns. Prefix( "rela", relationship. Uri); // Create the types of Property we need to describe relationships in the model Property child. Of = model. create. Property(relationship. Uri, "child. Of"); . . . // Create resources representing the people in our model Resource adam = model. create. Resource(family. Uri+"adam"); . . . 11/6/2020 22

Creating a simple RDF model // Add properties to describing the relationships between them

Creating a simple RDF model // Add properties to describing the relationships between them adam. add. Property(sibling. Of, beth); . . . // Statements can also be directly created. . . Statement statement 1 = model. create. Statement(edward, child. Of, adam); . . . //. . . then added to the model: model. add(statement 1); . . . // Arrays of Statements can also be added to a Model: Statement statements[] = new Statement[5]; statements[0] = model. create. Statement(fran, child. Of, adam); . . . // A List of Statements can also be added List list = new Array. List(); . . . C: JenaTutorialfamilytree Family. Model. java list. add(model. create. Statement(greg, spouse. Of, fran)); . . . model. add(list); model. write(System. out, "RDF/XML-ABBREV"); } } 11/6/2020 23

Creating a simple RDF model l l Store Family. Model. j ava in C:

Creating a simple RDF model l l Store Family. Model. j ava in C: JenaTutori alfamilytree Compile and Run 11/6/2020 24

Store this RDF model in file l l Store the RDF model in to

Store this RDF model in file l l Store the RDF model in to C: JenaTutorialfamilytreefamily. rdf Add following codes to Family. Model. java try{ File file=new File("C: \Jena\Tutorial\familytree\family. rdf"); File. Output. Stream f 1=new File. Output. Stream(file); RDFWriter d = model. get. Writer("RDF/XML-ABBREV"); d. write(model, f 1, null); }catch(Exception e) {} C: JenaTutorialfamilytreeFamily. Model 01. java 11/6/2020 25

Query family tree - list. Statement l Query : Show me who has which-kind-of

Query family tree - list. Statement l Query : Show me who has which-kind-of relation with whom. l import list. Statement (Subject s, Property p, RDFNode o) com. hpl. jena. rdf. model. *; com. hpl. jena. util. File. Manager; com. hpl. jena. vocabulary. *; java. io. *; C: JenaTutorialfamilytree Family. Query. java public class Family. Query { static final String input. File. Name = "family. rdf"; public static void main (String args[]) { // create an empty model Model model = Model. Factory. create. Default. Model(); // use the File. Manager to find the input file Input. Stream in = File. Manager. get(). open(input. File. Name); if (in == null) { throw new Illegal. Argument. Exception( "File: " + input. File. Name + " not found"); } model. read( in, ""); //query the statement: subject, property and object Stmt. Iterator iter = model. list. Statements(null, (RDFNode) null); if (iter. has. Next()) {System. out. println("They are: "); while (iter. has. Next()) {System. out. println(" " + iter. next. Statement()); } } else {System. out. println("They are not in the database"); } } 11/6/2020 26 }

Run Family. Query. java 11/6/2020 27

Run Family. Query. java 11/6/2020 27

Query family tree - list. Statement l Query 01: show me who are the

Query family tree - list. Statement l Query 01: show me who are the parent of whom l l 11/6/2020 list. Statements(null, model. get. Property("http: //purl. org/vocab/relationship/parent Of"), (RDFNode) null) C: JenaTutorialfamilytreeFamily. Query 01. java 28

Query family tree - list. Statement l Query 02: who are parent of edward

Query family tree - list. Statement l Query 02: who are parent of edward l model. list. Statements(model. get. Resource("http: //family/edward"), model. get. Property("http: //purl. org/vocab/relationship/child. Of"), (RDFNode) null) l C: JenaTutorialfamilytreeFamily. Query 02. java 11/6/2020 29

Query family tree - Sparql l Find grandparent? import import com. hpl. jena. rdf.

Query family tree - Sparql l Find grandparent? import import com. hpl. jena. rdf. model. *; com. hpl. jena. util. File. Manager; com. hpl. jena. query. * ; com. hpl. jena. sparql. *; java. io. *; public class Family. Query 03 { static final String input. File. Name = "family. rdf"; public static void main (String args[]) { // create an empty model Model model = Model. Factory. create. Default. Model(); // use the File. Manager to find the input file Input. Stream in = File. Manager. get(). open(input. File. Name); if (in == null) { throw new Illegal. Argument. Exception( "File: " + input. File. Name + " not found"); } model. read( in, ""); 11/6/2020 30

Query family tree - Sparql String query. String = "PREFIX rela: <http: //purl. org/vocab/relationship/>

Query family tree - Sparql String query. String = "PREFIX rela: <http: //purl. org/vocab/relationship/> " + "SELECT ? person ? grandparent " + "WHERE {" + " ? grandparent rela: parent. Of ? y. " + " ? y rela: parent. Of ? person. " + " }"; Query query = Query. Factory. create(query. String); // Execute the query and obtain results Query. Execution qe = Query. Execution. Factory. create(query, model); Result. Set results = qe. exec. Select(); C: JenaTutorialfamilytreeFamily. Query 03. java // Output query results Result. Set. Formatter. out(System. out, results, query); // Important - free up resources used running the query qe. close(); } } 11/6/2020 31

Query family tree - Sparql l l Who is uncle of harriet? C: JenaTutorialfamilytreeFamily.

Query family tree - Sparql l l Who is uncle of harriet? C: JenaTutorialfamilytreeFamily. Query 04. java String query. String = "PREFIX rela: <http: //purl. org/vocab/relationship/> " + "SELECT ? uncleoraunt " + "WHERE {" + " <http: //family/harriet> rela: child. Of ? x. " + " ? x rela: sibling. Of ? uncleoraunt. " + " }"; 11/6/2020 32

Reasoning family tree l import import Who are niece or nephew of edwar? java.

Reasoning family tree l import import Who are niece or nephew of edwar? java. io. *; java. util. Iterator; com. hpl. jena. util. *; com. hpl. jena. rdf. model. *; com. hp. hpl. jena. reasoner. rulesys. *; C: JenaTutorialfamilytree Family. Reason. java public class Family. Reason { private static String fname = "family. rdf"; private static String NS = "http: //family/"; public static void main(String args[]) { Model raw. Data = File. Manager. get(). load. Model(fname); String rules = "[r 1: (? x http: //purl. org/vocab/relationship/sibling. Of ? y), (? y http: //purl. org/vocab/relationship/parent. Of ? z)" + "-> (? x uncleoraunt. Of ? z)]"; Reasoner reasoner = new Generic. Rule. Reasoner(Rule. parse. Rules(rules)); Inf. Model inf = Model. Factory. create. Inf. Model(reasoner, raw. Data); Resource A = inf. get. Resource(NS + "edward"); System. out. println("A * * =>"); Iterator list = inf. list. Statements(A, null, (RDFNode)null); while (list. has. Next()) { System. out. println(" - " + list. next()); } 11/6/2020 } } 33

Reasoning family tree 11/6/2020 34

Reasoning family tree 11/6/2020 34

Reasoning family tree l l l Using Family. Model 02. java to delete some

Reasoning family tree l l l Using Family. Model 02. java to delete some statements (sibling. Of, child. Of, spouse. Of, …) and store it in N-Triple in family 1. nt. Who are the children of dotty? Using generic rule reasoning Using sparql to query C: JenaTutorialfamilytreeFamily. Reason 01. j ava 11/6/2020 35

Family. Reason 01. java import import java. io. *; java. util. Iterator; com. hpl.

Family. Reason 01. java import import java. io. *; java. util. Iterator; com. hpl. jena. util. *; com. hpl. jena. rdf. model. *; com. hp. hpl. jena. reasoner. rulesys. *; com. hpl. jena. query. * ; com. hpl. jena. sparql. *; public class Family. Reason 01 { private static String fname = "family 1. nt"; public static void main(String args[]) { Model raw. Data = File. Manager. get(). load. Model(fname); //setting up rules String rules = "[r 1: (? x http: //purl. org/vocab/relationship/parent. Of ? y), (? x http: //purl. org/vocab/relationship/spouse. Of ? z)" + "-> (? z http: //purl. org/vocab/relationship/parent. Of ? y)]"; 11/6/2020 36

Family. Reason 01. java C: JenaTutorialfamilytree /*query String*/ Family. Reason 01. java String query.

Family. Reason 01. java C: JenaTutorialfamilytree /*query String*/ Family. Reason 01. java String query. String = "SELECT ? dottychild " + "WHERE { <http: //family/dotty> <http: //purl. org/vocab/relationship/parent. Of> ? dottychild} "; Reasoner reasoner = new Generic. Rule. Reasoner(Rule. parse. Rules(rules)); Inf. Model inf = Model. Factory. create. Inf. Model(reasoner, raw. Data); Query query = Query. Factory. create(query. String); Query. Execution qe = Query. Execution. Factory. create(query, inf); Result. Set results = qe. exec. Select(); /*output result*/ Result. Set. Formatter. out(System. out, results, query); qe. close(); } } 11/6/2020 37

Reasoning family tree l l Using multiple rules to do complex reasoning. Dataset: C:

Reasoning family tree l l Using multiple rules to do complex reasoning. Dataset: C: JenaTutorialfamilytreefamily 2. nt <http: //family/harriet> <http: //purl. org/vocab/relationship/child. Of> <http: //family/fran> <http: //purl. org/vocab/relationship/spouse. Of> <http: //family/greg>. <http: //family/fran> <http: //purl. org/vocab/relationship/child. Of> <http: //family/adam> <http: //purl. org/vocab/relationship/spouse. Of> <http: //family/dotty>. <http: //family/adam> <http: //purl. org/vocab/relationship/sibling. Of> <http: //family/beth> <http: //purl. org/vocab/relationship/spouse. Of> <http: //family/chuck>. <http: //family/edward> <http: //purl. org/vocab/relationship/sibling. Of> <http: //family/fran>. <http: //family/edward> <http: //purl. org/vocab/relationship/child. Of> <http: //family/adam>. 11/6/2020 38

Family. Reason 02. java l Multiple rules: l l R 1: ? x parent.

Family. Reason 02. java l Multiple rules: l l R 1: ? x parent. Of ? y, ? y parent. Of ? z ->? x grandparent. Of ? z R 2: ? x parent. Of ? y, ? x spouse. Of ? z ->? z parent. Of ? y R 3: ? x child. Of ? y -> ? y parent. Of ? x Query: who can be grandparents? 11/6/2020 39

Family. Reason 02. java import import java. io. *; java. util. Iterator; com. hpl.

Family. Reason 02. java import import java. io. *; java. util. Iterator; com. hpl. jena. util. *; com. hpl. jena. rdf. model. *; com. hp. hpl. jena. reasoner. rulesys. *; com. hpl. jena. query. * ; com. hpl. jena. sparql. *; public class Family. Reason 02 { private static String fname = "family 2. nt"; public static void main(String args[]) { Model raw. Data = File. Manager. get(). load. Model(fname); //setting up rules String rules = "[r 1: (? x http: //purl. org/vocab/relationship/parent. Of ? y), (? y http: //purl. org/vocab/relationship/parent. Of ? z)" + "-> (? x grandparent. Of ? z)]" + "[r 2: (? x http: //purl. org/vocab/relationship/parent. Of ? y), (? x http: //purl. org/vocab/relationship/spouse. Of ? z)" + "-> (? z http: //purl. org/vocab/relationship/parent. Of ? y)]" + "[r 3: (? x http: //purl. org/vocab/relationship/child. Of ? y)" + "-> (? y http: //purl. org/vocab/relationship/parent. Of ? x)]"; 11/6/2020 40

Family. Reason 02. java /*query String*/ String query. String = "SELECT ? grandparent "

Family. Reason 02. java /*query String*/ String query. String = "SELECT ? grandparent " + "WHERE { ? grandparent <http: //purl. org/vocab/relationship/parent. Of> ? x. " + " ? x <http: //purl. org/vocab/relationship/parent. Of> ? y. } "; Reasoner reasoner = new Generic. Rule. Reasoner(Rule. parse. Rules(rules)); Inf. Model inf = Model. Factory. create. Inf. Model(reasoner, raw. Data); Query query = Query. Factory. create(query. String); Query. Execution qe = Query. Execution. Factory. create(query, inf); Result. Set results = qe. exec. Select(); /*output result*/ Result. Set. Formatter. out(System. out, results, query); qe. close(); } C: JenaTutorialfamilytree Family. Reason 02. java } 11/6/2020 41

Summary l l Practicing these examples Be able to create your own examples 11/6/2020

Summary l l Practicing these examples Be able to create your own examples 11/6/2020 42