programming the semantic web with java - jazoon€¦ · l = m.createtypedliteral("jena, rdf,...

38
Programming the Semantic Web with Java Taylor Cowan Travelocity 8982

Upload: others

Post on 15-Jul-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

Programming the Semantic Web with Java

Taylor Cowan

Travelocity

8982

Page 2: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

2

AGENDA

> Semant ic Web Introduct ion

> RDF basics

> Coding Towards Jena’s Semantic Web Framework API

> Java to Model Binding with JenaBean

> Open Source tools for Java Developers

Page 3: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

3

State of the Semantic Web

> Consumers

– Search Monkey (Yahoo)

– Rich Snippets (Google)

> Producers

– UK Civil Service (http:/ / www.civilservice.gov.uk)

– BackstageBBC

– Geonames.org

– DBPedia

– FreeBase

– New York Times

Page 4: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

4

Page 5: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

5

Page 6: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

Why Not Microformats?6

<xsl:choose>

<xsl:when test="(false() = not((.//*[not(ancestor-or-self::*[local-name() = 'del']) = true() and contains(concat(' ',normalize-space(@class),' '),' fn ') and (local-name() = 'img' or local-name() = 'area')]/@alt) and (string-length(normalize-space(.//*[not(ancestor-or-self::*[local-name() = 'del']) = true() and contains(concat(' ',normalize-space(@class),' '),' fn ') and (local-name() = 'img' or local-name() = 'area')]/@alt)) = string-length(translate(normalize-space(.//*[not(ancestor-or-self::*[local-name() = 'del']) = true() and contains(concat(' ',normalize-space(@class),' '),' fn ') and (local-name() = 'img' or local-name() = 'area')]/@alt),' ',''))))) or (false() = not((.//*[not(ancestor-or-self::*[local-name() = 'del']) = true() and contains(concat(' ',normalize-space(@class),' '),' fn ') and (local-name() = 'abbr')]/@title) and (string-length(normalize-space(.//*[not(ancestor-or-self::*[local-name() = 'del']) = true() and contains(concat(' ',normalize-space(@class),' '),' fn ') and (local-name() = 'abbr')]/@title)) = string-length(translate(normalize-space(.//*[not(ancestor-or-self::*[local-name() = 'del']) = true() and contains(concat(' ',normalize-space(@class),' '),' fn ') and (local-name() = 'abbr')]/@title),' ',''))))) or (false() = not((.//*[not(ancestor-or-self::*[local-name() = 'del']) = true() and contains(concat(' ',normalize-space(@class),' '),' fn ') and not(local-name() = 'abbr' or local-name() = 'img')]) and (string-length(normalize-space(.//*[not(ancestor-or-self::*[local-name() = 'del']) = true() and contains(concat(' ',normalize-space(@class),' '),' fn ') and not(local-name() = 'abbr' or local-name() = 'img' or local-name() = 'area')][1])) = string-length(translate(normalize-space(.//*[not(ancestor-or-self::*[local-name() = 'del']) = true() and contains(concat(' ',normalize-space(@class),' '),' fn ') and not(local-name() = 'abbr' or local-name() = 'img')][1]),' ','')))))">

http://suda.co.uk/projects/microformats/hcard/xhtml2vcard.xsl

Page 7: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

7

Semantic Web Basics

> Everything is identif ied by a URI

> All data as canonical RDF

> RDFS provides a schema

> OWL provides addit ional meaning

> SPARQL queries semantic web data

> RDFa encodes RDF within XHTML

> We share vocabularies when possible (FOAF, SIOC, SKOS)

“Web content that is meaningful to computers”

Page 8: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

RDF Triple Store VS Relational DB

Triples1. Data Oriented

2. Semi- structured

3. Add, Remove

4. Everything is indexed

5. All relat ionships are many to many

RDBMS1. Schema Oriented

2. All data must f it schema

3. Insert, Update, Delete

4. Explicit indexing

5. Best with many to one

8

Page 9: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

Combining Datasets with OWL9

owl:sameAs

Page 10: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

Combining Datasets10

owl:sameAs

Page 11: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

RDF != XML

“The site at ht tp:/ / www.travelocity.com, also known as Travelocity, is an online travel agency competing with expedia.com”

11

Page 12: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

Concepts as a Directed Graph12

Page 13: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

13

Concepts Serialized as N3

:OnlineTravelAgency

a owl:Class .

:hasCompetitor

a rdf:Property .

<http://www.travelocity.com>

a :OnlineTravelAgency ;

rdfs:label "Travelocity"@en ;

:hasCompetitor <http://www.expedia.com> .

Page 14: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

14

Concepts Serialized as RDF/ XML

< rdf:RDF …>

  < owl:Class

rdf:about= "http:/ / foo#OnlineTravelAgency"/ >

  < rdf:Property rdf:about= "http:/ / foo#hasCompetitor"/ >

  < OnlineTravelAgency

rdf:about= "http:/ / www.travelocity.com">

    < hasCompetitor

rdf:resource= "http:/ / www.expedia.com"/ >

    < rdfs:label xml:lang= "en"> Travelocity< / rdfs:label>

  < / OnlineTravelAgency>

< / rdf:RDF>

Page 15: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

As N- Triples (Most Canonical or Normalized)

<hasCompetitor> <rdf:type> <rdf:Property> .

<http://travelocity.com> <hasCompetitor> <http://expedia.com>.

<http://travelocity.com> <rdfslabel> "Travelocity"@en

<http://travelocity.com> <rdf:type> <OnlineTravelAgency> .

<OnlineTravelAgency> <rdf:type> <owl:Class> .

15

Subject, Verb, Object = a triple

Page 16: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

As Java Code, using the Jena API

OntModel m = ModelFactory.createOntologyModel();

OntClass ota = m.createClass("OnlineTravelAgency");

Individual tvly =

ota.createIndividual("http://www.travelocity.com");

tvly.setLabel("Travelocity", "en");

OntProperty p = m.createOntProperty("hasCompetitor");

tvly.setPropertyValue(p,

m.createResource("http://www.expedia.com"));

16

Page 17: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

Creating a Model1: Model m = ModelFactory.createDefaultModel();

2: m.setNsPrefix("foaf", FOAF.NS);

3: Resource jazoon = m.createResource("http://jazoon.com/");

4: Resource java = m.createResource(

5: "http://dbpedia.org/resource/Java_(software_platform)");

6: jazoon.addProperty(FOAF.primaryTopic, java);

7: m.write(System.out, "N3");

17

<http://jazoon.com/> foaf:primaryTopic <http://dbpedia.org/resource/Java_(software_platform)> .

Page 18: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

Assertion18

jazoon.com java

foaf:primaryTopic

Page 19: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

Creating an Inferencing ModelOntModel infModel = ModelFactory.createOntologyModel(

OntModelSpec.OWL_MEM_MICRO_RULE_INF, m);

infModel.read("http://xmlns.com/foaf/spec/index.rdf");

infModel.writeAll(System.out, "N3",null);

19

<http://dbpedia.org/resource/Java_(software_platform)> foaf:isPrimaryTopicOf <http://jazoon.com/>

In addition to known data, a new triple is inferred…

Page 20: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

Knowledge after Inference20

jazoon.com

java

foaf:primaryTopic

foaf:isPrimaryTopicOf

foaf:Document

Is a

Page 21: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

21

The Semantic Web is Property focused

> Propert ies have Classes, not vice versa

> Don’t read “the domain of foaf:knows is a foaf:Person”, but instead “anything with foaf:knows relat ionship is a foaf:Person”

> Propert ies can extend other propert ies

> Propert ies can be declared as inverse, symmetric, and t ransit ive, all result ing in new inferences.

Page 22: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

List All Classes from an Ontology

OntModel model = ModelFactory.createOntologyModel();

model.read("http://xmlns.com/foaf/spec/index.rdf");

ExtendedIterator<OntClass> it = model.listClasses();

while(it.hasNext()) {

OntClass cls = it.next();

if (cls.getNameSpace().equals(FOAF.NS))

System.out.println(cls.getURI());

}

22

Page 23: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

Models Can be Populated from URL, Either Public or Local

23

Page 24: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

Models can be Populated from Other Models24

Page 25: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

25

Some example foaf:

<http://www.ibm.com/developerworks/xml/library/j-jena/> a

dc:Article ; dc:creator "Philip McCarthy"^^xsd:string ;

dc:subject "jena, rdf, java, semantic web"^^xsd:string ;

dc:title "Introduction to Jena"^^xsd:string .

Page 26: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

26

Equivalent Raw Jena API Client Code

String NS = "http://purl.org/dc/elements/1.1/";

OntModel m = ModelFactory.createOntologyModel();

OntClass articleCls = m.createClass(NS +"Article");

Individual i = articleCls.createIndividual(

"http://www.ibm.com/developerworks/xml/library/j-jena/");

Property title = m.getProperty(NS + "title");

Literal l = m.createTypedLiteral("Introduction to Jena");

i.setPropertyValue(title,l);

Property creator = m.getProperty(NS + "creator");

l = m.createTypedLiteral("Philip McCarthy");

i.setPropertyValue(creator,l);

Property subject = m.getProperty(NS + "subject");

l = m.createTypedLiteral("jena, rdf, java, semantic web");

i.setPropertyValue(subject,l);

m.write(System.out, "N3");

Page 27: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

27

Pain Points of Raw Jena API Programming

> You need to create unique URI’s for every entity.

> You must specify the type of each primit ive value.

> Propert ies must be created for each bean property.

Page 28: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

28

Creating The Same Assertions with JenaBean

Page 29: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

29

The JenaBean Project

> Hosted at Google code

> Bean binding, not code generat ion

> Doesn’t use byte code interweaving

> Doesn’t require implementing an interface

> http:/ / jenabean.googlecode.com

Page 30: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

30

Programming with JenaBean is Simple

> Bean2RDF writes objects

> RDF2Bean reads objects

> 3 Annotat ions

– @Id specif ies unique f ield

– @Namespace provides a domain

– @RdfProperty maps java propert ies to RDF propert ies

Page 31: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

The Simplest Possible Example31

<http://examples.model/Person> a <http://www.w3.org/2000/01/rdf-schema#Class> ; <http://thewebsemantic.com/javaclass> "examples.model.Person" .

<http://examples.model/Person/[email protected]> a <http://examples.model/Person> ; <http://examples.model/email> "[email protected]"^^xsd:string .

Page 32: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

Saving an Instance of Person

Model m = ModelFactory.createOntologyModel();

Bean2RDF writer = new Bean2RDF(m);

Person p = new Person();

p.setEmail("[email protected]");

writer.save(p);

m.write(System.out, "N3");

<http://example/Person> a owl:Class ;

<http://thewebsemantic.com/javaclass> "example.Person" .

<http://example/Person/[email protected]> a <http://example/Person> ;

<http://example/email> "[email protected]"^^xsd:string .

32

Page 33: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

Overriding the Default Namespace33

<http://mydomain#Person> a <http://www.w3.org/2000/01/rdf-schema#Class> ; <http://thewebsemantic.com/javaclass> "examples.model.Person" .

<http://mydomain#Person/[email protected]> a <http://mydomain#Person> ; <http://mydomain#email> "[email protected]"^^xsd:string .

Page 34: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

Overriding the Default Property Bindings34

<http://mydomain#Person> a <http://www.w3.org/2000/01/rdf-schema#Class> ; <http://thewebsemantic.com/javaclass> "examples.model.Person" .

<http://mydomain#Person/[email protected]> a <http://mydomain#Person> ; <http://xmlns.com/foaf/0.1/name> "Taylor Cowan"^^xsd:string .

Page 35: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

Extending Person to Support Friendship

public Collection<Person> friends = new

LinkedList<Person>();

@RdfProperty("http://xmlns.com/foaf/0.1/knows")

public Collection<Person> getFriends() { return friends;}

35

Page 36: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

Loading Beans from a Model

RDF2Bean reader = new RDF2Bean(m);

Person p = reader.load(Person.class,"[email protected]");

Collection<Person> allPeople = reader.load(Person.class);

36

Page 37: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

37

Open Source Tools For Java Devs

> Java Triple Stores

– Jena (HP Labs)

– Sesame OpenRDF (Aduna)

– Mulgara

> Java Binding tools

– JenaBean (Jena)

– Jastor (Jena)

– Owl2Java (Jena)

– Elmo (Sesame)

cc nickjohnson http:/ / flickr.com/ photots/ npj/

Page 38: Programming the Semantic Web with Java - Jazoon€¦ · l = m.createTypedLiteral("jena, rdf, java, semantic web"); i.setPropertyValue(subject,l); m.write(System.out, "N3"); 27 Pain

Taylor Cowanhttp:/ / thewebsemantic.com

http:/ / twit ter.com/ tcowan

[email protected]