Programming the Semantic Web with Java Taylor Cowan Travelocity 8982
AGENDA 2 > 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
State of the Semantic Web 3 > Consumers Search Monkey (Yahoo) Rich Snippets (Google) > Producers UK Civil Service (http:/ / www.civilservice.gov.uk) BackstageBBC Geonames.org DBPedia FreeBase New York Times
4
5
Why Not Microformats? <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-orself::*[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(' ',normalizespace(@class),' '),' fn ') and (local-name() = 'abbr')]/@title) and (string-length(normalizespace(.//*[not(ancestor-or-self::*[local-name() = 'del']) = true() and contains(concat(' ',normalizespace(@class),' '),' fn ') and (local-name() = 'abbr')]/@title)) = string-length(translate(normalizespace(.//*[not(ancestor-or-self::*[local-name() = 'del']) = true() and contains(concat(' ',normalizespace(@class),' '),' fn ') and (local-name() = 'abbr')]/@title),' ',''))))) or (false() = not((.//*[not(ancestor-or-self::*[local-name() = 'del']) = true() and contains(concat(' ',normalizespace(@class),' '),' fn ') and not(local-name() = 'abbr' or local-name() = 'img')]) and (stringlength(normalize-space(.//*[not(ancestor-or-self::*[local-name() = 'del']) = true() and contains(concat(' ',normalize-space(@class),' '),' fn ') and not(local-name() = 'abbr' or localname() = 'img' or local-name() = 'area')][1])) = string-length(translate(normalizespace(.//*[not(ancestor-or-self::*[local-name() = 'del']) = true() and contains(concat(' ',normalizespace(@class),' '),' fn ') and not(local-name() = 'abbr' or local-name() = 'img')][1]),' ','')))))"> 6 http://suda.co.uk/projects/microformats/hcard/xhtml2vcard.xsl
Semantic Web Basics 7 > Everything is identified by a URI > All data as canonical RDF > RDFS provides a schema > OWL provides additional 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
RDF Triple Store VS Relational DB 8 Triples 1. Data Oriented 2. Semi- structured 3. Add, Remove 4. Everything is indexed 5. All relationships are many to many RDBMS 1. Schema Oriented 2. All data must fit schema 3. Insert, Update, Delete 4. Explicit indexing 5. Best with many to one
Combining Datasets with OWL 9 owl:sameas
Combining Datasets 10 owl:sameas
RDF!= XML 11 The site at http:/ / www.travelocity.com, also known as Travelocity, is an online travel agency competing with expedia.com
Concepts as a Directed Graph 12
Concepts Serialized as N3 13 :OnlineTravelAgency a owl:class. :hascompetitor a rdf:property. <http://www.travelocity.com> a :OnlineTravelAgency ; rdfs:label "Travelocity"@en ; :hascompetitor <http://www.expedia.com>.
< / OnlineTravelAgency> Concepts Serialized as RDF/ XML 14 < 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 x ml:lang= "en"> Travelocity< / rdfs:label>
As N- Triples (Most Canonical or Normalized) 15 <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>. Subject, Verb, Object = a triple
As Java Code, using the Jena API 16 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"));
Creating a Model 17 1: 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"); <http://jazoon.com/> foaf:primarytopic <http://dbpedia.org/resource/java_(software_platform)>.
Assertion 18 foaf:primarytopic jazoon.co m java
Creating an Inferencing Model 19 OntModel 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); In addition to known data, a new triple is inferred <http://dbpedia.org/resource/java_(software_platform)> foaf:isprimarytopicof <http://jazoon.com/>
Knowledge after Inference 20 foaf:document Is a foaf:primarytopic jazoon.com java foaf:isprimarytopicof
The Semantic Web is Property focused 21 > Properties have Classes, not vice versa > Don t read the domain of foaf:knows is a foaf:person, but inst ead anyt hing wit h foaf:knows relat ionship is a foaf:person > Properties can ex tend other properties > Properties can be declared as inverse, symmetric, and transitive, all resulting in new inferences.
List All Classes from an Ontology 22 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()); }
Models Can be Populated from URL, Either Public or Local 23
Models can be Populated from Other Models 24
Some example foaf: 25 <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.
Equivalent Raw Jena API Client Code 26 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");
Pain Points of Raw Jena API Programming 27 > You need to create unique URI s for every entity. > You must specify the type of each primitive value. > Properties must be created for each bean property.
28 Creating The Same Assertions with JenaBean
The JenaBean Project 29 > Hosted at Google code > Bean binding, not code generation > Doesn t use byte code int erweaving > Doesn t require implementing an interface > http:/ / jenabean.googlecode.com
Programming with JenaBean is Simple 30 > Bean2RDF writes object s > RDF2Bean reads object s > 3 Annotations @Id specifies unique field @Namespace provides a domain @RdfProperty maps java properties to RDF properties
The Simplest Possible Example 31 <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/thewebsemantic@gmail.com> a <http://examples.model/person> ; <http://examples.model/email> "thewebsemantic@gmail.com"^^xsd:string.
Saving an Instance of Person 32 Model m = ModelFactory.createOntologyModel(); Bean2RDF writer = new Bean2RDF(m); Person p = new Person(); p.setemail("person@example.com"); writer.save(p); m.write(system.out, "N3"); <http://example/person> a owl:class ; <http://thewebsemantic.com/javaclass> "example.person". <http://example/person/taylor_cowan@yahoo.com> a <http://example/person> ; <http://example/email> "taylor_cowan@yahoo.com"^^xsd:string.
Overriding the Default Namespace 33 <http://mydomain#person> a <http://www.w3.org/2000/01/rdf-schema#class> ; <http://thewebsemantic.com/javaclass> "examples.model.person". <http://mydomain#person/thewebsemantic@gmail.com> a <http://mydomain#person> ; <http://mydomain#email> "thewebsemantic@gmail.com"^^xsd:string.
Overriding the Default Property Bindings 34 <http://mydomain#person> a <http://www.w3.org/2000/01/rdf-schema#class> ; <http://thewebsemantic.com/javaclass> "examples.model.person". <http://mydomain#person/thewebsemantic@gmail.com> a <http://mydomain#person> ; <http://xmlns.com/foaf/0.1/name> "Taylor Cowan"^^xsd:string.
Extending Person to Support Friendship 35 public Collection<Person> friends = new LinkedList<Person>(); @RdfProperty("http://xmlns.com/foaf/0.1/knows") public Collection<Person> getfriends() { return friends;}
Loading Beans from a Model 36 RDF2Bean reader = new RDF2Bean(m); Person p = reader.load(person.class,"person@example.com"); Collection<Person> allpeople = reader.load(person.class);
Open Source Tools For Java Devs 37 > 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/
Taylor Cowan http:/ / thewebsemantic.com http:/ / twitter.com/ tcowan Travelocity taylor.cowan@travelocity.com