58
Developing Smart Java™ Code with Semantic Web Technology Holger Knublauch TopQuadrant, Inc. [email protected]

Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

  • Upload
    others

  • View
    26

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Developing Smart Java™ Code with Semantic Web TechnologyHolger KnublauchTopQuadrant, [email protected]

Page 2: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Semantic Web Elevator Pitch

2

Application-specific code

Generic code

Conferencemodel

JavaOnemodel

Geographymodel

DBpediamodelLinked

SmartData

SmartCode

Page 3: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Overview of this talk> Introduction to RDF

Resources, literals and triples> Linked Data> RDF Schema

Classes, properties, and instances> Java APIs and Tools> Data Binding and software architecture> SPARQL

Queries and rules> Model-driven applications

3

Page 4: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Semantic Web Technology> Infrastructure to link data on the Web> Global database> Borrows some ideas of object-oriented languages> Data is self-describing> W3C standards

RDF RDF Schema and OWL SPARQL RDFa, GRDDL

4

Page 5: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

RDF: Resources> Semantic Web entities have a unique identifier

5

http://examples.topquadrant.com/conferences/javaone#ScottMcNealy

Namespace Local name

abbreviated as

javaone:ScottMcNealy

Page 6: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

RDF: Triples

6

Subject Predicate Object

javaone:ScottMcNealy conf:employer javaone:Sun_Microsystems

Page 7: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

RDF: Graphs

7

Subject Predicate Object

javaone:ScottMcNealy conf:employer javaone:Sun_Microsystems

javaone:John_Rose conf:employer javaone:Sun_Microsystems

javaone:John_Rose conf:presenterOf javaone:JSR_292_Cookbook

Page 8: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

RDF: Datatype property values

8

Subject Predicate Object

javaone:ScottMcNealy conf:firstName “Scott”^^xsd:string

javaone:ScottMcNealy conf:lastName “McNealy”^^xsd:string

javaone:ScottMcNealy rdfs:label “Scott McNealy”

Page 9: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Linked Data

9

Page 10: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Linked Data: Wikipedia

10

Page 11: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Linked Data: DBpedia Browser

11

Page 12: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Linked Data: more RDF triples, for free!

12

Page 14: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Linked Data: Geography ontology

14

Page 15: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

RDF Schema: Type relationships

15

Subject Predicate Object

javaone:ScottMcNealy rdf:type conf:Attendee

Page 16: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

RDF Schema: Subclass relationships

16

Subject Predicate Object

javaone:ScottMcNealy rdf:type conf:Attendee

conf:Attendee rdfs:subClassOf conf:Person

Page 17: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

RDF Schema: Class Hierarchy

17

Page 18: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

RDF Schema: Property Domains

18

Page 19: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

RDF Schema: Property Ranges

19

Page 20: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

RDF Schema: from a UML point of view

20

Page 21: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

RDF Schema: linking namespaces

21

Page 22: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

General Java APIs for RDF> Jena

http://jena.sourceforge.net/> Sesame

http://www.openrdf.org/

22

Page 23: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Jena API: Core interfaces (simplified)

23

Resourceuri:String

RDFNode

Literal

lexicalForm:Stringdatatype:RDFDatatype

Statement Model

subject

object

Property

predicate

*

Page 24: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Jena API: “Hello, World”

24

// Create a Jena Model and init some namespace prefixesString NS = "http://examples.topquadrant.com/conferences/conf#";Model model = ModelFactory.createDefaultModel();model.setNsPrefix("conf", NS);model.setNsPrefix("rdfs", RDFS.getURI());model.setNsPrefix("xsd", XSD.getURI());

// Create an example tripleResource javaOne = model.createResource(NS + "JavaOne2009");Literal label = model.createTypedLiteral("JavaOne 2009");model.add(javaOne, RDFS.label, label);

// Write the Model in N3 format to the screenmodel.write(System.out, FileUtils.langN3);

Page 25: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Jena API: “Hello, World” Output

25

RDF file in N3 formatOptions include RDF/XML

@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .@prefix conf: <http://examples.topquadrant.com/conferences/conf#> .

conf:JavaOne2009 rdfs:label "JavaOne 2009"^^xsd:string .

Page 26: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Tools: OWL ontology editor Protege

26

Page 27: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Tools: TopBraid Composer

27

Page 28: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Development Methodology> Define/re-use schema> Play with example instances, queries etc> Take a data-centric approach!> Link key classes of the schema to your Java code

(Data Binding)> Assume schema evolution> Develop smart, generic code instead of domain-

specific code

28

Page 29: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Data Binding: Generated schema class

29

Page 30: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Data Binding: Basic Architecture

30

Resource

RDFNode

Literal

Person

getFirstName()getFullName()...

Gen

eric

API

Cus

tom

API

Generic Code(SPARQL etc)

SpecificCode

Page 31: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Data Binding: Example use

31

Model model = ModelFactory.createDefaultModel();

// Create a new Person instanceString steveURI = CONF.NS + "SteveJobs";Person steve = ConfFactory.createPerson(model, steveURI);

// Set some property valuessteve.setFirstName("Steve");steve.setLastName("Jobs");

// Query some property valuesString fullName = steve.getFullName();System.out.println("Person: " + fullName);

Page 32: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Data Binding: Example interface

32

public interface Person extends Resource {

String getFirstName();

String getFullName();

String getLastName();

void setFirstName(String value);

void setLastName(String value);}

Resource

Person

getFirstName()getFullName()...

Page 33: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Data Binding: Example implementation

33

public class PersonImpl extends ResourceImpl implements Person {

public PersonImpl(Node node, EnhGraph eg) { super(node, eg); }

public String getFirstName() { return getStringProperty(CONF.firstName); }

public String getFullName() { return getFirstName() + " " + getLastName(); }

Page 34: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Data Binding: Factory class

34

public class ConfFactory { static { register(Person.class, PersonImpl.class); } public static Person createPerson(Model model, String uri) { return model.createResource(uri, CONF.Person).as(Person.class); } public static Person getPerson(Model model, String uri) { return model.createResource(uri).as(Person.class); }...

Page 35: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Data Binding: Frameworks> ELMO

http://www.openrdf.org/doc/elmo/1.4/> Sommer

https://sommer.dev.java.net/sommer/index.html> JenaBean

http://code.google.com/p/jenabean/ http://www.incunabulum.de/projects/it/owl2java/

> Kazuki http://projects.semwebcentral.org/projects/kazuki/

35

Page 36: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Data Binding: Java code generators> RDF Reactor

http://semanticweb.org/wiki/RDFReactor> ELMO

http://www.openrdf.org/doc/elmo/1.4/> Jastor

http://jastor.sourceforge.net/> OWL2Java

36

Page 37: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Intermediate Summary> Data model in RDF and RDF Schema> Data binding to link RDF and Java worlds

> Next: let the data work for us with SPARQL

37

Page 38: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

SPARQL: Example Query

38

Page 39: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

SPARQL: Query results as subgraphs

39

Page 40: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

SPARQL: Example 2

40

?company

?person

?presentation

conf:employer

conf:presenterOf

?label

rdfs:label

Page 41: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

SPARQL: Example 3

41

Page 42: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Jena API: SPARQL

42

Model m = ModelFactory.createDefaultModel();m.read(new FileReader("conf.n3"), null, FileUtils.langN3);m.read(new FileReader("javaOne.rdf"), null);String queryString = "PREFIX " + CONF.PREFIX + ": <" + CONF.NS + "> \n" + "SELECT ?person ?company \n" + "WHERE { \n" + " ?person conf:employer ?company . \n" + "}";Query query = QueryFactory.create(queryString);QueryExecution qx = QueryExecutionFactory.create(query, m);

Page 43: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Jena API: SPARQL (cont’d)

43

ResultSet rs = qx.execSelect();while(rs.hasNext()) { QuerySolution s = rs.nextSolution(); Resource person = (Resource) s.get("person"); Resource company = (Resource) s.get("company"); System.out.println(person.getLocalName() + "\t" + company.getLocalName());}

Page 44: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

SPARQL: Querying the schema

44

“Which properties arerelevant for a givenresource?”

Page 45: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

SPARQL: Model-driven forms

45

Page 46: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

SPARQL: Model-driven applications

46

Page 47: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

SPARQL: CONSTRUCT rule

47

Page 48: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

SPARQL: generalized rule

48

Page 49: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Rules: Extending the core schema

49

Adding domain-specific properties;the application’s code knows nothingabout them at compile-time.

Page 50: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Rules: Conference Sponsorship

50

“Companies that are co-sponsors get 5 free conference passes”

Condition(triple pattern in the graph)

Action(triples to add/infer)

Page 51: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Rules: SPARQL Inferencing Notation

51

Page 52: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Example Application (Unit Conversion)

52

Page 53: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Example Application (Computer Game)

53

composing-the-semantic-web.blogspot.com

Page 54: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Summary> New development paradigm based on Linked Data> Smart code walks the data graph at run-time> Very little hard-coding of behavior> Benefits

flexible architecture faster turn-around times

> Challenges learning curve (this is not OO!) integration with established technologies

54

Page 55: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

55

Holger [email protected]

http://www.topquadrant.com

Page 56: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

56

Backup Slides

Page 57: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

RDF Databases> AllegroGraph> Jena (RDB, SDB, TDB)> Mulgara> Oracle 11g> Sesame

57

Page 58: Developing Smart Java™ Code with Semantic Web Technology · Semantic Web Technology > Infrastructure to link data on the Web > Global database > Borrows some ideas of object-oriented

Inferencing APIs> Often integrated into RDF stores:

Jena Rules Sesame Oracle 11g RDF

> OWLIM (Rule engine with RDFS/OWL support) http://ontotext.com/owlim/

> Pellet (OWL DL) http://clarkparsia.com/pellet/

> TopSPIN (Rules via SPARQL) http://spinrdf.org/api

58