#!/usr/bin/env ruby
# 
# This example works against a fragmented RDF graph 
# stored in an RDBMS. The defrag() method will smush together 
# nodes that denote the same resource, based on meta-information about
# the unambiguous RDF properties used in the graph. A list of these can
# optionally be passed in. The service may keep its own list, either in
# the main RDF store or externally.
# 
# Dan Brickley <danbri@w3.org>

#  seealso: aggregation strategies http://rdfweb.org/2001/01/design/smush.html
#
# usage: 
# psql test1 < examples.sql
# ruby defrag_test.rb

# Warning: This may damage your data! It is not well tested. Use with care.
#
# Known issues:
#  - doesn't treat URI node labels preferentially
#  - URIs may get lost
# todo: 
# - fix squish parser so USING not needed.
# - hide sql stuff behind rdfdatasource api? or make 
#   an rdf backend for dbi? test by intergrating soap query.

require '../squish'
require '../../basicrdf'
require 'dbi'

DBI_DRIVER = 'DBI:Pg:test1'
DBI_USER = 'danbri'
DBI_PASS=''

# get an RDF data service, ask it to defragment itself 

service = DBIDataService.new(DBI_DRIVER,DBI_USER,DBI_PASS)
service.defrag
service.addAllSuperProperties 

query = SquishQuery.new()

# Find details and ethical policies of owners of companies who
# have an interest in XML technology 
#
sq = 'SELECT ?name, ?mbox, ?owned, ?ticker, ?corphome, ?ethic, WHERE 
	(eg::technologyInterest ?x http://www.w3.org/XML/) 
	(eg::personalMailbox ?x ?mbox) 
	(eg::name ?x ?name) 
	(eg::owner ?c ?x) 
	(eg::ticker ?c ?ticker) 
	(eg::name ?c ?owned) 
	(eg::corporateHomepage ?c ?corphome) 
	(eg::ethicalPolicy ?c ?ethic)
	(rdf::type ?x eg::User) 
	using eg for http://example.com/xmlns/aggregation-demo#
	      rdf for http://www.w3.org/1999/02/22-rdf-syntax-ns# '

  DBI.connect( DBI_DRIVER, DBI_USER, DBI_PASS) do | dbh |

  dbh.select_all( query.parseFromText(sq).toSQLQuery ) do | row |  
    r = ResultRow.new(row) # a convenience class, methods map to fields

    puts " #{r.name}  <#{r.mbox}> owns #{r.owned} (ticker: #{r.ticker})"
    puts " corporate home page: #{r.corphome} ethical policy url: #{r.ethic}"
   end
end



##############################################################
# this is our dataset. Google for 'rdf smush' for writeup.
#

=begin

_:genid1 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/xmlns/aggregation-demo#Company> .
_:genid1 <http://example.com/xmlns/aggregation-demo#corporateHomepage> <http://megacorp.example.com/> .
_:genid1 <http://example.com/xmlns/aggregation-demo#name> "MegaCorp Inc." .
_:genid1 <http://example.com/xmlns/aggregation-demo#ticker> "MEGA" .
_:genid1 <http://example.com/xmlns/aggregation-demo#owner> _:genid2 .

_:genid2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/xmlns/aggregation-demo#Person> .
_:genid2 <http://example.com/xmlns/aggregation-demo#name> "Mr Mega" .
_:genid2 <http://example.com/xmlns/aggregation-demo#personalMailbox> <mailto:mega@megacorp.example.com> .
_:genid2 <http://example.com/xmlns/aggregation-demo#personalHomepage> <http://megacorp.example.com/~mega> .
_:genid2 <http://example.com/xmlns/aggregation-demo#age> "50" .


_:genid3 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/xmlns/aggregation-demo#User> .
_:genid3 <http://example.com/xmlns/aggregation-demo#personalMailbox> <mailto:mega@megacorp.example.com> .
_:genid3 <http://example.com/xmlns/aggregation-demo#technologyInterest> <http://www.w3.org/XML/> .
_:genid3 <http://example.com/xmlns/aggregation-demo#technologyInterest> <http://www.w3.org/RDF/> .
_:genid3 <http://example.com/xmlns/aggregation-demo#technologyInterest> <http://www.mozilla.org/> .

_:genid4 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/xmlns/aggregation-demo#Organisation> .
_:genid4 <http://example.com/xmlns/aggregation-demo#corporateHomepage> <http://megacorp.example.com/> .
_:genid4 <http://example.com/xmlns/aggregation-demo#ethicalPolicy> <http://dotherightthing.example.org/policy.xhtml> .

<http://dotherightthing.example.org/policy.xhtml> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://example.com/xmlns/aggregation-demo#PolicyStatement> .
<http://dotherightthing.example.org/policy.xhtml> <http://example.com/xmlns/aggregation-demo#title> "Ethical Business Shared Guidelines 1.1" .

=end
