Programming the Semantic Web with Java Taylor Cowan Travelocity 8982
Programming the Semantic Web with Java
Taylor Cowan
Travelocity
8982
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
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
4
5
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
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”
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
Combining Datasets with OWL9
owl:sameAs
Combining Datasets10
owl:sameAs
RDF != XML
“The site at ht tp:/ / www.travelocity.com, also known as Travelocity, is an online travel agency competing with expedia.com”
11
Concepts as a Directed Graph12
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> .
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>
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
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
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)> .
Assertion18
jazoon.com java
foaf:primaryTopic
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…
Knowledge after Inference20
jazoon.com
java
foaf:primaryTopic
foaf:isPrimaryTopicOf
foaf:Document
Is a
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.
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
Models Can be Populated from URL, Either Public or Local
23
Models can be Populated from Other Models24
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 .
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");
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.
28
Creating The Same Assertions with JenaBean
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
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
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 .
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
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 .
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 .
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
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
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/