Programming with JenaBean Sources for examples can be Taylor Cowan Travelocity 8982
2 [] a foaf:Person; foaf:name Taylor Cowan; foaf:weblog ; foaf:workplaceHomepage ; foaf:holdsAccount ; foaf:currentProject ;
3 Model m = ModelFactory.createDefaultModel(); Thing todaysTopic = new Thing(" m); new Thing(m).isa(Foaf.Person.class).name("Taylor Card.java in package example.fluentwriter
4 AGENDA >Semantic Web Introduction >RDF basics >Coding Towards Jena’s Semantic Web Framework API >Java to Model Binding with JenaBean >Java to Model Binding with Fluent Interfaces
Why Not Microformats? 5
6 Some example foaf: 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.
7 Equivalent Raw Jena API Client Code String NS = " OntModel m = ModelFactory.createOntologyModel(); OntClass articleCls = m.createClass(NS +"Article"); Individual i = articleCls.createIndividual( " 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");
8 Pain Points of Raw Jena API Programming >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. >The impedance mismatch is similar to what we had with RDBMS
9 Creating The Same Assertions with JenaBean Model m = ModelFactory.createDefaultModel(); Bean2RDF writer = new Bean2RDF(m); Article article = new Article( " article.setCreator("Philip McCarthy"); article.setTitle("Introduction to Jena"); article.setSubject("jena, rdf, java, semantic web"); writer.save(article); m.write(System.out, "N3");
10 The JenaBean Project >Hosted at Google code >Bean binding, not code generation >Doesn’t use byte code interweaving >Doesn’t require implementing an interface >
11 Programming with JenaBean is Simple >Bean2RDF writes objects >RDF2Bean reads objects >3 Annotations specifies unique field provides a domain maps java properties to RDF properties
The Simplest Possible Example package examples.model; import thewebsemantic.Id; public class Person private String ; public String get () { return ;} public void set (String ) { this. = ;} } 12 a ; "examples.model.Person". a ;
Saving an Instance of Person Model m = ModelFactory.createOntologyModel(); Bean2RDF writer = new Bean2RDF(m); Person p = new Person(); writer.save(p); m.write(System.out, "N3"); … a owl:Class ; "example.Person". a ; 13
Overriding the Default Namespace package examples.model; import thewebsemantic.Id; import public class Person { … } 14 a ; "examples.model.Person". a ;
Overriding the Default Property public class Person { private String + "name") private String name; 15 a ; "examples.model.Person". a ; "Taylor Cowan"^^xsd:string.
Extending Person to Support Friendship public Collection friends = new LinkedList public Collection getFriends() { return friends;} 16
Loading Beans from a Model RDF2Bean reader = new RDF2Bean(m); Person p = Collection allPeople = reader.load(Person.class); 17
JenaBean Support for OWL Entailments 18 public class Location public String id; public String public Collection public Collection contains; … a rdf:Property, owl:TransitiveProperty. a rdf:Property ; owl:inverseOf.
Reading Existing RDF/OWL 19 >Up till this point we were generating the triples >JenaBean + annotations controlled the URI’s >The model knew the provenance of all data (the originating java class)
Example Geonames “feature” entry 20 London Borough of Islington Islington Jenabean will bind to existing URI’s
Crafting beans for existing RDF requires care public class Feature private URI public double lat; Namespace must accurately match 2.Your java’s classname must match the Ontology class must be of type java.net.URI 4.All property URI’s must match the Ontology property
JenaBean can auto discover JenaBeans, provided it knows the package(s) 22 1: Model m = ModelFactory.createDefaultModel(); 2: m.read(" 3: RDF2Bean reader = new RDF2Bean(m); 4: reader.bindAll("com.foo", "com.bar"); // type safe binding by class reader.bind(Feature.class); // or package reader.bind(Feature.class.getPackage());
JenaBean tip: handling lang encoded strings If your data has something like this: Londres Then use JenaBean‘s special type “LocalizedString“ 23 public Collection alternateName; example.geonames
Query Support 24 // load using a Jena Resource reader.load(Human.class, jenaResource); // load any node using it’s URI reader.load(Human.class, " >Most most practical purposes there’s no need to utilize anything other than Jena’s ARQ api to query. JenaBean’s reader (RDF2Bean) can transform a node given it’s URI or it’s representation as a jena Resource…
thewebsemantic.Sparql Util 25 String query = "prefix ntn: \n" + "SELECT ?s WHERE { ?s a ntn:Woman }"; Model m = ModelFactory.createOntologyModel(); m.read("file:NTNames.owl"); m.read("file:NTN-individuals.owl"); RDF2Bean reader = new RDF2Bean(m); reader.bindAll("example.query"); Collection women = Sparql.exec(m, Woman.class, query); for (Human human : women) System.out.println(human.label + ":" + human.comment); example.query
Applies to class Applies to field or getter method Should be a String or primitive type, or wrapper type type java.net.URI is Applies to field or getter method Remember: by definition, JavaBeans must have a default constructor. 26
Summary writer.save(mybean) writer.saveDeep(mybean) Save this and all related objects reader.load(Class.class, key); reader.loadDeep(…); take care, could place entire graph into memory. reader.bindAll(package, package, …); Makes jenabean aware of your beans 27
JenaBean Fluent Programming API 28 >AKA method chaining, foo.this().that().bar(); >A “Fluent Interface” aims to provide more readable code >A significant departure from JavaBeans >Is always connected to the jena graph >Entirely interface (not class) driven >Allows Individuals to morph into their various classes >Allows use of vocabulary terms against any Individual regardless of classification.
Example: wgs84 geo vocabulary 29 import thewebsemantic.As; import thewebsemantic.Functional; import public interface Geo extends As { interface Point extends Geo lat(float l); Float Geo long_(float l); Float long_(); }
A fluent api + good IDE makes things fun 30
Create a new anonymous iCal event. 31 Ical t = new Thing(m).isa(Ical.Vevent.class); Create a new iCal event with URI Ical t = new Thing(“ m). isa(Ical.Vevent.class);
Full Example: Creating an iCal event for the meetup 32 1: Ical.Vevent t = new Thing(m).isa(Ical.Vevent.class); 2: 3: dtstart(" T200000Z"). 4: dtend(" T220000Z"). 5: summary("Jena Semantic Web…"). 6: location("Parisoma - …") 7:.as(Geo.class). 8: lat(37.77f). 9: long_( f); [] a ical:Vevent ; ical:dtend " T220000Z" ; ical:dtstart " T200000Z" ; ical:location "Parisoma - " ; ical:summary "Jena Semantic Web…" ; ical:uid ; geo:lat "37.77"^^xsd:float ; geo:long " "^^xsd:float.
JenaBean comes with a few common vocabulary interfaces 33 >thewebsemantic.vocabulary.Foaf >Geo >Ical >DCTerms >Sioc >Skos >Rdfs >ReviewVocab >You may want to copy and modify in some cases.
Fluent API summary: 34 >Your interface should extend thewebsemantic.As >Provides polymorphic “as(Class)” to other vocabs. >Provides easy type declaration with “isa(Class)” >Use annotation to bind to the vocabulary >Name setters according to vocab, taking either an Object (literal) or anther Thing (relationship to other Individuals) >Name getters according to vocab, returning the same vocabulary type and taking no arguments. >If the vocabulary term collides with reserved term (as with long), append a dash. >Plural properties should return a Collection.
35 Project Ideas >Create your own foaf document using the fluent interface. >Integrate Jena/JenaBean with restlets –restlets.org >Use your favorite framework (struts, stripes, spring mvc) and create a user registration screen. >Write JenaBeans that bind to jamendo or other music ontology at >Write a foaf crawler beginning with Berners-Lee that traverses his social graph. cc nickjohnson
Taylor Cowan Travelocity