12.07.2015 Views

The Seman c Web

The Seman c Web

The Seman c Web

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>The</strong> <strong>Seman</strong>)c <strong>Web</strong> RDF, SPARQL and so0ware APIs 4IZ440 Knowledge Representa4on and Reasoning on the WWW Josef Petrák | me@jspetrak.name


Worth a note § Open Data iniDave around world and Europe open staDsDcal and government data, usually in RDF format. § It has several success stories § hLp://headtoweb.posterous.com/open-­‐data-­‐success-­‐stories § Google is integraDng RDF encoded as RDFa in pages into its search results. § UEP is not behind – DIKE research group KEG started Czech–Slovak semanDc iniDaDve called <strong>Seman</strong>Dcs § And we have a RDF-­‐based website deployed, see hLp://keg.vse.cz/


Outline § RDF representaDon formats § Data handling approaches § So0ware APIs overview § Approaches in examples § MoDvaDon example: web applicaDon


RDF representaDon


Syntaxes § RDF has several syntaxes. § A “graph” is the reference syntax. § <strong>The</strong> W3C endorsed file format is RDF/XML § Other file formats: § N-­‐Triples § N3 § RDF/JSON § RDFa


RDF graphrdf:type gd:Person gd:name John Example gd:affiliaDon Sample Corp. Inc. PREFIX rdf: PREFIX gd:


RDF/XML John Example Sample Corp. Inc.


RDF/XML (shortened) John Example Sample Corp. Inc.


N3 NotaDon @prefix gd: . [] a gd:Person; gd:name “John Example”; gd:affiliation “Sample Corp. Inc.” .


N–Triples _:a . _:a “John Example” . _:a “Sample Corp. Inc.” . § Primarily used to defined RDF test cases § Subset of N3 NotaDon


RDF/JSON { “_:a“ : { “http://www.w3.org/1999/02/22-­‐rdf-­‐syntax-­‐ns#” : [ { “value“ : “http://rdf.data-­‐vocabulary.org/#Person”, “type“ : “uri“ } ], “http://rdf.data-­‐vocabulary.org/#name” : [ { “value“ : “John Example“, “type“ : “literal“ } ], “http://rdf.data-­‐vocabulary.org/#affiliation” : [ { “value“ : “Sample Corp. Inc.“, “type“ : “literal“ } ] } }


RDFa John Example is working at Sample Corp. Inc..


Resources about RDF formats § RDF/XML § hLp://www.w3.org/TR/rdf-­‐syntax-­‐grammar/ § N3 NotaDon § hLp://www.w3.org/DesignIssues/NotaDon3 § hLp://www.w3.org/2000/10/swap/Primer § N–Triples § hLp://www.w3.org/TR/rdf-­‐testcases/#ntriples § RDF/JSON § hLp://n2.talis.com/wiki/RDF_JSON_SpecificaDon § RDFa § hLp://www.w3.org/TR/rdfa-­‐syntax/


Task #1 1. Use Google Data Vocabulary § hLp://www.data-­‐vocabulary.org/Person/ 2. Created a NotaDon3 file with demo data about people


Data handling


RDF models § Statement–centric § Working with triples of ?subject ?predicate ?object § Resource–centric § Working with resources having properDes and their values § Ontology–centric § Working with classes, properDes, and individuals as defined in selected vocabulary/schema/ontology § Named graph § Triples belongs to a graph with URI name § Working with quads of ?graph ?subject ?predicate ?object


So0ware APIs


Java § Jena § hLp://openjena.org/ § hLp://sourceforge.net/projects/jena/ § Sesame § hLp://www.openrdf.org/ § Shellac RDFa Parser § hLps://github.com/shellac/java-­‐rdfa


PHP § RDF API for PHP (RAP) § hLp://www4.wiwiss.fu-­‐berlin.de/bizer/rdfapi/ § hLp://sourceforge.net/projects/rdfapi-­‐php/ § ARC2 § hLp://arc.semsol.org/


Ruby § RDF.rb § hLp://rdf.rubyforge.org/ § hLps://github.com/bendiken/rdf § Plus various modules


Code Examples


File-­‐read § Task: Read RDF data from a NotaDon3 file format into memory model.


Read N3 (Jena) InputStream is = FileManager.get().open("samples/people.n3"); Model model = ModelFactory.createDefaultModel(); RDFReader r = model.getReader("N3"); r.read(model, is, null); is.close();


Read N3 (RDF.rb) repository = RDF::Repository.new RDF::N3::Reader.open(‘samples/people.n3’) { |reader| } repository


Read N3 (ARC2) $parser = ARC2::getRDFParser(); $parser-­‐>parse(‘samples/people.n3'); $triples = $parser-­‐>getTriples();


Read N3 (RAP) $model = ModelFactory::getDefaultModel(); $model-­‐>load('samples/people.n3'); § This code won’t run, because there is N3Reader bug. § Works also for RDF/XML and N–Triples formats.


Traverse triples § Task: Take a memory model and list all or parDcular RDF triples.


Traverse triples (Jena) Model model = ModelFactory.createDefaultModel(); // Load data into model StmtIterator i = model.listStatements(); while (i.hasNext()) { Statement stmt = i.nextStatement(); Resource subject = stmt.getSubject(); Property predicate = stmt.getPredicate(); RDFNode object = stmt.getObject(); // Printing out }


Query Model (Jena) Resource rPerson = model.getResource("http://rdf.data-­vocabulary.org/#Person");Property rName = model.getProperty("http://rdf.data-­‐vocabulary.org/#name"); StmtIterator si = model.listStatements(null, RDF.type, rPerson); while (si.hasNext()) { } Resource r = si.nextStatement().getSubject(); StmtIterator sii = model.listStatements(r, rName, (RDFNode)null); if (sii.hasNext()) { System.out.println(sii.nextStatement().getObject().toString()); }


Traverse triples (RDF.rb) repository = RDF::Repository.new # Load data repository.each_statement { |statement| } puts statement


Query model (RDF.rb) GD = RDF::Vocabulary.new('http://rdf.data-­‐vocabulary.org/#') repository = RDF::Repository.new # Load data query = RDF::Query.new({ :person => { RDF.type => GD.Person, GD.name => :name } }) query.execute(graph).each do |person| puts "name=#{person.name}" end


Query SPARQL Endpoint § Task: Query a SPARQL endpoint and write out the data. E.g. from DBPedia, write one random blackboard gag wriLen by Bart Simpson.


Query SPARQL Endpoint (Jena) String qa = "PREFIX skos: " + "PREFIX dbpprop: " + "PREFIX dcterms: " + "PREFIX rdfs: " + "SELECT ?gag WHERE { " + "?series skos:broader ." + " ?episode dcterms:subject ?series ." + " ?episode dbpprop:blackboard ?gag ." + "}" + "LIMIT 1000"; Query q = QueryFactory.create(qa); QueryExecution qe = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", q); ResultSet rs = qe.execSelect(); ResultSetFormatter.out(System.out, rs, q); qe.close();


Query SPARQL Endpoint (RDF.rb) sparql = SPARQL::Client.new("http://dbpedia.org/sparql") result = sparql.query("SELECT ?gag WHERE { ?series skos:broader . ?episode dcterms:subject ?series . ?episode dbpprop:blackboard ?gag . } LIMIT 1000") puts result[rand(result.length)][:gag]


Query SPARQL Endpoint (RAP) $client = ModelFactory::getSparqlClient("http://www.exampleSparqlService.net:2020/example"); $query = new ClientQuery(); $query-­‐>query(‘PREFIX skos: PREFIX dbpprop: PREFIX dcterms: PREFIX rdfs: SELECT ?gag WHERE { ?series skos:broader . ?episode dcterms:subject ?series . ?episode dbpprop:blackboard ?gag . } LIMIT 1000’); $result = $client-­‐>query($query); foreach($result as $line){ echo($line['?gag']-­‐>toString()); }


Query SPARQL Endpoint (ARC2) $store = ARC2::getRemoteStore( array('remote_store_endpoint' => 'http://dbpedia.org/sparql')); $q = ‘PREFIX skos: PREFIX dbpprop: PREFIX dcterms: PREFIX rdfs: SELECT ?gag WHERE { ?series skos:broader . ?episode dcterms:subject ?series . ?episode dbpprop:blackboard ?gag . } LIMIT 1000’; $rows = $store-­‐>query($q, 'rows');


Task #2 1. Select one of frameworks (preferably Jena) 2. Load data from your NotaDon3 file 3. Write out names and work Dtles of people


MoDvaDon Example


Knowledge Engineering Group <strong>Web</strong>site § hLp://keg.vse.cz/ § RDF outside § Data dumps in RDF/XML § <strong>Web</strong> pages enriched with RDFa § RDF inside § Data created by SPARQL INSERT § Data queried by SPARQL SELECT § Data updated by SPARQL DELETE and SELECT § Data manipulated by user–friendly forms § Ongoing: data integraDon with ISIS VŠE and other department applicaDons


SPARQL: Create data INSERT INTO { rdf:type ical:Vevent . } ical:uid ”1" . ical:summary ”IZI440" . ical:dtstart ”2011-­‐02-­‐28" .


SPARQL: Read data SELECT ?summary ?uid ?dtstart ?dtend WHERE { rdf:type ical:Vevent . ical:uid ?uid . ical:summary ?summary . ical:dtstart ?dtstart . OPTIONAL { } } ical:dtend ?dtend .


SPARQL: Update data DELETE { ?p ?o . } INSERT INTO { rdf:type ical:Vevent . ical:uid ”1" . ical:summary ”4IZ440" . ical:dtstart ”2011-­‐02-­‐28" . }


References § hLp://dsic.zapisky.info/RDF/FOAF/parsingWithPHP/ § hLp://zapisky.info/?item=zverejnime-­‐akademicke-­‐projekty-­samozrejme-­‐semanDcky§ BOOK – John Hebeler (Author), MaLhew Fisher (Author), Ryan Blace (Author), Andrew Perez-­‐Lopez (Author), Mike Dean (Foreword): <strong>Seman</strong>4c <strong>Web</strong> Programming, Wiley, 2009


QuesDons?


Thank you

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!