#!/usr/local/bin/ruby

require '../basicrdf'

srand() # seed randomizer for blank node genids

# Some handy namespaces
FOAF 	= 'http://xmlns.com/foaf/0.1/'
DC 	= 'http://purl.org/dc/elements/1.1/'

# Some bits of RDF vocabulary
foaf_mbox = Node.getResource(FOAF+'mbox')
foaf_livesIn = Node.getResource(FOAF+'livesIn')
foaf_homepage = Node.getResource(FOAF+'homepage')


db = Graph.new ( [] )	# we can initialise a graph with content
print "Registering FOAF namespace" + db.reg_xmlns(FOAF, 'foaf') + "\n"


# Get some initial nodes and properties
# ie. some resources we'll be mentioning (no URIs known for the blanks)
#
bristol = db.getBlank()
libby = db.getBlank()
damey = db.getBlank()
danbri = db.getBlank()

dbhome = db.getResource('http://rdfweb.org/people/danbri/')
dbmail = db.getResource('mailto:danbri@rdfweb.org')

# make some statements from these raw materials
s1 = Statement.new(danbri, foaf_homepage,dbhome)
s2 = Statement.new(danbri, foaf_mbox,dbmail)
s3 = Statement.new(danbri, foaf_livesIn, db.getLiteral("Bristol"))
s4 = Statement.new(damey, foaf_livesIn, db.getLiteral("Bristol"))
lie = Statement.new(danbri, foaf_livesIn, db.getLiteral("Hong Kong"))

db.tell_all([s1, s2, s3, s4])




db.tell(Statement.new(libby, foaf_livesIn, Node.getLiteral("Bristol")))
db.tell(Statement.new(libby, foaf_mbox, Node.getResource('mailto:libby.miller@bristol.ac.uk')))
db.tell(Statement.new(libby, foaf_mbox, Node.getResource('mailto:libby@rdfweb.org')))
db.tell(Statement.new(damey, foaf_mbox, Node.getResource('mailto:d.m.steer@lse.ac.uk')))

print "Testing node-centric API (under development):\n"
print "Danbri mailbox: #{danbri.foaf_mbox} \n"
print "Libby mailbox: #{libby.foaf_mbox.join(' ' )} \n"



puts "Testing API:\n RDF Graph: ", db, "\n"

#puts g.toNtriples()

## Test our query code
# these should return a Graph
# we'll have prettier interfaces built ontop of this
# subjects() predicates() objects() gets blunt/label/sharp parts of the arcs
# also to come:
# we'll add mozilla-style graph nav API (GetSource/GetTarget etc)
# and a node centric API

# query 'xxx'
# todo!


print ("Statement 's3' (ie. #{s3.inspect}) is in the graph\n") if ((db.ask(s3)).size >0 ) 
print "Size s3: " , db.ask(s3).size  ,"\n"
print ("Statement 'lie' (ie. #{lie.inspect}) is (rightly) NOT in the graph\n") if((db.ask(lie)).size ==0)


# query 'xxo'
puts "Asking for mailboxes of libby:\n"
t1 = db.ask( Statement.new( libby, foaf_mbox, nil)).objects()
# maybe: db.ask(...).
puts "Answer was: #{t1} \n"

# query 'oxx'
puts "Mailboxes of people who live in bristol: \n\n"

who = db.ask(Statement.new( nil,foaf_livesIn,Node.getLiteral("Bristol")))

puts "Got an answer graph: #{who.inspect} \n"
puts "All the people in bristol: \n #{who.subjects()} \n"

puts "Their mailboxes...\n"
mbox=[]
puts "All the people in bristol: \n #{who.subjects()} \n"

who.subjects().each {
  |w| mbox.push( db.ask( Statement.new(w,foaf_mbox,nil)).objects() ) 
} 

puts "mbox is: #{mbox.inspect}\n"
puts mbox.each { |mailto| puts "Email: #{mailto} \n" }

# query 'xoo'
#

# query 'oox'

# query 'xox'

# query 'oxo'

# query 'ooo' (all unknown)
t2 = db.ask (Statement.new(nil,nil,nil))
# puts "Tripledump: #{t2.inspect}\n" # todo: no workie yet


## Test our simple parser
print "Testing NTriple parser..."
loaded = NTriples.nt2graph()
puts "Loaded from disk: #{loaded.inspect} \n\n"



























